一、EEPROM、AD/DA电路
AD/DA电路:光敏电阻、电压分压
EEOROM:保存数据——数据写进和读出
二、IIC协议介绍
三、PCF8591、AT24C02编程
3.1 PCF8591
先写在读
1.告诉ADC读哪个通道
2.然后接收回来
3.2 AT24C02
四、练习
1.编程实现PCF8591读取通道1和通道3的电压值:
2.编程实现AT24C02的读写, 让0x55单元的数值每次上电自加1。
五、代码展示
#include "reg52.h" #include "intrins.h" #include "iic.h" typedef unsigned char BYTE; typedef unsigned int WORD; unsigned char adc_table[3]; unsigned char volt_table[3]; unsigned char open_num_table[2]; //----------------------------------------------- /* define constants */ #define FOSC 11059200L #define T1MS (65536-FOSC/12/1000) //1ms timer calculation method in 12T mode /* define SFR */ sbit TEST_LED = P1^0; //work LED, flash once per second /* define variables */ WORD count; //1000 times counter //----------------------------------------------- unsigned char code T_display[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F}; unsigned char code T_COM[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80}; /* Timer0 interrupt routine */ void tm0_isr() interrupt 1 using 1 { static char i; TL0 = T1MS; //reload timer0 low byte TH0 = T1MS >> 8; //reload timer0 high byte if (count-- == 0) //1ms * 1000 -> 1s { count = 1; //reset counter P2=0xe0;P0=~open_num_table[i];P2=0; P2=0xc0;P0=T_COM[i];P2=0; i++; if(i==2) i=0; } } //----------------------------------------------- void Delay2ms() //@11.0592MHz { unsigned char i, j; _nop_(); i = 4; j = 146; do { while (--j); } while (--i); } /* main program */ void main() { unsigned char adc_val; unsigned char open_num=0; float volt; open_num =read_24c02(0x55); write_24c02(0x55,++open_num); open_num_table[0]=T_display[open_num/10]; open_num_table[1]=T_display[open_num%10]; TMOD = 0x01; //set timer0 as mode1 (16-bit) TL0 = T1MS; //initial timer0 low byte TH0 = T1MS >> 8; //initial timer0 high byte TR0 = 1; //timer0 start running ET0 = 1; //enable timer0 interrupt EA = 1; //open global interrupt switch count = 0; //initial counter write_adc(0x03); // ch3 while(1) { EA=0; adc_val=read_adc(0x03); EA=1; volt=adc_val/255.0f*5; adc_table[0]=adc_val/100;//bai adc_table[1]=adc_val/10%10;//shi adc_table[2]=adc_val%10; volt_table[0]=T_display[(unsigned char)volt] | 0x80; volt_table[1]=T_display[(unsigned char)(volt*10)%10]; volt_table[2]=T_display[(unsigned char)(volt*100)%10]; Delay2ms(); } }
一些事项
PCF8591的通道1是光敏电阻,通道3是ADC转换
24C02与EEPROM可以掉电保存