- C/C++ code
#include <reg52.h>#define uchar unsigned char #define uint unsigned intconst uchar code sk[12] = "Temperature:";sbit lcdrs = P2^1;sbit lcden = P2^0;sbit oe = P2^7;sbit eoc = P2^2;sbit st = P2^3;sbit ale = P2^6;void delay(uchar j) { // ÑÓʱº¯Êý uchar i = 0; while (--j) { for (; i<110; ++i) ; }}void write_data(uchar dat) { // write_data function lcdrs = 1; lcden = 1; P0 = dat; delay(3); lcden = 0; lcdrs = 0;}void write_com(uchar com) { // write_com function lcdrs = 0; lcden = 1; P0 = com; delay(3); lcden = 0; lcdrs = 1;}void lcd_init() { write_com(0x38); delay(3); write_com(0x8); delay(3); write_com(0x1); delay(3); write_com(0x6); delay(3); write_com(0xc); delay(3);}int main(){ uchar te[5] = {'0','0','0','.','0'}; int n = 0; int getdata = 0; lcd_init(); // ³õʼ»¯ write_com(0x80+0x2); for (; n<12; ++n) { write_data(sk[n]); delay(1); } while (1) { ale = 1; st = 0; st = 1; // Æô¶¯×ª»» st = 0; while (!eoc); // µÈ´ýÐźÅת»»Íê±Ï£¬ eoc=1 ʱÍê±Ï oe = 1; // enable output getdata = P1; oe = 0; getdata = getdata*10*0.4; te[0] = '0' + getdata/1000; te[1] = '0' + getdata%1000/100; te[2] = '0' + getdata%100/10; te[4] = '0' + getdata%10; for (n=0; n<5; ++n) { write_com(0x80+0x46+n); write_data(te[n]); } } }
这个程序运行出错,显示数字不对,但是我从P1 口手动输入数字是正确的,应该是A/D 的问题,求大神指点,在线等!
------解决方案--------------------
线路图看的不是很清楚,那个ADC好像是0808或者0809吧。
看了datasheet的时序图,好像ALE也需要置0的。
ale = 0;
st = 0;
ale = 1;
st = 1; // Æô¶¯×ª»»
ale = 0;
st = 0;
------解决方案--------------------
就是你while(1)的后面的四句话,改成上面的六句话试试看。
------解决方案--------------------
write_com(0x8);
delay(3);
write_com(0x1);
delay(3);
write_com(0x6);
delay(3);
write_com(0xc);
delay(3);
为什么这初始化跟我写的不一样?
------解决方案--------------------
我按照你那么写,能编译成功,但在仿真上不能输出
------解决方案--------------------
没看出来有什么错误,不过插入延时看看吧:
while (1) {
ale = 1; // 锁定地址
delay(1);
ale = 0;
delay(1);
st = 1; // 开始
delay(1);
st = 0;
delay(3);
while (!eoc); // 等待 eoc=1
oe = 1; // enable output
getdata = P1;
oe = 0;
。。。。
------解决方案--------------------