8051版本
2022-12-13_132230.png (21.2 KB, 下載次數(shù): 40)
下載附件
2022-12-13 13:24 上傳
- #include <reg52.h>
- typedef unsigned char u8; //0 to 255
- typedef unsigned int u16; //0 to 65535
- typedef unsigned long u32; //0 to 4294967295
- sbit EN_RS = P2^5;
- sbit D4_D6 = P2^6;
- sbit D5_D7 = P2^7;
- #define SetRS 0x01
- #define RstRS 0x00
- void Delay2ms() //@12.000MHz
- {
- unsigned char i, j;
- i = 4;
- j = 225;
- do
- {
- while (--j);
- }
- while (--i);
- }
- void Delay200us() //@12.000MHz
- {
- unsigned char i;
- i = 97;
- while (--i);
- }
- void ThreeWire(u8 dat)
- {
- EN_RS = (bit)(dat & 0x01);
- D4_D6 = (bit)(dat & 0x40);
- D5_D7 = (bit)(dat & 0x80);
- Delay200us();
- D4_D6 = (bit)(dat & 0x10);
- D5_D7 = (bit)(dat & 0x20);
- EN_RS = 1;
- EN_RS = 0;
- D4_D6 = 0;
- D5_D7 = 0;
- Delay200us();
- }
- void LCD_Init_4bit();
- void LCD_WChar_4bit(u8 Row, u8 Address, char chr);
- void LCD_WStr_4bit(u8 Row, u8 Address, char *Str);
- void LCD_WCMD(u8 Cmd)
- {
- ThreeWire(RstRS | (Cmd & 0xF0));
- ThreeWire(RstRS | (Cmd << 4));
- }
- void LCD_WDAT(u8 Dat)
- {
- ThreeWire(SetRS | (Dat & 0xF0));
- ThreeWire(SetRS | (Dat << 4));
- }
- void LCD_Init_4bit()
- {
- LCD_WCMD(0x02);
- LCD_WCMD(0x28);
- LCD_WCMD(0x0c);
- LCD_WCMD(0x06);
- LCD_WCMD(0x01);
- Delay2ms();
- }
- void LCD_WChar_4bit(u8 Row, u8 Address, char chr)
- {
- if(Row == 1)LCD_WCMD(0x80 + Address);
- if(Row == 2)LCD_WCMD(0xc0 + Address);
- LCD_WDAT(chr);
- }
- void LCD_WStr_4bit(u8 Row, u8 Address, char *Str)
- {
- if(Row == 1)LCD_WCMD(0x80 + Address);
- if(Row == 2)LCD_WCMD(0xc0 + Address);
- while(*Str)
- {
- LCD_WDAT(*Str);
- Str++;
- }
- }
- void Delay1000ms() //@12.000MHz
- {
- unsigned char i, j, k;
- i = 8;
- j = 154;
- k = 122;
- do
- {
- do
- {
- while (--k);
- }
- while (--j);
- }
- while (--i);
- }
- u8 Count = 0;
- void main()
- {
- EN_RS = 0;
- D4_D6 = 0;
- D5_D7 = 0;
- LCD_Init_4bit();
- LCD_WStr_4bit(1,0,"Hello World!");
- LCD_WStr_4bit(2,0,"3 Wire LCD");
- while (1)
- {
- LCD_WChar_4bit(2,13,Count/100%10 + '0');
- LCD_WChar_4bit(2,14,Count/10%10 + '0');
- LCD_WChar_4bit(2,15,Count%10 + '0');
- Count++;
- Delay1000ms();
- }
- }
復(fù)制代碼
|