#include<reg52.h> #include<I2C.H> #define SET 20 #define PCF8591 0X90 //PCF8591地址 code unsigned char t[]={ 0xc0,0xf9,0xa4,0xb0,0x99,0x92, 0x82,0xf8,0x80,0x90,0x88,0x83, 0xc6,0xa1,0x86,0x8e }; sbit D1=P2^0; sbit D2=P2^1; sbit D3=P2^2; sbit D4=P2^3; sbit BEEP=P1^7 void delayms(unsigned char); void display(unsigned char); void adc(void); bit ISendByte(unsugned char sla,unsignedchar c); unsigned char IRcvByte(unsigned char sla); void warning(void); unsigned char n; void main() {while(1) { adc(); display(n); warning(); } } bit ISendByte(unsugned char sla,unsignedchar c) { Start_I2c(); SendByte(sla); if(ack == 0) return 0; SendByte(c); if(ack == 0) return 0; Stop_I2c(); return 1; } unsigned char IRcvByte(unsigned char sla) { unsignedchar c; Start_I2c(); SendByte(sla+1); c=RcvByte(); Ack_I2c(1); Stop_I2c(); return c; } void adc(void) { ISendByte(PCF8591,0x00|2); n=IRcvByte(PCF8591); } void delayms(unsigned char x){ unsigned char i,j; for(i = 0;I < x;i++) { for(j = 0;j < 123;j++); } } void display(unsigned char x){ unsinged char bai,shi,ge; bai=x/100; shi=x%100/10; ge=x%10; D3=1; D1=0; P0=t[bai]; delayms(5); P0=0xff; D1=1; D2=0; P0=t[shi]; delayms(5); P0=0xff; D1=1; D3=0; P0=t[ge]; delayms(5); P0=0xff; } void warning(void){ if(n> SET) BEEP = 1; elseBEEP = 0; }
|