Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Need help programming timer interrupt for PIC16F877A

Status
Not open for further replies.

ccslearner

Junior Member level 1
Junior Member level 1
Joined
Feb 1, 2013
Messages
19
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,283
Location
India,tamilnadu
Visit site
Activity points
1,445
Hai friends, first of all i want tell one thing i am more happy to member of these website because the objective of the forum is more attracted and also this is my firs post along a long period of searching, my question is
1) any one can help me to write the LED blinking program like the order of given below
LED1 ON 2sec and OFF 5 sec
LED2 ON 3sec and OFF 6sec using Timer2 Interrupt(the interrupt is created every 5msec)

I am using PIC16F877A mcu and CCS compiler to work above assignment and i have the code for timer2 interrupt to LED1 ON 2sec and OFF 2sec , LED2 ON 5sec and OFF 5sec(symmetric) these code was working but i cant to modify the program to get output (asymmetric wise) like above my question . I hope more answer

thanks
 

Post your code.

You have to use this code for 5 ms Timer 2 Interrupt


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//Timer2
//Prescaler 1:16; Postscaler 1:7; TMR2 Preload = 223; Actual Interrupt Time : 4.9966 ms
 
//Place/Copy this part in declaration section
void InitTimer2(){
  T2CON  = 0x36;
  PR2        = 223;
  TMR2IE_bit     = 1;
  INTCON     = 0xC0;
}
 
void Interrupt(){
  if (TMR2IF_bit){ 
    TMR2IF_bit = 0;
    //Enter your code here
  } 
}



You have to use a 4 counters and flag in interrupt routine. Each counter wil have to be reset after reaching its max value and a flag has to be set. If the flag is set you call the blinking routine in main function.

counter max values are like this

2 sec count = 400
3 sec count = 600
5 sec count = 1000
6 sec count = 1200

My values are for 20 MHz crystal. What Clock are you using?
 
Last edited:

Yes friends

#include <timer interrupt.h>

#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)


int1 two_sec_timer_flag = FALSE,five_sec_timer_flag =FALSE,ten_sec_timer_flag = FALSE ;
int16 two_sec_count,five_sec_count,ten_sec_count;

#INT_TIMER2
void timer2_isr()
{
two_sec_count++;
five_sec_count++;
ten_sec_count++;
{
if(two_sec_count == 400) // 400 * 5 msec = 2000 msec = 2 sec
{
two_sec_count = 0;
two_sec_timer_flag = TRUE;
}
if(five_sec_count==1000) /// 1000 * 5 msec = 5000 msec = 5 sec
{
five_sec_count=0;
five_sec_timer_flag=TRUE;
}
if(ten_sec_count == 2000) // 2000 * 5 =10000 msec = 10 sec
{
ten_sec_count = 0 ;
ten_sec_timer_flag = TRUE;
}
}
}
void main ()
{
int1 last_2sec,last_5sec,last_10sec;

//setup_timer_2(T2_DIV_BY_4,78,16);//setup up timer2 to interrupt every 1ms
setup_timer_2(T2_DIV_BY_4,250,5); //setup up timer2 to interrupt every 1ms
//setup_timer_2 (mode, period, post scale)
//mode - T2_DISABLED, T2_DIV_BY_1, T2_DIV_BY_4, T2_DIV_BY_16
//period is a int 0-255 that determines when the clock value is reset,
//post scale is a number 1-16 that determines how many timer overflows before
//an interrupt: (1 means once, 2 means twice, and so on).

// calculating timer reload value to generate 1 msec interrupt
// T = 1/f
// T = 1/4MHZ = 1/4* 1000000
// But Microcontroller hardware will divide the clock by 4
// and if we chosen T2_DIV_BY_4 then again there will
// one more division happen by 4
// So T = 1/(4 * 1000000)/ 4 * 4 / T=4/4*1000000
// T = 4*4/4*1000000 = 0.000004 sec = 0.4 usec //4 usec
// ************************************************
// At4mhz, the timer will increment every 0.4 us // 4 usec *// 0.000004 sec
// ************************************************

// ********************************************************
// if period is chosen 250 then timer wi
//ll overflow every *
// 4 * 250 = 1000 usec //1 msec *
// ********************************************************

// *******************************************************************
// And if we chosen post scalar as 5 then timer interrupt will occur *
//1000 usec * 5 = 5000 usec /// 5 msec
// *******************************************************************
last_2sec=FALSE;
last_5sec=FALSE;
last_10sec=FALSE;
enable_interrupts(INT_TIMER2); // enable timer2 interrupt
enable_interrupts(GLOBAL);
output_b(0x0); // off all theLEDS
putc('z');
//infine loop
while (1)
{

if( two_sec_timer_flag == TRUE)
{
two_sec_timer_flag = FALSE;
//printf("I am in 2 sec \n\r");

last_2sec = ~last_2sec;
if ( last_2sec == TRUE)
{
output_b( (input_b() | 0x01)); // ON LED 1 alone
}
else
{
output_b( (input_b() & 0xFE)); // OFF LED 1 alone
}

}
if( five_sec_timer_flag == TRUE )
{
five_sec_timer_flag = FALSE;
// printf ("I am in 5 sec \n\r" );
last_5sec = ~last_5sec;
if ( last_5sec == TRUE)
{
output_b( (input_b() | 0x02)); // ON LED 2 alone
}
else
{
output_b( (input_b() & 0xFD)); // OFF LED 2 alone
}

}
if( ten_sec_timer_flag == TRUE)
{
ten_sec_timer_flag = FALSE;
//printf("i am in 10 sec \n\r");
last_10sec = ~last_10sec;
if(last_10sec==TRUE)
{
output_b( (input_b() | 0x04) ); // ON LED3 alone
}
else
{
output_b( (input_b() & 0xFB)); // OFF LED 3 alone
}






} // end of while


} // end of main

}

- - - Updated - - -

anyone cite me the small change of code and i am in beginning stage of embedded c
 

This code is for 4 MHz clock

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
//Timer2
//Prescaler 1:4; Postscaler 1:5; TMR2 Preload = 250; Actual Interrupt Time : 5.005 ms
 
//Place/Copy this part in declaration section
void InitTimer2(){
  T2CON  = 0x25;
  PR2        = 250;
  TMR2IE_bit     = 1;
  INTCON     = 0xC0;
}
 
void Interrupt(){
  if (TMR2IF_bit){ 
    TMR2IF_bit = 0;
    //Enter your code here
  } 
}



Edit your last post and post the code in between code or syntax tags.
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top