Pic16f877a timer interrupt

Status
Not open for further replies.
OK I am trying.


There is no effect after changings.
 
Last edited:

Use your old INTCONbits.INTF == 1 and use TRISB = 0xFF; PORTB = 0x00; in main() function.

Changes can not fix the problem.

- - - Updated - - -

Please let me that i want that Timer1 interrupt automatically interrupt after 1 sec exactly.What should I do?
 
Last edited:

hello,

in Post #15 , if your quartz is 2MHz !
you have correct value 3035=0x0BDB to get 1seconde with Timer1 (prescaler=1/8)

65535-3035=62500
62500 x 8 (prescaler) x 2 (1 cycle) µS => 1000 000µS soit 1seconde

but try to declare count as volatile...

volatile unsigned int Count ;

to use it in the main program.
 
Please let me know why use volatile or static before variable declaration.
 

see this link
https://www.edaboard.com/threads/214410/

microsoft general info :
The volatile keyword is a type qualifier used to declare that an object can be modified in
the program by something such as the operating system, the hardware, or a concurrently executing thread.
One use of the volatile qualifier is to provide access to memory locations
used by asynchronous processes such as interrupt handlers.

i had myself probleme if using variable in interrupt and in main program
so now i allways follow the rule !
 

Please let me know that I want to generate 1 sec delay by timer interrupt but crystal frequency must be 8MHz or 12MHz not 4MHz or 2MHz, but timer1 prescaler maximum is 1:8, how to implement ot how to increase prescaler value for timer1. Can I use timer0 interrupt it has prescaler rate 1:256?

Another question is that if I enabled WatchDog Timer so the display on LCD reset after resetting WDT.How to prevent WDT reset on program execution.?
 

This is for 100ms timer1 interrupt. Use a counter and increment it in step of one on every interrupt (of 100ms). For counter value = 10 you will have 100ms or 1sec timer.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
//Timer1
//Prescaler 1:4; TMR1 Preload = 15533; Actual Interrupt Time : 100 ms
 
//Place/Copy this part in declaration section
void InitTimer1(){
  T1CON  = 0x21;
  TMR1IF_bit     = 0;
  TMR1H  = 0x3C;
  TMR1L  = 0xAD;
  TMR1IE_bit     = 1;
  INTCON     = 0xC0;
}
 
void Interrupt(){
  if (TMR1IF_bit){ 
    TMR1IF_bit = 0;
    TMR1H    = 0x3C;
    TMR1L    = 0xAD;
    //Enter your code here
  }
}

 
hello



how to implement ot how to increase prescaler value for timer1.

you can not increasing the internal prescaler register !
Read the example on post#10 !
by using an extra counter, witch count the number of timer interrupts .
With this counter you can reach every time value by setting a flag inside the interrupt
or testing the counter value in the main program..


Adjust timer init for the correct value of quartz...
you can uses the attached document excel97 sheet
put your quartz value .. and pick up the result for your programm.


Can I use timer0 interrupt it has prescaler rate 1:256?

it is the same probleme as with Timer1 .. because timer0 size is 8 bits !
so better capacity of counting with Timer1 : 8 x 65536 =524280 maximum
instead of 256x256=65536 with timer0

How to prevent WDT reset on program execution.?
use clrwdt instruction in the main loop
and maybe in another place in subroutines.
 

Attachments

  • Timer16F.xls.txt
    93 KB · Views: 54
Last edited:
Dear paul,

Thank you for excel file, but i want only to increase crystal frequency at 8MHz.For same 1 sec delay.

- - - Updated - - -

it is the same probleme as with Timer1 .. because timer0 size is 8 bits !
so better capacity of counting with Timer1 : 8 x 65536 =524280 maximum
instead of 256x256=65536 with timer0.

Please let me know why you multiply 8 to ==>> 8 x 65536=524280 max. and 524280 is the maximum counting by timer1?
 

for 8 Mhz
you can use interrupt every 100mS or 250ms
count 4 interrupts gives 1 second

Code:
volatile int Sec ; // compteur de secondes
volatile int count;
volatile int Flag;

void InitTimer1(){
  T1CON  = 0x21;
  TMR1IF_bit     = 0;
  TMR1H  = 0x0B;  // 3035 => 65535-3035=62500  62500x8x0.5=250000µS=>250mS
  TMR1L  = 0xDB;
  TMR1IE_bit     = 1;
  INTCON     = 0xC0;
}




if (PIR1bits.TMR1IF==1)
{
PIR1bits.TMR1IF=0;
count++;
TMR1H = 0x0B; // you have to load the timer value 18660 in the interrupt routine also
TMR1L = 0xDB;
if (count>4)
 {
   Flag=1;  // one second elapsed used for TMR0 counting
 count=0;
  Sec ++; // increment secondes
}
}


void main()
{
Sec=0;
count=0;
Flag=0;
InitTimer1(); // IT every 250mS 
......

while(1)
{
...
//here test Flag value to detect one second elapsed
//and RAZ the Flag !!

if( Flag==1)
{
// do it every second
Flag=0;
....
....

}

....
}
 

Attachments

  • Image1.jpg
    29.1 KB · Views: 62
This is for 100ms timer1 interrupt. Use a counter and increment it in step of one on every interrupt (of 100ms). For counter value = 10 you will have 100ms or 1sec timer.

//Timer1
//Prescaler 1:4; TMR1 Preload = 15533; Actual Interrupt Time : 100 ms


But for 8MHz = 8MHz / 4 / prescaler 4 ====>> 500000 ,this is big value to 65535 how to use it and even for prescaler 8 this is 250000. it also big value than 65535 but timer1 is 16-bit the highest value is 2^16 = 65536.

How to calculate you the value 15533 by using prescaler 1:4.?
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…