Microchip
直播中

朱梁贡

7年用户 207经验值
私信 关注
[问答]

PIC12F675 Timer1问题

我配置timer1以允许我的代码每20mS运行一次有困难……我的主程序执行它的事情,然后检查PIR1、TMR1IF,等待Timer1到期……;按照Timer1所确定的,在20mS时间范围内循环;通过打开main_.bsf GPIO、PWM_BIT上的PWM输出开始;输出在bcf PIR1,TMR1IF;清除定时器1溢出标志bsf T1CON,TMR1ON;定时器使能调用a_子例程调用另一个调用和_a_.;等待20msframewait_20m***tfss PIR1,TMR1IF的结束;等待定时器1溢出标志goto wait_20msgoto main_loopI正在使用PICs内部振荡器,因此我想我可以设置定时器配置错误...;设置定时器1控制寄存器movlw b'00001000';u0xxxxxx:TMR1GE OFF: ux00xxxx:Prescaler 1:1;uxxx1xx:Enable LP振荡器;uxxxxxxxx1xx:Do Not Synchronise to External Clock;uxxxxxxxxxx0x:设置内部时钟(Fosc/4);uxxxxxxxxxxx0:Do No No Not启用Timer1 movwf T1CON;写到T1CON;设置Timer1 Timing Registers movlw 0x17;将定时器设置为20ms movwf TMR1H movlw 0xe4 movwf TMR1LI尚未确定TMR1H和TMR1L值应该是什么,虽然我只是将其连接到频率计上使其点亮,但我所能得到的是15Hz,即使我改变了这些价值,我也必须做一些愚蠢的事情。任何想法!

以上来自于百度翻译


      以下为原文

    I'm having trouble configuring Timer1 to allow my code to run every 20mS...
My main program does its thing then inspects PIR1,TMR1IF to wait for Timer1 to expire.....

; This loops at a 20mS time-frame, as determined by Timer1
; Start by turning the PWM output on
            
main_loop   
    bsf    GPIO,PWM_BIT        ;Output on
    bcf    PIR1,TMR1IF        ;Clear timer 1 overflow flag
    bsf    T1CON,TMR1ON        ;Timer enable

    call    a_subroutine
    call    another
    call    and_a_third

; Wait for end of 20ms frame

wait_20ms   
    btfss    PIR1,TMR1IF        ;Wait timer 1 overflow flag
    goto    wait_20ms

    goto    main_loop

I'm using the PICs internal oscillator, so I think I may have the Timer configuration wrong....

;Set Timer 1 Control Register

    movlw    b'00001000'        ;u0xxxxxx : TMR1GE OFF :
                                           ;ux00xxxx : Prescaler 1:1
                                           ;uxxx1xxx : Enable LP Oscillator
                                           ;uxxxx1xx : Do Not Synchronise to External Clock
                                           ;uxxxxx0x : Set internal clock (Fosc/4)
                                           ;uxxxxxx0 : Do Not Enable Timer1
    movwf    T1CON                ;write to T1CON

;Set Timer 1 Timing Registers

    movlw    0x17                   ;Set timer to 20ms
    movwf    TMR1H
    movlw    0xe4
    movwf    TMR1L


I have yet to determine what TMR1H and TMR1L values should be, thought I'd just hook it up to a frequency meter to get it spot-on, but all I can get is 15Hz, even when I change those values...

I must be doing something daft.... any ideas !

回帖(5)

tijing忽忽

2019-7-2 09:46:19
你用什么作为时钟源?您没有显示配置。你有一个LP外部振荡器选择为Time1。你的三个电话需要多长时间才能执行?这也将限制环路速度。

以上来自于百度翻译


      以下为原文

    What are you using as a clock source? You do not show your configuration. You have the LP external oscillator selected for TImer1. Also how long do your three calls require to execute? That will limit the loop speed also.
举报

庞哲

2019-7-2 09:51:55
我确实说过……“我用的是PICs内部振荡器,所以我认为我的定时器配置错了……”u CON._CP_OFF&_CPD_OFF&_WDT_OFF&_BODEN_OFF&_INTRC_OSC_NOCLKOUT&_MCLRE_OFFI,我想我可能对振荡器的选择和时钟源感到困惑…如果TMR1CS=0(Internal Osc,Fosc/4),那么我认为我不需要LP振荡器,至少这是Timer 1Block Diagram所建议的……我会关掉它,看看它是否有任何不同。子例程并不支持它,事实上我已经让他们尝试让这个20mS循环协同工作直截了当地

以上来自于百度翻译


      以下为原文

    I did say....
"I'm using the PICs internal oscillator, so I think I may have the Timer configuration wrong...."
 
__CONFIG    _CP_OFF & _CPD_OFF & _WDT_OFF & _BODEN_OFF & _INTRC_OSC_NOCLKOUT & _MCLRE_OFF
 
I think I may be being confused about the oscillator selection and the clock source.... If TMR1CS = 0 (Internal Osc, Fosc/4), then I don't think I need the LP oscillator, at least that's what the Timer 1 Block Diagram suggests....
 
I'll turn it off and see if it makes any difference.
 
The subroutines don't hold it up, in fact I've REM'd them out to try to get this 20mS loop functioning correctly.
举报

郑雅颖

2019-7-2 10:06:37
每次通过主循环,在第一次之后,我都看不出你在哪里加载了定时器1寄存器,所以它在前一次溢出之后溢出65536计数。对于指令时钟=4MHz/4=1MHz,我得到:1MHz/65536=(大约)15.26Hz

以上来自于百度翻译


      以下为原文

    Hmmm...
 
Each time through your main loop, after the first time, I don't see where you load the Timer 1 registers again, so it overflows 65536 counts after the previous overflow.
 
With instruction clock = 4 MHz / 4 = 1 MHz, I get:
    1 MHz / 65536 = (approximately) 15.26 Hz
 
Regards,

Dave
举报

萧治维

2019-7-2 10:13:54
啊,我从来没有意识到H寄存器会被计时器损坏。我也不知道定时器寄存器“时钟上升”到65535溢出。我已经将它们的预置移动到主回路,并计算出16位的预置为4MHz/4=1MHz100000(Hz)/50(Hz)=2000065536-20000=45536=B1E0H我的频率现在是49.3Hz-接近50Hz I nEDE,只是需要调整一下。谢谢你的洞察力…

以上来自于百度翻译


      以下为原文

    Ahhh - I never realised the H an L registers would be destroyed by the Timer working. Neither was I aware that the timer registers "clock up" to overflow at 65535.
 
I have moved the presetting of them to the main loop, and calculated the 16-bit preset as
 
4MHz / 4 = 1 MHz
100000 (Hz)/ 50 (Hz) = 20000
65536 - 20000 = 45536 = B1E0H
 
My frequency is now 49.3 Hz - getting close to the 50 Hz I need, just needs a tweak.
 
Thanks for the insight ...
举报

更多回帖

发帖
×
20
完善资料,
赚取积分