简体   繁体   English

如何在PIC​​18上使能定时器2?

[英]How to enable Timer 2 on PIC18?

I want to enable timer2 to be able to use pulse width modulation. 我想使Timer2能够使用脉冲宽度调制。 I am using PIC18F87J11 with internal 8MHz oscillator. 我正在使用带有内部8MHz振荡器的PIC18F87J11 I have minimum knowledge about PIC programming, so I want to know what else is missing in my code to enable the timer. 我对PIC编程知之甚少,所以我想知道在启用计时器的代码中还缺少什么。

#define TMR2_IF          PIR1bits.TMR2IF 
#define TMR2_IE          PIE1bits.TMR2IE
#define TMR2_IP          IPR1bits.TMR2IP
#define TMR2_CON         T2CON

Let's say this is my main code for now 假设这是我目前的主要代码

void main()
{
     TMR2_CON = 0b00000100 | CLOCK_DIVIDER_SETTING; // CLOCK_DIVIDER_SETTING = 4

     TMR2_IF = 0;
     TMR2_IE = 1;
     TMR2_IP = 1;

 while(1);

}

The rest of the code that I don't yet know where to place it. 我尚不知道将其余代码放在哪里。

if(TMR2_IF)
    {

        printf("\r\nHello");

        if(TMR2_IE)

         {
            TMR2_IF = 0;
         }

   }

Do I need an Interrupt Service Routine? 我需要中断服务程序吗? If Yes, how do I add it? 如果是,该如何添加? Remember my goal is to use the PWM which I still did not get there yet, but for now I want to make sure the settings for timer2 are correct. 请记住,我的目标是使用仍未到达那里的PWM,但是现在我要确保timer2的设置正确。

What else is missing? 还缺少什么?

Thanks in advance! 提前致谢!

change the while(1); 更改while(1);

to

while(1)
{

if(TMR2_IF)
    {

        printf("\r\nHello");

        if(TMR2_IE)

         {
            TMR2_IF = 0;
         }

   }
}

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

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