- #include "msp430F5529.h"
- #include "ADS1256.h"
- void main(void)
- {
-
- WDTCTL = WDTPW + WDTHOLD; // Stop WDT
-
- P3SEL = BIT3+BIT4;
- UCA0CTL1|=UCSWRST;
- UCA0CTL1|=UCSSEL_1;
- UCA0BR0 = 0x03; // 32kHz/9600=3.41 (see User's Guide)
- UCA0BR1 = 0x00; //
- UCA0MCTL = UCBRS_3+UCBRF_0; // Modulation UCBRSx=3, UCBRFx=0
- UCA0CTL1&=~UCSWRST;
- UCA0IE|=UCRXIE;
- _EINT();
-
- ADS1256_IO_init();
- ADS1256_Init( );
- while(1)
- {
- ADS_sum(ADS1256_MUXP_AIN0 | ADS1256_MUXN_AINCOM);
-
- ADS_sum(ADS1256_MUXP_AIN1 | ADS1256_MUXN_AINCOM);
-
- ADS_sum(ADS1256_MUXP_AIN2 | ADS1256_MUXN_AINCOM);
-
- ADS_sum(ADS1256_MUXP_AIN3 | ADS1256_MUXN_AINCOM);
-
- ADS_sum(ADS1256_MUXP_AIN4 | ADS1256_MUXN_AINCOM);
-
- ADS_sum(ADS1256_MUXP_AIN5 | ADS1256_MUXN_AINCOM);
-
- ADS_sum(ADS1256_MUXP_AIN6 | ADS1256_MUXN_AINCOM);
-
- ADS_sum(ADS1256_MUXP_AIN7 | ADS1256_MUXN_AINCOM);
-
- }
- }
- // Echo back RXed character, confirm TX buffer is ready first
- /*#pragma vector=USCIAB0RX_VECTOR
- __interrupt void USCI0RX_ISR(void)
- {
- while (!(IFG2&UCA0TXIFG)); // USCI_A0 TX buffer ready?
- UCA0TXBUF = UCA0RXBUF; // TX -> RXed character
- }*/
- /*******************ADS1256的模拟SPI设置********************/
- unsigned char ADS1256_SPI(unsigned char dat)
- {
- unsigned char i,r=0;
- ADS1256_SCLK_L;
- for(i=0;i<8;i++)
- {
- r = r<< 1; //SOMI接收的数据左移
- if(dat & 0x80)
- ADS1256_SIMO_H; //高
- else //
- ADS1256_SIMO_L; //低
- //
- ADS1256_SCLK_H; //SCK置高 |下
- dat =dat << 1; // |降
- ADS1256_SCLK_L; //SCK置低 V沿
-
- if(ADS1256_SOMI) //判断接受的数据
- r = r | 0x01;
- else
- r = r & 0xfe;
-
- }
- delay_us(100); //延时 t11
- return r;
- }
- /************************************************************/
- /*****************ADS1256写寄存器******************************/
- void ADS1256_WREG(unsigned char regaddr,unsigned char dat)
- {
- while(ADS1256_DRDY); //当DRDY为低是才能写寄存器
- ADS1256_SPI(ADS1256_CMD_WREG | (regaddr & 0xF)); //WREG指令+地址
- ADS1256_SPI(0x00);
- ADS1256_SPI(dat); //向ADS1256寄存器写数据
- }
- /************************************************************/
- /*****************ADS1256读数据*******************************/
- unsigned long ADS1256_ReadData(void)
- {
- unsigned char i=0;
- unsigned long sum=0;
- unsigned long r=0;
- while(ADS1256_DRDY); //当DRDY为低是才能写指令
- ADS1256_SPI(ADS1256_CMD_SYNC); //ADS1256转换指令
- ADS1256_SPI(ADS1256_CMD_WAKEUP); //ADS1256唤醒指令
- ADS1256_SPI(ADS1256_CMD_RDATA); //ADS1256转换指令
-
- for(i=0;i<3;i++) //接收数据24位
- {
- sum = sum << 8;
- r = ADS1256_SPI(0x00); //接收数据
- sum |= r;
- }
- return sum; //返回数据
- }
- /************************************************************/
- /********************ADS1256初始化******************************/
- void ADS1256_Init(void)
- {
- // ADS1256_CS_L;
- ADS1256_REST_L; //ADS1256复位
- delay_ms(2);
- ADS1256_REST_H;
- ADS1256_WREG(ADS1256_STATUS,0x04); // 高位在前、校准、不使用缓冲
- ADS1256_WREG(ADS1256_MUX,0x08); // 初始化端口A0为‘+’,AINCOM位‘-’
- ADS1256_WREG(ADS1256_ADCON,0x00); // 放大倍数1
- ADS1256_WREG(ADS1256_DRATE,ADS1256_DRATE_5SPS); // 数据5sps
- ADS1256_WREG(ADS1256_IO,0x00); // IO状态输入
-
- }
- /************************************************************/
- /********************ADS转换结果*****************************/
- void ADS_sum(unsigned char road)
- {
- unsigned long results=0;
- unsigned long Result_sum=0;
- unsigned long fVoltage;
- unsigned char i,rod;
- ADS1256_WREG(ADS1256_MUX,road); //设置通道
- ADS1256_SPI(ADS1256_CMD_SELFCAL); //偏移和增益自动校准
- results = ADS1256_ReadData(); //读取AD值,返回24位数据。
- Result_sum = results >> 4;
- if(Result_sum<0x80000) //测得电压值为正的情况
- {
- fVoltage=Result_sum*954; //954=2*2.5/2^19*100000000 2.5为基准电压 100000000为为了计算的准确性转换为整形计算
- ADS1256_buf[0]=(unsigned char)(fVoltage/100000000);
- ADS1256_buf[0]=ADS1256_buf[0]+0x30;
- ADS1256_buf[1]='.';
- ADS1256_buf[2]=(unsigned char)((fVoltage%100000000)/10000000);
- ADS1256_buf[2]=ADS1256_buf[2]+0x30;
- ADS1256_buf[3]=(unsigned char)((fVoltage%10000000)/1000000);
- ADS1256_buf[3]=ADS1256_buf[3]+0x30;
- ADS1256_buf[4]=(unsigned char)(fVoltage%1000000/100000);
- ADS1256_buf[4]=ADS1256_buf[4]+0x30;
- ADS1256_buf[5]=(unsigned char)(fVoltage%100000/10000);
- ADS1256_buf[5]=ADS1256_buf[5]+0x30;
- ADS1256_buf[6]=(unsigned char)(fVoltage%10000/1000);
- ADS1256_buf[6]=ADS1256_buf[6]+0x30;
- ADS1256_buf[7]=(unsigned char)(fVoltage%1000/100);
- ADS1256_buf[7]=ADS1256_buf[7]+0x30;
- ADS1256_buf[8]='V'; //V
- ADS1256_buf[9]=0x20;
- }
- else //测得电压值为负的情况
- {
- Result_sum=0xfffff-Result_sum; //取反运算
- fVoltage=Result_sum*954; //954=2*2.5/2^19*100000000 2.5为基准电压 100000000为为了计算的准确性转换为整形计算
- ADS1256_buf[0]=(unsigned char)(fVoltage/100000000);
- ADS1256_buf[0]=ADS1256_buf[0]+0x30;
- ADS1256_buf[1]='.';
- ADS1256_buf[2]=(unsigned char)((fVoltage%100000000)/10000000);
- ADS1256_buf[2]=ADS1256_buf[2]+0x30;
- ADS1256_buf[3]=(unsigned char)((fVoltage%10000000)/1000000);
- ADS1256_buf[3]=ADS1256_buf[3]+0x30;
- ADS1256_buf[4]=(unsigned char)(fVoltage%1000000/100000);
- ADS1256_buf[4]=ADS1256_buf[4]+0x30;
- ADS1256_buf[5]=(unsigned char)(fVoltage%100000/10000);
- ADS1256_buf[5]=ADS1256_buf[5]+0x30;
- ADS1256_buf[6]=(unsigned char)(fVoltage%10000/1000);
- ADS1256_buf[6]=ADS1256_buf[6]+0x30;
- ADS1256_buf[7]=(unsigned char)(fVoltage%1000/100);
- ADS1256_buf[7]=ADS1256_buf[7]+0x30;
- ADS1256_buf[8]='V'; //V
- ADS1256_buf[9]='-';
- }
- if(road/16 == 0) //本次读取的数据是上一次转换的值
- rod = (7 + 0x30); //进行通道-1计算
- else
- rod = ((road/16 + 0x30) -1);
- PutString("第");
- PutChar(rod);
- PutString("路:");
-
- i=0;
- while(i<12)
- {
- PutChar(ADS1256_buf[i]);
- i++;
- }
- while (!(UCA0IFG&UCTXIFG));
- UCA0TXBUF = '\n';
- Delays( );
- }
-
- /**********************ADS1256_IO初始化****************************/
- void ADS1256_IO_init()
- {
- P2SEL &=~ 0XFF;
- P2DIR |= BIT0 + BIT3 + BIT4;
- P2DIR &=~ (BIT2 + BIT5);
- }
- /************************************************************/
- /*******************************************
- 函数名称:PutSting
- 功 能:向PC机发送字符串
- 参 数:ptr--指向发送字符串的指针
- 返回值 :无
- ********************************************/
- void PutString(uchar *ptr)
- {
- while(*ptr != '\0')
- {
- while (!(UCA0IFG&UCTXIFG)); // TX缓存空闲?
- UCA0TXBUF = *ptr++; // 发送数据
- }
- while (!(UCA0IFG&UCTXIFG));
- UCA0TXBUF = '\n';
- }
- /*******************************************
- 函数名称:PutChar
- 功 能:向PC机发送一个字符对应的ASCII码
- 参 数:zifu--发送的字符
- 返回值 :无
- ********************************************/
- void PutChar(uchar zifu)
- {
- while (!(UCA0IFG&UCTXIFG));
- UCA0TXBUF = zifu;
-
- }
- /*******************************************
- 函数名称:Delays
- 功 能:延时一会
- 参 数:无
- 返回值 :无
- ********************************************/
- void Delays(void)
- {
- uchar i=20;
- uint j;
- while(i--)
- {
- j=2000;
- while(j--);
- }
- }
复制代码 |