该楼层疑似违规已被系统折叠 隐藏此楼查看此楼
// Description: This example shows how to perform repeated conversions on a// single channel using "repeat-single-channel" mode. AVcc is used for the// reference and repeated conversions are performed on Channel A0. Each// conversion result is moved to an 8-element array called results[]. Test by// applying a voltage to channel A0, then running. To view the conversion results// open a watch window and view 'results.'// This can run even in LPM4 mode as ADC has its own clock
#include "io430.h"
#include "in430.h"
#include "type.h"
#include "nbc430.h"
#include "lcd1602.h"
#define Num_of_Results 8
static U16 result[Num_of_Results];
float voltage;
//************************************IO_init(void)
void IO_init(void)
{
P6SEL |= 0X01; // Enable P6.0 channel A0
}
//************************************ADC12_init(void)
void ADC12_init(void)
{ ADC12CTL0 = ADC12ON + MSC + SHT0_15;
ADC12CTL1 = SHP + CONSEQ_2;
ADC12IE = 0X01; // Enable ADC12FIG.0
ADC12CTL0 |= ENC; // Enable conversions}
//*************************************ADC12_count(void)
float ADC12_count(U16 temp)
{ voltage = (float)temp / 4095 * 3.3;
return voltage;
}
//************************************main(void)
int main( void )
{ // Stop watchdog timer to prevent time out reset
WDTCTL = WDTPW + WDTHOLD;
IO_init();
LCD_Init();
ADC12_init();
LCD_Print_String(0,0,"HELLO MAZIRONG");
while(1) {
ADC12CTL0 |= ADC12SC; // Start conversion
LCD_Print_Char(0,1,(int)ADC12_count(result[0])%10 + 0X30);
LCD_Print_Char(1,1,'.');
LCD_Print_Char(2,1,(int)(ADC12_count(result[0])*10)%10 + 0X30);
LCD_Print_Char(3,1,(int)(ADC12_count(result[0])*100)%10 + 0X30);
LCD_Print_Char(4,1,(int)(ADC12_count(result[0])*1000)%10 + 0X30);
_BIS_SR(LPM0_bits + GIE);
}
}
//******************************ADC12 interrupt services rontine
#pragma vector = ADC12_VECTOR__interrupt void ADC12_ISR(void)
{ static U8 index = 0;
result[index] = ADC12MEM0;
/*index++;
if(index == 8)
{
index = 0;
}*/
index = (index + 1) % 8; // (这句话和上面的三个语句理论上功效一样,但用了这句程序就跑飞了(while(1)中的语句只能执行一次然后程序就不能进入while(1)执行了用仿真试过)(?)
_BIC_SR_IRQ(LPM0_bits);}