基于MSP430内部10位ADC的模数转化并通过串口发送

来源:互联网 发布:速卖通翻译软件 编辑:程序博客网 时间:2024/05/22 06:22
//******************************************************************************//  MSP-FET430P140 Demo - ADC12, Using the Internal Reference////  Description: //////                MSP430F149//             ---------------//            |               |//     Vin -->|P6.0/A0        |//            |               |//////  ZhenHua Liu//  NWSUAF.//  2017,12,05//  Built with IAR Embedded Workbench Version: 3.21A//******************************************************************************#include  <msp430x14x.h>#include"adc.h"void DelayXms(unsigned int i);      //delay about x msvoid uartInit();                    //Init the usart0 void sendChar(char c);              //send a charvoid sendString(unsigned char *p);  //send a string,input must be pointervoid int2charssend(int value);     //convert the adc value to string and send itvoid main(void){   int adcValue1,adcValue2;   WDTCTL = WDTPW+WDTHOLD;                   // Stop watchdog timer   uartInit();                               //init the uart   while (1)   {       adcValue1 = GetAdcValue1();   // adcValue2 = GetAdcValue2();    int2charssend(adcValue1);    DelayXms(2000);       }}/*****************************************************************************函数名: DelayXms()                                                          /*作用  :800KHz下大约1ms                                                     /*返回值:无                                                                  /*参数  :一个字符                                                            /*作者  :Zhenhua Liu                                                         / *时间  :2017.12.07                                                          / *****************************************************************************/void DelayXms(unsigned int i){    unsigned int j;    for( ; i>0; i--){      for(j=0;j<200;j++);     }  }/*****************************************************************************函数名: int2charssend()                                                     /*作用  :启动ADC转化之后,将P6.0口的模拟信号转化为数字量(0-4096)并通过串口发送 /*返回值:无                                                                  /*参数  :整数                                                                /*作者  :Zhenhua Liu                                                         / *时间  :2017.12.07                                                          / *****************************************************************************/void int2charssend(int value){   unsigned char s[] = "0000";   unsigned char *p;   int bits=0,ten = 0,hundred = 0,thousand = 0;      thousand = value/1000;   hundred  = value%1000/100;   ten = value%100/10;   bits = value%10;   s[0]=thousand+'0';   s[1]=hundred+'0';   s[2]=ten+'0';   s[3]=bits+'0';   p = s;   sendString(p);}/*****************************************************************************函数名: sendChar()                                                          /*作用  :USART0发送一个字符                                                  /*返回值:无                                                                  /*参数  :一个字符                                                            /*作者  :Zhenhua Liu                                                         / *时间  :2017.12.07                                                          / *****************************************************************************/void sendChar(char c){   TXBUF0=c;   while((UTCTL0&0X01)==0);//等待数据发送完毕 }/*****************************************************************************函数名: sendString()                                                        /*作用  :USART0发送字符串                                                    /*返回值:无                                                                  /*参数  :char型指针                                                          /*作者  :Zhenhua Liu                                                         / *时间  :2017.12.07                                                          / *****************************************************************************/void sendString(unsigned char *p){     while(*p!='\0'){            TXBUF0=*p++;            while((UTCTL0&0X01)==0);//等待数据发送完毕             }}/*****************************************************************************函数名: uartInit()                                                          /*作用  :USART0的初始化配置,使用P3.4和P3.5,不使用中断                      /*返回值:无                                                                  /*参数  :无                                                                  /*作者  :Zhenhua Liu                                                         / *时间  :2017.12.07                                                          / *****************************************************************************/void uartInit(){       P3DIR|=BIT4+BIT5;        P3SEL|=BIT4+BIT5;                ME1 |= UTXE0 + URXE0;  // 使能USART0收发        UCTL0 |= CHAR;         // 8-bit 数据,一位停止位        UTCTL0 |= SSEL0;       // 选择时钟,UCLK = ACLK,32768        UBR00 = 0x03;          // 32k/9600        UBR10 = 0x00;          //        UMCTL0 = 0x4a;         // Modulation        UCTL0 &= ~SWRST;       // 初始化UART0状态机,一般要设置好串口之后才复位      }

其中需要添加ADC的头文件和源文件,并且要把adc.c包含到工程中去

adc.c如下:

#include<msp430x14x.h>unsigned int GetAdcValue1(void){   unsigned int value;                       //digital value   P6SEL |= BIT0+BIT1;                            // Enable A/D channel A0   ADC12CTL0 = ADC12ON+SHT0_6;               // Turn on and set up ADC12   ADC12CTL1 = SHP;                          // Use sampling timer   ADC12MCTL0 =INCH_0+ SREF_0;               // Vr+=VCC,Vr+=VSS,switch the inch1   ADC12CTL0 |= ENC;                         // Enable conversions   ADC12MEM0 = 0;                            //首先对转化结果的寄存器清零   ADC12CTL0 |= ADC12SC;                     // Start conversion   while ((ADC12IFG & BIT0)==0);   _NOP();                                   // SET BREAKPOINT HERE   value = ADC12MEM0;   return value;}unsigned int GetAdcValue2(void){   unsigned int value;                       //digital value   P6SEL |= BIT0+BIT1;                            // Enable A/D channel A0   ADC12CTL0 = ADC12ON+SHT0_6;               // Turn on and set up ADC12   ADC12CTL1 = SHP;                          // Use sampling timer   ADC12MCTL0 =INCH_1+ SREF_0;               // Vr+=VCC,Vr+=VSS,switch the inch1   ADC12CTL0 |= ENC;                         // Enable conversions   ADC12MEM0 = 0;                            //首先对转化结果的寄存器清零   ADC12CTL0 |= ADC12SC;                     // Start conversion   while ((ADC12IFG & BIT0)==0);   _NOP();                                  // SET BREAKPOINT HERE   value = ADC12MEM0;   return value;}
adc.h如下:

#ifndef _ADC_H_ #define _ADC_H_unsigned int GetAdcValue1(void);     //start adc and return a digital value (0-4096)unsigned int GetAdcValue2(void);     //start adc and return a digital value (0-4096)#endif



原创粉丝点击