AT24C02——就是一个存储的东西,可以给他写东西,掉电不丢失。
void EEPROM_Write(unsigned char * EEPROM_String,unsigned char addr , unsigned char num)
{IIC_Start();IIC_SendByte(0xA0);IIC_WaitAck();IIC_SendByte(addr);IIC_WaitAck();while(num--){IIC_SendByte(*EEPROM_String++);IIC_WaitAck();IIC_Delay(200);}IIC_Stop();
}
void EEPROM_Read(unsigned char * EEPROM_String,unsigned char addr , unsigned char num)
{IIC_Start();IIC_SendByte(0xA0);IIC_WaitAck();IIC_SendByte(addr);IIC_WaitAck();IIC_Start();IIC_SendByte(0xA1);IIC_WaitAck();while(num--){*EEPROM_String++=IIC_RecByte();if(num) IIC_SendAck(0);//·¢ËÍÓ¦´ðelse IIC_SendAck(1);}IIC_Stop();
}
这个是这个代码,先开始IIC看他在不在(0xA0)等待应答,然后发送要写的页,和字节。然后发送然后等待 停止。
读——
一样的是先开始,然后找他,等待,然后写入页,然后IIC开始,进入读模式,然后等待。然后将读的东西给,数组。if num==0 就停止应答
下面是测试代码:
#include <STC15F2K60S2.H>
#include <iic.h>
void EEPROM_Write(unsigned char * EEPROM_String,unsigned char addr , unsigned char num);
void EEPROM_Read(unsigned char * EEPROM_String,unsigned char addr , unsigned char num);
unsigned char code Duan[22]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x80,
0xc6,0xc0,0x86,0x8e,0xbf,0x7f,0XC1,0X8C,0XC8,0xc3};
unsigned char dat[2]={30,60},keynum;
void Delay(unsigned int t)
{while(t--);}
void suocun(unsigned char n)
{switch(n){case 4:P2=(P2 & 0x1f)|0x80;break;case 5:P2=(P2 & 0x1f)|0xa0;break;case 6:P2=(P2 & 0x1f)|0xc0;break;case 7:P2=(P2 & 0x1f)|0xe0;break;}
}
void smg(unsigned char wei,unsigned char shu)
{suocun(6);P0=0x01<<wei;suocun(7);P0=Duan[shu];Delay(500);P0=0xff;
}
void shuma()
{smg(0,dat[0]/10);Delay(100);smg(1,dat[0]%10);Delay(100);smg(6,dat[1]/10);Delay(100);smg(7,dat[1]%10);Delay(100);
}
void jvzhen()
{P30=P31=P32=P33=1;P34=P35=P42=1;P44=0;if(P33==0){Delay(100);keynum=4;while(P33==0){shuma();}}if(P32==0){Delay(100);keynum=5;while(P32==0){shuma();}}if(P31==0){Delay(100);keynum=6;while(P31==0){shuma();}}if(P30==0){Delay(100);keynum=7;while(P30==0){shuma();}}
}
void zhixing()
{if(keynum==4){dat[0]+=10;keynum=0;}if(keynum==5){dat[0]-=10;keynum=0;}if(keynum==6){EEPROM_Write(dat,0,2);keynum=0;}}
void EEPROM_Write(unsigned char * EEPROM_String,unsigned char addr , unsigned char num)
{IIC_Start();IIC_SendByte(0xA0);IIC_WaitAck();IIC_SendByte(addr);IIC_WaitAck();while(num--){IIC_SendByte(*EEPROM_String++);IIC_WaitAck();IIC_Delay(200);}IIC_Stop();
}
void EEPROM_Read(unsigned char * EEPROM_String,unsigned char addr , unsigned char num)
{IIC_Start();IIC_SendByte(0xA0);IIC_WaitAck();IIC_SendByte(addr);IIC_WaitAck();IIC_Start();IIC_SendByte(0xA1);IIC_WaitAck();while(num--){*EEPROM_String++=IIC_RecByte();if(num) IIC_SendAck(0);//·¢ËÍÓ¦´ðelse IIC_SendAck(1);}IIC_Stop();
}void main()
{EEPROM_Read(dat,0,2);while(1){shuma();jvzhen();zhixing();}
}
DS18B20——温度读取
float read_t()
{unsigned char low,high;init_ds18b20();Write_DS18B20(0xcc);Write_DS18B20(0x44);init_ds18b20();Write_DS18B20(0xcc);Write_DS18B20(0xbe);low = Read_DS18B20();high = Read_DS18B20();return ((high<<8)|low)/16.0;}
下面是测试代码,可能会有温度显示不正常,一直是85,就是DS18B20的Delay不正常,要在里面有一句 t*=12
#include <STC15F2K60S2.H>
#include <onewire.h>
#include <intrins.h>
sbit s0 = P3^0;
sbit s1 = P3^1;
sbit s2 = P3^2;
sbit s3 = P3^3;sbit r0 = P3^4;
sbit r1 = P3^5;
sbit r2 = P4^2;
sbit r3 = P4^4;
float t=31.5;
unsigned char keynum;
unsigned char code Duan[22]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x80,
0xc6,0xc0,0x86,0x8e,0xbf,0x7f,0XC1,0X8C,0XC8,0xc3};
void Delay750ms() //@12.000MHz
{unsigned char i, j, k;_nop_();_nop_();i = 35;j = 51;k = 182;do{do{while (--k);} while (--j);} while (--i);
}void Delaysmg(unsigned int t)
{while(t--);
}
void Delayjan(unsigned char t)
{while(t--);
}
void suocun(unsigned char n)
{switch(n){case 4:P2 = (P2 & 0x1f) | 0x80; break;case 5:P2 = (P2 & 0x1f) | 0xa0; break;case 6:P2 = (P2 & 0x1f) | 0xc0; break;case 7:P2 = (P2 & 0x1f) | 0xe0; break;case 0:P2 = (P2 & 0x1f) | 0x00; break;}
}
void shuma(unsigned char wei,unsigned char shu)
{suocun(6);P0=0x01<<wei;suocun(0);suocun(7);P0=Duan[shu];Delaysmg(1000);P0=0xff;suocun(0);
}
void shumadong()
{t = read_t();shuma(5,(int)t/10%10);Delaysmg(500);shuma(6,(int)t%10);Delaysmg(500);shuma(7,(int)(t*10)%10);Delaysmg(500);}
void Delay_Key(unsigned char t)
{while(t--);
}
void jvzhen()
{s0=0;s1=s2=s3=1;r0=r1=r2=r3=1;if(r3==0){while(r3==0);keynum=0;shumadong();}
//=============================s0=0;s1=s2=s3=1;r0=r1=r2=r3=1;if(r2==0){while(r2==0);keynum=1;shumadong();}
//=============================s0=0;s1=s2=s3=1;r0=r1=r2=r3=1;if(r1==0){while(r1==0);keynum=2;shumadong();}
//=============================s0=0;s1=s2=s3=1;r0=r1=r2=r3=1;if(r2==0){while(r2==0);keynum=2;shumadong();}
//=========================================================s1=0;s0=s2=s3=1;r0=r1=r2=r3=1;if(r3==0){while(r3==0);keynum=3;shumadong();}
//=============s1=0;s0=s2=s3=1;r0=r1=r2=r3=1;if(r2==0){while(r2==0);keynum=4;shumadong();}
//=============s1=0;s0=s2=s3=1;r0=r1=r2=r3=1;if(r1==0){while(r1==0);keynum=5;shumadong();}
//====================================s2=0;s0=s1=s3=1;r0=r1=r2=r3=1;if(r3==0){while(r3==0);keynum=6;shumadong();}
//===================s2=0;s0=s1=s3=1;r0=r1=r2=r3=1;if(r2==0){while(r2==0);keynum=7;shumadong();}
//====================s2=0;s0=s1=s3=1;r0=r1=r2=r3=1;if(r1==0){while(r1==0);keynum=8;shumadong();}
//=================================s3=0;s0=s1=s2=1;r0=r1=r2=r3=1;if(r3==0){while(r3==0);keynum=9;shumadong();}
}
void main()
{while(1){shumadong();}
}
DS1302——时间的计时
unsigned char wirter[7]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c};
unsigned char read[7]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};
//20Äê18ÈÕ ÖÜÁù 23.59.24
unsigned char Timer[7]={0x24,0x59,0x23,0x18,0x04,0x06,0x20};
unsigned char code Duan[22]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x80,
0xc6,0xc0,0x86,0x8e,0xbf,0x7f,0XC1,0X8C,0XC8,0xc3};
void Delaysmg(unsigned int t)
{while(t--);
}
void Ds1302()
{char i;Write_Ds1302_Byte(0x8e,0x00);for (i=0;i<7;i++){Write_Ds1302_Byte(wirter[i],Timer[i]);}Write_Ds1302_Byte(0x8e,0x80);
}
void readds()
{char i;for(i=0;i<7;i++){Timer[i]=Read_Ds1302_Byte(read[i]);}
}
写入有七个地址,读也是七个,然后存入数据 ,,上面我是 2020年周六4月18日,23分59分24秒
刚刚好七位,就是分别写入七个地址。
下面是测试代码:
#include <STC15F2K60S2.H>
#include <ds1302.h>
unsigned char wirter[7]={0x80,0x82,0x84,0x86,0x88,0x8a,0x8c};
unsigned char read[7]={0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};
//20Äê18ÈÕ ÖÜÁù 23.59.24
unsigned char Timer[7]={0x24,0x59,0x23,0x18,0x04,0x06,0x20};
unsigned char code Duan[22]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x80,
0xc6,0xc0,0x86,0x8e,0xbf,0x7f,0XC1,0X8C,0XC8,0xc3};
void Delaysmg(unsigned int t)
{while(t--);
}
void Ds1302()
{char i;Write_Ds1302_Byte(0x8e,0x00);for (i=0;i<7;i++){Write_Ds1302_Byte(wirter[i],Timer[i]);}Write_Ds1302_Byte(0x8e,0x80);
}
void readds()
{char i;for(i=0;i<7;i++){Timer[i]=Read_Ds1302_Byte(read[i]);}
}
void suocun(unsigned char n)
{switch(n){case 4:P2=(P2 & 0x1f)|0x80;break;case 5:P2=(P2 & 0x1f)|0xa0;break;case 6:P2=(P2 & 0x1f)|0xc0;break;case 7:P2=(P2 & 0x1f)|0xe0;break;case 0:P2=(P2 & 0x1f)|0x00;break;}
}
void smg(unsigned char wei,unsigned char shu)
{suocun(6);P0=0x01<<wei;suocun(7);P0=Duan[shu];Delaysmg(500);P0=0xff;
}
void xianshi()
{smg(0,Timer[2]/16);Delaysmg(100);smg(1,Timer[2]%16);Delaysmg(100);smg(3,Timer[1]/16);Delaysmg(100);smg(4,Timer[1]%16);Delaysmg(100);smg(6,Timer[0]/16);Delaysmg(100);smg(7,Timer[0]%16);Delaysmg(100);}
void main()
{Ds1302();while(1){readds();xianshi();}
}
PCF8591——数模转换 AD/DA
unsigned char Ad_read(unsigned char addr)
{unsigned char temp;IIC_Start();IIC_SendByte(0x90);//дÈëIIC_WaitAck();IIC_SendByte(addr);//Òª¶ÁµÄÊÇIIC_WaitAck();IIC_Start();IIC_SendByte(0x91);//¶ÁIIC_WaitAck();temp = IIC_RecByte();IIC_SendAck(1);IIC_Stop();return temp;
}void Da_write(unsigned char dat)
{IIC_Start();IIC_SendByte(0x90);//дÈëIIC_WaitAck();IIC_SendByte(0x41);//ʹÄÜDACIIC_WaitAck();IIC_SendByte(dat);//дÈëIIC_WaitAck();IIC_Stop();
}
经典的IIC。
读——
开始IIC,发送找到模块(0x90),等待,然后从哪里读数据,等待,然后IIC开始读,停止响应,然后IIC停止。
写——
开始 写数据,写入到0x41(0x43)等待响应,写入,然后写入。
测试代码:
#include <STC15F2K60S2.H>
#include <iic.h>
unsigned char code Duan[22]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x80,
0xc6,0xc0,0x86,0x8e,0xbf,0x7f,0XC1,0X8C,0XC8,0xc3};
unsigned char dat;
void Delay(unsigned int t)
{while(t--);
}
void suocun(unsigned char n)
{switch(n){case 4:P2=(P2 & 0x1f)|0x80;break;case 5:P2=(P2 & 0x1f)|0xa0;break;case 6:P2=(P2 & 0x1f)|0xc0;break;case 7:P2=(P2 & 0x1f)|0xe0;break;}
}
void shuma(unsigned char wei,unsigned char shu)
{suocun(6);P0=0x01<<wei;suocun(7);P0=Duan[shu];Delay(500);P0=0xff;
}
void xianshi()
{shuma(1,1);Delay(100);shuma(2,2);Delay(100);
}
void chuli()
{dat=Ad_read(0x43);shuma(0,dat/100%10);Delay(100);shuma(1,dat/10%10);Delay(100);shuma(2,dat%10);Delay(100);}
void main()
{while(1){chuli();}
}
超声波——
左边发松所以左边为TX ,右边为RX
void Ut_Wave_init()
{unsigned char i;for(i=0;i<8;i++){TX=1;Delay12us();TX=0;Delay12us();}
}
unsigned char Ut_Wave_Data()
{unsigned int time;TMOD &= 0x0f;TH1=TL1=0;Ut_Wave_init();TR1=1;while((RX==1)&&(TF1 == 0));TR1=0;if(TF1==0){time = TH1<<8|TL1;return (time*0.017);}else{TF1=0;return 0;}
}
发送一个方波信号,
然后就是用定时器1去完成,波的计算。发送完成之后开启定时器,然后等待,如果RX==0 或者计数溢出(TF1=1)就会退出,停止计数。得到TH1和TL1。然后输出距离.
实例代码:
#include <STC15F2K60S2.H>
#include <intrins.h>
sbit TX = P1^0;
sbit RX = P1^1;
floatjvli;
unsigned char code Duan[22]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x80,
0xc6,0xc0,0x86,0x8e,0xbf,0x7f,0XC1,0X8C,0XC8,0xc3};void Delay12us() //@12.000MHz
{unsigned char i;_nop_();_nop_();i = 33;while (--i);
}
void Delay(unsigned int t)
{while(t--);
}
void suocun(unsigned char n)
{switch(n){case 4:P2= (P2 & 0x1f)|0x80;break;case 5:P2= (P2 & 0x1f)|0xa0;break;case 6:P2= (P2 & 0x1f)|0xc0;break;case 7:P2= (P2 & 0x1f)|0xe0;break;}
}
void smg(unsigned char wei,unsigned char shu)
{suocun(6);P0=0x01<<wei;suocun(7);P0=Duan[shu];Delay(500);P0=0xff;
}
void Ut_Wave_init()
{unsigned char i;for(i=0;i<8;i++){TX=1;Delay12us();TX=0;Delay12us();}
}
unsigned char Ut_Wave_Data()
{unsigned int time;TMOD &= 0x0f;TH1=TL1=0;Ut_Wave_init();TR1=1;while((RX==1)&&(TF1 == 0));TR1=0;if(TF1==0){time = TH1<<8|TL1;return (time*0.017);}else{TF1=0;return 0;}
}
void xianshi()
{smg(1,(int)jvli/100%10);Delay(100);smg(2,(int)jvli/10%10);Delay(100);smg(3,(int)jvli%10);Delay(100);}
void main()
{while(1){jvli=Ut_Wave_Data();xianshi();}
}