简体   繁体   English

PIC16F883定时器不起作用

[英]PIC16F883 timer won't work

I am currently working on programming a PIC16F883 with a 3.2768 MHz oscilator. 我目前正在使用3.2768 MHz振荡器编程PIC16F883 I need to make some LED blink at the right time, but that is really not relevant here. 我需要在正确的时间点亮一些LED ,但这在这里确实无关紧要。

The problem is that have set up Timer0, but it isn't working. 问题是已经设置了Timer0,但它无法正常工作。 I am going to post my code and initialization here so you can see :). 我将在这里发布我的代码和初始化,以便你可以看到:)。 By the way I am programming in MpLap IDE, in normal C with the Hi-Tech C Compiler. 顺便说一下,我在MpLap IDE中进行编程,使用Hi-Tech C编译器进行正常的C编程。

Initialization: 初始化:

T0CS = 0x00;            //Set Timer0 to Timer-Mode
GIE = 0x01;             //Enable all interrupts
PSA = 0x00;             //Prescaler enable
PS0 = 0x01;             //Prescaler set
PS1 = 0x00;             //Prescaler set
PS2 = 0x01;             //Prescaler set

The interrupt service routine itself: 中断服务程序本身:

void interrupt timer()
{
    T0IF = 0x00;             //Reset timer
    millicounter++;          //Add one to the helper variable
    PORTA = 0x00;

    if (millicounter == 25)  //Check if one second has passed.
    {
        millicounter = 0;    //Reset helper variable
        seconds++;           //Add one to elapsed seconds.
    }
}

The problem is that it doesn't look like the timer is running. 问题是它看起来不像计时器正在运行。 I have now simulated the program various times with different settings, the latest to make a pin open when the interrupt is being run, and then turned on again in the main. 我现在已经使用不同的设置模拟了程序,最新的是在中断运行时使引脚打开,然后在主中再次打开。 The problem was it never happend. 问题是它永远不会发生。 The timer isn't running I think. 我认为计时器没有运行。 Why? 为什么?

You have set the Global Interrupt Enable bit. 您已设置全局中断使能位。 But for the timer interrupt to work, you need to set the timer interrupt enable bit(T0IE) too. 但是要使定时器中断起作用,还需要设置定时器中断使能位(T0IE)。

As per your timer register values and crystal frequency, the "seconds" variable will be incremented 256 times per second. 根据您的定时器寄存器值和晶体频率,“秒”变量将每秒增加256次。 ie, If you are using this "seconds" variable to provide blinking delay, your LED on time will be 3.9 milliseconds approximately. 即,如果您使用此“秒”变量来提供闪烁延迟,则您的LED开启时间约为3.9毫秒。 An human eyes cannot detect this fast blinking. 人眼无法检测到这种快速闪烁。

thank you for your help, i got the timer working. 谢谢你的帮助,我让计时器正常工作。 I deleted my whole config and rewrited the timer, and now it's working fine. 我删除了我的整个配置并重新计算了计时器,现在它工作正常。 I do have another problem that i have written a new post for :) Check it out if you want. 我确实有另一个问题,我已经写了一篇新文章:)如果你想要,请查看它。

New Post 最新帖子

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM