前几天买了个小开发板,初学430,希望和大家多多交流~
/*
* mengxp works 2015/10/28
*/
#include
#include
#define SHUTDOWN_WATCHDOG() do{WDTCTL = WDTPW + WDTHOLD;}while(0)
#define SET_MCLK_XT2() do{BCSCTL2 |= SELM_2;}while(0)
#define SET_SMCLK_XT2() do{BCSCTL2 |= SELS;}while(0)
#define ENABLE_XT2() \
do{ \
BCSCTL1 &= ~XT2OFF; \
do { \
volatile unsigned int _i = 0xFF; \
IFG1 &= ~OFIFG; \
while(_i--); \
}while ((IFG1 & OFIFG)); \
}while(0)
#define DEAD_LOOP() while(1){__no_operation();}
#define WAIT_FOR_REFERENCE() do{int _i;for(_i=0;_i<0x3600;_i++);}while(0)
void InitTimer(void)
{
_EINT(); //启用中断
TACTL = TASSEL_2 + TACLR + MC_1; //定时器时钟源为SMCLK
TACCR0 = 7999; //假装8000分频得到1KHz
TACCTL0 = CCIE; //允许定时器中断
}
unsigned long timer = 0;
#pragma vector=TIMERA0_VECTOR
__interrupt void TA_ISR(void)
{
timer++;
}
void InitGPIO(void)
{
P2DIR |= 1;
P2OUT = 0;
}
//115200 8N1
void InitUSART0(void)
{
P3SEL |= 0x30; //P3.4 P3.5 USART0 TXD/RXD
ME1 |= UTXE0 + URXE0; //Enable USART0 TXD/RXD
UCTL0 |= CHAR;
UTCTL0 |= SSEL1;
UBR00 = 69 & 0xFF;
UBR10 = 69 >> 8;
UMCTL0 = 0x00;
UCTL0 &= ~SWRST;
IE1 |= URXIE0;
}
void USART0_Tx(char x)
{
while(!(IFG1 & UTXIFG0));
TXBUF0 = x;
}
void USART0_Puts(char *str)
{
while(1)
{
char x = *str;
if(!x)
break;
USART0_Tx(x);
str++;
}
}
#pragma vector=USART0RX_VECTOR
__interrupt void USART0_Rx(void)
{
if(IFG1 & URXIFG0)
{
char x = RXBUF0;
}
}
unsigned long ad = 0;
void InitADC(int _SHT, int _DIV)
{
_EINT();
ADC12CTL0 &= ~ENC;
P6SEL |= 0x01; //P6.0 启用AD转换通道 A0
ADC12CTL0 = _SHT + ADC12ON + MSC; //内部2.5V参考
ADC12CTL1 = SHP + CONSEQ_2 + ADC12SSEL_3 + _DIV + ADC12DIV_0;
ADC12IE = 1;
ADC12CTL0 |= ENC; //启用采样
ADC12CTL0 |= ADC12SC; //开始采样
}
#pragma vector=ADC12_VECTOR
__interrupt void ADC12_ISR(void)
{
int i;
ad++;
i = ADC12MEM0;
}
void main( void )
{
char buf[32];
unsigned long t0 = 0;
int sht, div;
SHUTDOWN_WATCHDOG();
ENABLE_XT2();
SET_MCLK_XT2();
SET_SMCLK_XT2();
InitGPIO();
InitTimer();
InitUSART0();
for(sht = 0; sht < 9; sht++)
{
for(div = 0; div < 8; div++)
{
InitADC(sht * SHT0_1, div * ADC12DIV_1);
ad = 0;
t0 = timer;
while(timer < t0 + 1000)
{
_NOP();
}
P2OUT ^= 1;
sprintf(buf,"SHT_%d DIV_%d %dK sample/s\r\n",div,sht,(int)(ad / 1000));
USART0_Puts(buf);
}
}
DEAD_LOOP();
}
运行结果
SHT_0 DIV_0 235K sample/s
SHT_1 DIV_0 235K sample/s
SHT_2 DIV_0 156K sample/s
SHT_3 DIV_0 117K sample/s
SHT_4 DIV_0 94K sample/s
SHT_5 DIV_0 78K sample/s
SHT_6 DIV_0 67K sample/s
SHT_7 DIV_0 58K sample/s
SHT_0 DIV_1 245K sample/s
SHT_1 DIV_1 190K sample/s
SHT_2 DIV_1 126K sample/s
SHT_3 DIV_1 95K sample/s
SHT_4 DIV_1 76K sample/s
SHT_5 DIV_1 63K sample/s
SHT_6 DIV_1 54K sample/s
SHT_7 DIV_1 47K sample/s
SHT_0 DIV_2 274K sample/s
SHT_1 DIV_2 137K sample/s
SHT_2 DIV_2 91K sample/s
SHT_3 DIV_2 68K sample/s
SHT_4 DIV_2 55K sample/s
SHT_5 DIV_2 45K sample/s
SHT_6 DIV_2 39K sample/s
SHT_7 DIV_2 34K sample/s
SHT_0 DIV_3 177K sample/s
SHT_1 DIV_3 88K sample/s
SHT_2 DIV_3 59K sample/s
SHT_3 DIV_3 44K sample/s
SHT_4 DIV_3 35K sample/s
SHT_5 DIV_3 29K sample/s
SHT_6 DIV_3 25K sample/s
SHT_7 DIV_3 22K sample/s
SHT_0 DIV_4 103K sample/s
SHT_1 DIV_4 51K sample/s
SHT_2 DIV_4 34K sample/s
SHT_3 DIV_4 25K sample/s
SHT_4 DIV_4 20K sample/s
SHT_5 DIV_4 17K sample/s
SHT_6 DIV_4 14K sample/s
SHT_7 DIV_4 12K sample/s
SHT_0 DIV_5 73K sample/s
SHT_1 DIV_5 36K sample/s
SHT_2 DIV_5 24K sample/s
SHT_3 DIV_5 18K sample/s
SHT_4 DIV_5 14K sample/s
SHT_5 DIV_5 12K sample/s
SHT_6 DIV_5 10K sample/s
SHT_7 DIV_5 9K sample/s
SHT_0 DIV_6 56K sample/s
SHT_1 DIV_6 28K sample/s
SHT_2 DIV_6 18K sample/s
SHT_3 DIV_6 14K sample/s
SHT_4 DIV_6 11K sample/s
SHT_5 DIV_6 9K sample/s
SHT_6 DIV_6 8K sample/s
SHT_7 DIV_6 7K sample/s
SHT_0 DIV_7 39K sample/s
SHT_1 DIV_7 19K sample/s
SHT_2 DIV_7 12K sample/s
SHT_3 DIV_7 9K sample/s
SHT_4 DIV_7 7K sample/s
SHT_5 DIV_7 6K sample/s
SHT_6 DIV_7 5K sample/s
SHT_7 DIV_7 4K sample/s
SHT_0 DIV_8 29K sample/s
SHT_1 DIV_8 14K sample/s
SHT_2 DIV_8 9K sample/s
SHT_3 DIV_8 7K sample/s
SHT_4 DIV_8 5K sample/s
SHT_5 DIV_8 4K sample/s
SHT_6 DIV_8 4K sample/s
SHT_7 DIV_8 3K sample/s
很奇怪这一行
while(timer < t0 + 1000)
如果改成这样的写法
while(timer - 1000 < t0)
程序就不能正常执行了,像死机了一样。我没有jtag调试器,不知道究竟是什么原因
难道是当timer = 0的时候做减法会溢出,导致了cpu异常?
还有就是sprintf格式化long 32位整数
我的写法是
sprintf(buf,"%ld",ad);
貌似不能正常格式化,是因为ccs的c库的实现有问题吧?
|