新聞中心

EEPW首頁(yè) > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > proteus 仿真AD5241程序。。8位數(shù)字電位計(jì)

proteus 仿真AD5241程序。。8位數(shù)字電位計(jì)

作者: 時(shí)間:2016-11-13 來(lái)源:網(wǎng)絡(luò) 收藏

本文引用地址:http://2s4d.com/article/201611/316373.htm

#include
#include
#include
#include "skey.c"
#define uchar unsigned char
#define uint unsigned int
#define START 0X08
#define MT_SLA_ACK 0X18
#define MT_SLA_NOACK 0X20
#define MT_DATA_ACK 0X28
#define MT_DATA_NOACK 0X30

#define Start() (TWCR=(1<#define Stop() (TWCR=(1<#define Wait() while(!(TWCR&(1<#define TestACK() (TWSR&0xF8) //取出狀態(tài)碼
#define SetACK() (TWCR|=(1<#define Writebyte(twi_d) {TWDR=(twi_d);TWCR=(1<


void twi_init()
{
TWBR=0X20;
TWSR=0;
TWCR=0X44;//時(shí)能twi應(yīng)答。twi使能

}
//手冊(cè)172頁(yè)
uchar AD5241_writeB(uchar devadd,uchar cmd,uchar rdata)
{
Start();
Wait();
if(TestACK()!=START)
return 0;
Writebyte(devadd);

Wait();
if(TestACK()!=MT_SLA_ACK)
return 0;

Writebyte(cmd);
Wait();
if(TestACK()!=MT_DATA_ACK)
return 0;
Writebyte(rdata);
Wait();
if(TestACK()!=MT_DATA_ACK)
return 0;

Stop();
delay(10);
return 1;

}

void main()
{
uchar i,data=128;
twi_init();
AD5241_writeB(0x58,0x10,0);
delay(10);
AD5241_writeB(0x58,0x10,data);//
while(1)

{
if(key_press())
{
i=key_scan();
switch(i)
{
case 0:
data-=16;break;
case 1:
data+=16;break;
case 2:
data=0;break;
case 3:
data=255;break;

}
}
AD5241_writeB(0x58,0,data);


}
}



評(píng)論


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

關(guān)閉