完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
电子发烧友论坛|
大家好,我目前正在使用PIC16F1938,我正在寻找一种方法来测量脉冲从低到高或高到低的时间。我在使用:-8MHz时钟源-PIC16F1938 -中断在寻找低到高和高到低的边缘- RA5-time0在1MSE工作。问题是,我找不到或想一个好的算法来测量从那个管脚输入的每一个时间。你知道测量微控制器中输入的时间的正确方法吗?我所想的是对RA5状态的每一个变化进行中断处理,然后,如果计时器达到1ms,检查RA5中的IOC-FLAG(当PIN状态发生变化时,它将建议),并保存该事件的时间或持续时间。口粮的特定时间,看看是否有足够的龙做其他事情:让我分享你的一段代码,让你看看我实际上:
以上来自于百度翻译 以下为原文 Hello People, I'm currently working with the PIC16F1938 and I'm looking for a way to measure the time that a pulse go from low to high or high to low. I'm using:- 8MHz clock source- PIC16F1938- Interrupt-on-Change to look for the both low to high and high to low edges - RA5- timer0 working at 1ms The problem is that I can't find or think a good algorithm to measure the time that last every input from that pin. Did you know a proper way to measure the time that last an input in the microcontroller? What I was thinking is to work with Interrupts for every change on the RA5 state, then, if the timer gets 1ms elapsed, check for the IOC-FLAG in RA5 (which will advice when there is a change in the state of the pin) and save the time or duration of that event. What I need is to compare later this durations for a specific time to see if is loong enough to do something else: Let me share you a piece of the code to show you what I have actually: void processor_init(void) { OSCCONbits.IRCF = 0b1110; // 8MHz Clock Source selected OSCCONbits.SCS = 0b00; // Internal Oscillator selected ANSELA = 0x00; // Setting all PORTA as Digital TRISAbits.TRISA5 = ON; // PIR_INPUT pin as Input LATAbits.LATA4 = OFF; // Initially Off state in DETECTION_OUTPUT } void IOC_init(void) { INTCONbits.GIE = 1; // Enable Global Interrupts INTCONbits.IOCIF = 0; // Clean interrupt flags INTCONbits.IOCIE = 1; // Enable IOC Interrupts IOCBPbits.IOCBP5 = 1; // Enable IOC RA5 Positive Edge (PIR_INPUT) IOCBNbits.IOCBN5 = 1; // Enable IOC RA5 Negative Edge (PIR_INPUT) IOCBFbits.IOCBF5 = 0; // Clean IOC Interrupt flag } void timer0_init(void) { // TMR0 Initialization OPTION_REGbits.TMR0CS = 0; // As timer Fosc/4 OPTION_REGbits.TMR0SE = 0; // Increment on low-to-high transition. No relevance. OPTION_REGbits.PSA = 0; // Prescaler is assigned to TMR0. OPTION_REGbits.PS = 0b111; // 256 Prescaler selected // TMR0 overflow = 5ms //TMR0 = 217; // 5ms overflow TMR0 = 247; // 1 ms overflow = 247 } void interrupt IOC() { if(TMR0 == 255) // if TMR0 has reached 1 ms already { if(IOCBFbits.IOCBF5) { IN_STATE = TMR0; // This is where I need to improve better my code TMR0 = 247; // Restore the timer IOCBFbits.IOCBF5 = 0; // Clean IOC flag } } else { if(IOCBFbits.IOCBF5) { // I'm almost sure that I need to do something here, when is not elapsed 1ms yet IOCBFbits.IOCBF5 = 0; } } } void main(void) { processor_init(); IOC_init(); timer0_init(); while(1); } |
|
相关推荐
8个回答
|
|
|
这是错误的
以上来自于百度翻译 以下为原文 This is wrong IOC_init(void); IOC_init(); but possibly not your issue [:)] |
|
|
|
|
|
对不起,这个错误,是一个语法错误,我已经编辑过了。
以上来自于百度翻译 以下为原文 Sorry for the mistake, was a syntax error, I editated already. |
|
|
|
|
|
你需要的是CCP模块。你的照片很少。如果将CCP模块置于“捕获”模式,则记录在PIN上发生转换的时间。你需要做的是得到两个读数和减法。
以上来自于百度翻译 以下为原文 What you need is CCP module. Your PIC has few of them. If the CCP module is put into "capture" mode, it records the time where there was a transition on the pin. All you need to do is to get two readings and subtract. |
|
|
|
|
|
…如果发生的话,检测定时器是否在转换之间滚动超过一次。
以上来自于百度翻译 以下为原文 ... and detect if the timer rolls over more than once in between the transitions if that could happen. |
|
|
|
|
|
没有共产党,还有别的办法吗?问题是我打算在PIC12LF1552中重写这个代码。这一个NLT有一个CCP模块。
以上来自于百度翻译 以下为原文 Is there another way to do it without CCP? Thing is that I'm planning to rewrite this code in a PIC12LF1552. This one does nlt have a CCP module. |
|
|
|
|
|
取决于你需要的速度和精度。如果你不需要高速(比如说边之间的距离是30个周期或更多),并且可以容忍时间测量中的错误(比如10-15个周期),你可以轮询或使用汇编程序写的IOC/INT中断。如果你的速度还低,你可以忍受。更大的误差,你可以通过简单地计算插脚的次数来从定时器中断来完成。一旦一次C程序,过200次循环或慢可能会出错。
以上来自于百度翻译 以下为原文 Depends on the speed and precision you need. If you don't need high speed (say the distance between edges is 30 cycles or more) and can tolerate errors in time measurements (say 10-15 cycles), you can poll or use IOC/INT interrupts written in assembler. If your speed is yet lower and you can tolerate much bigger errors, you can do it from timer interrupt by simply counting the number of times the pin was up. Once per 200 cycles or slower may be reasonable for a C program. |
|
|
|
|
|
速度是慢的,考虑到:-Test= 4 /FoSC=4/8MHZ=500 MSON(200个周期*STE= 0.0001=100US),这是我在计时器中需要的大约1ms(从TMR0=247到TMR0=255),时间将从000111到000999,并且是可以接受的。如果我的计算没有错。对不起,如果我犯了一个错误。我要做的是使用IOC中断,检查状态的变化,并保存输入“发生”的当前时间。
以上来自于百度翻译 以下为原文 The speed is slower, considering: -Tcy = 4/Fosc = 4/8MHz = 500ms And (200 cycles * tcy = 0.0001 = 100us) is what I approximately need in the timer to count and reach 1ms (from TMR0 = 247 ---- to TMR0 = 255) The time would be going from .000111 to .000999 and is acceptable. If I'm not wrong with the calculations. Sorry if I made a mistake. What I would do is to use the IOC Interrupt and check for change of states and save the current time that the input "happened". if(IOCBFbits.IOCBF5) { if(TMR0 == 255) // Polling if 1ms has reached { LATBbits.LATB3 = 0; // just to check that I reach 1ms in this condition save_time = TMR0; // save this time TMR0 = 247; // restore the timer: 1ms } else { LATBbits.LATB3 = 1; // another check save_time = TMR0; // save the timer value if is detected a pulse, the timer has not reached 1 ms } IOCBFbits.IOCBF5 = 0; // Restore the IOC Flag } |
|
|
|
|
|
如果你想使用IOC中断,那么你只需记录中断的时间(从TMR读取它),然后减去两个边的时间。为此,最好使定时器自由运行(即避免改变TMR)。定时器应该足够慢,以确保定时器周期大于最大脉冲长度。否则,您将需要处理计时器中断,并计数计时器溢出。
以上来自于百度翻译 以下为原文 If you want to use IOC interrupts then you just record the time of the interrupt (read it from TMR) and then you subtract the times of two edges. For that, it is better to make the timer free-running (that is avoid altering TMR). Timer should be slow enough to ensure that timer period is greater than the maximum pulse length. Otherwise, you will need to process the timer interrupt too and count timer overflows. |
|
|
|
|
只有小组成员才能发言,加入小组>>
MPLAB X IDE V6.25版本怎么对bootloader和应用程序进行烧录
501 浏览 0 评论
5808 浏览 9 评论
2350 浏览 8 评论
2237 浏览 10 评论
请问是否能把一个ADC值转换成两个字节用来设置PWM占空比?
3543 浏览 3 评论
1155浏览 1评论
有偿咨询,关于MPLAB X IPE烧录PIC32MX所遇到的问题
1119浏览 1评论
我是Microchip 的代理商,有PIC16F1829T-I/SS 技术问题可以咨询我,微信:A-chip-Ti
887浏览 1评论
MPLAB X IDE V6.25版本怎么对bootloader和应用程序进行烧录
501浏览 0评论
/9
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2025-12-12 05:53 , Processed in 0.775622 second(s), Total 88, Slave 71 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191

淘帖
1390