MSP430内部10位ADC的模数转化并通过串口发送
[复制链接]
//******************************************************************************
// MSP-FET430P140 Demo - ADC12, Using the Internal Reference
// Description:
// MSP430F149
// ---------------
// | |
// Vin -->|P6.0/A0 |
// | |
//******************************************************************************
#include <msp430x14x.h>
#include"adc.h"
void DelayXms(unsigned int i); //delay about x ms
void uartInit(); //Init the usart0
void sendChar(char c); //send a char
void sendString(unsigned char *p); //send a string,input must be pointer
void int2charssend(int value); //convert the adc value to string and send it
void 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 /
*返回值:无 /
*参数 :一个字符 /
*****************************************************************************/
void DelayXms(unsigned int i){
unsigned int j;
for( ; i>0; i--){
for(j=0;j<200;j++);
}
}
/****************************************************************************
*函数名: int2charssend() /
*作用 :启动ADC转化之后,将P6.0口的模拟信号转化为数字量(0-4096)并通过串口发送 /
*返回值:无 /
*参数 :整数 /
*****************************************************************************/
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发送一个字符 /
*返回值:无 /
*参数 :一个字符 /
*****************************************************************************/
void sendChar(char c){
TXBUF0=c;
while((UTCTL0&0X01)==0);//等待数据发送完毕
}
/****************************************************************************
*函数名: sendString() /
*作用 :USART0发送字符串 /
*返回值:无 /
*参数 :char型指针 /
*****************************************************************************/
void sendString(unsigned char *p){
while(*p!='\0'){
TXBUF0=*p++;
while((UTCTL0&0X01)==0);//等待数据发送完毕
}
}
/****************************************************************************
*函数名: uartInit() /
*作用 :USART0的初始化配置,使用P3.4和P3.5,不使用中断 /
*返回值:无 /
*参数 :无 / /
*****************************************************************************/
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
下面给出TI官方的例程供参考:
//******************************************************************************
// MSP-FET430P140 Demo - ADC12, Sample A0, Set P1.0 if A0 > 0.5*AVcc
//
// Description: A single sample is made on A0 with reference to AVcc.
// Software sets ADC10SC to start sample and conversion - ADC12SC
// automatically cleared at EOC. ADC12 internal oscillator times sample (16x)
// and conversion. In Mainloop MSP430 waits in LPM0 to save power until ADC12
// conversion complete, ADC12_ISR will force exit from LPM0 in Mainloop on
// reti. If A0 > 0.5*AVcc, P1.0 set, else reset.
//
// MSP430F149
// -----------------
// /|\| XIN|-
// | | |
// --|RST XOUT|-
// | |
// Vin-->|P6.0/A0 P1.0|--> LED
//
// M. Buccini
// Texas Instruments Inc.
// Feb 2005
// Built with IAR Embedded Workbench Version: 3.21A
//******************************************************************************
#include <msp430x14x.h>
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // Stop WDT
ADC12CTL0 = SHT0_2 + ADC12ON; // Set sampling time, turn on ADC12
ADC12CTL1 = SHP; // Use sampling timer
ADC12IE = 0x01; // Enable interrupt
ADC12CTL0 |= ENC; // Conversion enabled
P6SEL |= 0x01; // P6.0 ADC option select
P1DIR |= 0x01; // P1.0 output
for (;;)
{
ADC12CTL0 |= ADC12SC; // Sampling open
_BIS_SR(CPUOFF + GIE); // LPM0, ADC12_ISR will force exit
}
}
// ADC12 interrupt service routine
#pragma vector=ADC_VECTOR
__interrupt void ADC12_ISR (void)
{
if (ADC12MEM0 < 0x7FF)
P1OUT &= ~0x01; // Clear P1.0 LED off
else
P1OUT |= 0x01; // Set P1.0 LED on
_BIC_SR_IRQ(CPUOFF); // Clear CPUOFF bit from 0(SR)
}
|