Microchip PIC 12F617;使用TMR2计算延迟

问题描述 投票:0回答:1

我熟悉MPLAB X IDE,并开始使用Microchip PIC 12F617进行C语言开发。

我有一个简单的程序(从其他地方复制)来使LED闪烁:先亮1秒钟,然后熄灭1秒钟。一切正常,但是我计算的延迟不匹配1秒。谁能告诉我我要去哪里错了?

选择内部振荡器@ 8 MHz

到TMR2的时钟是FOSC / 4是2 MHz

PR2默认为255,因此2 MHz / 255处的中断为7843 Hz

TMR2预分频比= 1;后缩放比例设置为16;是490 Hz

循环计数675为0.726 Hz,因此LED应该开/关1.38秒,但不是

考虑到执行指令的时间,LED开/关应该更长。

我想念什么?

/*
 * File:   main.c
 * Author: user2439830
 * Target: PIC12F617
 * Compiler: XC8 v2.05
 * 
 *             PIC12F617
 *          +------v------+
 *    5v0 ->:1 VDD   VSS 8:<- GND
 *        <>:2 GP5   GP0 7:<> PGD
 *        <>:3 GP4   GP1 6:<> PGC
 *    VPP ->:4 GP3   GP2 5:<> 
 *          +-------------+
 *               DIP-8
 *
 * Created on May 20, 2020, 5:51 PM
 */
#pragma config FOSC = INTOSCIO  // Oscillator Selection bits (INTOSCIO oscillator: I/O function on RA4/AN3/T1G/OSC2/CLKOUT, I/O function on RA5/T1CKI/OSC1/CLKIN)
#pragma config WDTE = ON        // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = ON       // MCLR Pin Function Select bit (MCLR pin is MCLR function and weak internal pull-up is enabled)
#pragma config CP = OFF         // Code Protection bit (Program memory is not code protected)
#pragma config IOSCFS = 8MHZ    // Internal Oscillator Frequency Select (8 MHz)
#pragma config BOREN = ON       // Brown-out Reset Selection bits (BOR enabled)
#pragma config WRT = OFF        // Flash Program Memory Self Write Enable bits (Write protection off)

#include <xc.h>

void t2delay( void );

void main(void) 
{
    // Note: TRISIO = TRISA; IO direction register
    TRISIO = 0;                 // 0 set corresponding pin in GPIO to output

    //        76543210
    T2CON = 0b01111000;         // postscale=16, prescale=1, timer off

    T2CON |= ( 1 << 2 );        //timer2 on TMR2ON set to 1

    while (1)
    {
        // Note: GPIO = PORTA
        GPIO = 255;
        t2delay();

        GPIO = 0;
        t2delay();
    }
    return;
}

// TMR2IF set to when when TMR2 == PR2 (set to 255 on reset)
void t2delay( void )
{
    unsigned int i;
    for( i = 0; i < 675; i++)
    {
        while( !TMR2IF );

        TMR2IF = 0;
    }
}
c pic microchip
1个回答
2
投票

您的主要错误是看门狗计时器已打开,并且您的代码未将其重置。

© www.soinside.com 2019 - 2024. All rights reserved.