电路应该是没问题,想请大家帮我看一下程序有没有问题,好像是LCD的初始化问题,具体小弟也不太懂,麻烦各位啦。程序是用光敏电阻做光照度计的。如果大家有其他方法解决的话也想请教一下,新手Proteus仿真的时候都没问题。
程序:
- #include <reg52.h>
- #define uint unsigned int
- #define uchar unsigned char
- sbit _st= P2^2;
- sbit _eoc=P2^1;
- sbit _oe=P2^0;
- uchar ad_result=0;
- int vol,lux;
- float realv;
- sbit rs=P2^5;
- sbit rw=P2^6;
- sbit en=P2^7;
- uchar code t1[]={" illuminometer "};
- uchar code t2[]={" Lux . Vol"};
- void delay(uint ms)
- {
- uint i,j;
- for(i=ms;i>0;i--)
- for(j=110;j>0;j--);
- }
- void writelcd_cmd(uchar cmd)
- {
- en=0;
- rs=0;
- rw=0;
- delay(1);
- P0=cmd;
- en=1;
- delay(1);
- en=0;
- }
- void writelcd_dat(uchar dat)
- {
- en=0;
- rs=1;
- rw=0;
- delay(1);
- P0=dat;
- en=1;
- delay(1);
- en=0;
- }
- void lcd_init()
- {
- uchar i,j;
- writelcd_cmd(0x38);
- delay(5);
- writelcd_cmd(0x38);
- delay(5);
- writelcd_cmd(0x38);
- writelcd_cmd(0x08);
- writelcd_cmd(0x01);
- writelcd_cmd(0x06);
- writelcd_cmd(0x0c);
- writelcd_cmd(0x80);
-
- for(i=0;i<16;i++)
- {
- writelcd_dat(t1[i]);
- }
- writelcd_cmd(0xc0);
- for(j=0;j<16;j++)
- {
- writelcd_dat(t2[j]);
- }
- }
- void display(uint dat,uchar add)
- {
- uchar bai,shi ,ge,sfen;
- bai=dat/1000;
- shi=dat/100%10;
- ge=dat/10%10;
- sfen=dat%10;
- writelcd_cmd(add);
- writelcd_dat(bai+0x30);
- writelcd_cmd(add+2);
- writelcd_dat(shi+0x30);
- writelcd_dat(ge+0x30);
- writelcd_dat(sfen+0x30);
- }
- void display2(uint dat,uchar add)
- {
- uchar bai,shi ,ge,sfen,wan;
- wan=dat/10000;
- bai=dat/1000&10;
- shi=dat/100%10;
- ge=dat/10%10;
- sfen=dat%10;
- writelcd_cmd(add);
- writelcd_dat(wan+0x30);
- writelcd_dat(bai+0x30);
- writelcd_dat(shi+0x30);
- writelcd_dat(ge+0x30);
- writelcd_dat(sfen+0x30);
- }
- void main(void)
- {
- lcd_init();
- EA=1;
- TH0=(65536-50000)/256;
- TL0=(65536-50000)%256;
- TR0=1; //?ú2??D??T0?a??
- ET0=1;
- while(1)
- {
-
- _st=0;_st=1;_st=0;
- while(!_eoc);
- _oe=1;
- ad_result=P1;
- _oe=0;
-
- vol=5000-(100.0*ad_result/5.1);
- realv=vol/1000.0;
- lux=864/((10*realv)/(5-realv)-1.36);
- if(lux<=30000 && lux>=1)
- {
- lux=864/((10*realv)/(5-realv)-1.36);
- display2(lux,0xc0);
- display(vol,0xc8);
- }
- else
- {
- display(vol,0xc8);
- }
- }
- }
- void count0() interrupt 1
- {
- P3=0xff;
-
- P3=0xfe;
-
- }
复制代码
程序最后这一步没看懂是在干嘛,也想问一下
void count0() interrupt 1
{
P3=0xff;
P3=0xfe;
}
|