|
/*------------AD7705头文件开始-------------------*/
#include<msp430x24x.h>
#include<stdio.h>
#include<math.h>
#include"AD7794.h"
void Delayms(unsigned char ms)
{
unsigned char i;
while(ms--)
{
for(i = 0; i < 120; i++);
}
}
void AD7794_Write_1_BYTE(unsigned char Data_byte)/*poll send 8bits,MSB first*/
{
unsigned char i;
for(i = 0; i < 8; i++)
{ AD7794_CLK_LOW();
if(Data_byte & 0x80 == 1)
AD7794_IN_HIGH();
else
AD7794_IN_LOW();
AD7794_CLK_HIGH();
Data_byte <<= 1;
}
}
void AD7794_Write_1_WORD( unsigned int Data_byte)/*poll send 8bits,MSB first*/
{
unsigned char i;
for(i = 0; i < 16; i++)
{
AD7794_CLK_LOW();
if(Data_byte & 0x8000 == 1)
AD7794_IN_HIGH();
else
AD7794_IN_LOW();
AD7794_CLK_HIGH();
Data_byte <<= 1;
}
}
void AD7794_Write_1_DWORD(unsigned long int Data_byte)/*poll send 8bits,MSB first*/
{
unsigned char i;
for(i = 0; i < 24; i++)
{
AD7794_CLK_LOW();
if(Data_byte & 0x800000 == 1)
AD7794_IN_HIGH();
else
AD7794_IN_LOW();
AD7794_CLK_HIGH();
Data_byte <<= 1;
}
}
void AD7794_INIT()
{
unsigned char i;
AD7794_IN_HIGH();
AD7794_Write_1_BYTE(0x04);
for(i=0;i<=10;i++) ;
AD7794_Write_1_BYTE(0x08);
for(i=0;i<=10;i++) ;
AD7794_Write_1_BYTE(0x08);
for(i=0;i<=10;i++);
AD7794_Write_1_WORD(0x0005);
for(i=0;i<=10;i++);
AD7794_Write_1_BYTE(0x10);
for(i=0;i<=10;i++);
AD7794_Write_1_WORD(0x1030);
for(i=0;i<=10;i++) ;
}
unsigned char AD7794_Read_1_BYTE() /*poll receive 8bits*/
{
unsigned char i,Data_read;
for(i = 0; i < 8; i++)
{
Data_read <<= 1;
AD7794_CLK_LOW();
if(P1IN&0x01)
Data_read |= 0x01;
else
Data_read &= 0xfe;
AD7794_CLK_HIGH();
}
return(Data_read);
}
unsigned int AD7794_Read_1_WORD() /*poll receive 8bits*/
{
unsigned char i;
unsigned int Data_read;
for(i = 0; i < 16; i++)
{
Data_read <<= 1;
AD7794_CLK_LOW();
if( P1IN&0x0001)
Data_read |= 0x0001;
else
Data_read &= 0xfffe;
AD7794_CLK_HIGH();
}
return(Data_read);
}
unsigned long int AD7794_Read_1_DWORD() /*poll receive 8bits*/
{
unsigned char i;
unsigned long int Data_read;
for(i = 0; i < 24; i++)
{
Data_read <<= 1;
AD7794_CLK_LOW();
if(P1IN&0x000001)
Data_read |= 0x000001;
else
Data_read &= 0xfffffe;
AD7794_CLK_HIGH();
}
return(Data_read);
}
unsigned int AD7794_Data_Process()
{
unsigned char MSB_Data,LSB_Data,T_Voltage;
// unsigned long int temp;
AD7794_Write_1_BYTE(0x5c);/*set the next operation for 24 bit read from the data register */
// HSB_Data = AD7794_Read_1_BYTE();
MSB_Data = AD7794_Read_1_BYTE();
LSB_Data = AD7794_Read_1_BYTE();
// temp = AD7794_Read_1_DWORD();
// LSB_Data = (unsigned char)( temp%256 );
// MSB_Data = (unsigned char)( (temp/256)%256 );
// HSB_Data = (unsigned char)( (temp/65536)%256 );
T_Voltage = (unsigned int)MSB_Data*256+(unsigned int)LSB_Data;
return (T_Voltage);
}
int ad××e(char n) //求n次A/D转换的平均值
{unsigned char i,pit,ue,max,min;
unsigned int ADC_tem=0;
unsigned int ADC_temp[100];
for(ue=0;ue<n;ue++)
{
ADC_temp[ue]=AD7794_Data_Process(); //读走温度数据
}
for(pit=4;pit<n;pit++)
{
ADC_tem+=ADC_temp[pit]; //求和
}
max=ADC_temp[4];
min=ADC_temp[4];
for(i=4;i<n;i++)
{
if(ADC_temp[i]>max)
max=ADC_temp[i];
if(ADC_temp[i]<min)
min=ADC_temp[i];
}
ADC_tem=(ADC_tem-max-min)/(n-6);//去掉最大最小值然后求平均值
return ADC_tem;
}
void InitClock()
{
DCOCTL=BCSCTL1=XT2OFF;
}
void main()
{
float RT;
unsigned int data;
P2DIR |= 0x02;
P2OUT |= BIT1; // ADVCC;
P2DIR |= 0x10;
P2OUT |= BIT4; // COMVCC;
P2DIR |= 0x20;
P2OUT &= ~BIT5; // A
P2DIR |= 0x40;
P2OUT &= ~BIT6; //B
P1DIR &=~BIT6;
WDTCTL = WDTPW + WDTHOLD;/*close the watch dog timer*/
InitClock();
AD7794_INIT();
unsigned int j; // 指示灯
P2DIR |= BIT7;
while(1)
{
data= ad××e(20);
RT=data*1500/65536;
printf("RT=%.1f,data=%d",RT,data);
if(RT>1116)
{ P2OUT &= ~(BIT7); //输出低电平
for (j=10000;j>0;j--) ; //延时
P2OUT |= BIT7;
}
else
{
P2OUT |= BIT7; //输出高电平
}
}
} |
|