| ON Timer Program |
Sometimes we require TURNING ON/OFF a device for a specific duration & then going back to the initial state. Here is a program that will activate/deactivate a relay for short duration. We will use two switches one to start the timer & another one to stop the timer i.e. a START Switch & a STOP Switch. If the Start Switch is pressed when the Timer is ON then the timer starts counting again. One LED is used which will toggle after every second indicating that the timer is ON. The anode of the LED is connected to the Vcc & the cathode to the controller PIN so when the PIN is low the LED will turn ON & vice versa. Please click here to see how to interface LED to Microcontroller. For interfacing a Relay a relay drive circuit is required. It is basically an NPN transistor BC 547. So when the port pin is high the transistor as well as the relay is turned ON & when the Pin is low the relay is turned OFF. Check this tutorial for more information of Relay driver circuit. Two pins are used to interface the two switches. Normally the port pins are high because of the internal pull up resistors. Now when the switch is pressed a low is detected at the port pin. For learning how to interface switch to a controller see ‘Interfacing Switch to Microcontroller’. This code will be easier to understand if you first see the following codes: · Timer Interrupt based LED Blinking Program. · Switch Interfacing & Debouncing Program. Here we will be using Timer 0 in 16 bit mode to generate One second delay. The maximum delay that can be generated using TIMER 0 in any mode is 65535 clock cycles & since we are using 12MHz crystal maximum delay is 65.535ms. For generating One second we generate 50ms delay using TIMER 0 & when 20 such delays have been generated we come to know that one second has elapsed. So basically 0.05ms X 20 =1 second. So we use a RAM location “multiplier” load it with 20 and whenever the timer overflows the ram location multiplier is decremented by one. Thus when multiplier becomes zero we come to know that one second has elapsed & we toggle the LED. Another location “time” is used which contains the duration (in seconds) for which the relay should remain ON. We have defined this ON duration as ‘TIME_DEFAULT’ the maximum duration that the Relay can be turned ON is 255 seconds & the minimum is 1 second. start_switch bit P2.0 //start switch connected to P2.0 stop_switch bit P2.1 //stop switch connected to P2.1 led bit P2.2 //LED connected to P2.2 relay bit P2.3 //Relay connected to P2.3 multiplier equ 30h time equ 31h MULTIPLIER_DEFAULT data 20 //0.05 x 20 =1seconds TIME_DEFAULT data 100 //100 seconds ON delay org 0000h ajmp main org 000bh jmp timer0_interrupt // Jump To Interrupt Subroutine main: setb start_switch //initialize start switch pin as input setb stop_switch //initialize stop switch pin as input setb led //turn OFF led clr relay //turn OFF relay setb EA //Enable Interrupt setb ET0 //Enable timer 0 Interrupt mov multiplier,#MULTIPLIER_DEFAULT mov TMOD,#01h // Timer 0 Mode 1(16 bit mode)
loop: jnb start_switch,c1_main //Check if start switch is LOW jnb stop_switch,c2_main //Check if stop switch is LOW ajmp loop c1_main: call debounce_delay //wait for the debounce period of 20ms jb start_switch,loop //if pin high after debounce period then return mov TH0,#3ch //0.05 sec mov TL0,#0B0H //load timer values mov multiplier,#MULTIPLIER_DEFAULT //load default values mov time,#TIME_DEFAULT jnb start_switch,$ //Wait for the switch to be released setb TR0 //Start Timer 0 setb relay //Turn ON relay clr led //Turn ON led ajmp loop c2_main: call debounce_delay //wait for the debounce period of 20ms jb stop_switch,loop //if pin high after debounce period then return jnb stop_switch,$ //wait for the switch to be released clr TR0 //Stop Timer 0 clr relay //Turn OFF relay setb led //Turn OFF led ajmp loop
timer0_interrupt: push acc push b push psw push dph push dpl clr TF0 clr TR0 //Stop Timer mov TH0,#3ch //0.05 sec mov TL0,#0B8H //Reload Timer djnz multiplier,c1_timer0_interrupt mov multiplier,#MULTIPLIER_DEFAULT //if one second over then cpl led //Toggle LED djnz time,c1_timer0_interrupt //decrement time by one setb led //if time=00 then turn OFF LED clr relay //turn OFF relay pop dpl //no need to start timer again pop dph pop psw pop b pop acc reti c1_timer0_interrupt: setb TR0 //Start Timer 0 pop dpl pop dph pop psw pop b pop acc reti debounce_delay: mov r7,#245 l1_debounce_delay: mov r6,#40 djnz r6,$ djnz r7,l1_debounce_delay ret END
|
No comment posted
mXcomment 1.0.5 © 2007-2008 - visualclinic.fr
License Creative Commons - Some rights reserved
| < Prev | Next > |
|---|





















