?? 掉電存儲..c
字號:
/********************************************************************
匯誠科技
實現(xiàn)功能:掉電存儲控制程序
使用芯片:AT89S52
晶振:11.0592MHZ
波特率:9600
編譯環(huán)境:Keil
作者:zhangxinchunleo
網(wǎng)站:www.ourhc.cn
淘寶店:匯誠科技 http://shop36330473.taobao.com
【聲明】此程序僅用于學(xué)習(xí)與參考,引用請注明版權(quán)和作者信息!
*********************************************************************/
/********************************************************************
程序說明:
當IN0口與GND口接通后,對應(yīng)的繼電器K8吸合(即:COM7與CK接通CB斷開);
當IN1口與GND口接通后,對應(yīng)的繼電器K7吸合(即:COM6與CK接通CB斷開);
當IN2口與GND口接通后,對應(yīng)的繼電器K6吸合(即:COM5與CK接通CB斷開);
當IN3口與GND口接通后,對應(yīng)的繼電器K5吸合(即:COM4與CK接通CB斷開);
當電源斷電后,或者按下復(fù)位按鍵后,控制板再次供電,控制板恢復(fù)斷電前或者復(fù)位前狀態(tài)。
*********************************************************************/
#include<reg52.h> //庫文件
#define uchar unsigned char//宏定義無符號字符型
#define uint unsigned int //宏定義無符號整型
uchar dat; //存儲數(shù)據(jù)值
bit write=0; //寫24C08 的標志;
/********************************************************************
初始定義
*********************************************************************/
/*定義八位輸入為單片機P1口*/
sbit IN0=P1^0;
sbit IN1=P1^1;
sbit IN2=P1^2;
sbit IN3=P1^3;
/*定義八位輸出為單片機P2口*/
sbit K1=P2^0;
sbit K2=P2^1;
sbit K3=P2^2;
sbit K4=P2^3;
sbit scl=P3^6;
sbit sda=P3^7;
/********************************************************************
24C08 讀寫驅(qū)動程序
*********************************************************************/
void delay1(uchar x)
{
uint i;
for(i=0;i<x;i++);
;
}
void flash(){ ; ; }
void x24c08_init() //24c08 初始化子程序
{
scl=1;
flash();
sda=1;
flash();
}
void start() //啟動(I方C)總線
{
sda=1; flash(); scl=1; flash(); sda=0; flash(); scl=0; flash();
}
void stop() //停止(I方C)總線
{
sda=0; flash(); scl=1; flash(); sda=1; flash();
}
void writex(uchar j) //寫一個字節(jié)
{
uchar i,temp;
temp=j;
for (i=0;i<8;i++)
{
temp=temp<<1; scl=0; flash(); sda=CY; flash(); scl=1; flash();
}
scl=0; flash(); sda=1; flash();
}
uchar readx() //讀一個字節(jié)
{
uchar i,j,k=0;
scl=0; flash(); sda=1;
for (i=0;i<8;i++)
{
flash(); scl=1; flash();
if (sda==1) j=1;
else j=0;
k=(k<<1)|j;
scl=0;}
flash(); return(k);
}
void clock() //(I方C)線時鐘
{
unsigned char i=0;
scl=1; flash();
while ((sda==1)&&(i<255))i++;
scl=0; flash();
}
/********************************************************************
從24c08 的地址address 中讀取一個字節(jié)數(shù)據(jù)
*********************************************************************/
unsigned char x24c08_read(unsigned char address)
{
unsigned char i;
start(); writex(0xa0);
clock(); writex(address);
clock(); start();
writex(0xa1); clock();
i=readx(); stop();
delay1(10);
return(i);
}
/********************************************************************
向24c08 的address地址中寫入一字節(jié)數(shù)據(jù)
*********************************************************************/
void x24c08_write(unsigned char address,unsigned char info)
{
EA=0;
start(); writex(0xa0);
clock(); writex(address);
clock(); writex(info);
clock(); stop();
EA=1;
delay1(50);
}
/********************************************************************
延時函數(shù)
*********************************************************************/
void delay(uchar t)
{
uchar i,j;
for(i=0;i<t;i++)
{
for(j=13;j>0;j--);
{ ;
}
}
}
/********************************************************************
主函數(shù)
*********************************************************************/
void main(void)
{
x24c08_init(); //初始化24C08
dat=x24c08_read(2);//讀出保存的數(shù)據(jù)賦于dat
P2=dat;//將存儲的數(shù)據(jù)賦予P2口
while(1)
{
if(IN0==0){delay(100);while(!IN0);P2=0XFE;write=1;} //當IN0與GND接通后,P2口輸出11111110,繼電器K8吸合其它繼電器斷開;
if(IN1==0){delay(100);while(!IN1);P2=0XFD;write=1;} //當IN1與GND接通后,P2口輸出11111101,繼電器K7吸合其它繼電器斷開;
if(IN2==0){delay(100);while(!IN2);P2=0XFB;write=1;} //當IN2與GND接通后,P2口輸出11111011,繼電器K6吸合其它繼電器斷開;
if(IN3==0){delay(100);while(!IN3);P2=0XF7;write=1;} //當IN3與GND接通后,P2口輸出11110111,繼電器K5吸合其它繼電器斷開;
if(write==1) //判斷計時器是否計時一秒
{
write=0; //清零
dat=P2; //將P2口的值賦予dat
x24c08_write(2,dat); //在24c08 的地址2 中寫入數(shù)據(jù)dat
}
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -