新聞中心

armega16與24c16的操作

作者: 時間:2016-11-26 來源:網(wǎng)絡(luò) 收藏
atmega16有硬件IIC(TWI)接口,不需模擬,直接操作寄存器即可

本文引用地址:http://m.butianyuan.cn/article/201611/321657.htm

#include
#include
#define uchar unsigned char
#define uint unsigned int

#define Start() TWCR|=(1<#define Stop() TWCR|=(1<//終止信號
#define Wait() while(!(TWCR&(1<//等待數(shù)據(jù)發(fā)送完
#define Test() (TWSR&0XF8)//檢查twsr的狀態(tài)碼
#define Write(X) {TWDR=X;TWCR=(1<//寫字節(jié)
#define Read() TWCR|=(1<//讀字節(jié)

extern const uchar table[]={
~0x3f,~0x06,~0x5b,~0x4f,
~0x66,~0x6d,~0x7d,~0x07,
~0x7f,~0x6f,~0x77,~0x7c,
~0x39,~0x5e,~0x79,~0x71};
void delay(uint t)
{
uint i,j;
for(i=0;i<10;i++)
for(j=0;j}

void init()
{
TWSR=0;
TWBR=0X23;
TWCR=0X44;
}
//可以通過讀出Test()來看TWSR處于什么狀態(tài),也可以不用使用Test()

uchar write()
{
Start();
Wait();
//if(Test()!=0x08)
//return 0;
Write(0xa0);
Wait();

if(Test()!=0x18)
return 0;
Write(3);

Wait();

//if(Test()!=0x28)
//return 0;
Write(56);
Wait();

// if(Test()!=0x28)
// return 0;

Stop();
delay(1000);
}
uchar read()
{


uchar dat;
TWSR=0;
Start();
Wait();
//if(Test()!=0x08)
// return 0;

Write(0xa0);

Wait();


//if(Test()!=0X18)
//return 0;
Write(3);
Wait();

// if(Test()!=0x28)
// return 0;


Start();
Wait();
// if(Test()!=0x10)//這里必須寫10,因為是第二次開始
// return 0;
Write(0xa1);
Wait();

if(Test()!=0x40)
return 0;
Read();
Wait();

// if(Test()!=0x58)//讀完無應(yīng)答
// return 0;
dat=TWDR;

Stop();


}
void main()
{
uchar z,shi,ge;
init();
write();

z=read();
DDRA=0XFF;

shi=z/10;
ge=z;


while(1) {


PORTB=0xfe;
PORTA=table[shi];
delay(10);
PORTB=0xfd;
PORTA=table[ge];
delay(10);

}
}



關(guān)鍵詞: armega1624c1

評論


技術(shù)專區(qū)

關(guān)閉