我用ti的一颗模数转换芯片ADS1110,写了一个驱动程序,但是板子怎么也不工作,想请教下各位?能否给指点下,不胜感激。主控是AVR mega16,以下是我的驱动程序
#include <iom16v.h> //包含型号头文件
#include <macros.h> //包含"位"操作头文件
#define uchar unsigned char
#define uint unsigned int
#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
#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 SendAck (TWCR|=(1<<TWEA))
#define SendNoAck (TWCR&=~(1<<TWEA))
#define Twi() (TWCR=(1<<TWINT)|(TWEN))
#define Writebyte(x) {TWDR=(x);TWCR=(1<<TWINT)|(1<<TWEN);}
uint linnp;
uint dataout;
const uchar LED_DATA[26]={ 0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E,0x89,0xC7,0xC8,0xC1,0x8C,0xA3,0xBF,0xFF};
uchar led_out[4]={2,5,8,0};
void port_init(void)
{
//段选
DDRA=0XFF;
PORTA=0XFF;
//位选
DDRB=0XFF;
PORTB=0XFF;
//上拉和
DDRC&=(~(1<<PC0)|(1<<PC1));
PORTC|=((1<<PC0)|(1<<PC1));//MEGA16,使能内部上拉电阻
//清零和标定
DDRD=0X22;
PORTD=0X0C;
}
void shortdelay(uchar t)
{
uchar i,kt;
for(kt=0;kt<t;kt++)
{
for(i=0;i<100;i++)
{
;
}
}
}
void time0_init(void)
{
TCCR0=0b00000100; // bit 76(A) 54(B)比较模式00-IO口0;bit 10 11-10位快速PWM
TCNT0=0X0000;
TCCR1A=0b00000000; // bit 76(A) 54(B)比较模式00-IO口01-取反10-清零11-置位;bit32强制比较;bit 10 11-10位快速PWM
TCCR1B=0b00000000; //TCCR1B=0b00000101 停止,在按键中断中启动 bit 43 01-10位快速PWM bit2-0 clk分频:0-无时钟 1-不分频2-8 3-64 4-256 5-1024
TCNT1=63900; //1024==7812
OCR0=0;
TIMSK=0b00000101;
}
void sel_led(uchar ledi)
{
switch(ledi)
{
case 0:
PORTB=0XE0;
break;
case 1:
PORTB=0XD0;
break;
case 2:
PORTB=0XB0;
break;
case 3:
PORTB=0X70;
break;
default:
{
PORTB=0XF0;//11110000
}
}
}
void cdisout(uint disdata)
{
led_out[0]=disdata/1000;
linnp=disdata%1000;
led_out[1]=linnp/100;
linnp=linnp%100;
led_out[2]=linnp/10;
linnp=linnp%10;
led_out[3]=linnp;
}
void lelc_led(void)
{
uchar i,j;
uchar data;
for(i=0;i<4;i++)
{
j=led_out[i];
if(j>18) j=0;
data=LED_DATA[j];//段码赋值
PORTA=data;
sel_led(i);
shortdelay(7);
sel_led(5);
}
}
#pragma interrupt_handler time0_isr:10
void time0_isr(void)
{
lelc_led();
}
void Twi_init(void)
{
TWBR= 0x20; //设置波特率
TWSR= 0x00; //设置预分频比 1
TWCR= 0x04; //使能应答,使能TWI
}
uchar ADS1110_config()
{
Start(); //产生START信号
Wait(); //等待START信号发送完毕
if(TestAck()!=START) //检查是否是START信号发送完毕状态
{
return 1;
}
Writebyte(0x90); //发送从器件地址
Wait(); //等待发送完毕
if(TestAck()!=MT_SLA_ACK)//检查是否是从器件地址发送完毕状态
{
return 1;
}
Writebyte(0x8C); //发送命令字节
Wait(); //等待发送完毕
if(TestAck()!=MT_DATA_ACK)//检查是否是数据发送完毕
{
return 1;
}
Stop(); //操作完毕,发送STOP信号
shortdelay(10); //等待发送完毕
return 0; //返回写入成功状态
}
uint read_ads(void)
{
uchar adch;
uchar adcl;
uchar conf;
uint adc;
Start(); //产生START信号
Wait(); //等待START信号发送完毕
if(TestAck()!=START) //检查是否是START信号发送完毕状态
{
return 1;
}
Writebyte(0x91); //发送从器件地址
Wait(); //等待发送完毕
if(TestAck()!=MR_SLA_ACK) //检查是否是从器件地址发送完毕状态
{
return 1;
}
adch= TWDR;
SendAck;
adcl= TWDR;
SendAck;
conf= TWDR;
SendNoAck;
adc=adch*256+adcl;
Stop();
return adc;
}
void main(void)
{
CLI();
time0_init();
Twi_init( );
port_init();
SEI();
ADS1110_config();
while(1)
{
dataout=read_ads();
shortdelay(10);
cdisout(dataout);
} //写
} |