您的位置 首页 方案

AVR128的程序——24C02记载复位次数

includeiom128vh>includemacrosh>defineucharunsignedchardefineuintunsignedintdefinerd_device_add

#include

#include
#define uchar unsigned char
#define uint unsigned int
#define rd_device_add 0xA1
#define wr_device_add 0xA0
#define START 0x08
#define RE_START 0x10
#define MT_SLA_ACK 0x18
#define MT_SLA_NOACK 0x20
#define MT_DATA_ACK 0x28
#define MT_DATA_NOACK 0x30
#define MR_SLA_ACK 0x40
#define MR_SLA_NOACK 0x48
#define MR_DATA_ACK 0x50
#define MR_DATA_NOACK 0x58
#define Start() (TWCR=(1<
#define Stop() (TWCR=(1<
#define Wait() {while(!(TWCR&(1<
#define TestAck() (TWSR&0xf8)
#define SetAck() (TWCR|=(1<
#define SetNoAck() (TWCR&=~(1<
#define Twi() (TWCR=(1<
#define Write8Bit(x) {TWDR=(x);TWCR=(1<
void Delayms(uint MS);
void display(uchar FirstBit,uchar Num);
void i2cini(void) ;
void delay_nus(unsigned int n);
void delay_nms(unsigned int n);
unsigned char i2c_Write(unsigned char Wdata,unsigned char RomAddress) ;
unsigned char i2c_Read(unsigned char RomAddress) ;
uchar TempData[8];
uchar WeiMa[8] = {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
uchar led_discode[11]={0xc0,0xf9,0xa4,0xb0,0x99,
0x92,0x82,0xf8,0x80,0x90,0xff};
void main(void)
{
uchar i;
DDRA=0XFF;
PORTA=0XFF;
DDRC=0XFF;
PORTC=0XFF;
DDRF|=0X0E;
PORTF|=0X0E;
PORTF&=0xF7;
DDRB |=0X10;
PORTB|=0X10;
i2cini();
i=i2c_Read(0x03);
i++;
i=i;
i2c_Write(i,0x03);
TempData[0] = led_discode[i];
while(1)
{
display(0,1);
}
}
void i2cini(void)
{
TWBR=0x20;
TWCR=0x04;
TWSR=0;
}
unsigned char i2c_Write(unsigned char Wdata,unsigned char RomAddress)
{
unsigned char i;
Start();//I2C发动
Wait();
if(TestAck()!=START) return 1;//ACK
Write8Bit(wr_device_add);//写I2C从器材地址和写方法
Wait();
if(TestAck()!=MT_SLA_ACK) return 1;//ACK
Write8Bit(RomAddress);//写24C02的ROM地址
Wait();
if(TestAck()!=MT_DATA_ACK) return 1;//ACK
Write8Bit(Wdata);//写数据到24C02的ROM
Wait();
if(TestAck()!=MT_DATA_ACK) return 1;//ACK
Stop();//I2C中止
for(i=0;i<250;i++);
return 0;
}
unsigned char i2c_Read(unsigned char RomAddress)
{
unsigned char temp;
Start();//I2C发动
Wait();
if (TestAck()!=START) return 1;//ACK
Write8Bit(wr_device_add);//写I2C从器材地址和写方法
Wait();
if (TestAck()!=MT_SLA_ACK) return 1;//ACK //
//
Write8Bit(RomAddress);//写24C02的ROM地址
Wait();
if (TestAck()!=MT_DATA_ACK) return 1; //?
//data1=TestAck();
Start();//I2C重新发动
Wait();
if (TestAck()!=RE_START) return 1;
Write8Bit(rd_device_add);//写I2C从器材地址和读方法
Wait();
if(TestAck()!=MR_SLA_ACK) return 1;//ACK
Twi();//发动主I2C读方法
Wait();
if(TestAck()!=MR_DATA_NOACK) return 1;//ACK
temp=TWDR;//读取I2C接纳数据
Stop();//I2C中止
return temp;
}
void delay_nus(unsigned int n) //N us延时函数
{
unsigned int i=0;
for (i=0;i
asm(“nop”);
}
void delay_nms(unsigned int n) //N ms延时函数
{
unsigned int i=0,j=0;
for (i=0;i
for (j=0;j<1140;j++);
}
void Delayms(uint MS)
{
uint i,j;
for( i=0;i
for(j=0;j<1141;j++);
}
void display(uchar FirstBit,uchar Num)
{
unsigned char i;
for(i=0;i
{
PORTC=led_discode[10];
PORTA=WeiMa[i+FirstBit];
PORTC=TempData[i];
Delayms(5);
}
}

声明:本文内容来自网络转载或用户投稿,文章版权归原作者和原出处所有。文中观点,不代表本站立场。若有侵权请联系本站删除(kf@86ic.com)https://www.86ic.net/fangan/262336.html

为您推荐

联系我们

联系我们

在线咨询: QQ交谈

邮箱: kf@86ic.com

关注微信
微信扫一扫关注我们

微信扫一扫关注我们

返回顶部