Again Problem on Push Button (LED Blinking) PIC16F877A

Thread Starter

Shyamal805

Joined Mar 20, 2012
27
Hi,
I am new in micro-controller programming and starting a new project on 4 LED blinking using MikroC PRO for PIC v5.6.1. I choose PIC-16F877A for this project.

I want to blink the 4 LED sequentially (24 times clock-wise and another 24 times anti clock-wise) when the Push Button is pressed.

I also used Proteus Professional for checking my program. I see that my code is working good in Proteus Professional.

No problem arise when the .hex code is burnt on the PIC16F877A. Burn successfully!

But a problem arise when the push button is pressed. 4 LED blink sequentially (first for loop infinitely) but need still pressing the button. When i leave the Button the STOP the LED blink.

2nd for loop not working any time. That's why i am very very upset and full of frustration on my project.

This is my code:

Rich (BB code):
void main(){
  TRISA = 0b11111111;
  TRISB = 0b00000000;
  PORTB = 0b00000000;
  CMCON = 7;
  ADCON1 = 7;

// Turn ON/OFF LEDs on PORTB
  while(1)
  {
    int i,j;
      if(PORTA.F0 == 0)
      {
        for(i=1; i<7; i++)
        {
          PORTB.F1=1;       //(1st LED) or 34 No. PIN is ON
            Delay_ms(500);
          PORTB.F1=0;
           
          PORTB.F2=1;       //(2nd LED) or 35 No. PIN is ON
            Delay_ms(500);
          PORTB.F2=0;
           
          PORTB.F4=1;       //(3rd LED) or 37 No. PIN is ON
            Delay_ms(500);
          PORTB.F4=0;

          PORTB.F5=1;       //(4th LED) or 38 No. PIN is ON
            Delay_ms(500);
          PORTB.F5=0;
         }
        for(j=1; j<7; j++)
        {
          PORTB.F4=1;       //(3rd LED) or 37 No. PIN is ON
            Delay_ms(500);
          PORTB.F4=0;

          PORTB.F2=1;       //(2nd LED) or 35 No. PIN is ON
            Delay_ms(500);
          PORTB.F2=0;
         
          PORTB.F1=1;       //(1st LED) or 34 No. PIN is ON
            Delay_ms(500);
          PORTB.F1=0;

          PORTB.F5=1;       //(4th LED) or 38 No. PIN is ON
            Delay_ms(500);
          PORTB.F5=0;
         }
       }
   }
}

I've also attached the Circuit Diagram, MikroC PRO file and Proteus Professional simulation file. Please give me some solution about my project.

Please help help help ......................
 

Attachments

THE_RB

Joined Feb 11, 2008
5,438
Didn't you already ask this question? Or was it on another forum?

I think it might be some weird issue with your variables, try moving the variable declarations for i and j to the very top of the code.

Also if you want to loop for 6 loops, this is better coding;
for(i=0; i<6; i++)

As the number 6 properly represents the number of loops (and could be replaced later by a constant or a variable, to select the number of loops). :)
 

ErnieM

Joined Apr 24, 2011
8,377
Code looks just fine to me too. No need to change anything there.

I'm not sure why you say "But a problem arise when the push button is pressed. 4 LED blink sequentially (first for loop infinitely) but need still pressing the button. When i leave the Button the STOP the LED blink." That is precisely what your code says to do, so you would have to explain what your intent was for any help on the code.
 

Thread Starter

Shyamal805

Joined Mar 20, 2012
27
Code looks just fine to me too. No need to change anything there.

I'm not sure why you say "But a problem arise when the push button is pressed. 4 LED blink sequentially (first for loop infinitely) but need still pressing the button. When i leave the Button the STOP the LED blink." That is precisely what your code says to do, so you would have to explain what your intent was for any help on the code.
The problem arise after written the hex file on the PIC.

The problem is - If i press the push button, LED's Blink but when i release the push button LED's not blink.

In the time of still pressing the push button, only first loop is activated.

I cann't find the problem. What can i do now? please help...
 

ErnieM

Joined Apr 24, 2011
8,377
The problem is - If i press the push button, LED's Blink but when i release the push button LED's not blink.
That is what your code says to do: blink when pressed, don't blink when not pressed.

If that is not what you want you must tell us what is the proper sequence.

In the time of still pressing the push button, only first loop is activated.
That we all got the first time. The code looks fine, why it does this is not something obvious.
 

absf

Joined Dec 29, 2010
1,968
I converted your software to Hitech C and simulate you circuit on proteus and it worked fine...

Here's the code and simulation as attached.

Rich (BB code):
#include <htc.h>
#define _XTAL_FREQ 4000000	//I used 4 MHz xtal

__CONFIG(FOSC_HS &// High Speed Crystal.
WDTE_OFF &// Disable Watchdog Timer.
PWRTE_ON &// Enable Power Up Timer.
BOREN_OFF &// Disable Brown Out Reset.
//MCLRE_ON & Enable MCLR
LVP_OFF);// Disable Low Voltage Programming.


