我使用一个标准的定时器模块采取了一些在设定的时间间隔的交流波形样本;我基本上是做一个RMS电压测量,所以需要能够确保我取32个样本在输入的交流信号的半个周期,利用定时器,设定采样周期。根据输入交流信号的频率(或50Hz或60Hz),我需要改变我的计时器- 50Hz工频周期的缓冲值需要的样品应稍远时相比,60Hz的周期。
在我的代码中,首先上电,我计算输入的交流信号的频率,然后我写了一个适当的值(freqval)进入我的定时器使用命令
timer_writeperiod时期缓冲(freqval);这似乎正确的工作。
然后,当我要开始我的定时器,我使用的命令timer_start();全是工作…我这样想。
这个问题似乎是,而不是使用价值freqval我写进期缓冲之前,启动定时器使用timer_start()命令出现抢我设置组件的周期缓冲区的默认值,有效地忽略了以前的提姆ErthRead时期(FRQVALL)命令。
在阅读这一部分的数据表,这实际上是做了应该做的:
tcpwm_start虚空(void)描述:初始化默认值时,称为定制tcpwm第一次使tcpwm。随后调用配置保持不变,成分简单地启用
但我不想这样做。我想用我写的freqval值在早期对我的代码每次我启动它(我做每10秒左右,在操作过程中)。但因为我开始计时的第一次后,我已经写了freqval更新后的值到定时器周期,它只是默认设定值和我的RMS计算都是错的!
我也试着改变所有引用timer_start()到timer_enable();看来这一命令不使用默认的周期值从定制。然而我的代码开始表现得有点不规律基本上与看门狗定时器超时,所以我怀疑我从来没有真正触发定时器中断。我没有真正深入研究这一点。
我已经成功地能够正常工作的唯一方法,就是如果我打电话给timer_start()第一,然后立即打电话给timer_stop(),使初始设置使用默认值完成。然后我打电话给timer_writeperiod(freqval)设置定时器周期,以及所有后续调用将确保我timer_start()定时器运行使用freqval价值的时期。
这似乎有点凌乱的我,所以我想初始化一个计时器的正确方式是什么,所以,我写任何值期缓冲区将以确定其时代价值。
干杯,迈克
以上来自于百度翻译
以下为原文
I am using a standard TIMER block to take a number of samples of an AC waveform at set time intervals; I'm basically doing an RMS voltage measurement, so need to be able to ensure I take 32 samples over one half cycle of the incoming AC signal, and the TIMER is used to set the sample period. Depending upon the frequency of the input AC signal (either 50Hz or 60Hz), I need to change the value of the Period buffer in my timer - a 50Hz cycle requires the samples to be taken slightly further apart when compared to a 60Hz cycle.
In my code, at first power on, I calculate the frequency of the incoming AC signal, then I write an appropriate value (FreqVal) into the Period buffer of my timer using the command Timer_WritePeriod(FreqVal); This appears to work correctly.
Then, when I need to start my timer, I use the command
Timer_Start(); That all seemed to work...or so I thought.
The issue with this, it seems, is that rather than use the value of FreqVal that I wrote into the Period buffer earlier, starting the Timer using the Timer_Start() command appears to grab the default value for the Period buffer from the component I have set up, effectively ignoring the previous
Timer_WritePeriod(FreqVal) command.
In reading the datasheet for this component, that is actually what it is supposed to do:
void TCPWM_Start(void)
Description: Initializes the TCPWM with default customizer values when called the first time and enables the TCPWM. For subsequent calls the configuration is left unchanged and the component is simply enabled
But I don't want it to do that. I want it to use the FreqVal value I wrote in their earlier on in my code each time I start it (which I do about every 10 seconds during operation). But because I start the Timer for the first time after I have written the updated value of FreqVal into the Timer Period, it just defaults to the preset value and my RMS calcs are all wrong!
I did try changing all references to
Timer_Start() to
Timer_Enable(); as it appears this latter command doesn't use the default Period value from the customizer. However my code started to behave a bit erratically - basically timed out with the Watchdog timer, so I suspect my Timer was never actually triggering an interrupt. I didn't really look into this much further.
The only way I have been successfully able to get things to work correctly, is if I make a call to
Timer_Start() first, then immediately make a call to
Timer_Stop(), so that the initial setting up using the default value is completed. Then I make a call to
Timer_WritePeriod(FreqVal) to set the Timer Period, and all subsequent
Timer_Start() calls will ensure my timer runs using the value of FreqVal for the period.
This seems a bit messy to me, so I was wonder what the correct way to initialise a Timer is, so that whatever value I write into the Period buffer will be the value it uses to set its period.
Cheers,
Mike