📄 7920.c
字号:
//本程序是7920的LM3037的液晶驱动程序
//汉字2x9
//ATMEGA16, 内部晶振8M
//使用串行传输
//本程序测试通过
//绿叶子整理 -- 2005.08.30
#include <iom16v.h>
#include <macros.h>
#define LCD_DATA_PORT PORTA
#define LCD_DATA_DDR DDRA
#define LCD_DATA_PIN PINA
#define LCD_DATA 0x0ff //portA out
#define LCD_CONTROL_PORT PORTB
#define LCD_CONTROL_DDR DDRB
#define RS 0 //RS(CS) 可直接接VCC -- 替代
#define RW 1 //RW(SID)
#define E 2 //E(sclk)
#define PSB 3 //可以直接接地 -- 替代
#define _RES 4 //可以去掉
/*--------------------------------------------------------------------------------------------------
Public function prototypes
--------------------------------------------------------------------------------------------------*/
void delay_1us(void);
void delay_1ms(void);
void delay_nus (unsigned int n);
void delay_nms (unsigned int n);
#define SETBIT(x,y) (x|=(1<<y)) //set bit y in byte x
#define CLRBIT(x,y) (x&=(~(1<<y))) //clear bit y in byte x
#define CHKBIT(x,y) (x&(1<<y)) //check bit y in byte x
#define uchar unsigned char // 0~255
#define uint unsigned int // 0~65535
void CheckState();
void initLCDM(void);
void Send(unsigned char senddata);
void SdCmd(unsigned char scmd);
void SdData(unsigned char DData);
void WriteTextScreen(unsigned char *TxtData);
void WriteTextScreen2(unsigned char *pstr);
void WriteGraphicScreen(unsigned char *GDData);
void DispSetCursor(unsigned char LineNum, unsigned char ColumnNum);
unsigned char TextLogo[]={"液晶显示模块(LCDM)"};
void initLCDM(void)
{
delay_nms(100);
//端口初始化
//LCD_DATA_DDR=0xFF;
LCD_CONTROL_DDR=0xFF;
CLRBIT(LCD_CONTROL_PORT,E);
CLRBIT(LCD_CONTROL_PORT,RW);
CLRBIT(LCD_CONTROL_PORT,RS);
CLRBIT(LCD_CONTROL_PORT,PSB);
//LCD_DATA_PORT|=0x0;
//delay_nms(1500);
LCD_DATA_PORT|=0xFF;
//SETBIT(LCD_CONTROL_PORT,_RES);
CLRBIT(LCD_CONTROL_PORT,_RES);
delay_nms(1);
SETBIT(LCD_CONTROL_PORT,_RES);
SdCmd(0x20); // 8bit I/F, basic command, graphic off
SdCmd(0x20); // 8bit I/F, basic command, graphic off
SdCmd(0x0C); // display on
SdCmd(0x06); // cursor right shift
SdCmd(0x01); // cursor right shift
}
/*-----------------------------------------------------
状态检查函数,判断是否处于忙状态
-------------------------------------------------------*/
void CheckState()
{
unsigned char dat;
CLRBIT(LCD_CONTROL_PORT,RS); //RS=0
SETBIT(LCD_CONTROL_PORT,RW); //RW=1
LCD_DATA_DDR=0x00; // portA as input
do
{
SETBIT(LCD_CONTROL_PORT,E);
NOP();
CLRBIT(LCD_CONTROL_PORT,E);
}
while (LCD_DATA_PIN&0x80);
}
void Send(unsigned char senddata)
{
unsigned char i;
for(i=0;i<8;i++)
{
if((senddata)&0x80)
{
//D_OUT=1 ;
SETBIT(LCD_CONTROL_PORT,RW);
}
else
{
//D_OUT=0;
CLRBIT(LCD_CONTROL_PORT,RW);
}
//SCK=1;
SETBIT(LCD_CONTROL_PORT,E);
NOP();
//SCK=0;
CLRBIT(LCD_CONTROL_PORT,E);
senddata<<=1;
}
}
void SdCmd(unsigned char scmd) //send command
{
//ST7920CS=1;
SETBIT(LCD_CONTROL_PORT,RS);
Send(0xf8);
Send(scmd&0xf0);
Send(scmd<<4);
//ST7920CS=0;
SETBIT(LCD_CONTROL_PORT,RS);
delay_nus(20);
}
void SdData(unsigned char DData)
{
//ST7920CS=1;
SETBIT(LCD_CONTROL_PORT,RS);
Send(0xfa);
Send(DData&0xf0);
Send(DData<<4);
//ST7920CS=0;
SETBIT(LCD_CONTROL_PORT,RS);
delay_nus(20);
}
void main(void)
{
CLI(); // disable interrupts
initLCDM();
while(1)
{
SdCmd(0x20); // 8bit I/F, basic command, graphic off
SdCmd(0x01); // clr text screen
delay_nms(100);
WriteTextScreen2(TextLogo);
delay_nms(1500);
WriteTextScreen2("世界你好12345678901234567890");
delay_nms(1500);
}
}
void DispSetCursor(unsigned char LineNum, unsigned char ColumnNum)
{
unsigned char i=0x00;
switch(LineNum&0x0f) //确定行号
{
case 0x00:
i=0x80;
break;
case 0x01:
i=0x90;
break;
case 0x02:
i=0x88;
break;
case 0x03:
i=0x98;
break;
default :
break;
}
i = (ColumnNum&0x0f)|i; //确定列号
SdCmd(i);
}
void WriteTextScreen2(unsigned char *pstr)
{
unsigned char i;
unsigned char j;
SdCmd(0x34); // 8bit I/F, basic command
SdCmd(0x30); // 8bit I/F, basic command, graphic off
for(i=0;i<36;i++) //清空屏幕
{
if (i%18==0) //判断是否换行
{
DispSetCursor(i/18,0); //如换行, 则光标移动到行首
}
SdData(' '); //
}
j=0;
//while (*pstr)
while (*pstr && j<36)
{
if (j%18==0) //判断是否换行
{
DispSetCursor(j/18,0); //如换行, 则光标移动到行首
}
//避免最后一格写半个汉字, 把汉字写到下一行
if (((j+1)%18==0) && *pstr>127 && *(pstr-1)<128)
{
SdData(' '); //
j++;
}
else
{
SdData(*pstr++);
j++;
}
//SdData(*pstr++);
//j++;
}
}
/*-----------------------------------------------------------------------
延时函数
系统时钟:8M
-----------------------------------------------------------------------*/
void delay_1us(void) //1us延时函数
{
asm("nop");
}
void delay_nus(unsigned int n) //N us延时函数
{
unsigned int i=0;
for (i=0;i<n;i++)
delay_1us();
}
void delay_1ms(void) //1ms延时函数
{
unsigned int i;
for (i=0;i<1140;i++);
}
void delay_nms(unsigned int n) //N ms延时函数
{
unsigned int i=0;
for (i=0;i<n;i++)
delay_1ms();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -