我有一个用的是51和ADC0832,代码如下,希望有帮助,代码如下#include <reg52.h> #include "intrins.h" #define u8 unsigned char #define u16 unsigned int #define uchar unsigned char #define uint unsigned int uchar yushe_Moisture=20; uchar yushe_MoistureH=40; uchar Moisture; //运行模式 uchar Mode=0; uchar Mode_1=0; //管脚声明 sbit LED_Moisture= P1^6; void delay_ms(uint q) { uint i,j; for(i=0;i<q;i++) for(j=0;j<110;j++); } /********************************************************************** LCD1602相关函数 **********************************************************************/ //LCD管脚声明 sbit LCDRS = P2^7; sbit LCDEN = P2^6; //LCD延时 void LCDdelay(uint z)// { uint x,y; for(x=z;x>0;x--) for(y=10;y>0;y--); } //写命令 void write_com(uchar com) { LCDRS=0; P0=com; LCDdelay(5); LCDEN=1; LCDdelay(5); LCDEN=0; } //写数据 void write_data(uchar date) { LCDRS=1; LCD_WriteData(date); P0=date; LCDdelay(5); LCDEN=1; LCDdelay(5); LCDEN=0; } //选择写入位置 void SelectPosition(unsigned char x,unsigned char y) { if (x == 0) { write_com(0x80 + y); } else { write_com(0xC0 + y); } } //写入字符串函数 void LCD_Write_String(unsigned char x,unsigned char y,unsigned char *s) { SelectPosition(x,y) ; while (*s) { write_data( *s); s ++; } } void LCD_Write_Char(u8 x,u8 y,u16 s,u8 l) { SelectPosition(x,y) ; if(l>=5) write_data(0x30+s/10000%10); if(l>=4) write_data(0x30+s/1000%10); if(l>=3) write_data(0x30+s/100%10); if(l>=2) write_data(0x30+s/10%10); if(l>=1) write_data(0x30+s%10); } write_com(0x38); write_com(0x0c); write_com(0x0d); write_com(0x0d); //1602初始化 void Init1602() { uchar i=0; write_com(0x38); write_com(0x0c); write_com(0x06); write_com(0x01); } void Display_1602(yushe_Moisture,yushe_MoistureH,temp) { //显示预设湿度 LCD_Write_Char(0,12,yushe_MoistureH,3) ; LCD_Write_Char(1,12,yushe_Moisture,3) ; LCD_Write_Char(0,3,temp,3) ; } /**********************************************************************ADC0832相关函数 **********************************************************************/ sbit ADCS =P1^3; sbit ADCLK =P1^0; sbit ADDI =P1^1; sbit ADDO =P1^1; unsigned int Adc0832(unsigned char channel) { uchar i=0; uchar j; uint dat=0; uchar ndat=0; uchar Vot=0; if(channel==0)channel=2; if(channel==1)channel=3; ADDI=1; _nop_(); _nop_(); ADCS=0; _nop_(); _nop_(); ADCLK=1; _nop_(); _nop_(); ADCLK=0; _nop_(); _nop_(); ADCLK=1; ADDI=channel&0x1; _nop_(); _nop_(); ADCLK=0; _nop_(); _nop_(); ADCLK=1; ADDI=(channel>>1)&0x1; _nop_(); _nop_(); ADCLK=0; ADDI=1; _nop_(); _nop_(); dat=0; for(i=0;i<8;i++) { dat|=ADDO; ADCLK=1; _nop_(); _nop_(); ADCLK=0; _nop_(); _nop_(); dat<<=1; if(i==7)dat|=ADDO; } for(i=0;i<8;i++) { j=0; j=j|ADDO; ADCLK=1; _nop_(); _nop_(); ADCLK=0; _nop_(); _nop_(); j=j<<7; ndat=ndat|j; if(i<7)ndat>>=1; } ADCS=1; ADCLK=0; ADDO=1; dat<<=8; dat|=ndat; return(dat); //return ad data } /**********************************************************************按键检测相关函数 **********************************************************************/ sbit Key1=P1^4; sbit Key2=P3^2; sbit Key3=P3^3; sbit Key4=P3^4; #define KEY_SET 1 #define KEY_ADD 2 #define KEY_MINUS 3 u8 Key_Scan() { static u8 key_up=1; //按键按松开标志 if(key_up&&(Key1==0||Key2==0||Key3==0)) { delay_ms(10); //去抖动 key_up=0; if(Key1==0) return 1; else if(Key2==0)return 2; else if(Key3==0)return 3; } else if(Key1==1&&Key2==1&&Key3==1) key_up=1; return 0; } void main (void) { u8 key; Moisture=Adc0832(0); Moisture=100-(Moisture*100/256); Init1602(); LCD_Write_String(0,0,"Ms:000% SEH:000%"); LCD_Write_String(1,0,"MODE:A SEL:000%"); delay_ms(1000); Moisture=Adc0832(0); Moisture=100-(Moisture*100/256); while (1) { key=Key_Scan(); Moisture=Adc0832(0); Moisture=100-(Moisture*100/256); if(key==KEY_SET) { Mode++; } switch(Mode) { case 0: { if(key==KEY_ADD) { Mode_1=!Mode_1; if(Mode_1) LCD_Write_String(1,5,"H"); else LCD_Write_String(1,5,"A"); } if(Mode_1) { if(key==KEY_MINUS) { LED_Moisture=!LED_Moisture; } } Display_1602(yushe_Moisture,yushe_MoistureH,Moisture); if(!Mode_1) { if(Moisture>=yushe_MoistureH) { LED_Moisture=1; } if(Moisture<=yushe_Moisture) { LED_Moisture=0; } } break; } case 1: { SelectPosition(0,11) ; write_com(0x0d); if(key==KEY_ADD) { if(yushe_MoistureH>=99) yushe_MoistureH=99; yushe_MoistureH++; LCD_Write_Char(0,12,yushe_MoistureH,3) ; } if(key==KEY_MINUS) { if(yushe_MoistureH<=1) yushe_MoistureH=1; yushe_MoistureH--; LCD_Write_Char(0,12,yushe_MoistureH,3) ; } break; } case 2: { SelectPosition(1,11) ; write_com(0x0d); if(key==KEY_ADD) { if(yushe_Moisture>=99) yushe_Moisture=99; yushe_Moisture++; LCD_Write_Char(1,12,yushe_Moisture,3) ; } if(key==KEY_MINUS) { if(yushe_Moisture<=1) yushe_Moisture=1; yushe_Moisture--; LCD_Write_Char(1,12,yushe_Moisture,3) ; } break; } default : { write_com(0x38); write_com(0x0c); Mode=0; break; } } } } |