|
//hd44780在四位时是先处理高四位后处理低四位 #include<io8515.h> #include<macros.h> #include<slavr.h> #define uchar unsigned char #define uint unsigned int #define RS_H asm("sbi 0x12,3") #define RS_L asm("cbi 0x12,3") #define RW_H asm("sbi 0x12,4") #define RW_L asm("cbi 0x12,4") #define E_H asm("sbi 0x12,5") #define E_L asm("cbi 0x12,5") #define SB7 asm("cbi 0x18,7") //portb7-port4为数据口 //portd3,4,5为控制端 void port_init(void) { PORTA = 0xFF; DDRA = 0x00; PORTB = 0xFF; DDRB = 0xFF; PORTC = 0xFF; DDRC = 0x00; PORTD = 0xFF; DDRD = 0xFF; }
void Delay(void) { unsigned char di; for(di=0;di<20;di++); } void Delay1(void) { unsigned char di; for(di=0;di<5;di++); } void busywait(void)//忙检测 { uchar flag; asm("sbi 0x18,7"); DDRB=0; do { RS_L; RW_H; Delay(); E_H; Delay(); flag=PINB&0X80; E_L; Delay1(); E_H; Delay(); E_L; Delay(); } while(flag); E_L; DDRB=0xff; //端口B设为输出方 }
void writedata(unsigned char data)//写数据 { busywait(); RS_H;//rs高电平 RW_L;//rw低电平 Delay1(); PORTB=data&0XF0;//输出data高四位到pb4-7未考虑对低四位的影响 E_H; Delay(); E_L; Delay1(); PORTB=(data&0X0F)<<4;;/*输出data低四位到pb4-7未考虑对低四位影响*/
E_H; Delay(); E_L; }
void writecomand(uchar CMD,uchar cc)//写命令 { if(cc){busywait();} RS_L; RW_L; Delay1(); PORTB=CMD&0XF0;输出data高四位到pb4-7未考虑对低四位的影响
E_H; Delay(); E_L; Delay1(); E_L; PORTB=(CMD&0X0F)<<4;;;/*输出data低四位到pb4-7未考虑对低四位影响*/
Delay1(); E_H; Delay1(); E_L; Delay1(); }
void lcdreset(void) { writecomand(0x28,0); delay_ms(5); writecomand(0x28,0); delay_ms(5); writecomand(0x28,0); delay_ms(5); writecomand(0x01,1); writecomand(0x28,1); writecomand(0x08,1); writecomand(0x01,1); writecomand(0x06,1); writecomand(0x0C,1); writecomand(0x0F,1); }
void LocateXY(char ex,char ey) { uchar temp; temp = ex & 0xf; ey &= 0x1; if (ey)temp |= 0x40; temp |= 0x80; writecomand(temp,0); }
void writeachar(char x,char y,char Wdata) { LocateXY(x,y); writedata(Wdata); } void Lcdputs(char x,char y, char *ptr) { uchar i,l=0; while (ptr[l] >31){l++;}; for (i=0;i<l;i++) {delay_ms(70); writeachar(x++,y,ptr[i]); if ( x == 16 ){ x = 0; y ^= 1; } } }
void main(void) { port_init(); lcdreset(); while(1){ delay_ms(5000); Lcdputs(0,0,"hello!! word!!ha ha"); delay_ms(5000); writecomand(0x01,1); } }
|