Help me blink this LED!

General discussion on mikroC PRO for PIC.
Post Reply
Author
Message
shaydzmi
Posts: 1
Joined: 19 Mar 2022 08:13

Help me blink this LED!

#1 Post by shaydzmi » 20 Mar 2022 16:34

Hi,
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.
PIC16F628A.png
PIC16F628A.png (46.66 KiB) Viewed 496 times
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) ;
      
}

mikroBee
Posts: 12
Joined: 12 Nov 2011 15:28

Re: Help me blink this LED!

#2 Post by mikroBee » 02 Apr 2022 12:02

Hello shaydzmi,

I am not familar with your PIC and its datasheet but some recommendations and ideas:
- is LED working without any TMR0 usage? (strip down your code to ensure this area is working)
- is Tmr0 working? (strip down your code, if you have oszi just use and output pin to control your timing)
In general what I would say:
ensure with small separate programs that single parts are working! Then increase complexity of program code & functionality.
It is highly recommended to insert a resistor (e.g. 1k) daisy-chain to LED.

I hope the picture you attached is not your complete circuit around PIC... :idea:

BR
mikroBee

Post Reply

Return to “mikroC PRO for PIC General”