-1

I familiarising myself with MPLAB X IDE and staring development with C using a Microchip PIC 12F617.

I have a simple program (copied from elsewhere) to flash a LED: on for 1 second then off for 1 second. It all works but I my calculation of the delay does not match 1 second. Anybody able to tell me where I am going wrong?

Internal osc is selected @ 8 MHz

clock to TMR2 is FOSC / 4 is 2 MHz

PR2 default to 255 so interrupt at 2 MHz / 255 is 7843 Hz

TMR2 prescale = 1; postscale set to 16; is 490 Hz

Loop counts 675 is 0.726 Hz so LED should be on/off for 1.38 seconds, yet it isn't

Taking into account time for executing instructions the led on/off should be even longer.

what am I missing?

/*
 * 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;
    }
}
Dan1138
  • 1,150
  • 8
  • 11

1 Answers1

3

Your main bug is the watchdog timer is on and your code does not reset it.

Dan1138
  • 1,150
  • 8
  • 11