Page 1 of 2

Timer1 interrupt

PostPosted: Sat Aug 09, 2014 12:03 pm
by Dimebag
Hi all, I have just a little code problem. Question as follows. 1; is it better to load TMR1H/TMR1L with values or, 2; just load TMR1 with a values buffer LOW/HIGH bytes.
Code: Select all
 #include <xc.h>

//Configurations and Fuses
#pragma config  OSC = HS, OSCS = OFF,PWRT = ON,BOR = OFF
#pragma config  BORV = 45,WDT = OFF,WDTPS = 1,CCP2MUX = OFF
#pragma config  STVR = OFF,LVP = OFF,DEBUG = OFF
#pragma config  CP0 = OFF,CP1 = OFF,CP2 = OFF,CP3 = OFF,CPB = OFF,CPD = OFF
#pragma config WRT0 = OFF,WRT1 = OFF,WRT2 = OFF,WRT3 = OFF,WRTB = OFF,WRTC = OFF,WRTD = OFF
#pragma config EBTR0 = OFF,EBTR1 = OFF,EBTR2 = OFF,EBTR3 = OFF,EBTRB = OFF

int Lap = 0;
int timeout = 1000;

void Initialization(void) {
    LATA = 0x00;
    LATB = 0X00;
    LATC = 0X00;
    LATD = 0X00;
    LATE = 0X00;
    TRISA = 0x00;
    TRISB = 0x00;
    TRISC = 0x00;
    TRISD = 0x00;
    TRISE = 0x00;
    PORTA = 0x00;
    PORTB = 0x00;
    PORTC = 0x00;
    PORTD = 0x00;
    PORTE = 0x00;
}

//REGISTERS ASSOCIATED WITH TIMER1 AS A TIMER/COUNTER

void Timer1Setup(void) {

    T1CONbits.RD16 = 1; //1 = Enables one 16-bit operation
    //0 = Enables two 8-bit operations
    T1CONbits.T1CKPS1 = 1;
    T1CONbits.T1CKPS0 = 1;
    //Input Clock Prescale Select bits
    //11 = 1:8 Prescale value
    //10 = 1:4 Prescale value
    //01 = 1:2 Prescale value
    //00 = 1:1 Prescale value
    T1CONbits.T1OSCEN = 0;
    //1 = Timer1 Oscillator is enabled
    //0 = Timer1 Oscillator is shut-off
    T1CONbits.T1SYNC = 0;
    T1CONbits.TMR1CS = 0; //0 = Internal clock (FOSC/4)
    T1CONbits.TMR1ON = 1; //Timer1 Disabled = 0 /Enabled = 1

    //Interrupt flags
    INTCONbits.GIE = 0;
    INTCONbits.PEIE = 0;
}
void test2loop (void){
    PORTB = 0X04;
}
void test1loop(void) {
    PORTB = 0x02;
}
void testloop(void) {
    PORTB = 0x01;
}
void main(void) {
    Initialization(); //Set all device pins.
    Timer1Setup(); //Init Timer1 module
    TMR1 = 0xffff; // TMR1L = 0xFF - TMR1H = 0xFF
    while (1) {
        PORTB = 0x00;
        if (PIR1bits.TMR1IF == 0) { // did Timer1 overflow?
            testloop();
            Lap++; // increment +1
        }
        if (Lap == timeout) {
            test1loop(); //CODE ends
        }
    }
}
 

I have given total code in test program.

Re: Timer1 interrupt

PostPosted: Sat Aug 09, 2014 12:53 pm
by vloki
Even your code looks strange here my answer to your explicit question:

Since you configure for 16bit R/W mode you should write or read the in two steps
-> FIGURE 11-2: TIMER1 BLOCK DIAGRAM: 16-BIT READ/WRITE MODE

If write TMR1H first
If read TMR1L first

( you don't know what the compiler created code will do )

Re: Timer1 interrupt

PostPosted: Sat Aug 09, 2014 2:04 pm
by Ian.M
"( you don't know what the compiler created code will do..." but you can bet it will be wrong for one of either reading or writing.)

Re: Timer1 interrupt

PostPosted: Sat Aug 09, 2014 2:45 pm
by Dimebag
It is very difficult to ask for the help while "Professional programmers" aren't willing to (code) ("tag") I am reading as much as I can. I am finding it hard to decide (Complier error) or, (Programme error) All codes I give compile. I am not sure about aspects of the code.

Re: Timer1 interrupt

PostPosted: Sat Aug 09, 2014 3:18 pm
by vloki
Dimebag wrote:Hi all, I have just a little code problem. Question as follows. 1; is it better to load TMR1H/TMR1L with values or, 2; just load TMR1 with a values buffer LOW/HIGH bytes.

Your question was not answered sufficiently ?

Re: Timer1 interrupt

PostPosted: Sat Aug 09, 2014 3:37 pm
by Olin Lathrop
vloki wrote:Your question was not answered sufficiently ?

You are wasting your time. This guy has a history of getting a good answer, then ignoring it and continuing to ask questions about the original issue. Go look at his other threads.

Re: Timer1 interrupt

PostPosted: Sat Aug 09, 2014 3:39 pm
by Dimebag
yes, I am still going over the TIMER1 reference guide and datasheet. Please bare with me as I try to convert datasheet to C. I know I am doing it the hard way, it is in some ways the best way to learn.

Re: Timer1 interrupt

PostPosted: Sat Aug 09, 2014 3:51 pm
by Dimebag
Until then, we need to make this a frustrating experience by withholding any real help. If his game is "poor little me, oops, I didn't know I wasn't supposed to do that", he's never going to get any help, at least not from me.
No reference given.

Re: Timer1 interrupt

PostPosted: Sat Aug 09, 2014 3:58 pm
by vloki
Olin Lathrop wrote:You are wasting your time. This guy has a history ...

I know him ;-)

Re: Timer1 interrupt

PostPosted: Sat Aug 09, 2014 4:19 pm
by Ian.M
Dimebag wrote:It is very difficult to ask for the help while "Professional programmers" aren't willing to (code) ("tag")

... So stop asking. We aren't going to write code for you because it reduces your chance of learning.

--
Give a man a fish; you have fed him for today. Teach a man to fish; and you will not have to listen to his incessant whining about how hungry he is.