New to PIC programming

Trying to Simulate a PIC16F627A TIMER 1 using MPLAB X IDE where RB7/T1OSI increments, and it toggles RB4 through TMR1 ISR.

#include <stdio.h>

#include <xc.h

#pragma config WDTE = 0;

//period 1 ms or every 100 T1CKI
//external from T1CKI rising edge (1 sec Period) T1OSCEN = 0; //

void interrupt Pulse_ISR (void)
    PORTBbits.RB4 = !PORTBbits.RB4;
    PIR1bits.TMR1IF = 0;
    TMR1L = 0x96;
    TMR1H = 0xFF;    

void Init (void)
    //Global Interrupt
    //Peripheral Interrupt
   //1:8 prescalar
   //t = 1us*8*N    N = 100 
   //TMR1 = (2^16+1) - 100 = 65436 FF96

   TRISB4 = 0;//RB4 is output
   TRISB6 = 1;//RB6 and RB7 are input
   TRISB7 = 1;

  INTCONbits.GIE = 1;
  INTCONbits.PEIE = 1;
  PIE1bits.TMR1IE = 1;

  T1CONbits.T1CKPS = 0b11;//1:8
  T1CONbits.T1OSCEN = 1;//TMR1 increments on T1OSI RB7
  T1CONbits.TMR1CS = 1;

  TMR1L = 0x96;
  TMR1H = 0xFF;

  PORTBbits.RB4 = 1;

  T1CONbits.TMR1ON = 1;    

void main (void)
    Init ();

    //I will simulate a 1 ms pulse 50% Duty Cycle at T1CKI/RB6


I created a stimuli where I created a pulse on pin RB7 with period of 500us

enter image description here

and I watch the result throught IO pins

enter image description here

I build the project and Debug it, watch RB4 IO Pins and it never toggles.

Can someone tell me why? Thank you.

MPLAB X IDE v3.55 PIC16F627A Debugging Tool: Simulator

  • \$\begingroup\$ If you add a debug breakpoint in the ISR, does it ever execute that code? \$\endgroup\$ – Roger Rowland Apr 20 '17 at 7:19
  • \$\begingroup\$ You might also need to set LVP off, otherwise RB4 will be used as PGM, and won't work as a digital output. \$\endgroup\$ – Roger Rowland Apr 20 '17 at 7:29
  • \$\begingroup\$ @RogerRowland No it doesn't \$\endgroup\$ – Forat Apr 20 '17 at 11:38
  • \$\begingroup\$ @RogerRowland, I don't think that's the case, because before I got this issue, I did the same thing with RB6/T1CKI... (same code with the only difference T1OSCEN = 1;//to enable T1OSO), and it worked. \$\endgroup\$ – Forat Apr 20 '17 at 11:39
  • \$\begingroup\$ @RogerRowland, I read something at the PIC16F627A Manual, that I think might be related. Page 53 of the Manual "A crystal oscillator circuit is built in between pins T1OSI (input) and T1OSO (amplifier output). It is enabled by setting control bit T1OSCEN (T1CON<3>). It will continue to run during Sleep. It is primarily intended for a 32.768 kHz watch crystal. Table 7-1 shows the capacitor selection for the Timer1 oscillator. The user must provide a software time delay to ensure proper oscillator start-up." This I don't understand what it means? \$\endgroup\$ – Forat Apr 20 '17 at 12:21

From page 170 of the MPLAB® IDE User’s Guide about the simulation capabilities on 14-bit core PICs:

Timer1 in its various modes is supported, except when running in counter mode by an external crystal.

  • \$\begingroup\$ I tried searching it on MPLAB X IDE couldn't find it, but maybe that makes sense of why it wasn't working on simulation. \$\endgroup\$ – Forat Apr 22 '17 at 14:45
  • \$\begingroup\$ I tried your code and it didn't work for me either but it somehow worked if I injected the clock on the T1OSOpin. Since, if you're using a crystal, you'd have both pins connected to it you may try simulating your program with that workaround. \$\endgroup\$ – zakkos Apr 22 '17 at 15:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.