完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
我正在使用STM8S103测量分别命名为vs12,vs5和vs3的三个电压信号通道。定时器4使能每10ms产生一次中断,并交替测量三个电压信号。也就是说,在第1个10ms中断中,测量vs12。在第二个10ms中断中,测量vs5。在第3个10ms中断中,测量vs3,依此类推。在每次测量期间,mcu进行16次捕获以进行进一步的平均处理。测量结果将通过UART显示在超级终端中。
问题是当只启用1个通道(vs12,vs5或vs3)时,测量是正确的。但是当启用两个通道时,两个通道的测量都是错误的。谁能看看下面的代码并给我一些建议? C代码描述如下。 主要() { ADC1-> CR1 | =(ADC1_CR1_SPSEL&amp;(0x02 CR2 | = ADC1_CR2_ALIGN; //正确的对齐 而(1){ //选择ADC通道 if(vs_sel == 0){ ADC1-> CSR | =(ADC1_CSR_CH&amp; CH_VS12); } else if(vs_sel == 1){ ADC1-> CSR | =(ADC1_CSR_CH&amp; CH_VS5); } } } 定时器4中断: @far @interrupt void irq_system_tim4_ovf(void) { //如果采用MAX NO OF OF Sample采样电压 //值不会记录在数组vs12_arr中 no_of_sample_vals = 0; while(no_of_sample_vals&lt; MAX_NO_SAMPLE_VAL) { //启动ADC ADC1-> CR1 | = ADC1_CR1_ADON; // ADC启动 ADC1-> CR1 | = ADC1_CR1_ADON; //开始转换 //检查EOC while(!(ADC1-&gt; CSR&amp; ADC1_CSR_EOC)){}; //必须先读LSB if(vs_sel == 0){ vs12_arr [no_of_sample_vals] = ADC1-&gt; DRL; vs12_arr [no_of_sample_vals] | = ADC1-&gt; DRH&lt;&lt; 8; } else if(vs_sel == 1){ vs5_arr [no_of_sample_vals] = ADC1-&gt; DRL; vs5_arr [no_of_sample_vals] | = ADC1-&gt; DRH&lt;&lt; 8; } // ADC断电 ADC1-> CR1 | = ~ADC1_CR1_ADON; no_of_sample_vals ++; } //更新时间计数器 flag_10ms = 1; if(t_10ms == 100){/ * 1秒完成* / flag_1sec = 1; t_10ms = 0; } T_10MS ++; vs_sel ++; if(vs_sel == 3)vs_sel = 0; //清除旗帜 TIM4-> SR1&amp; =〜(0x01); 返回; } 以上来自于谷歌翻译 以下为原文 I am using STM8S103 to measure three channels of voltage signals named vs12, vs5 and vs3 respectively. Timer 4 is enabled to generate interrupt every 10ms and three voltage signals are measured alternately. That is, in the 1st 10ms interrupt, vs12 is measured. In the 2nd 10ms interrupt, vs5 is measured. In the 3rd 10ms interrupt, vs3 is measured, and so on. During each measurement, mcu makes 16 captures for further average processing. The measurement result will be displayed in HyperTerminal via UART. The problem is that when only 1 channel (vs12, vs5, or vs3) is enabled, the measurement is correct. But when two channels are enabled, both channels' measurements are wrong. Could anyone have a look at the following code and give me some suggestions? The C code is described as follows. main() { ADC1->CR1 |= (ADC1_CR1_SPSEL & (0x02<<4)); //fADC = 4 MHz ADC1->CR2 |= ADC1_CR2_ALIGN; //right aligment while(1){ // select the ADC channel if (vs_sel==0){ ADC1->CSR |= (ADC1_CSR_CH & CH_VS12); } else if (vs_sel==1){ ADC1->CSR |= (ADC1_CSR_CH & CH_VS5); } } } Timer 4 Interrupt: @far @interrupt void irq_system_tim4_ovf (void) { // If MAX NO OF SAMPLES are taken then voltage // values will not be recorded in array vs12_arr no_of_sample_vals = 0; while( no_of_sample_vals < MAX_NO_SAMPLE_VAL ) { //start ADC ADC1->CR1 |= ADC1_CR1_ADON; //ADC powered up ADC1->CR1 |= ADC1_CR1_ADON; //begin conversion //check for the EOC while (!(ADC1->CSR & ADC1_CSR_EOC)){}; //must read LSBs first if (vs_sel==0){ vs12_arr[no_of_sample_vals] = ADC1->DRL; vs12_arr[no_of_sample_vals] |= ADC1->DRH << 8; } else if (vs_sel==1){ vs5_arr[no_of_sample_vals] = ADC1->DRL; vs5_arr[no_of_sample_vals] |= ADC1->DRH << 8; } //ADC powered down ADC1->CR1 |= ~ADC1_CR1_ADON; no_of_sample_vals++; } // Update the time counter flag_10ms = 1; if (t_10ms==100){ /* 1 sec is completed */ flag_1sec = 1; t_10ms = 0; } t_10ms++; vs_sel++; if (vs_sel==3) vs_sel = 0; // Clear the flag TIM4->SR1 &=~(0x01); return; } |
|
相关推荐
2个回答
|
|
|
|
|
|
那么问题是什么呢?
|
|
|
|
只有小组成员才能发言,加入小组>>
请教:在使用UDE STK时,单片机使用SPC560D30L1,在配置文件怎么设置或选择?里面只有SPC560D40的选项
2642 浏览 1 评论
3208 浏览 1 评论
请问是否有通过UART连接的两个微处理器之间实现双向值交换的方法?
1783 浏览 1 评论
3611 浏览 6 评论
5989 浏览 21 评论
939浏览 4评论
1316浏览 4评论
在Linux上安装Atollic TRUEStudio的步骤有哪些呢?
584浏览 3评论
使用DMA激活某些外设会以导致外设无法工作的方式生成代码是怎么回事
1303浏览 3评论
1361浏览 3评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-24 22:34 , Processed in 1.262153 second(s), Total 77, Slave 61 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号