完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
典型89C52单片机,电流采集元件用ACS712,AD芯片是ADC0832,显示为LCD12864。 给一个定值可以显示,证明显示部分是没问题的。 接上传感器和AD之后就一直是显示定值(有时还是乱码),测不到电流变化。 程序和电路图都给上了,大神帮忙看看是什么原因 #include #include #include #include #define delay4us() {_nop_();_nop_();_nop_();_nop_();} #define uint unsigned int #define uchar unsigned char #define ulong unsigned long ***it CS =P1^5; ***it DIO =P1^7; ***it CLK =P1^6; ***it RS=P2^2; ***it RW=P2^1; ***it E=P2^0; ulong dat1,dat2; uchar code Dianliu_DAT[ ]={"电流值:"}; uchar code Wwendu_DAT[ ]={"温度值:"}; uchar Display_Dianliu_Buffer[]="0.00A"; uchar Display_Wendu_Buffer[]="0.00℃ " ; uchar code Dianya_DAT1[]={"电压值1:"}; uchar code Dianya_DAT2[]={"电压值2:"}; uchar code Dianya_DAT3[]={"电压值3:"}; uchar code Dianya_DAT4[]={"电压值4:"}; uchar code Dianya_DAT5[]={"电压值5:"}; uchar code Dianya_DAT6[]={"电压值6:"}; uchar code Dianya_DAT7[]={"电压值7:"}; uchar code Dianya_DAT8[]={"电压值8:"}; uchar Display_Dianya_Buffer1[]="0.00V " ; uchar Display_Dianya_Buffer2[]="0.00V " ; uchar Display_Dianya_Buffer3[]="0.00V " ; uchar Display_Dianya_Buffer4[]="0.00V " ; uchar Display_Dianya_Buffer5[]="0.00V " ; uchar Display_Dianya_Buffer6[]="0.00V " ; uchar Display_Dianya_Buffer7[]="0.00V " ; uchar Display_Dianya_Buffer8[]="0.00V " ; void Delayms(uint ms) { uchar t; while(ms--) for (t=0;t<120;t++); } bit LCD_Busy_Check() { bit result; RS=0; RW=1; E=1; delay4us(); result=(bit)(P0&0x80); E=0; return result; } void LCD_Write_Command(uchar cmd) { while(LCD_Busy_Check()); RS=0;RW=0;E=0;_nop_();_nop_();P0=cmd;delay4us(); E=1;delay4us();E=0; } void Set_Disp_Pos(uchar pos) { LCD_Write_Command(pos|0x80); } void LCD_Write_Data(uchar dat) { while(LCD_Busy_Check()); RS=1; RW=0; E=0; P0=dat; delay4us(); E=1; delay4us(); E=0; } void LCD_Initialise() { LCD_Write_Command(0x30);Delayms(1); LCD_Write_Command(0x38);Delayms(1); LCD_Write_Command(0x0c);Delayms(1); LCD_Write_Command(0x06);Delayms(1); LCD_Write_Command(0x01);Delayms(1); } void lcd_mesg(unsigned char code *addr1,unsigned int m,unsigned int n) { unsigned char i; Delayms(1); for(i=m;i LCD_Write_Data(*addr1); addr1++; } } uchar Get_AD_Result() { uchar i; //ulong dat1=0,dat2=0; CS = 0; //使能芯片 CLK = 0; //拉低CLK形成下降沿 DIO = 1; _nop_(); _nop_(); //第一个下降沿之前DI为高电平,表启始信号 CLK = 1; _nop_(); _nop_(); CLK = 0;DIO = 1; _nop_(); _nop_(); //拉低CLK形成下降沿,设置D1=1/0,选择单端输入模式 CLK = 1; _nop_(); _nop_(); CLK = 0;DIO = 0; _nop_(); _nop_(); //设置D1=0/1,选择通道ch0 CLK = 1;DIO = 1;_nop_(); _nop_(); CLK = 0;DIO = 1; _nop_(); _nop_(); //D1口失去输入作用,D0口作输出准备 for(i=0;i<8;i++) { CLK=1;_nop_();_nop_(); CLK=0;_nop_();_nop_(); dat1=dat1<<1|DIO; } for(i=0;i<8;i++) { dat2=dat2|((uchar)(DIO)< CLK=1;_nop_();_nop_(); CLK=0;_nop_();_nop_(); } CS=1; return(dat1==dat2)?dat1:0; } void main(void) { uchar i; float temp; uint AD_ad; LCD_Initialise(); do { dat1=Get_AD_Result(); temp=((float)dat1)*5/256;// 实际电压值 AD_ad=((temp-2.5)/0.1)*100; //换算电流 Display_Dianliu_Buffer[0]=AD_ad/100+'0'; Display_Dianliu_Buffer[2]=AD_ad/10%10+'0'; Display_Dianliu_Buffer[3]=AD_ad%10+'0'; Set_Disp_Pos(0x10); lcd_mesg(Dianliu_DAT,0,8); Set_Disp_Pos(0x14); i=0; while(Display_Dianliu_Buffer[i]!=' |