#include <ioCC2530.h>
int h,l;
void init(void)
{
CLKCONCMD = 0x28; //System clock-source select:32MHz,Timer ticks output setting:250kHz
while(CLKCONSTA & ~0x28); //wait for CLKCONCMD selected
PERCFG = 0x00; //alternative 1 location
P0SEL = 0x3c; //P0_2:RX P0_2:TX P0_2:CT P0_2:RT
P2DIR &= ~0xc0; //set port 0 1st priority: USART 0 2nd priority: USART 1 3rd priority: Timer 1
U0CSR |= 0x80; //usart mode select : UART mode
U0GCR |= 9;
U0BAUD |= 59; //set baud rate:19200
UTX0IF = 1; //USART 0 TX interrupt flag : interrupt pending
APCFG |= 0x80; //P0_7 analog I/O enable
P0SEL |= 0x80; //P0_7 function select peripheral function
P0DIR &= ~0x80; //p0_7 Input
}
void get_adc(void)
{
ADCIF = 0;
ADCCON3 = (0x80 | 0x30 | 0x07);
while(!ADCIF);
h = ADCH;
l = ADCL;
}
void delay(int n)
{
int i, j;
for(i = 0; i < n; i++)
for(j = 0; j < n; j++);
}
void uart_send(int arg)
{
int j;
while(1)
{
j = arg % 10;
arg /= 10;
U0DBUF = j + 48;
while(!UTX0IF);
UTX0IF = 0;
if(arg == 0)break;
}
}
void main(void)
{
init();
while(1)
{
get_adc();
uart_send(h);
uart_send(l);
U0DBUF = '\n';
while(!UTX0IF);
UTX0IF = 0;
delay(300);
}
}
功能:实时将电压值捕获传输至UART。
转动开发板上的蓝色旋钮可观察到电压变化。