项目需要一个定时器10us一次中断计时间,然后定时器里判断时间,每250us adc采样一次(单通道单次采样),这个方式下,原本采样完8000次,用时应该是2s(250*8000us),结果自己掐表8s多,是否由于定时器中断太多,导致采样耗费时间了,将定时器中断改成50us的话,再测试采样那么多数据,掐表2s多,这个暂时认为比较准确,但是定时器的50us,时间太长,需要10us精度的计时,不知道有没有什么更好的办法控制adc采样,又不耽误计时。10us计时下开adc的中断,由于定时器中断优先级高,次数又太快,adc中断无法进入。照理来说adc最少需要3+13个ADC时钟,不会达到10us这么长时间的呀?求指教。单片机是MSP430FR5949,内部16MHz的DCO作为时钟。
下面是直接在定时器里去采样的,没有用ADC中断的方式。
定时器配置。
void TimerB0_Init(uint16_t t)
{
TB0CCTL0 = CCIE; // TBCCR0 interrupt enabled
TB0CCR0 = 160; // 10us
TB0CTL = TBSSEL__SMCLK | MC__UP; // SMCLK, 16M UP mode
}
复制代码
定时器中断
/*定时器B0中断服务函数*/
#pragma vector = TIMER0_B0_VECTOR
__interrupt void Timer0_B0_ISR(void)
{
RealTime.u10us++; //10US
if(RealTime.u10us==100000)
{
RealTime.u10us=0;
RealTime.usec++;
if(RealTime.usec==60)
{
RealTime.usec=0;
RealTime.umin++;
if(RealTime.umin==60)
{
RealTime.umin=0;
RealTime.uhour++;
if(RealTime.uhour==24)
{
RealTime.uhour=0;
}
}
}
}
if(RealTime.u10%25==0)//250us采样一次
{
A9_sample_start();
}
}
ADC配置
void ADC_Init(void)
{
while(REFCTL0 & REFGENBUSY); // If ref generator busy, WAIT
REFCTL0 |= REFVSEL_2 | REFON; // Select internal ref = 2.5V
// Internal Reference ON
// Configure ADC12
//ADC12CTL0 = ADC12ON | ADC12SHT0_2; // Turn on ADC12, set sampling time
ADC12CTL0 |= ADC12ON + ADC12SHT0_2+ADC12SHT1_2;
//ADC12CTL1 = ADC12SHP;
ADC12CTL1 = ADC12SHP | ADC12SSEL_3 ; // Use sampling clock SMCLK 16M;
ADC12CTL3 &= 0xFFE0;
ADC12CTL3 |= ADC12CSTARTADD_9;
ADC12MCTL9 = ADC12INCH_9 | ADC12VRSEL_1;
ADC12CTL0 |= ADC12ENC;
//ADC12IFGR0 = BIT9;
}
ADC采样
void A9_sample_start(void)
{
//while(REFCTL0 & REFGENBUSY); // If ref generator busy, WAIT
//REFCTL0 |= REFVSEL_2 | REFON; // Select internal ref = 2.5V
// Internal Reference ON
// Configure ADC12
//ADC12CTL0 = ADC12ON | ADC12SHT0_2; // Turn on ADC12, set sampling time
//ADC12CTL1 = ADC12SHP;
//ADC12CTL1 = ADC12SHP | ADC12SSEL_3 ; // Use sampling clock SMCLK 8M;
//ADC12CTL3 &= 0xFFE0;
//ADC12CTL3 |= ADC12CSTARTADD_9;
//ADC12MCTL9 &= 0xFFE0;//先清空原来的值
//ADC12MCTL9 |= ADC12INCH_9 | ADC12VRSEL_1;
ADC12CTL0 |= ADC12SC;
while (!(ADC12IFGR0 & BIT9));
__data20_write_short(Cur_Addr+position,ADC12MEM9);
//ADC12CTL0 &=~ (ADC12ENC + ADC12SC);
position+=2;
if(position==0x4000)
position=0;
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>