LaunchPad实现AD转换 转换结果经UART通讯送给上位机
[复制链接]
// 本程序用定时器A的比较/捕获功能来模拟UART接口,通信的波特率为9600.
// 定义P1.1口为模拟串行发送,P1.2为模拟串行接收
//------------------------------------------------------------------------------
#include
//------------------------------------------------------------------------------
// 宏定义
//------------------------------------------------------------------------------
#define UART_TXD BIT1 // TXD on P1.1 (Timer0_A.OUT0)
#define UART_RXD BIT2 // RXD on P1.2 (Timer0_A.CCI1A)
//------------------------------------------------------------------------------
// 配置模拟串行收发的波特率, SMCLK = 1MHz
//------------------------------------------------------------------------------
#define UART_TBIT_DIV_2 (1000000 / (9600 * 2))
#define UART_TBIT (1000000 / 9600)
//------------------------------------------------------------------------------
// 定义串行通信有关的全局变量
//------------------------------------------------------------------------------
unsigned int txData; // 串行发送寄存器
unsigned char rxBuffer; // 接收缓存
double results[2] = {0x00,0x00}; // 外部中断计数寄存器
//------------------------------------------------------------------------------
// 函数声明
//------------------------------------------------------------------------------
void TimerA_UART_init(void);
void TimerA_UART_tx(unsigned char byte);
void Init_CLK(void);
void Init_IO(void);
//------------------------------------------------------------------------------
// 主函数
//------------------------------------------------------------------------------
void main(void)
{
WDTCTL = WDTPW + WDTHOLD; // 关闭看门狗
Init_CLK(); // 初始化时钟
Init_IO(); // 初始化I/O口
_EINT(); // 允许中断
TimerA_UART_init(); // 初始化TimerA_UART
ADC10CTL0 = ADC10SHT_2 + ADC10ON + ADC10IE; // 转换时钟选择 ADC10ON(打开ADC10), interrupt enabled(使能中断)
ADC10CTL1 = INCH_5; // input A5(选择输入通道A1,对应P1.5口)
ADC10AE0 |= 0x05; // PA.1 ADC option select (使能P1.5模拟输入)
P1DIR |= 0x01; // Set P1.0 to output direction (P1.0为输出模式)
while(1)
{
ADC10CTL0 |= ENC + ADC10SC; // Sampling and conversion start (ADC10开始采样转换)
__bis_SR_register(CPUOFF + GIE); // LPM0, ADC10_ISR will force exit (开总中断,低功耗模式)
switch(rxBuffer)
{
case 0x00 :
{
TimerA_UART_tx(results[0]);
rxBuffer = 0x00;
break;
}
default :
{
break;
}
}
}
}
//-----------------------初始化时钟-------------------------------------------//
void Init_CLK(void)
{
DCOCTL = 0x00; // 设置时钟DCOCLK为1MHz
BCSCTL1 = CALBC1_1MHZ;
DCOCTL = CALDCO_1MHZ;
}
//-----------------------初始化I/O口------------------------------------------//
void Init_IO(void)
{
P1OUT = 0x00; // 初始化I/O口
P1SEL = UART_TXD + UART_RXD; // 定时器模拟RXD,TXD接口
P1DIR = UART_TXD + BIT0 + BIT6; // P1.0,P1.1,P1.6为输出
P1DIR &= ~UART_RXD; // P1.2为输入
P1OUT &= ~(BIT0 + BIT6); // p1.0输出低
P1IES &= ~(BIT3 + BIT4); // 下降沿中断
P1IE |= BIT3 + BIT4; // 允许外部中断
P1IFG =0; // 清除中断标志
}
//------------------------------------------------------------------------------
// 定时器A模拟全双工串行通信
//------------------------------------------------------------------------------
void TimerA_UART_init(void)
{
TACCTL0 = OUT; // 设置端口空闲作为标记= ' 1 '
TACCTL1 = SCS + CM1 + CAP + CCIE; // Sync, Neg Edge, Capture, Int
TACTL = TASSEL_2 + MC_2; // 时钟 = SMCLK, 连续模式开始
}
//------------------------------------------------------------------------------
// Outputs one byte using the Timer_A UART
//------------------------------------------------------------------------------
void TimerA_UART_tx(unsigned char byte)
{
while (TACCTL0 & CCIE); // Ensure last char got TX'd
TACCR0 = TAR; // Current state of TA counter
TACCR0 += UART_TBIT; // One bit time till first bit
TACCTL0 = OUTMOD0 + CCIE; // Set TXD on EQU0, Int
txData = byte; // Load global variable
txData |= 0x100; // Add mark stop bit to TXData
txData <<= 1; // Add space start bit
}
//------------------------------------------------------------------------------
// Prints a string over using the Timer_A UART
//------------------------------------------------------------------------------
/* void TimerA_UART_print(char *string)
{
while (*string)
{
TimerA_UART_tx(*string++);
}
} */
//------------------------------------------------------------------------------
// Timer_A UART - Transmit Interrupt Handler
//------------------------------------------------------------------------------
#pragma vector = TIMERA0_VECTOR
__interrupt void Timer_A0_ISR(void)
{
static unsigned char txBitCnt = 10;
TACCR0 += UART_TBIT; // Add Offset to CCRx
if (txBitCnt == 0) // All bits TXed?
{
TACCTL0 &= ~CCIE; // All bits TXed, disable interrupt
txBitCnt = 10; // Re-load bit counter
}
else
{
if (txData & 0x01)
{
TACCTL0 &= ~OUTMOD2; // TX Mark '1'
}
else
{
TACCTL0 |= OUTMOD2; // TX Space '0'
}
txData >>= 1;
txBitCnt--;
}
}
//------------------------------------------------------------------------------
// Timer_A UART - Receive Interrupt Handler
//------------------------------------------------------------------------------
#pragma vector = TIMERA1_VECTOR
__interrupt void Timer_A1_ISR(void)
{
static unsigned char rxBitCnt = 8;
static unsigned char rxData = 0;
switch (__even_in_range(TAIV, TAIV_TAIFG)) // Use calculated branching
{
case TAIV_TACCR1: // TACCR1 CCIFG - UART RX
TACCR1 += UART_TBIT; // Add Offset to CCRx
if (TACCTL1 & CAP) // Capture mode = start bit edge
{
TACCTL1 &= ~CAP; // 切换到比较模式
TACCR1 += UART_TBIT_DIV_2; // Point CCRx to middle of D0
}
else
{
rxData >>= 1;
if (TACCTL1 & SCCI)
{
rxData |= 0x80;
}
rxBitCnt--;
if (rxBitCnt == 0) // 所有数据是否发送完毕
{
rxBuffer = rxData; // 将接收的数据寄存在rxBuffer中
rxBitCnt = 8;
TACCTL1 |= CAP; // 切换到捕获模式
__bic_SR_register_on_exit(LPM0_bits); // 退出低功耗模式
}
}
break;
}
}
//-------------------------------------------------------------------------------
// ADC10 interrupt service routine
#pragma vector=ADC10_VECTOR
__interrupt void ADC10_ISR(void)
{
results[0] = (ADC10MEM);
__bic_SR_register_on_exit(CPUOFF); // Clear CPUOFF bit from 0(SR)
}