完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
你好,我想测量某个信号的脉冲宽度/周期。我最初的计划是在上升沿和下降沿上产生一个中断,然后重置/重新初始化某个定时器(time2),以刷新该中断内的时间计数。因此,每次UC检测到下降沿时,定时器初始化,然后当定时器产生中断时,我将增加某个变量。当下一个边沿到来时,计数被停止,保存,然后计时器重新初始化。这是可行的吗?我想象的重新初始化部分就像下面的那个。
以上来自于百度翻译 以下为原文 Hi, I want to measure the pulse width/period of a certain signal. My initial plan is to generate an interrupt on both rising and falling edges then reset/re-initialize a certain timer (timer2) to refresh time count inside that interrupt. So everytime that the uC detects a falling edge, the timer initializes then as the timer generates an interrupt, I'll increment a certain variable. When the next edge comes, the count is stopped, saved and then the timer re-initializes. Is this even feasible? The reinitialization part I imagine is like the one below. void initialize_tmr2(void) { TMR2ON = 1; T2CONbits.T2CKPS = 0b00; T2CONbits.T2OUTPS = 0b0001; PR2 = 249; } void interrupt ISR(void) { if (IOCBF4 == 1) { IOCBF4 = 0; initialize_tmr2(); } |
|
相关推荐
15个回答
|
|
|
|
|
|
|
|
|
|
HI,在许多PIC16和PIC18微控制器中,定时器2是一个具有周期寄存器的8位定时器。将周期寄存器设置为249将提供每隔250次指令的中断。或者与预分频器,每4×250或16×250指令周期……在用4 MHz振荡器运行的PIC16中,将存在。是一个指令周期,每一微秒增加一个定时器2。这可以用于微秒时间测量。我认为每当发生任何事情时,调用一个函数来初始化定时器2是一个坏主意。相反,我建议,启用定时器2的中断,以及中断是从时间开始的。R 2出现,作为软件定时器增加一个变量。对于软件定时器有16位整数变量,4000个计数将产生1秒。为了测量脉冲宽度和/或周期,每当信号中有一个转变时,拾取TMR2寄存器的值和软件定时器变量的值。当下一个定时事件发生时,计算间隔,并将TMR2和软件定时器的当前值存储为下一个定时事件的起始值。许多PIC16和PIC18设备具有定时器1,具有16位计数器和CCP捕获模块,这可能更适合于脉冲宽度或PU。LSE周期测量。迈西尔
以上来自于百度翻译 以下为原文 Hi, In many PIC16 and PIC18 microcontrollers, Timer 2 is a 8 bit timer with Period register. Setting the Period register to 249 will provide a interrupt every 250'th instruction. Or with the prescaler, every 4 * 250 or 16 * 250 instruction cycles ... In a PIC16 running with 4 MHz oscillator, there will be an instruction cycle, and Timer 2 increment, every microsecond. This may be used for microsecond time measurements. I think that calling a function to Initialize Timer 2 every time something happen, is a bad idea. Instead I suggest, to enable Interrupts for Timer 2, and when interrupt is from Timer 2 occur, Increment a variable as a software timer. With a 16 bit integer variable for software timer, 4000 counts will make 1 second. To measure Pulse width, and / or period, whenever there is a transition in the signal, pick up the value of TMR2 register and the value of the software timer variable. When the next timing event happen, calculate the interval, and also store the now current values of TMR2 and software timer as start values for the next timing event. Many PIC16 and PIC18 devices have Timer 1 with 16 bit counter and CCP Capture module, that may be more suitable for pulse width or pulse period measurements. Mysil |
|
|
|
通常我发现使用ECCP模块更容易测量脉宽/周期。
以上来自于百度翻译 以下为原文 Usually I find pulse width/period measurements easier using the ECCP modules. |
|
|
|
Mysil,谢谢你的精彩建议和指导。虽然,我只想深入了解为什么每次中断重置计时器都是一个坏主意,而且,我应该考虑TMR2值吗?由于计时器2从TMR2 Val& & Pr2 Var递增,您能进一步解释为什么我必须挖掘TMR2值吗?谢谢,杰克,是的,先生。这将是更容易的方法。然而,并非所有的微控制器(或我认为)都没有CCP模块。这就是为什么我想做的“计时器方式”。
以上来自于百度翻译 以下为原文 @Mysil, thanks for that wonderful suggestion and guide. Although, I just want to dig deeper about the reason why resetting the timer every interrupt would mean a bad idea. Also, should I consider the TMR2 value? Since Timer 2 increments from TMR2 val --> PR2 val, can you explain to me further why I have to tap the TMR2 value? Thanks @jack, Yes, sir. That would be the easier method. However, not all microcontrollers (or so, I think) have no ccp modules. That's why I want to do it the "timer way". |
|
|
|
重置它(或者甚至只是写一个新的计数),清除预分频器和后级计数器,向计数添加抖动。最好让计时器连续运行,这样就不会有抖动。
以上来自于百度翻译 以下为原文 Resetting it (or even just writing a new count to it), clears the prescaler and postscaler counters, adding jitter to the count. It's much better to let the timer run continuously so there is no jitter. |
|
|
|
|
|
|
|
嗨,我只是想澄清一下我在Microchip网站上找到的应用笔记。AFAIK,只有一个标志控制IOC模块,也就是IOCBFx(X是PORTB PIN的数量)。所以,我想知道的是,如何配置IOC中断来执行步骤5和7?http://ib.c/kvybbsii对不起,我很困惑。
以上来自于百度翻译 以下为原文 Hi, I just want to clarify something on this application note I found in microchip's web. AFAIK, there is only one flag that controls the IOC module and that is IOCBFx (x being the number of PORTB pin). So what I want to know here is how will I configure the IOC interrupt to do steps 5 and 7? https://ibb.co/kvYEBx if (IOCBF4 == 1) { IOCBF4 = 0; temp = W; //W being the timer count //.... now when the next edge occurs, how will I stop the timer and save the timer in the temp 2? } I'm sorry I'm just so confused. |
|
|
|
AppNoT告诉你使用IOCBP和IOCBN。PIC16F1937确实有这些寄存器,所以为什么不能使用它们呢?还有,你有什么“W”?这就是你应该在计时器中读取当前计数的地方。
以上来自于百度翻译 以下为原文 The AppNote tells you to use IOCBP and IOCBN. A PIC16F1937 does have those registers, so why can't you use them? Also, where you have temp = W; //W being the timer count what is "W"? That is where you are supposed to read the current count in the timer. |
|
|
|
是的,我很清楚我必须同时使用它们,IOCBPx和IOCBNX。问题是,当我只能使用一个IOBFX控件时,我将如何分离这两个任务?想想这个场景,我希望我的信号是一个活跃的低信号。所以当IOC标志被设置(下降沿)时,我会清除IF位,然后将计时器保存到某个变量中。这很容易。但是当下一个边沿出现(上升沿)时,IOC标志被设置并再次清除。这次我必须停止计时器,但是我已经设置了我想要在IF(IOCBFX==1)条件下做的事情,这只是为了节省时间。我如何决定设置哪一个边并为它做特定的任务?我不知道我是否正确表达了我的想法。请容忍我。
以上来自于百度翻译 以下为原文 Yup, I'm well aware that I have to use them both, IOCBPx and IOCBNx. Question is how will I separate the two tasks when I can only use one control which is the IOCBFx? Think of this scenario, I expect my signal to be an active low. So when the IOC flag is set (falling edge), I'm gonna clear the IF bit then save the timer into a certain variable. That's easy. But when the next edge comes (rising edge), the IOC flag is set and cleared again. This time I'm gonna have to stop the timer, but I have already set the things I want to do inside the if (IOCBFx == 1) condition which is just to save the time. How am I suppose to determine which edge is set and do the certain task for it? I don't know if I expressed my thought correctly. Please bear with me. |
|
|
|
您一次执行一个操作,即设置IOCBPX位(和清除IOCBNX),并等待中断。当您为中断服务(为上升沿)时,保存到TEMP中,清除IOCBPX,并设置IOCBNX。当您为下一个中断服务(为下降沿)时,保存到TEMP2中,设置IOCBPX,并清除IOCBNX。知道它是上升沿还是下降沿中断?只是测试IOCBPX或IOCBNXI不知道这意味着什么。
以上来自于百度翻译 以下为原文 You do one at a time. i.e. Set the IOCBPx bit (and clear IOCBNx), and wait for an interrupt. When you service the interrupt (for rising edge), save into temp, clear IOCBPx, and set IOCBNx. When you service the next interrupt (for falling edge), save into temp2, set IOCBPx, and clear IOCBNx. How do you know if it is the rising edge or falling edge interrupt? Just test IOCBPx or IOCBNx I have no idea what that is supposed to mean. Explained above. |
|
|
|
我只是在这部分困惑,所以我写了一个伪代码,你能指导我实现这一部分,因为我不知道如何写它。
以上来自于百度翻译 以下为原文 Im just confused in this part so I wrote a pseudocode, can you please guide me into implementing that part as I'm confused how to write it. volatile bit X = 1; volatile bit Y = 0; int main(void) { IOCBPx = X; IOCBNx = Y; //.... next line } void interrupt ISR (void) { if (IOCBFx == 1) { IOCBFx = 0; temp = W; IOCBPx = ~X; IOCBNx = ~Y; temp2 = temp; //(I'm not so sure where to include temp 2 or if my IOCBPx/BNx configuration is right) } if (TMRxIF == 1) { TMRxIF = 0; W++; //increments every ** us/ms } |
|
|
|
只要从我的头中断服务的顶部,这应该为一个8位计数。如果你想使用一个16位计数定时器1,你将不得不停止阅读时,因为这个PIC没有一个锁存16位模式的定时器。
以上来自于百度翻译 以下为原文 Just off the top of my head volatile bit new_value; volatile unsigned char value=0; int main(void) { //init code ... new_value = 0; IOCBP4 = 0; IOCBN4 = 1; //enable interrupt on negative edge only IOCBF4 = 0; //main loop while(1) { ... if (new_value) { //add code here to use "value" variable new_value=0; //ready for next one } } Interrupt service { static unsigned char temp=0, temp2=0; ... if (IOCB4) { IOCB4=0; if (IOCBN4) //negative edge? { IOCBP4=1; //change trigger to positive edge IOCBN4=0; temp = TMR2; } else { //was positive edge IOCBP4=0; IOCBN4=1; //change trigger to negative temp2 = TMR2; value = temp2 - temp; // save for non-interrupt code new_value = 1; //say there is a new value available } } } This should work for an 8 bit count. If you want to use timer 1 for a 16 bit count, you will have to stop it while reading as this PIC does not have a latched 16 bit mode on the timer. |
|
|
|
我从来没有想过这样的工作,我应该花更多的时间编码,你不觉得吗?如果有的话,你有一个好的阅读推荐的新手在嵌入式PROG?非常感谢!
以上来自于百度翻译 以下为原文 I never thought of a workaround just like that, I should spend more time coding don't you think? If ever, do you have a recommendation of good reads for a newbie in embedded prog? Thank you so much! |
|
|
|
嗨,我想你可以通过自己动手做实验来学习最多的东西,并用这种方式来构建经验。看看别人如何解决类似的任务也是有用的。在MPLAB XXY中有一些示例性的项目。本次论坛多次:在论坛上与KATELA成员相关的HTTP://www-GooLuMu.com A/PIC-教程网站:HTTPS://www-SubjuliNo.C.ZA/Microchip网站上有很多示例项目。几乎每一个开发板都有可以下载和使用的示例代码,即使你不购买该板本身。许多应用笔记都有示例项目代码。有几个网站报告实验和项目。也有教科书,那些可以针对特定的MA定制。在这些书中的例子可以指导你使用那些过时的微控制器设备。对于PIC单片机来说,虽然过时的产品通常是可用的,但几乎总是有一个更现代化的设备可以做同样的事情,而且更普遍。大多数PIC12、PIC16和PIC18设备具有相同的引脚数,可以使用相同的开发板。不管怎样,在进行开发或一次性项目时,微控制器通常是最便宜的组件。板,连接器,编程工具,和你的时间可能是更重要的。然后有C编程一般。Brian Kernighan和Dennis Ritchie的教科书:C编程语言,第二版,是一本好书,仍然与声音C编程有关。还有很多其他的。迈西尔
以上来自于百度翻译 以下为原文 Hi, I think you may learn the most by doing experiments yourself, and build experience that way. It is also useful to look up how others may have solved a similar task. There are some example projects builtin in MPLAB X Then there are websites with tutorials, Gooligum tutorials have been suggested in this forum many times: http://www.gooligum.com.au/pic-tutorials Website related to member katela in this forum: https://www.studentcompanion.co.za/ There are lots of example projects in Microchip website. Nearly every development board have example code that may be downloaded and used, even if you do not buy the board itself. Many Application notes have example project code. There are several websites reporting experiments and projects. There are also textbooks, those may be tailored toward specific manufacturer or family of devices. Examples in such books may direct you to use microcontroller devices that are badly out of date. For PIC microcontrollers, although outdated products are usually still available, there are nearly always a more modern device that can do the same, and more, and generally is lower in price. Most PIC12, PIC16 and PIC18 devices with the same number of pins, may use the same development board. Anyway, when doing development, or one-off projects, the microcontroller is usually the least expensive component. Board, connectors, programming tool, and your time may be much more significant. Then there is C programming in general. The textbook by Brian Kernighan and Dennis Ritchie: The C Programming language, Second edition, is a good book, and still relevant for sound C programming. There are many others. Mysil |
|
|
|
只有小组成员才能发言,加入小组>>
5158 浏览 9 评论
1997 浏览 8 评论
1926 浏览 10 评论
请问是否能把一个ADC值转换成两个字节用来设置PWM占空比?
3169 浏览 3 评论
请问电源和晶体值之间有什么关系吗?PIC在正常条件下运行4MHz需要多少电压?
2222 浏览 5 评论
723浏览 1评论
606浏览 1评论
有偿咨询,关于MPLAB X IPE烧录PIC32MX所遇到的问题
494浏览 1评论
PIC Kit3出现目标设备ID(00000000)与预期的设备ID(02c20000)不匹配。是什么原因
620浏览 0评论
519浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-19 15:21 , Processed in 1.458728 second(s), Total 108, Slave 90 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号