共2条
1/1 1 跳转至页
ZOGLAB 一个实用的 A/D 例子 ZOGLAB
问
void timer_init(void){ //定时器初始化
TACTL = TACLR+TASSEL_1+ID1+ID0; // start TIMER_A continous mode, ACLK as input clock
CCR0 = timerA0_offset; // DIV by 8 to give 16 seconds maximum duration
CCTL0 = CCIE;
CCR1 = timerA1_offset;
//CCTL1 = CCIE;
//TIMER_B sample timing setting
TBCCR0=SAMPLE_PERIOD; //CCR0 determines the sample period = 9
TBCCR1=RESULT_READY_TIME; //give enough time for the ADC12 to work and then start = 7
//processing
TBCCTL0=CCIE; //enables interrupt at CCR0 equ
TBCCTL1=OUTMOD_3; //EQU1 sets OUT1, EQU0 resets OUT1
TBCTL=TBCLR+MC_1+TBSSEL_1; //0x0214;
// start TIMER_B up mode, ACLK as input clock */
}
/*
***************************************************************************************
* ADC12采样型号为TIMEB OUT1,3276.8HZ,转换时钟为4MHz,
***************************************************************************************
*/
void adc12_init(void){
P6SEL = 0xFF;
ADC12CTL0 &=~ ENC; // Disable conversion before changing
ADC12CTL0 = ADC12ON+REFON+MSC+REF2_5V; //
ADC12CTL1 = SHS_3+CONSEQ_3+ADC12SSEL_2+ADC12DIV_1; //
ADC12MCTL0 = VREF+INCH_0;
ADC12MCTL1 = VREF+INCH_1;
ADC12MCTL2 = VREF+INCH_2;
ADC12MCTL3 = VREF+INCH_3;
ADC12MCTL4 = VREF+INCH_4;
ADC12MCTL5 = VREF+INCH_5;
ADC12MCTL6 = VREF+INCH_6;
ADC12MCTL7 = VREF+INCH_7;
ADC12MCTL8 = VREF+INCH_8;
ADC12MCTL9 = VREF+INCH_9;
ADC12MCTL10 = VREF+INCH_10;
ADC12MCTL11 = VREF+INCH_11+EOS;
ADC12IE = 0x0800; // Enable ADC12IFG.11
ADC12CTL0 |= ENC; // Enable conversions
ADC12CTL0 |= ADC12SC;
}
unsigned int adc12_results[adc_channel][Num_of_Results];
interrupt[ADC_VECTOR] void ADC12ISR (void)
{
adc12_results[0][index] = ADC12MEM0;
adc12_results[1][index] = ADC12MEM1;
adc12_results[2][index] = ADC12MEM2;
adc12_results[3][index] = ADC12MEM3;
adc12_results[4][index] = ADC12MEM4;
adc12_results[5][index] = ADC12MEM5;
adc12_results[6][index] = ADC12MEM6;
adc12_results[7][index] = ADC12MEM7;
adc12_results[8][index] = ADC12MEM8;
adc12_results[9][index] = ADC12MEM9;
adc12_results[10][index] = ADC12MEM10;
adc12_results[11][index] = ADC12MEM11;
index = (index+1)&(Num_of_Results-1); // Increment results index,
adc_isr_signal = 1;
}
//计算AD结果,去极值,然后加权平均
void cap_adc_results(void)
{
INT16U i = 0;
clear_WDG;
for(i = 0;i<12;i++){
clear_WDG;
copy_int(adc12_results,buf,8);
shell(buf,8);
//adc_results = (buf[2] + buf[3] + buf[4] + buf[5])>>2;
if(ABS(adc_results - buf[2]) > 30) adc_results = buf[2];
adc_results = (double)((double)GAIN*adc_results + buf[2] + buf[3] + buf[4] + buf[5])/(double)(GAIN + 4);
adc_results = adc_results*(double)ADC_VREF/(double)4096;
clear_WDG;
}
}
TACTL = TACLR+TASSEL_1+ID1+ID0; // start TIMER_A continous mode, ACLK as input clock
CCR0 = timerA0_offset; // DIV by 8 to give 16 seconds maximum duration
CCTL0 = CCIE;
CCR1 = timerA1_offset;
//CCTL1 = CCIE;
//TIMER_B sample timing setting
TBCCR0=SAMPLE_PERIOD; //CCR0 determines the sample period = 9
TBCCR1=RESULT_READY_TIME; //give enough time for the ADC12 to work and then start = 7
//processing
TBCCTL0=CCIE; //enables interrupt at CCR0 equ
TBCCTL1=OUTMOD_3; //EQU1 sets OUT1, EQU0 resets OUT1
TBCTL=TBCLR+MC_1+TBSSEL_1; //0x0214;
// start TIMER_B up mode, ACLK as input clock */
}
/*
***************************************************************************************
* ADC12采样型号为TIMEB OUT1,3276.8HZ,转换时钟为4MHz,
***************************************************************************************
*/
void adc12_init(void){
P6SEL = 0xFF;
ADC12CTL0 &=~ ENC; // Disable conversion before changing
ADC12CTL0 = ADC12ON+REFON+MSC+REF2_5V; //
ADC12CTL1 = SHS_3+CONSEQ_3+ADC12SSEL_2+ADC12DIV_1; //
ADC12MCTL0 = VREF+INCH_0;
ADC12MCTL1 = VREF+INCH_1;
ADC12MCTL2 = VREF+INCH_2;
ADC12MCTL3 = VREF+INCH_3;
ADC12MCTL4 = VREF+INCH_4;
ADC12MCTL5 = VREF+INCH_5;
ADC12MCTL6 = VREF+INCH_6;
ADC12MCTL7 = VREF+INCH_7;
ADC12MCTL8 = VREF+INCH_8;
ADC12MCTL9 = VREF+INCH_9;
ADC12MCTL10 = VREF+INCH_10;
ADC12MCTL11 = VREF+INCH_11+EOS;
ADC12IE = 0x0800; // Enable ADC12IFG.11
ADC12CTL0 |= ENC; // Enable conversions
ADC12CTL0 |= ADC12SC;
}
unsigned int adc12_results[adc_channel][Num_of_Results];
interrupt[ADC_VECTOR] void ADC12ISR (void)
{
adc12_results[0][index] = ADC12MEM0;
adc12_results[1][index] = ADC12MEM1;
adc12_results[2][index] = ADC12MEM2;
adc12_results[3][index] = ADC12MEM3;
adc12_results[4][index] = ADC12MEM4;
adc12_results[5][index] = ADC12MEM5;
adc12_results[6][index] = ADC12MEM6;
adc12_results[7][index] = ADC12MEM7;
adc12_results[8][index] = ADC12MEM8;
adc12_results[9][index] = ADC12MEM9;
adc12_results[10][index] = ADC12MEM10;
adc12_results[11][index] = ADC12MEM11;
index = (index+1)&(Num_of_Results-1); // Increment results index,
adc_isr_signal = 1;
}
//计算AD结果,去极值,然后加权平均
void cap_adc_results(void)
{
INT16U i = 0;
clear_WDG;
for(i = 0;i<12;i++){
clear_WDG;
copy_int(adc12_results,buf,8);
shell(buf,8);
//adc_results = (buf[2] + buf[3] + buf[4] + buf[5])>>2;
if(ABS(adc_results - buf[2]) > 30) adc_results = buf[2];
adc_results = (double)((double)GAIN*adc_results + buf[2] + buf[3] + buf[4] + buf[5])/(double)(GAIN + 4);
adc_results = adc_results*(double)ADC_VREF/(double)4096;
clear_WDG;
}
}
共2条
1/1 1 跳转至页
回复
有奖活动 | |
---|---|
【有奖活动】分享技术经验,兑换京东卡 | |
话不多说,快进群! | |
请大声喊出:我要开发板! | |
【有奖活动】EEPW网站征稿正在进行时,欢迎踊跃投稿啦 | |
奖!发布技术笔记,技术评测贴换取您心仪的礼品 | |
打赏了!打赏了!打赏了! |
打赏帖 | |
---|---|
与电子爱好者谈读图四被打赏50分 | |
与电子爱好者谈读图二被打赏50分 | |
【FRDM-MCXN947评测】Core1适配运行FreeRtos被打赏50分 | |
【FRDM-MCXN947评测】双核调试被打赏50分 | |
【CPKCORRA8D1B评测】---移植CoreMark被打赏50分 | |
【CPKCORRA8D1B评测】---打开硬件定时器被打赏50分 | |
【FRDM-MCXA156评测】4、CAN loopback模式测试被打赏50分 | |
【CPKcorRA8D1评测】--搭建初始环境被打赏50分 | |
【FRDM-MCXA156评测】3、使用FlexIO模拟UART被打赏50分 | |
【FRDM-MCXA156评测】2、rt-thread MCXA156 BSP制作被打赏50分 |