#define	 button	RA0		//same as PORTA.F0
#define  LED1	RB1		//same as PORTB.F1
#define  LED2	RB2		//ditto
#define  LED3	RB4		//ditto
#define  LED4	RB5		//ditto

void main(){
  TRISA = 0b11111111;	//port A all inputs
  TRISB = 0b00000000;	//port B all Outputs
  PORTB = 0b00000000;	//All LED are off
  CMCON = 7;			//switch off comparator
  ADCON1 = 7;			//all ADC off

// Turn ON/OFF LEDs on PORTB
  while(1)
  {
    int i,j;
      if(button == 0)
      {
        for(i=1; i<7; i++)
        {
          LED1 = 1;       //(1st LED) or 34 No. PIN is ON
            __delay_ms(500);	//delay 500ms in HiTech C
          LED1 = 0;
           
          LED2=1;       //(2nd LED) or 35 No. PIN is ON
            __delay_ms(500);
          LED2=0;
           
          LED3=1;       //(3rd LED) or 37 No. PIN is ON
            __delay_ms(500);
          LED3=0;

          LED4=1;       //(4th LED) or 38 No. PIN is ON
            __delay_ms(500);
          LED4=0;
         }
        for(j=1; j<7; j++)
        {
          LED3=1;       //(3rd LED) or 37 No. PIN is ON
            __delay_ms(500);
          LED3=0;

          LED2=1;       //(2nd LED) or 35 No. PIN is ON
            __delay_ms(500);
          LED2=0;
         
          LED1=1;       //(1st LED) or 34 No. PIN is ON
            __delay_ms(500);
          LED1=0;

          LED4=1;       //(4th LED) or 38 No. PIN is ON
            __delay_ms(500);
          LED4=0;
         }
       }
   }
}
I also tested the program on my PIC dev. board SK40C with the LED and button on breadboard. It works fine too.

Allen
 

Attachments

Last edited:

Thread Starter

Shyamal805

Joined Mar 20, 2012
27
absf, so many thanks. Your code is working good. I understand where was my problem.

