Problem Using timer

General discussion on mikroC PRO for 8051.
Post Reply
Author
Message
coolamigo
Posts: 28
Joined: 19 May 2012 19:12

Problem Using timer

#1 Post by coolamigo » 23 Jun 2012 16:53

Hi!

I am using timer T1 in a digital clock project and have set TH1 to 0x01F (=31) and TL1 =0x00; I supposed counting 16 interupts in this way would count 1 sec. as 256*(256-31)*16*12 = 11059200. Running the code on proteus gives an output of 48 secs in 1 min which is .8 the required speed. Where am I going Wrong?

Code: Select all

void Timer1InterruptHandler() org IVT_ADDR_ET1{//<===0

  EA_bit = 0;        // Clear global interrupt enable flag / seems unnecessary here

  //TR1_bit = 0;       // Stop Timer1
  TH1 = 0x1F;        // Reset Timer1 high byte  <===0
  //TL1 = 0x00;        // Reset Timer1 low byte

  Sms++;

  EA_bit = 1;        // Set global interrupt enable flag
  //TR1_bit = 1;       // Run Timer1
}

Code: Select all

 TF1_bit = 0;       // Timer 1 Overflow: Ensure that Timer1 interrupt flag is cleared
  ET1_bit = 1;       // Enable Timer1 interrupt
  EA_bit = 1;       // Set global interrupt enable
  GATE1_bit = 0;     // Clear this flag to enable Timer1 whenever TR1 bit is set (not only at input at P3.3).
  C_T1_bit = 0;     // Set Timer operation: Timer1 counts the divided-down system clock
                     // When this bit is clear the timer will be incremented every machine cycle (not event counter for P3.5)..
  M11_bit = 0;     // M1 of Timer 1 Mode 1(16-bit Timer/Counter)
  M01_bit = 1;     // M0 of Timer 1  M0 an M1 = 1&0 resp.
  TR1_bit = 0;       // Turn off Timer1

  TH1 = 0x01F;        // Set Timer1 high byte    Do I have to set it back to this value?
  TL1 = 0x00;        // Set Timer1 low byte

  cval = 16; // Or (crystal/12)/(65536-(TH1*256+Tl1));
  TR1_bit = 1;
Also please confirm if setting Timer1 high byte each time is necessary.

Could it be because of using 2*16 LCD. I have heard (but am not sure) that the LCD Library also makes use of internal timers.

User avatar
janko.kaljevic
Posts: 3565
Joined: 16 Jun 2011 13:48

Re: Problem Using timer

#2 Post by janko.kaljevic » 25 Jun 2012 12:41

Hello,

Your calculations are correct.
I have tested it on Easy8051 board with AT89S8253 controller (11.0592 MHz Crystal), and 16 interrupts were exactly 1 second.

Best regards.

coolamigo
Posts: 28
Joined: 19 May 2012 19:12

Re: Problem Using timer

#3 Post by coolamigo » 25 Jun 2012 13:56

Thanks.

Before using timers,I had also tried similar code using delay_ms function on both LCD and 7-panel and the same type of poblem occured only while using LCD. Any way I can know the internal coding of functions of LCD library (like Lcd_Init, Lcd_Out and Lcd_Out_Cp) in C or Basic. I think somehow attaching the LCD or using the functions for it, is to blame.

In any case if a function like one wire library (I am using it) or any other makes use of timers, is there any way of knowing what values it sets TH and TL to :?:

Best Regards
Muhammad Asad

User avatar
janko.kaljevic
Posts: 3565
Joined: 16 Jun 2011 13:48

Re: Problem Using timer

#4 Post by janko.kaljevic » 26 Jun 2012 10:57

Hello,

Delay_ms function will make correct delay if the oscillator settings are correct.
Also timer interrupt will generate interrupts on selected intervals no matter which libraries or functions you use in the code.

So I disagree that LCD functions could cause difference in timer intervals.

Best regards.

Post Reply

Return to “mikroC PRO for 8051 General”