热度 1|||
AT2402程序记录开关机次数的程序(C)
作者:高鹏
#include <reg51.h>
#include<absacc.h>
#include<intrins.h>
// extern void DelayMs(unsigned int);
//extern void Read24c02(unsigned char *RamAddress,
// unsigned char RomAddress,unsigned char bytes);
// extern void Write24c02(unsigned char *RamAddress,
// unsigned char RomAddress,unsigned char bytes);
/****************************************************************/
#define WriteDeviceAddress 0xa0 //写格式
#define ReadDviceAddress 0xa1 //读格式
#define uchar unsigned char
#define uint unsigned int
#define PA8255 XBYTE[0x3ffc]
#define PB8255 XBYTE[0x3ffd]
#define PC8255 XBYTE[0x3ffe]
#define COM8255 XBYTE[0x3fff]
/***************************************************************/
void DelayMs(uchar number);
void Start();
void Stop();
void Ack(); //应答格式
void NoAck(); //非应答格式
bit TestAck(); //检查应答的ACK
void Write8Bit(uchar input);//写一个字节
void Write24c02(uchar *Wdata,uchar RomAddress, //写入I2C程序
uchar number);
uchar Read8Bit(); //读一个字节
void Read24c02(uchar *RamAddress,uchar RomAddress, //读出I2C程序
uchar bytes) ;
void Delay(uchar i) ;
sbit SCL=P1^4;
sbit SDA=P1^5;
sbit DOG=P3^7;
sbit cs138=P1^6;
sbit led0=P1^0;
sbit led1=P1^1;
sbit led2=P1^2;
sbit led3=P1^3;
/***************************************************************/
uchar T[1]; //I2C数据存储区
uchar code tab[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,
0x77,0x7c,0x39,0x5e,0x79,0x71};//0~f
void main()
{
uchar tmp;
cs138=0;
P1=0;
COM8255=0X88;
Read24c02(T,0x00,1);//读程序从0X00单元开始,读出一个字节
while(1)
{
{
tmp=T[0];if(tmp>9){T[0]=0;tmp=0;}else{T[0]++;}
Write24c02(T,0x00,1);//写程序从0X00单元开始,写入一个字节
}
while(1)
{PB8255=tab[tmp];led3=1;Delay(2);led3=0;}
}
}
void DelayMs(uchar number)
{
uchar temp;
for(;number!=0;number--,DOG=!DOG)
{
for(temp=112;temp!=0;temp--) {}
}
}
/*******************启动I2C*/
void Start()
{
SDA=1;
SCL=1;
SDA=0;
SCL=0;
}
/*********************///停止I2C
void Stop()
{
SCL=0;
SDA=0;
SCL=1;
SDA=1;
}
/**********************/
void Ack() //应答格式
{
SDA=0;
SCL=1;
SCL=0;
SDA=1;
}
/***********************/
void NoAck() //应答格式
{
SDA=1;
SCL=1;
SCL=0;
}
/*************************************/
bit TestAck() //检查应答的ACK
{
bit ErrorBit;
SDA=1;
SCL=1;
ErrorBit=SDA;
SCL=0;
return(ErrorBit);
}
/*************************************/
void Write8Bit(uchar input) //写一个字节
{
uchar temp;
for(temp=8;temp!=0;temp--)
{
if(input&0x80)SDA=1;
else SDA=0;
SCL=1;
SCL=0;
input=input<<1;
}
}
/****************************************/
void Write24c02(uchar *Wdata,uchar RomAddress, //写入I2C程序
uchar number)
{
Start(); //开始条件
Write8Bit(WriteDeviceAddress); //写8位设备地址
TestAck();
Write8Bit(RomAddress);
TestAck();
for(;number!=0;number--)
{
Write8Bit(*Wdata);
TestAck();
Wdata++;
}
Stop();
DelayMs(10);
}
/***************************************************/
uchar Read8Bit() //读一个字节
{
uchar temp,rbyte=0;
for(temp=8;temp!=0;temp--)
{
SCL=1;
rbyte=rbyte<<1;
if(SDA)rbyte|=0x01;
else rbyte|=0x00;
SCL=0;
}
return(rbyte);
}
void Read24c02(uchar *RamAddress,uchar RomAddress, //读出I2C程序
uchar bytes)
{
Start();
Write8Bit(WriteDeviceAddress);
TestAck();
Write8Bit(RomAddress);
TestAck();
Start();
Write8Bit(ReadDviceAddress);
TestAck();
while(bytes!=0)
{
*RamAddress=Read8Bit();
Ack(); //应答
RamAddress++;
bytes--;
}
*RamAddress=Read8Bit();
NoAck(); //非应答
Stop(); //结束条件
}
//延时程序
void Delay(uchar i) //延时程序,i是形式参数
{
uchar j;
while(i--) //变量i由实际参数传入一个值,因此i不能赋初值
for(j=0;j<125;j++);
}