This is my first topic in this forum, I hope that I can find some help..
I'm a beginner in this field, I've been searching on how to do this code for a longtime, but I couldn't do it. Can you help me in this project?
I want to use TMR0 (not TMR1 nor TMR2) the MCU is PIC16f628A .
I want to do a code that can turn an LED ON and OFF as the following:
while BTN=0 : LED_oFF xx seconds.
LED_ON xx seconds.
if BTN=1 : LED_OFF immediately , but when BTN=0 again, LED resumes its blinking from last state it was upon before BTN=1, not counting from the beginning.
For example:
LED off 100s.
LED on 10s.
So, for instance:
If the LED had been OFF 60 s before BTN=1, then it will be OFF for 40 s more when BTN=0. If the LED had been ON 7 s before BTN=1, then it will be ON for 3 s more when BTN=0.
Thank you.
This is one of my failed attempts :
Code: Select all
#define LED PORTA.F0
#define BTN PORTB.F0
unsigned int cnt; sec;
bit flag;
//Timer0
//Prescaler 1:64; TMR0 Preload = 6; Actual Interrupt Time : 8 ms
//Place/Copy this part in declaration section
void InitTimer0(){
OPTION_REG = 0x05;
TMR0 = 6;
INTCON = 0B10100000;
}
void Interrupt(){
if (TMR0IF_bit)
{
cnt++;
TMR0IF_bit = 0;
TMR0 = 6;
}
if (INTF_bit==1)
{
flag=1;
INTF_bit=1;
}
}
void main() {
OPTION_REG = 0x05;
INTCON = 0B10100000;
TRISA.F0=0;
TRISB.F0=1;
TRISA.F1=0;
PORTA.F1=0;
FLAG=0;
LED=0;
do {
PORTA.F1=FLAG;
// while( BTN==1){
if(cnt==100) LED=1;
if(cnt==200) LED=0;
if(cnt==400) cnt=0;
// }
if(BTN==1) LED=0;
}
while(1) ;
}