|
|
View previous topic :: View next topic |
Author |
Message |
jahan
Joined: 04 Apr 2005 Posts: 63
|
Light Timer with Interrupts |
Posted: Fri Sep 23, 2005 3:31 pm |
|
|
I'm writing a simple code to practice using my INT's and TIMERS.
3 Push Bottons are connected to RB0, RB1, RB2
RB0 = Start Timer (turn LED ON for 2 minutes and then turn it OFF)
RB1 = Turn LED ON (permenant)
RB2 = Turn LED OFF (if LED is ON even if on TIMER).
compiler: PCB,PCM,PCH=3.223
Code: | #include <18F452.h>
#device adc=8
#fuses NOWDT,WDT128,XT, NOPROTECT, NOOSCSEN, NOBROWNOUT, BORV20, NOPUT, \
NOSTVREN, NODEBUG, NOLVP, NOWRT, NOWRTD, NOWRTB, NOWRTC, NOCPD, \
NOCPB, NOEBTR, NOEBTRB
#use delay(clock=4000000)
#define ON 1
#define OFF 0
#define INTS_PER_SECOND 25 // (4,000,000/(4*16*250*10)) = 25
// Global Variables
int8 seconds; // A running seconds counter
int8 int_count; // Number of interrupts left before a second has elapsed
void Lights(int stat) {
if (stat == ON)
output_high(PIN_A0);
else
output_low(PIN_A0);
}
#int_TIMER2
void TIMER2_isr() {
// timer 2 overflows (250->0), which is apx 25 times/Sec
if(--int_count == 0) {
++seconds;
if (seconds == 120) {
Lights(OFF);
disable_interrupts(INT_TIMER2);
} else {
int_count = INTS_PER_SECOND;
}
}
}
#int_EXT
EXT_isr() { // Timer switch
Lights(ON);
int_count = INTS_PER_SECOND;
seconds = 0;
enable_interrupts(INT_TIMER2);
}
#int_EXT1
EXT1_isr() { // Light ON
disable_interrupts(INT_TIMER2);
Lights(ON);
}
#int_EXT2
EXT2_isr() { // Light OFF
disable_interrupts(INT_TIMER2);
Lights(OFF);
}
void main() {
int_count = INTS_PER_SECOND;
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_wdt(WDT_OFF);
setup_timer_2(T2_DIV_BY_16, 250, 10);
set_timer2(0);
setup_ccp1(CCP_OFF);
enable_interrupts(INT_EXT);
enable_interrupts(INT_EXT1);
enable_interrupts(INT_EXT2);
enable_interrupts(GLOBAL);
while (TRUE) {
;
}
}
|
Please give me feed back as if this is the best way of doing this using INT and TMR2?
Thankx in advance |
|
|
treitmey
Joined: 23 Jan 2004 Posts: 1094 Location: Appleton,WI USA
|
|
Posted: Mon Sep 26, 2005 8:10 am |
|
|
1)I would not pass an int8 into light() function when a int1 would do
2)in an isr just set a flag or buffer data. Dont go calling all sorts of funtions. KEEP IT SHORT
3)in general if a function[[light()]] is that simple, you should do it inline to save a calling level.
4)So if you move the bulk of the code out of the isr then you would have a loop at the end of main that would do something like
- - - -I don't know if this will work, but i thnk you'll get my meaning.- - - -
Code: | #include <18F452.h>
#fuses NOWDT,WDT128,XT, NOPROTECT, NOOSCSEN, NOBROWNOUT, BORV20, NOPUT, \
NOSTVREN, NODEBUG, NOLVP, NOWRT, NOWRTD, NOWRTB, NOWRTC, NOCPD, \
NOCPB, NOEBTR, NOEBTRB
#use delay(clock=16000000)
//#use delay(clock=4000000)
#define ON 1
#define OFF 0
#define INTS_PER_SECOND 25 // (4,000,000/(4*16*250*10)) = 25
// Global Variables
int8 seconds; // A running seconds counter
int8 int_count; // Number of interrupts left before a second has elapsed
int1 light_on,timer_on; // Number of interrupts left before a second has elapsed
#int_TIMER2
void TIMER2_isr() {
// timer 2 overflows (250->0), which is apx 25 times/Sec
if(--int_count == 0) {
++seconds;
}
}
#int_EXT
EXT_isr() { // Timer switch
timer_on=TRUE;
}
#int_EXT1
EXT1_isr() { // Light ON
light_on=TRUE;
}
#int_EXT2
EXT2_isr() { // Light OFF
light_on=FALSE;
}
void main() {
int_count = INTS_PER_SECOND;
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
setup_wdt(WDT_OFF);
setup_timer_2(T2_DIV_BY_16, 250, 10);
set_timer2(0);
setup_ccp1(CCP_OFF);
enable_interrupts(INT_EXT);
enable_interrupts(INT_EXT1);
enable_interrupts(INT_EXT2);
enable_interrupts(GLOBAL);
while (TRUE) {
if(timer_on)
{
output_high(PIN_A0);
int_count = INTS_PER_SECOND;
seconds = 0;
enable_interrupts(INT_TIMER2);
timer_on=false;
}
if(light_on)
{
output_high(PIN_A0);
}
if(!light_on)
{
output_low(PIN_A0);
}
if (seconds == 120)
{
light_on=false;
disable_interrupts(INT_TIMER2);
}
else
{
int_count = INTS_PER_SECOND;
}
}
} |
|
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|