王林夕吧 关注:7贴子:372
  • 0回复贴,共1
#include "msp430g2553.h"
void ADC10_Init(void); //函数声明
void USCI_A0_Init(void);
void putchar(unsigned char data);
void putstring(unsigned char *s);unsigned char output[6];
unsigned int temp=0;
float vol=0;
void main(void)
{
WDTCTL=WDTPW+WDTHOLD;
//系统时钟初始化1MHz
BCSCTL1=CALBC1_1MHZ;
DCOCTL=CALDCO_1MHZ;
ADC10_Init();//ADC初始化
USCI_A0_Init();
while(1)
{
ADC10CTL0 |= ENC + ADC10SC; //ADC10SC=1,软件触发ADC
__bis_SR_register(CPUOFF + GIE); // LPM0, ADC10_ISR will force exit
//while(!(ADC10CTL0 & ADC10IFG)); //等待转换完成
temp=ADC10MEM;//读取ADC结果
vol=temp*3.3/1023; //转换成实际电压
output[0]=(unsigned char)vol%10+'0'; //个位ASCII码字符
output[1]='.';
output[2]=(unsigned char)(vol*10)%10+'0';
output[3]=(unsigned char)(vol*100)%10+'0';
output[4]='V';
output[5]=0; //终止符
putstring(output);
putchar('\n'); //换行
__delay_cycles(2000); }
}//ADC10初始化
void ADC10_Init(void)
{ ADC10CTL0&=~ENC;
ADC10CTL0=SREF_0+ADC10SHT_2+ADC10ON+ ADC10IE;//参考电压Vcc,16 x ADC10CLK采样保持时间, ADC10 ON,开ADC中断
ADC10CTL0&=~ADC10IFG; //ADC10中断标志
ADC10CTL1=INCH_0; //ADC 通道A7(P1_7)
ADC10AE0 |= 0x00; //A7 P1_7通道使能
ADC10CTL0|=ENC;//ADC 使能开启
}
#pragma vector=ADC10_VECTOR
__interrupt void ADC10_ISR (void)
{
__bic_SR_register_on_exit(CPUOFF); // Clear CPUOFF bit from 0(SR)
}void USCI_A0_Init(void)
{
IE2|=UCA0TXIE+UCA0RXIE;
IFG2=0x00; P1SEL|=BIT1+BIT2; //P1.1 P1.2为UART口
P1SEL2|=BIT1+BIT2;
UCA0CTL1|=UCSWRST; //USCI复位状态保持
UCA0CTL0=0x00; //无奇偶校验,8bit数据位,1bit停止位
UCA0CTL1=UCSSEL_2+UCSWRST; //SMCLK时钟源
UCA0BR0=104; //9600bps N=104
UCA0MCTL=UCBRF_0+UCBRS_1; //round((104.17-104)*8)
UCA0CTL1&=~UCSWRST; //USCI复位状态释放}
//发送字符
void putchar(unsigned char data)
{
while((IFG2&UCA0TXIFG)==0);//等待发送缓冲区为空
UCA0TXBUF=data;
}
//发送字符串
void putstring(unsigned char *s)
{
while(*s!=0)
{
putchar(*s);
s++;
}
}


IP属地:四川1楼2019-01-10 21:07回复