Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

pic18f4431 timer1 problem

Status
Not open for further replies.

dmta

Member
Hi all,

I want to use the timer1 of the PIC18f4431 to increment every instruction cycle. I wrote a function to do this and simulated using pic18 simulator IDE but I am not seeing the TMRL register increment. Please take a look at my code and correct me.

Code:
void timer1_init(){

     T1CON.RD16    = 0;          // enable register read/write of TIMER1 in two 8-bit operations
     T1CON.T1RUN   = 0;          // system clock is derived from another source
     T1CON.T1CKPS1 = 0;          // ##increments with 1:1 prescale value
     T1CON.T1CKPS0 = 0;          // ##
     T1CON.T1OSCEN = 0;          // TIMER1 oscillator is shut-off
     T1CON.TMR1CS  = 0;          // enable TIMER1
     T1CON.TMR1ON  = 1;          // TIMER1 uses internal clock (FOSC/4)
     TMR1L         = 0;          // clear TIMER1 lower register
}

Regards
 
Last edited:
The bit of code you've posted looks fine and so your problem must be in the bit you didn't post.

Post code that can be compiled and someone will find your problem.

Mike.
 
:confused:
T1CON.TMR1CS = 0; // enable TIMER1
T1CON.TMR1ON = 1; // TIMER1 uses internal clock (FOSC/4)

I'm no programmer and I'm sorry this doesn't solve your problem, but I'm curious as to whether those two comments are the right way round? Intuitively, TMR1CS is the Clock-Select bit.
 
Alec_t! They are the right way round....

dmta!! I had this problem several days ago... I am furiously looking through my code to find out where it was used..

I think the problem was with Oshonsoft.... Try turning the OSCEN on... I think it was a bug..... If I find my code I'll repost.... It may be at home.
 
Hi Ian,

Yes, I tried the code below, and it works well. I even changed the prescaler values and it works accordingly.

Code:
void main(){

T1CON.RD16    = 0;          
     T1CON.T1RUN   = 0;          
     T1CON.T1CKPS1 = 0;          
     T1CON.T1CKPS0 = 0;          
     T1CON.T1OSCEN = 0;         
     T1CON.TMR1CS  = 0;          
     T1CON.TMR1ON  = 1;         
     TMR1L         = 0;        


while(1){

}
}

It is when I put these under my main loop (before my while(1)) everything goes to **** !!!!!

Code:
Lcd_Init();                // LCD display initialization
     Lcd_Cmd(_LCD_CURSOR_OFF);  // LCD command (cursor off)
     Lcd_Cmd(_LCD_CLEAR);       // LCD command (clear LCD)

     PWM1_Init(10000);          // ccp1 pwm frequency 10kHz
     PWM1_Set_Duty(PWM1);
     PWM1_Start();

     PWM2_Init(10000);          // ccp2 pwm frequency 10kHz
     PWM2_Set_Duty(PWM2);
     PWM2_Start();
 
Hi everyone thankyou for all your replies. I tried the code in proteus and it works fine. The problem was not with the code but was in the PIC18 simulator IDE.

@Ian, this is written in mikroc pro for pic.
@alec_t, yes the two comments have to be swaped

I have another problem though and that is the register read/write of TImer1 in one 16-bit operation.
At the moment I have set T1CON.RD16 = 1; and reading the timer1 value as

Code:
TIME1 = TMR1L;
          TIME2 = TMR1H;
          tot   = TIME2*256 + TIME1;

Am I doing it the correct way or what should I be doing ?
 
The problem with the 8 bit reads it that if timer1 rolls over after the first instruction then your 16 bit value will be out by 256 counts. With RD16=1, the way you are doing it is correct.

Note, you don't need 3 variables, you can simply do,
Code:
    tot = TMR1L;
    tot += TMR1H * 256;

Mike.
 
Glad you've resolved the problem, dmta.

:confused:
You say "@alec_t, yes the two comments have to be swaped"
Ian says "Alec_t! They are the right way round..."
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top