用51通过MAX6675读取温度,并在1602上显示。
现在的程序只能显示开机读取的温度,温度变化后,不能显示在1602上,请大侠看看是程序什么地方出了问题。
单片机源码:
- #include <reg51.h>
- #include <Intrins.h>
- #include <LCD1602.H>
- #include <math.H>
- #define uchar unsigned char
- #define uint unsigned int
- sbit MAX6675_SO=P1^0;
- sbit MAX6675_SCK=P1^1;
- sbit MAX6675_CS=P1^2;
- bit ack,flag=0,flag1=0;
- uint tz;
- int t_zhi=0;
- char code keytab[]={0xEE,0xDE,0xBE,0x7E,0xED,0xDD,0xBD,0x7D,0xEB,0xDB,0xBB,0x7B,0xE7,0xD7,0xB7,0x77};
- unsigned char code str3[]={" "};
- unsigned char data disdata[5];
- unsigned int ReadMAX6675()
- {
- unsigned char count;
- unsigned int Value;
- MAX6675_CS=0;
- MAX6675_SCK=0;
- Value=0;
- _nop_() ;_nop_();_nop_();_nop_();
- for(count=0;count<16;count++)
- {
- Value=Value<<1;
- MAX6675_SCK=1;
- _nop_() ;_nop_();_nop_();_nop_();
- if(MAX6675_SO==1)
- Value|=0x01;
- MAX6675_SCK=0;
- }
- MAX6675_CS=1;
- return Value;
- }
- unsigned int GetCurrentTemp(unsigned int CurrentValue)
- {
- unsigned int TempValue;
- TempValue=CurrentValue;
- if(TempValue&0x8000)
- return 0;
- if(TempValue&0x0004)
- return 1;
- }
- void tempdisp() //ζèÖμÏÔê¾
- { unsigned char i;
- unsigned int TempValue;
- unsigned int testD2;
- int xiaoshu;
- TempValue=ReadMAX6675();
- testD2=GetCurrentTemp(TempValue);
- TempValue=ReadMAX6675();
- TempValue&=0x7ff8;
- TempValue>>=3;
- xiaoshu= (TempValue*1023.75/4095)*10-262;
- t_zhi=xiaoshu/10;
- disdata[0]=xiaoshu/10000+0x30;
- xiaoshu=xiaoshu%10000;
- disdata[1]=xiaoshu/1000+0x30;
- xiaoshu=xiaoshu%1000;
- disdata[2]=xiaoshu/100+0x30;
- xiaoshu=xiaoshu%100;
- disdata[3]=xiaoshu/10+0x30;
- xiaoshu=xiaoshu%10;
- disdata[4]=xiaoshu/1+0x30;
- LCD_set_position(68);
- for(i=0;i<4;i++)
- LCD_write_data(disdata[i]);
- LCD_write_data('.');
- LCD_write_data(disdata[4]);
- LCD_write_data(0xDF);
- LCD_write_data(0x43);
- }
- void main()
- {
- LCD_initial();
- LCD_cls();
- LCD_set_position(0);
- //LCD_prints(str1);
- LCD_set_position(3);
- MAX6675_CS=1;
- while(1)
- {
- ReadMAX6675();
- GetCurrentTemp();
- tempdisp();
- }
- }
复制代码
|