#include <iom16v.h>
#include <macros.h>
#define uchar unsigned char
#define uint unsigned int
uchar dig[]="0123456789";
void delay(uchar MS) //延时
{
uchar i,j;
for (i=0;i<MS;i++ )
{
for(j=0;j<1141;j++);
}
}
void start() //twi启动
{
TWCR=(1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
while(!(TWCR&(1<<TWINT)));
}
void stop() //twi 停止
{
TWCR=(1<<TWINT)|(1<<TWEN)|(1<<TWSTO);
}
void write_one(uchar add,uchar data) //往特定地址写数据
{
start();
TWDR=(0xa0+0x00)<<1+0x00;
TWCR = (1<<TWINT) | (1<<TWEN);
while (!(TWCR & (1<<TWINT)));
TWDR =add;
TWCR = (1<<TWINT) | (1<<TWEN);
while (!(TWCR & (1<<TWINT)));
TWDR = data;
TWCR=(1<<TWINT)|(1<<TWEN);
while(!(TWCR&(1<<TWINT)));
stop();
delay(10);
}
uchar read_one(uchar add) //读取特定地址的单个字节
{
uchar temp;
start();
TWDR=(0xa0+0x00)<<1+0x00;
TWCR = (1<<TWINT) | (1<<TWEN);
while (!(TWCR & (1<<TWINT)));
TWDR =add;
TWCR = (1<<TWINT) | (1<<TWEN);
while (!(TWCR & (1<<TWINT)));
start();
TWDR=(0xa0+0x00)<<1+0x01;
TWCR = (1<<TWINT) | (1<<TWEN);
while (!(TWCR & (1<<TWINT))); //0x40
TWCR = (1<<TWINT) | (1<<TWEN);
while (!(TWCR & (1<<TWINT))); //0x58
delay(10);
temp = TWDR;
return temp;
}
void twi_init() //twi 初始化
{
DDRC|=BIT(0)|BIT(1);
TWBR=0X30;
TWCR=0x00;
TWSR=0X02;
}
void write_com(uchar com) //液晶设置
{
PORTA&=~BIT(4);
PORTA&=~BIT(5);
PORTB=com;
PORTA|=BIT(6);
delay(1);
PORTA&=~BIT(6);
}
void yejing_init() //1602液晶初始化
{
DDRB=0Xff;
DDRA|=BIT(4)|BIT(5)|BIT(6);
DDRC=0XFF;
PORTC=0XFF;
PORTC&=~(1<<6);
delay(50);
write_com(0x38);
delay(5);
write_com(0x01);
delay(5);
write_com(0x0c);
delay(5);
write_com(0x06);
delay(5);
}
void write_data(uchar data) //液晶写数据
{
PORTA|=BIT(4);
PORTA&=~BIT(5);
PORTB=data;
PORTA|=BIT(6);
delay(1);
PORTA&=~BIT(6);
}
void main()
{
uchar i,j,a,b;
yejing_init();
twi_init();
write_one(0x02,0x11);
i=read_one(0x02);
a=i/10;
b=i%10;
write_com(0x80+0x00);
delay(5);
write_data(dig[a]);
delay(5);
write_data(dig[b]);
delay (5);
}
液晶显示不是17而是e8.到底是咋回事啊
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>