AVR單片機驅(qū)動高精度AD芯片ADS1256的C程序
/*************************************************************************************/
unsigned long int ads_sum(unsigned char data)//讀A/D數(shù)據(jù)。
{
unsignedlongint aa=0;
if((data>=0)&&(data<=7))
{
ad_write(01,0x80+data); //第幾通道為選通
ads_senddata(0xf0); //自校準(zhǔn)
aa=ad_read(); //讀24位A/D數(shù)據(jù)。
}
return aa;
}
volatile unsigned charsum=0; //定義全局的指令變量
//設(shè)置串口的參數(shù)
void USART_Init( unsigned int baud )
{
/* 設(shè)置波特率9600*/
UBRRH = 0;
UBRRL = 47;
/* 接收器與發(fā)送器使能*/
UCSRB = (1<
UCSRC = (1<
void USART_Transmit( unsigned char data )
{PORTD|=(1<
while ( !( UCSRA & (1<
/* 將數(shù)據(jù)放入緩沖器,發(fā)送數(shù)據(jù) */
UDR =data;
}
unsigned char USART_Receive( void )
{
/* 等待接收數(shù)據(jù)*/
while ( !(UCSRA & (1<
/* 從緩沖器中獲取并返回數(shù)據(jù)*/
return UDR;
}
SIGNAL (SIG_UART_RECV) //接收中斷
{sum=UDR;}
SIGNAL (SIG_UART_TRANS) //發(fā)送結(jié)束中斷 void SPI_MasterInit(void) //配置SPI為主機 //--------------------------------------------------------------------------------------------// void port (void) while(1) }
{PORTD&=~(1<
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~配置SPI~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
{
/* 設(shè)置MOSI 和SCK 為輸出,其他為輸入 */
DDRB|=(1<
SPCR = 0x52; //7e 高位首發(fā),SPI使能,主機模式,SCK空閑保持為低電平,數(shù)據(jù)在上升沿采樣,速率為1:64(FOSC/128)
SPSR = 0x01; //使能倍速1:32
}
{
DDRD|=(1<
//主函數(shù)
main(void)
{
unsignedlongint d=0,p[12],n=0,m=0;
unsigned char aa=0,a=0,b=0,c=0,i=0;
port(); //端口初始化
SPI_MasterInit(); //SPI端口初始化
USART_Init(1); //串口初始化
ADS1256(); //初始化ADS1256
sei();
{
aa=sum;
if(aa<=10)
{sum=15;
a=0;b=0;c=0;n=0x400000;m=0;
for (i=0;i<12;i++)
{
p[i]=ads_sum(aa);//讀A/D數(shù)據(jù)。通道地址0~7 0、1、2、3、4、5、6、7
if(n>p[i])
n=p[i];
if (m
d=d+p[i];
}
d=(d-n-m)/10;
a=d%256;
d=d>>8;
b=d%256;
d=d>>8;
c=d%256;
USART_Transmit(c);
USART_Transmit(b);
USART_Transmit(a);
}
}
評論