微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > AVR128的程序——24C02记录复位次数

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

时间:11-22 来源:互联网 点击:
#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);

}

}

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top