Now want to know more every part of this configuration.
__CONFIG(FOSC_HS &// High Speed Crystal. WDTE_OFF &// Disable Watchdog Timer. PWRTE_ON &// Enable Power Up Timer. BOREN_OFF &// Disable Brown Out Reset. //MCLRE_ON & Enable MCLR LVP_OFF);// Disable Low Voltage Programming.
Do you help me again and give me some tutorial that will help me easily understand.
------------------------------------
Again thanks,
Shyamal
 

ErnieM

Joined Apr 24, 2011
8,377
Do you help me again and give me some tutorial that will help me easily understand.
Learning the in and out of the Configuration Bits can be tough but it's something you need go thru to get good with PICs. They are detailed in the data sheet in section: 14.1 Configuration Bits

I would suggest you read read and re read that section till it makes some sense.

Do come back and ask about anything specific in there you don't understand.
 

absf

Joined Dec 29, 2010
1,968
Besides doing what Ernie has said, I alo refer to the .INC file for 16F877A under directory \microchip\mpasm suite\.....
It always needs time to learn something. Guess there's no shortcut to learn PIC

Rich (BB code):
;==========================================================================
;
;       Configuration Bits
;
;   NAME            Address
;   CONFIG            2007h
;
;==========================================================================

; The following is an assignment of address values for all of the
; configuration registers for the purpose of table reads
_CONFIG          EQU  H'2007'

;----- CONFIG Options --------------------------------------------------
_FOSC_LP             EQU  H'3FFC'    ; LP oscillator
_LP_OSC              EQU  H'3FFC'    ; LP oscillator
_FOSC_XT             EQU  H'3FFD'    ; XT oscillator
_XT_OSC              EQU  H'3FFD'    ; XT oscillator
_FOSC_HS             EQU  H'3FFE'    ; HS oscillator
_HS_OSC              EQU  H'3FFE'    ; HS oscillator
_FOSC_EXTRC          EQU  H'3FFF'    ; RC oscillator
_RC_OSC              EQU  H'3FFF'    ; RC oscillator

_WDTE_OFF            EQU  H'3FFB'    ; WDT disabled
_WDT_OFF             EQU  H'3FFB'    ; WDT disabled
_WDTE_ON             EQU  H'3FFF'    ; WDT enabled
_WDT_ON              EQU  H'3FFF'    ; WDT enabled

_PWRTE_ON            EQU  H'3FF7'    ; PWRT enabled
_PWRTE_OFF           EQU  H'3FFF'    ; PWRT disabled

_BOREN_OFF           EQU  H'3FBF'    ; BOR disabled
_BODEN_OFF           EQU  H'3FBF'    ; BOR disabled
_BOREN_ON            EQU  H'3FFF'    ; BOR enabled
_BODEN_ON            EQU  H'3FFF'    ; BOR enabled

_LVP_OFF             EQU  H'3F7F'    ; RB3 is digital I/O, HV on MCLR must be used for programming
_LVP_ON              EQU  H'3FFF'    ; RB3/PGM pin has PGM function; low-voltage programming enabled

_CPD_ON              EQU  H'3EFF'    ; Data EEPROM code-protected
_CPD_OFF             EQU  H'3FFF'    ; Data EEPROM code protection off

_WRT_HALF            EQU  H'39FF'    ; 0000h to 0FFFh write-protected; 1000h to 1FFFh may be written to by EECON control
_WRT_1FOURTH         EQU  H'3BFF'    ; 0000h to 07FFh write-protected; 0800h to 1FFFh may be written to by EECON control
_WRT_256             EQU  H'3DFF'    ; 0000h to 00FFh write-protected; 0100h to 1FFFh may be written to by EECON control
_WRT_OFF             EQU  H'3FFF'    ; Write protection off; all program memory may be written to by EECON control

_DEBUG_ON            EQU  H'37FF'    ; In-Circuit Debugger enabled, RB6 and RB7 are dedicated to the debugger
_DEBUG_OFF           EQU  H'3FFF'    ; In-Circuit Debugger disabled, RB6 and RB7 are general purpose I/O pins

_CP_ON               EQU  H'1FFF'    ; All program memory code-protected
_CP_ALL              EQU  H'1FFF'    ; All program memory code-protected
_CP_OFF              EQU  H'3FFF'    ; Code protection off
I attach the complete file here.

Allen
 

Attachments

Last edited:

Maliha

Joined Dec 17, 2013
2
I know I am jumping in from no where but thought it would be helpful.The thing is that I am new to PIC 16F877A and have made this simple code on PROTON IDE which is abt controlling 2 traffic signals and running a counter at the same time. Its running fine but implementing it on PROTEUS is a little issue.I am not sure how make connections for a 7 segment common anode display and my ports are not turning on when i burn my software.Its a very small problem but i would appreciate if somebody from among those who replied to this thread helps with the connections in Proteus. I just need the connections for Proteus for this code on PROTON IDE. Please reply I would be highly obliged.


'****************************************************************
'* Name : 2 way Traffic Lights Control *
'and 7 segment display.BAS *
'* Author : [Maliha Arif] * *
'* Notice : Copyright (c) 2013 [select VIEW...EDITOR OPTIONS] *
'* : All Rights Reserved *
'* Date : 12/12/2013 *
'* Version : 1.0 *
'* Notes : *
' *
'****************************************************************
Device=16F877A
XTAL 20
TRISC=0 'Declaration: All port C pins as output
TRISD=0 'Declaration: All port D pins as output

main:

PORTD=%00100001 'Red LED (1st pole) ON,Green LED (2nd Pole) ON


PORTC=%01000000 'Digit 0 '7 segment Display**
DelayMS 100 ' 1s
PORTC=%11111001 'Digit 1
DelayMS 100 ' 1s
PORTC=%00100100 'Digit 2
DelayMS 100 ' 1s
PORTC=%00110000 'Digit 3
DelayMS 100 ' 1s
PORTC=%00011001 'Digit 4
DelayMS 100 ' 1s
PORTC=%00010010 'Digit 5
DelayMS 100 ' 1s
PORTC=%00000010 'Digit 6
DelayMS 100 ' 1s
PORTC=%11111000 'Digit 7
DelayMS 100 ' 1s
PORTC=%00000000 'Digit 8
DelayMS 100 ' 1s
PORTC=%00010000 'Digit 9
DelayMS 100 ' 1s


PORTD=%00010010 'Yellow LED(both poles) ON


PORTC=%01000000 'Digit 0 '7 segement Display**
DelayMS 100 ' 1s
PORTC=%11111001 'Digit 1
DelayMS 100 ' 1s
PORTC=%00100100 'Digit 2
DelayMS 100 ' 1s
PORTC=%00110000 'Digit 3
DelayMS 100 ' 1s
PORTC=%00011001 'Digit 4
DelayMS 100 ' 1s
PORTC=%00010010 'Digit 5


PORTD=%00001100 'Green LED(1st pole) ON,Red LED(2nd pole) ON


PORTC=%01000000 'Digit 0 '7 segement Display**
DelayMS 100 ' 1s
PORTC=%11111001 'Digit 1
DelayMS 100 ' 1s
PORTC=%00100100 'Digit 2
DelayMS 100 ' 1s
PORTC=%00110000 'Digit 3
DelayMS 100 ' 1s
PORTC=%00011001 'Digit 4
DelayMS 100 ' 1s
PORTC=%00010010 'Digit 5
DelayMS 100 ' 1s
PORTC=%00000010 'Digit 6
DelayMS 100 ' 1s
PORTC=%11111000 'Digit 7
DelayMS 100 ' 1s
PORTC=%00000000 'Digit 8
DelayMS 100 ' 1s
PORTC=%00010000 'Digit 9
DelayMS 100 ' 1s

GoTo main
 
Top