TMR1 wont work correctly

Status
Not open for further replies.

BGAmodz

Member
Hi all .

I don't know whats wrong with my program , it is for a simple led blinking with a delay of 5 seconds .
The led turns on after 5 seconds but wont turn off after another delay .


This is the program script :

Code:
int time = 0;

void interrupt () {

  if (TMR1IF_bit ) {

  TMR1H = 0x0B ;
  TMR1L = 0xDB ;

  time++ ;

}
  PIR1.TMR1IF = 0;
}
  void main() {

T1CON = 0x39 ;
INTCON = 0xC0 ;
TMR1IE_bit = 1 ;
TMR1IF_bit = 0 ;
TMR1H = 0x0B ;
TMR1L = 0xDB ;
TRISA = 0x00 ;
PORTA = 0 ;

  while(1) {
  if ( time == 10) {
  porta = ~porta ;
  time=0;

  }
}
}
 
Have you copied that code or re-written it.... It won't compile like that... porta is in lower case..

If you can simulate, check the PORTA = ~PORTA; operation to make sure it's doing what you want!!
 
Have you copied that code or re-written it.... It won't compile like that... porta is in lower case..

If you can simulate, check the PORTA = ~PORTA; operation to make sure it's doing what you want!!

yes its written by me and it is compiling , but when i simulate it , the led wont react as it should be ; blinking , it only lights up after 5 seconds and stays like that .

The PORTA = ~PORTA; is working since the initial state of the led is 0 , and after the delay is passed it changes its state to 1
 
also wouldn't something like these work better :
TRISCbits.TRISC7 = 1;
PIE1bits.TMR1IE = 1;
PIE4bits.CCP4IE = 1;

then:
TMR1IE_bit = 1 ;
TMR1IF_bit = 0 ;
 
If you have a shadow register "leds" and use it.. it works

C:
    while(1) {
        if ( time == 10) {
            leds = ~leds ;
            PORTA = leds;
            time=0;
        }

I don't think the ~ operator works very well with ports..
 
Hi Ian roger , i have replaced porta.b0 by led but that didnt make any difference .



I have another program using timer 0 and its working fine , thats really weird .

Code:
int counter=0;

void interrupt () {

if(intcon.t0if==1){
counter++;
TMR0=100;
}
intcon.t0if=0;
}

void main() {

porta=0x00;
Trisa=0x00;
option_reg=0x86;
intcon=0xA0;
TMR0=100;

while(1){

if(counter==313){
porta=~porta;
counter=0;
}
}
}
 
You need to turn off the analog function, all analog pins read as zero so complementing wont work. Try adding adcon1=6.

Mike.
 
Pass.... I can't see how the timer can effect the loading of a port... Just work round it!!!
 
here was my example.... I use XC8 so I mad subtle changes..

As you can see I also made PORTA digital.... Although when I did, it still didn't work until I made a shadow register.

C:
#include<xc.h>
int time=0;

void interrupt ISR () {

  if (TMR1IF) {

    TMR1H = 0x0B ;
    TMR1L = 0xDB ;
    time++ ;
    }
  TMR1IF = 0;
}

void main() {
    unsigned char leds;
    T1CON = 0x39 ;
    INTCON = 0xC0 ;
    TMR1IE = 1 ;
    TMR1IF = 0 ;
    TMR1H = 0x0B ;
    TMR1L = 0xDB ;
    TRISA = 0x00 ;
    ADCON1 = 0x7;
    leds = 0 ;

    while(1) {
        if ( time == 10) {
            leds = ~leds ;
            PORTA = leds;
            time=0;
        }
    }
}
 
Any pins that are analog read as zero and so porta=~porta results in 0xff being written every time.

Yes I get that. But even when they were digital PORTA still returned wrong...
 
Thanks guys i have solved the problem by implementing ADCON1 configuration.

But the question is why ADCON1 is necessary for the pins to work correctly , ADCON1 only got 3 programmable bits .

and by using 0x07 as ADCON1 value we are assigning value 1 to 3 bits that have no use on that register , only usable ones are the ones bellow and are all set to 0 i dont see how this register could affect the pins operations :

ADFM - A/D Result Format Select bit
1 - Conversion result is right justified. Six most significant bits of the ADRESH are not
used.
0 - Conversion result is left justified. Six least significant bits of the ADRESL are not
used.
VCFG1 - Voltage Reference bitselects negative voltage reference source needed for the
operation of A/D converter.

1 - Negative voltage reference is applied to the Vref- pin.
0 - Power supply voltage Vss is used as negative voltage reference source.
VCFG0 - Voltage Reference bitselects positive voltage reference source needed for the
operation of A/D converter.

1 - Positive voltage reference is applied to the Vref+ pin.
0 - Power supply voltage Vdd is used as positive voltage reference source.
 
Last edited:

This seems to be from the 16F88x (or similar) data sheet, not from the 16F87x ??

Dave
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…