|
代碼
- #include<reg51.h>
- #include<intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
-
- #define NOP _nop_();_nop_();_nop_();_nop_();_nop_();_nop_();
- sbit RS=P3^2;
- sbit RW=P3^3;
- sbit E =P3^4;
- #define DPORT P1
- const uchar NoDisp=0;
- const uchar NoCur=1;
- const uchar CurNoFlash=2;
- const uchar CurFlash=3;
- void LcdPos(uchar,uchar);
- void LcdWd(uchar);
- void LcdWc(uchar);
- void LcdWcn(uchar);
- //void WaitIdle();
- void Lcd1602_Delay1ms(uint c) //誤差 0us
- {
- uchar a,b;
- for (; c>0; c--)
- {
- for (b=199;b>0;b--)
- {
- for(a=1;a>0;a--);
- }
- }
-
- }
- void mDelay(uint DelayTim)
- {
- uchar i;
- for(;DelayTim>0;DelayTim--)
- {
- for(i=0;i<125;i++);
- }
- }
- void WriteChar(uchar c,uchar xPos,uchar yPos)
- {
- LcdPos(xPos,yPos);
- LcdWd(c);
- }
- void WriteString(uchar *s,uchar xPos,uchar yPos)
- {
- uchar i;
- if(*s==0)
- return;
- for(i=0;;i++)
- {
- if(*(s+i)==0)
- break;
- WriteChar(*(s+i),xPos,yPos);
- xPos++;
- if(xPos>=15)
- break;
- }
- }
- void SetCur(uchar Para)
- {
- mDelay(2);
- switch(Para)
- {
- case 0:
- LcdWc(0x08);break;
- case 1:
- LcdWc(0x0c);break;
- case 2:
- LcdWc(0x0e);break;
- case 3:
- LcdWc(0x0f);break;
- default:
- break;
- }
- }
- void ClrLcd()
- {
- LcdWc(0x01);
- }
- //void WaitIdle()
- //{
- // uchar tmp;
- // RS=0;RW=1;E=1;
- // NOP;
- // for(;;)
- // {
- // DPORT=0XFF;
- // tmp=DPORT;
- // tmp&=0x80;
- // if(tmp==0)
- // break;
- //
- // }
- // E=0;
- //}
- void LcdWd(uchar c)
- {
- //WaitIdle();
- E=0;
- RS=1;
- RW=0;
- DPORT=c;
- Lcd1602_Delay1ms(1);
- E=1;
- Lcd1602_Delay1ms(5);
- E=0;
- DPORT=c<<4;
- Lcd1602_Delay1ms(1);
- E=1;
- Lcd1602_Delay1ms(5);
- E=0;
- }
- void LcdWc(uchar c)
- {
- //WaitIdle();
- LcdWcn(c);
- }
- void LcdWcn(uchar c)
- {
- E=0;
- RS=0;
- RW=0;
- DPORT=c;
- Lcd1602_Delay1ms(1);
- E=1;
- Lcd1602_Delay1ms(5);
- E=0;
- DPORT=c<<4;
- Lcd1602_Delay1ms(1);
- E=1;
- Lcd1602_Delay1ms(5);
- E=0;
- }
- void LcdPos(uchar xPos,uchar yPos)
- {
- uchar tmp;
- xPos&=0X0F;
- yPos&=0x01;
- if(yPos==0)
- tmp=xPos;
- else
- tmp=xPos+0x40;
- tmp|=0x80;
- LcdWc(tmp);
- }
- void RstLcd()
- {
- mDelay(15);
- LcdWc(0x32);
- LcdWc(0x28);
- LcdWc(0x01);
- LcdWc(0x06);
- LcdWc(0x0c);
- LcdWc(0x80);
- }
- void main(void)
- {
- RstLcd();
- //SetCur(CurFlash);
- WriteString("Hello World!",0,1);
-
- while(1)
- {
- }
- }
復(fù)制代碼 仿真效果圖:
9.jpg (242.68 KB, 下載次數(shù): 4)
下載附件
2024-5-25 21:44 上傳
仿真文件及C工程
LCD1602.rar
(118.35 KB, 下載次數(shù): 5)
2024-5-25 21:45 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|
評分
-
查看全部評分
|