Pic16F18855 Timer 1 Not Working.

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
Ditch
Posts: 71
Joined: 23 Jan 2017 23:14

Pic16F18855 Timer 1 Not Working.

#1 Post by Ditch » 22 Jul 2023 00:42

Hi All,

I've spent hours on this but i can not get the timer 1 interrupt working using the Pic16F18855 and Pro for Pic.

I've tried the example given by the Timer Calculator (V4.0.0) and looked through the datasheet but i'm obviously missing something. I have proven that test_led does light by using the dip switches.

I've added my code below and would appreciate any help with this.

Ian


Code: Select all



sbit test_led at RB4_bit;

//mode
sbit mode_dip_sw_1 at RA0_bit;
sbit mode_dip_sw_2 at RA0_bit;
//sbit mode_dip_sw_3 at RE3_bit;
sbit mode_dip_sw_4 at RB5_bit;


//ON period
sbit on_dip_sw_1 at RB3_bit;
sbit on_dip_sw_2 at RB2_bit;
sbit on_dip_sw_3 at RB1_bit;
sbit on_dip_sw_4 at RB0_bit;
sbit on_dip_sw_5 at RC7_bit;
sbit on_dip_sw_6 at RC6_bit;
sbit on_dip_sw_7 at RC5_bit;
sbit on_dip_sw_8 at RC4_bit;


//OFFperiod
sbit off_dip_sw_1 at RA2_bit;
sbit off_dip_sw_2 at RA3_bit;
sbit off_dip_sw_3 at RA4_bit;
sbit off_dip_sw_4 at RA5_bit;
sbit off_dip_sw_5 at RA7_bit;
sbit off_dip_sw_6 at RA6_bit;
sbit off_dip_sw_7 at RC2_bit;
sbit off_dip_sw_8 at RC3_bit;

//required varibles
unsigned int

             cnt = 0;
             
//dip switches
unsigned int 

             mode,
             on_period,
             off_period;



void interrupt() {
  if (TMR1IF_bit) {

    TMR1IF_bit = 0;    // clear TMR0IF
    TMR1H = 0x80;
    TMR1L = 0x00;
    
    cnt++;             // increment cnt
    if(cnt >= 1000){
    
      test_led = 1;
      cnt = 0;
    }
  }
}

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

void main() {

    OSCCON1    = 0x92; // 16MHz internal oscillator

    TRISA      = 0xFF; // PORTA Directions
    ANSELA     = 0x00; // PORTA all digital pins
    PORTA      = 0;    // clear PORTA

    TRISB      = 0x2F; // PORTB Directions
    ANSELB     = 0x00; // PORTB all digital pins
    PORTB      = 0;    // clear PORTA

    TRISC      = 0xFF; // PORTC Directions
    ANSELC     = 0x00; // PORTC all outputs low
    PORTC      = 0x00; // clear PORTC
    
    //Disable Comparators
    CM1CON0 = CM2CON0 = 0x00;

    //configure timer 1
    T1CON      = 0x01; // Timer1 settings
    TMR1IF_bit = 0;    // Clear the Timer 1 interrupt flag
    TMR1H      = 0xF0; // Initialize Timer1 register
    TMR1L      = 0x60;
    TMR1IE_bit = 1;    // Enable Timer 1 interrupt
    cnt        = 0;    // initialize cnt
    INTCON     = 0xC0; // Set GIE, PEIE
    
    //set weak pullups
    WPUA = 0b11111111;
    WPUB = 0b00101111;
    WPUC = 0b11111100;
    
   //////////////////////////////////////////////////////////////////////////////////////////////////////////////
   
    //main loop
    while(1) {

        
        //read the mode dipswitches
        mode = 0;
        if (!mode_dip_sw_1) { mode = mode + 1;}
        if (!mode_dip_sw_2) { mode = mode + 2;}
        if (!mode_dip_sw_4) { mode = mode + 4;}
        
        //read the ON period dipswitches
        on_period = 0;
        if (!on_dip_sw_1) { on_period = on_period + 1;}
        if (!on_dip_sw_2) { on_period = on_period + 2;}
        if (!on_dip_sw_3) { on_period = on_period + 4;}
        if (!on_dip_sw_4) { on_period = on_period + 8;}
        if (!on_dip_sw_5) { on_period = on_period + 16;}
        if (!on_dip_sw_6) { on_period = on_period + 32;}
        if (!on_dip_sw_7) { on_period = on_period + 64;}
        if (!on_dip_sw_8) { on_period = on_period + 128;}

        //read the OFF period dipswitches
        off_period = 0;
        if (!off_dip_sw_1) { off_period = off_period + 1;}
        if (!off_dip_sw_2) { off_period = off_period + 2;}
        if (!off_dip_sw_3) { off_period = off_period + 4;}
        if (!off_dip_sw_4) { off_period = off_period + 8;}
        if (!off_dip_sw_5) { off_period = off_period + 16;}
        if (!off_dip_sw_6) { off_period = off_period + 32;}
        if (!off_dip_sw_7) { off_period = off_period + 64;}
        if (!off_dip_sw_8) { off_period = off_period + 128;}
        
        
        //if(off_period == 2){ test_led = 1; } else { test_led = 0; }
    }
}
}

mikroBee
Posts: 12
Joined: 12 Nov 2011 15:28

Re: Pic16F18855 Timer 1 Not Working.

#2 Post by mikroBee » 22 Jul 2023 07:19

Hi Ian,
for the first view of your code:
check with uC datasheet if your port direction is correct. Input/Output direction I mean.

Best regards
Nico

Ditch
Posts: 71
Joined: 23 Jan 2017 23:14

Re: Pic16F18855 Timer 1 Not Working.

#3 Post by Ditch » 22 Jul 2023 09:53

Thanks mikroBee,

As far as I can see, i've set all I/O directions correctly. I have 3 banks of dip switches which create a binary value depending on their settings.
I can set any binary number on any dip switch and assign this value to the test_led and this works as expected.

The test_led is also set correct.

I'm not sure if having the I/O direction set wrong for one of he pins would effect the opertation of timer 1 would it?

Any other comments or help would be appriciated.

Ian

Ditch
Posts: 71
Joined: 23 Jan 2017 23:14

Re: Pic16F18855 Timer 1 Not Working.

#4 Post by Ditch » 22 Jul 2023 10:35

I think ive sorted this.

I've changed OSCCON1 = 0x92; to OSCCON1 = 0x60; and the Timer 1 interrupt is now working.

Ian

mikroBee
Posts: 12
Joined: 12 Nov 2011 15:28

Re: Pic16F18855 Timer 1 Not Working.

#5 Post by mikroBee » 22 Jul 2023 11:34

Ok, maybe I have overlooked something, sorry for confusion.
Anyway, you solved it. Good look.
Nico

Post Reply

Return to “mikroC PRO for PIC General”