小弟用atmega16的twi接口,控制液晶和AT24c02,想用24c02来记忆开机次数,可是目前不知为何始终在两个数之间变化,重启下变2,重启下变1,不知是何原因?现将源码贴出来,请大神们帮忙:
#include"iic.h"
#include<avr/io.h>
#include<util/delay.h>
#define uint unsigned int
#define uchar unsigned char
#define BIT(x) (1 << (x))
#define RS_set PORTB |=BIT(0)
#define RS_clr PORTB &=~BIT(0)
#define RW_set PORTB |=BIT(1)
#define RW_clr PORTB &=~BIT(1)
#define E_set PORTB |=BIT(2)
#define E_clr PORTB &=~BIT(2)
#define baudo 9600
#define fosc 7372800
uchar table[16]=" I LIKE MCU! ";
uchar table1[16]=" LCD1602 is OK! ";
uchar LCD[]="0123456789";
void GPIO_init(void)
{
DDRB=0XFF;
DDRA=0XFF;
PORTB=0XFF;
PORTA=0XFF;
DDRD=0XFF;
PORTD=0XFF;
DDRC&=(~(1<<PC0)|(1<<PC1));
PORTC|=((1<<PC0)|(1<<PC1));//MEGA16,使能内部上拉电阻
}
void IIC_init(void)
{
TWBR=0x20;
TWCR=0X04;
TWSR=0;
}
void lcd1602_write_cmd(uchar cmd)
{
RS_set;
RS_clr;
RW_clr;
E_clr;
PORTA=cmd;
_delay_ms(4);
E_set;
_delay_ms(15);
E_clr;
_delay_ms(1);
RS_set;
RW_set;
}
void lcd1602_write_dat(uchar dat)
{
RS_clr;
RS_set;
RW_clr;
E_clr;
PORTA=dat;
_delay_ms(4);
E_set;
_delay_ms(15);
E_clr;
_delay_ms(1);
RS_clr;
RW_set;
}
void lcd1602_init(void)
{
lcd1602_write_cmd(0x38); //显示模式设置第一次
_delay_ms(3);
lcd1602_write_cmd(0x38); //显示模式设置第二次
_delay_ms(3);
lcd1602_write_cmd(0x38); //显示模式设置第三次
_delay_ms(3);
lcd1602_write_cmd(0x38); //显示模式设置第四次
_delay_ms(3);
lcd1602_write_cmd(0x08); //显示关闭
lcd1602_write_cmd(0x01); //显示清屏
_delay_ms(3);
lcd1602_write_cmd(0x06); //显示光标移动设置
lcd1602_write_cmd(0x0c); //显示开机光标设置
}
int main(void)
{
uchar i;
GPIO_init();
lcd1602_init();
IIC_init();
while(1)
{
//i2c_Write(0x01,0);
i=i2c_Read(0x01);
i++;
i2c_Write(0x01,i);
lcd1602_write_cmd(0x80);
lcd1602_write_dat(LCD
);
PORTD=i;
while(1);
}
return 0;
}
这个是iic.c的内容:
#include<avr/io.h>
#include<util/delay.h>
//TWI状态定义:MT为主方式传输,MR为主方式接收
#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
//常用TWI操作(主模式写和主模式读)
#define Start() (TWCR=(1<<TWINT)|(1<<TWSTA)|(1<<TWEN))
#define Stop() (TWCR=(1<<TWINT)|(1<<TWSTO)|(1<<TWEN))
#define Wait() {while(!(TWCR&(1<<TWINT)));}
#define TestAck() (TWSR&0xf8)
#define SetAck() (TWCR|=(1<<TWEA))
#define SetNoAck() (TWCR&=~(1<<TWEA))
#define Twi() (TWCR=(1<<TWINT)|(1<<TWEN))
#define Write8Bit(a) {TWDR=(a);TWCR=(1<<TWINT)|(1<<TWEN);}
//-----------------------------------------------------------------------------
unsigned char i2c_Write(unsigned char Address,unsigned char Wdata)
{
Start(); //I2C启动
Wait();
Write8Bit(0xa0); //写I2C从器件地址和写方式
Wait();
Write8Bit(Address); //写子地址
Wait();
Write8Bit(Wdata); //写数据
Wait();
Stop(); //I2C停止
_delay_ms(10); //延时等EEPROM写完
return 0;
}
unsigned char i2c_Read(unsigned char Address)
{
unsigned char temp;
Start(); //I2C启动
Wait();
Write8Bit(0xa0); //写I2C从器件地址和写方式
Wait();
if(TestAck()!=MT_SLA_ACK) return 0;
Write8Bit(Address); //写子地址
Wait();
if(TestAck()!=MT_DATA_ACK) return 0;
Start(); //I2C重新启动
Wait();
if(TestAck()!=RE_START) return 0;
Write8Bit(0xa1); //写I2C从器件地址和读方式
Wait();
if(TestAck()!=MR_SLA_ACK) return 0; //ACK
Twi(); //启动I2C读方式
Wait();
if(TestAck()!=MR_DATA_NOACK) return 0; //ACK
temp=TWDR; //读取I2C接收数据
Stop(); //I2C停止
return temp;
}
此帖出自小平头技术问答
断点显示正确吗?
一周热门 更多>