#include
/*这一块是定义DS18B20的*/ #include \
#define uchar unsigned char #define uint unsigned int
/*这一块是负责AT24C02的*/
#include\为了调用_nop_字符的 #define OP_READ 0xa1 #define OP_WRITE 0xa0 unsigned char SavedData[7];
#define SaveKey 0 //保存数据键:第1号键 #define DisKey 1 //显示数据键:第2号键 #define SaveNumPlusKey 2 //数据自加键:第3号键
uchar atemp=0,write_dat=0;
uchar code num[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; //数字数组0-9
uchar code drop_num[10]={0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10}; //带点数字数组0-9
sbit buzz=P2^1;
sbit bit_select4=P2^7;//数码管的第四位选 sbit bit_select3=P2^6;//数码管的第二位选 sbit bit_select2=P2^5; //数码管的第三位选 sbit bit_select1=P2^4; //数码管的第一位选 sbit SDA = P3^3; //定义AT24C02的数据位 sbit SCL = P3^2;//定义AT42C02的时钟位 sbit ds=P2^0;//定义DS18B20的管脚 uint temp,flag=0;//定义整形的温度数据 float f_temp;//定义一个浮点型的温度数据 void delay_nms(unsigned int ms); void BUZZ(unsigned int ms); unsigned char key_value;
//=================8个独立按键位定义============================= sbit key0=P1^0; sbit key1=P1^1; sbit key2=P1^2; sbit key3=P1^3; sbit key4=P1^4; sbit key5=P1^5; sbit key6=P1^6; sbit key7=P1^7;
//===================独立按键扫描子函数========================== unsigned char KeyScan() {
// 器件地址以及读取操作 // 器件地址以及写入操作
// AT24C02最大地址
#define MAX_ADDR 0x7f
if(P1!=0xff) {
delay_nms(38);
if(key0==0) key_value=0; else if(key1==0) key_value=1; else if(key2==0) key_value=2; else if(key3==0) key_value=3; else if(key4==0) key_value=4; else if(key5==0) key_value=5; else if(key6==0) key_value=6; else if(key7==0) key_value=7; BUZZ(100);
while(P1!=0XFF); return key_value; }
return 16;
}
/*************1 MS 延时函数*************************************/ void delay_nms(unsigned int ms) { unsigned char i;
while(ms--) { }
for(i = 0; i < 120; i++);
}
/****************蜂鸣器子函数************************************/ void BUZZ(unsigned int ms) {
buzz=0; delay_nms(ms); buzz=1;
}
//============数码管显示函数===================================== void LedDis(unsigned char where,unsigned char what)
{ }
unsigned char i; for(i=0;i<125;i++) {
P0= num[what]; P2=where; delay_nms(1); }
/***********AT24C02的初始位*************************************/ void AT24C_Init() {
SDA = 1; SCL = 1;
}
/****************AT24C02的开始位******************************/ void start() { SDA = 1;
SCL = 1; _nop_(); _nop_(); SDA = 0; _nop_(); _nop_(); _nop_(); _nop_();
SCL = 0; }
/****************AT24C02的停止位******************************/ void stop() {
SDA = 0; _nop_(); _nop_(); SCL = 1; _nop_(); _nop_(); _nop_(); _nop_(); SDA = 1;
}
void clean() //消影 {
P0=0xff; bit_select1=1; bit_select2=1; bit_select3=1; bit_select4=1;
}
/****************从AT24Cxx移入数据到单片机******************************/ unsigned char shin() { }
unsigned char i,read_data; for(i = 0; i < 8; i++) {
SCL = 1;
read_data <<= 1;
read_data |= (unsigned char)SDA; SCL = 0;
} return(read_data);
/****************从MCU移出数据到AT24Cxx*****************************/ bit shout(unsigned char write_data) {
unsigned char i; bit ack_bit;
for(i = 0; i < 8; i++) { }
// 循环移入8个位
SDA = (bit)(write_data & 0x80); _nop_(); SCL = 1; _nop_(); _nop_(); SCL = 0;
write_data <<= 1;
// 读取应答
SDA = 1; _nop_(); _nop_(); SCL = 1; _nop_(); _nop_(); _nop_();
_nop_();
ack_bit = SDA; SCL = 0;
return ack_bit; // 返回AT24Cxx应答位
写
入
数
据
}
/**************** 在指定地址addr处write_data*****************************/
void AT24C_WriteByte(unsigned char addr, unsigned char write_data) {
start();
shout(OP_WRITE); shout(addr); shout(write_data); stop(); delay_nms(10);
// 写入周期
}
/**************** 填充数据fill_data到EEPROM内*****************************/ void fill_byte(unsigned char fill_data) {
unsigned char i;
for(i = 0; i < MAX_ADDR; i++) { }
AT24C_WriteByte(i, fill_data);
}
/**************** 在当前地址读取*****************************/ unsigned char AT24C_ReadCurrent() {
unsigned char read_data; start();
shout(OP_READ); read_data = shin(); stop();
return read_data;
}
/**************** 在指定地址读取*****************************/ unsigned char AT24C_ReadRandom(unsigned char random_addr) {
start();
shout(OP_WRITE);
shout(random_addr);
return(AT24C_ReadCurrent());
}
/****************18B20复位,初始化函数*****************************/ void dsreset(void)
百度搜索“77cn”或“免费范文网”即可找到本站免费阅读全部范文。收藏本站方便下次阅读,免费范文网,提供经典小说综合文库基于GSM模块的房间报警程序设计在线全文阅读。
相关推荐: