|
请问为什么我在实物仿真的时候给AD5161写阻值的时候,电位器没有反应?????w位一直保持在AB中间
#include
#include
#define uchar unsigned char
#define uint unsigned int
#define START 0X08
#define MT_SLA_ACK 0X18
#define MT_DATA_ACK 0X28
#define Start() (TWCR=(1<
#define Stop() (TWCR=(1<
#define Wait() while(!(TWCR&(1<
#define TestACK() (TWSR&0xF8)
#define SetACK() (TWCR|=(1<
#define Writebyte(twi_d) {TWDR=(twi_d);TWCR=(1<
/**********延时程序**********/
void delay(uint ms)
{
uint i,j;
for(i=0;i
{
for(j=0;j<1141;j++);
}
}
/******TWI初始化**********/
void twi_init()
{
TWBR=0X20;
TWSR=0x00;
TWCR=0X44;
}
/******AD5161写数据**********/
uchar AD5161_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=0;
twi_init();
AD5161_writeB(0X58,0x00,0);
/*while(1)
{
if(keypress())
{
i=keyscanf();
switch(i)
{
case 0:
PORTA=0XFE;
data=0;
break;
case 1:
data=data+16;
break;
case 2:
data=0;
break;
case 3:
data=255;
break;
}
AD5161_writeB(0x58,0,data);
}
}*/
}
/*********按键确认电路**********/
uint keypress()//按键确认函数
{
uint j;
DDRD=0XFF;//将c接口置高上拉;
PORTD=0XFF;//该语句如果不加则
DDRD=0XF0;
j=PIND;
j&=0X0F;
if (j==0x0F)
{
return 0;
}
else
{return 1;
}
}
uint keyscanf()//位按键确认
{
uint l,o;
l=PIND;
delay(10);
if (keypress())
{
switch (l)
{case 0xFE:
o=0;
break;
case 0xFD:
o=1;
break;
case 0xFB:
o=2;
break;
case 0xF7:
o=3;
break;
}
return o;
while (keypress());
}
}
|
|