单⽚机断电记忆⽅法C语⾔,单⽚机实现具有掉电记忆功能的计
时器
#define uchar unsigned char
#define uint unsigned int
/*IIC总线端⼝定义*/
sbit SDA = P2^0;
sbit SCL = P2^1;
/*LCD1602显⽰模块端⼝定义*/
sbit lcdrs = P1^2;
sbit lcdrw = P1^3;
sbit lcden = P1^4;
/
*主函数模块函数声明*/
void writeData24C02(uchar address, uchar dat);
void readData24C02(uchar address, uchar * dat);
void checkData(uchar * dat);
void timerInit();
/*IIC总线模块函数声明*/
void initIIC();
void startIIC();
void responseIIC();
void stopIIC();
void writeDataIIC(uchar dat);
void readDataIIC(uchar * dat);
/*LCD1602显⽰模块函数声明*/
void LCDInit();
void display(uchar dat1, uchar dat2);
void writeCommand(uchar command);
void writeDataLCD(uchar dat);
void delay(uchar xms);
/*--------------------------------------------
FileName: main.c
Author: Zhang Kaizhou
Date: 2019-6-12 18:06:18
-
-------------------------------------------*/
#include "clock.h"
/*定义全局变量*/
uchar second = 0, count = 0;
bit flag = 0;
void main(){
LCDInit();
initIIC();
readData24C02(1, &second); // 通过IIC总线将24C02的第1个存储单元中的数据读取到变量second中checkData(&second); // ⾸次读取数据时,原来芯⽚中的数据可能超过100(⾮法数据)
timerInit();
TR0 = 1; // 开始计时
while(1){
display(second / 10, second % 10);
if(flag){
flag = 0;
writeData24C02(1, second);
}
}
}
/*向24C02的任意地址address处写⼊数据dat*/
void writeData24C02(uchar address, uchar dat){
startIIC();
writeDataIIC(0xa0); // 向IIC总线发送寻址信号,并声明要进⾏写操作
responseIIC();
writeDataIIC(address);
responseIIC();
writeDataIIC(dat);
responseIIC();
stopIIC();
void readData24C02(uchar address, uchar * dat){
startIIC();
writeDataIIC(0xa0);
responseIIC();
writeDataIIC(address);
responseIIC();
startIIC();
writeDataIIC(0xa1); // 向IIC总线发送寻址信号,并声明要进⾏读操作responseIIC();
readDataIIC(dat);
stopIIC();
}
/*检测从24C02读取的数据是否合法*/
void checkData(uchar * dat){
if((* dat) > 100){
* dat = 0;
}
}
/*timer初始化*/
void timerInit(){
TMOD = 0x01; // timer0 定时模式 ⼯作⽅式1(16位定时器)
TH0 = (65536 - 46080) / 256; // 系统晶振为11.0592MHz,定时50ms TL0 = (65536 - 46080) % 256;
ET0 = 1; // 开定时器0溢出中断
EA = 1; // 开全局中断
}
/*timer0溢出中断服务程序*/
void timer0Service() interrupt 1{
TH0 = (65536 - 46080) / 256; // 重装初值
TL0 = (65536 - 46080) % 256;
count++;
if(count == 20){ // 50ms * 20 = 1s
if(second == 100){ // 0 ~ 99s
second = 0;
function怎么记忆
}
}
}
/*------------------------------
FileName: IIC.c
Function: 实现IIC总线的基本操作
Author: Zhang Kaizhou
Date: 2019-6-12 18:08:47
------------------------------*/
#include "clock.h"
/*IIC总线初始化*/
void initIIC(){
SCL = 1;
_nop_();
SDA = 1;
_nop_();
}
/*IIC总线启动信号*/
void startIIC(){ // 在SCL⾼电平期间,SDA产⽣⼀个下降沿表⽰启动SDA = 1;
_nop_();
SCL = 1;
_nop_();
SDA = 0;
_nop_();
}
/*IIC总线应答信号*/
void responseIIC(){ // 从机在SCL⾼电平期间将SDA拉低表⽰应答SDA = 0;
SCL = 0;
_nop_();
}
/*IIC总线停⽌信号*/
void stopIIC(){ // 在SCL⾼电平期间,SDA产⽣⼀个上升沿表⽰停⽌SDA = 0;
_nop_();
SCL = 1;
_nop_();
SDA = 1;
_nop_();
}
/
*向IIC总线写⼀个字节数据*/
void writeDataIIC(uchar dat){
uchar i, temp;
temp = dat;
for(i = 0; i < 8; i++){
temp = temp << 1;
SCL = 0;
_nop_();
SDA = CY;
_nop_();
SCL = 1;
_nop_();
}
SCL = 0;
_nop_();
SDA = 1;
_nop_();
}
/*从IIC总线读⼀个字节数据*/

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。