Again Problem on Push Button (LED Blinking) PIC16F877A

Discussion in 'Embedded Systems and Microcontrollers' started by Shyamal805, Aug 18, 2013.

  1. Shyamal805

    Thread Starter New Member

    Mar 20, 2012
    27
    0
    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:

    Code ( (Unknown Language)):
    1.  
    2. void main(){
    3.   TRISA = 0b11111111;
    4.   TRISB = 0b00000000;
    5.   PORTB = 0b00000000;
    6.   CMCON = 7;
    7.   ADCON1 = 7;
    8.  
    9. // Turn ON/OFF LEDs on PORTB
    10.   while(1)
    11.   {
    12.     int i,j;
    13.       if(PORTA.F0 == 0)
    14.       {
    15.         for(i=1; i<7; i++)
    16.         {
    17.           PORTB.F1=1;       //(1st LED) or 34 No. PIN is ON
    18.             Delay_ms(500);
    19.           PORTB.F1=0;
    20.            
    21.           PORTB.F2=1;       //(2nd LED) or 35 No. PIN is ON
    22.             Delay_ms(500);
    23.           PORTB.F2=0;
    24.            
    25.           PORTB.F4=1;       //(3rd LED) or 37 No. PIN is ON
    26.             Delay_ms(500);
    27.           PORTB.F4=0;
    28.  
    29.           PORTB.F5=1;       //(4th LED) or 38 No. PIN is ON
    30.             Delay_ms(500);
    31.           PORTB.F5=0;
    32.          }
    33.         for(j=1; j<7; j++)
    34.         {
    35.           PORTB.F4=1;       //(3rd LED) or 37 No. PIN is ON
    36.             Delay_ms(500);
    37.           PORTB.F4=0;
    38.  
    39.           PORTB.F2=1;       //(2nd LED) or 35 No. PIN is ON
    40.             Delay_ms(500);
    41.           PORTB.F2=0;
    42.          
    43.           PORTB.F1=1;       //(1st LED) or 34 No. PIN is ON
    44.             Delay_ms(500);
    45.           PORTB.F1=0;
    46.  
    47.           PORTB.F5=1;       //(4th LED) or 38 No. PIN is ON
    48.             Delay_ms(500);
    49.           PORTB.F5=0;
    50.          }
    51.        }
    52.    }
    53. }
    54.  

    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 ......................
     
  2. THE_RB

    AAC Fanatic!

    Feb 11, 2008
    5,435
    1,305
    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). :)
     
  3. blueroomelectronics

    AAC Fanatic!

    Jul 22, 2007
    1,758
    98
    You need 0.1uf decoupling caps, also where are the config fuse settings?
     
  4. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,392
    1,606
    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.
     
  5. Shyamal805

    Thread Starter New Member

    Mar 20, 2012
    27
    0
    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...
     
  6. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,392
    1,606
    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.

    That we all got the first time. The code looks fine, why it does this is not something obvious.
     
  7. absf

    Senior Member

    Dec 29, 2010
    1,493
    372
    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.

    Code ( (Unknown Language)):
    1. #include <htc.h>
    2. #define _XTAL_FREQ 4000000  //I used 4 MHz xtal
    3.  
    4. __CONFIG(FOSC_HS &// High Speed Crystal.
    5. WDTE_OFF &// Disable Watchdog Timer.
    6. PWRTE_ON &// Enable Power Up Timer.
    7. BOREN_OFF &// Disable Brown Out Reset.
    8. //MCLRE_ON & Enable MCLR
    9. LVP_OFF);// Disable Low Voltage Programming.
    10.  
    11.  
    12. #define  button RA0     //same as PORTA.F0
    13. #define  LED1   RB1     //same as PORTB.F1
    14. #define  LED2   RB2     //ditto
    15. #define  LED3   RB4     //ditto
    16. #define  LED4   RB5     //ditto
    17.  
    18. void main(){
    19.   TRISA = 0b11111111;   //port A all inputs
    20.   TRISB = 0b00000000;   //port B all Outputs
    21.   PORTB = 0b00000000;   //All LED are off
    22.   CMCON = 7;            //switch off comparator
    23.   ADCON1 = 7;           //all ADC off
    24.  
    25. // Turn ON/OFF LEDs on PORTB
    26.   while(1)
    27.   {
    28.     int i,j;
    29.       if(button == 0)
    30.       {
    31.         for(i=1; i<7; i++)
    32.         {
    33.           LED1 = 1;       //(1st LED) or 34 No. PIN is ON
    34.             __delay_ms(500);    //delay 500ms in HiTech C
    35.           LED1 = 0;
    36.            
    37.           LED2=1;       //(2nd LED) or 35 No. PIN is ON
    38.             __delay_ms(500);
    39.           LED2=0;
    40.            
    41.           LED3=1;       //(3rd LED) or 37 No. PIN is ON
    42.             __delay_ms(500);
    43.           LED3=0;
    44.  
    45.           LED4=1;       //(4th LED) or 38 No. PIN is ON
    46.             __delay_ms(500);
    47.           LED4=0;
    48.          }
    49.         for(j=1; j<7; j++)
    50.         {
    51.           LED3=1;       //(3rd LED) or 37 No. PIN is ON
    52.             __delay_ms(500);
    53.           LED3=0;
    54.  
    55.           LED2=1;       //(2nd LED) or 35 No. PIN is ON
    56.             __delay_ms(500);
    57.           LED2=0;
    58.          
    59.           LED1=1;       //(1st LED) or 34 No. PIN is ON
    60.             __delay_ms(500);
    61.           LED1=0;
    62.  
    63.           LED4=1;       //(4th LED) or 38 No. PIN is ON
    64.             __delay_ms(500);
    65.           LED4=0;
    66.          }
    67.        }
    68.    }
    69. }
    I also tested the program on my PIC dev. board SK40C with the LED and button on breadboard. It works fine too.

    Allen
     
    Last edited: Aug 19, 2013
  8. Shyamal805

    Thread Starter New Member

    Mar 20, 2012
    27
    0
    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
     
  9. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,392
    1,606
    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.
     
  10. absf

    Senior Member

    Dec 29, 2010
    1,493
    372
    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

    Code ( (Unknown Language)):
    1. ;==========================================================================
    2. ;
    3. ;       Configuration Bits
    4. ;
    5. ;   NAME            Address
    6. ;   CONFIG            2007h
    7. ;
    8. ;==========================================================================
    9.  
    10. ; The following is an assignment of address values for all of the
    11. ; configuration registers for the purpose of table reads
    12. _CONFIG          EQU  H'2007'
    13.  
    14. ;----- CONFIG Options --------------------------------------------------
    15. _FOSC_LP             EQU  H'3FFC'    ; LP oscillator
    16. _LP_OSC              EQU  H'3FFC'    ; LP oscillator
    17. _FOSC_XT             EQU  H'3FFD'    ; XT oscillator
    18. _XT_OSC              EQU  H'3FFD'    ; XT oscillator
    19. _FOSC_HS             EQU  H'3FFE'    ; HS oscillator
    20. _HS_OSC              EQU  H'3FFE'    ; HS oscillator
    21. _FOSC_EXTRC          EQU  H'3FFF'    ; RC oscillator
    22. _RC_OSC              EQU  H'3FFF'    ; RC oscillator
    23.  
    24. _WDTE_OFF            EQU  H'3FFB'    ; WDT disabled
    25. _WDT_OFF             EQU  H'3FFB'    ; WDT disabled
    26. _WDTE_ON             EQU  H'3FFF'    ; WDT enabled
    27. _WDT_ON              EQU  H'3FFF'    ; WDT enabled
    28.  
    29. _PWRTE_ON            EQU  H'3FF7'    ; PWRT enabled
    30. _PWRTE_OFF           EQU  H'3FFF'    ; PWRT disabled
    31.  
    32. _BOREN_OFF           EQU  H'3FBF'    ; BOR disabled
    33. _BODEN_OFF           EQU  H'3FBF'    ; BOR disabled
    34. _BOREN_ON            EQU  H'3FFF'    ; BOR enabled
    35. _BODEN_ON            EQU  H'3FFF'    ; BOR enabled
    36.  
    37. _LVP_OFF             EQU  H'3F7F'    ; RB3 is digital I/O, HV on MCLR must be used for programming
    38. _LVP_ON              EQU  H'3FFF'    ; RB3/PGM pin has PGM function; low-voltage programming enabled
    39.  
    40. _CPD_ON              EQU  H'3EFF'    ; Data EEPROM code-protected
    41. _CPD_OFF             EQU  H'3FFF'    ; Data EEPROM code protection off
    42.  
    43. _WRT_HALF            EQU  H'39FF'    ; 0000h to 0FFFh write-protected; 1000h to 1FFFh may be written to by EECON control
    44. _WRT_1FOURTH         EQU  H'3BFF'    ; 0000h to 07FFh write-protected; 0800h to 1FFFh may be written to by EECON control
    45. _WRT_256             EQU  H'3DFF'    ; 0000h to 00FFh write-protected; 0100h to 1FFFh may be written to by EECON control
    46. _WRT_OFF             EQU  H'3FFF'    ; Write protection off; all program memory may be written to by EECON control
    47.  
    48. _DEBUG_ON            EQU  H'37FF'    ; In-Circuit Debugger enabled, RB6 and RB7 are dedicated to the debugger
    49. _DEBUG_OFF           EQU  H'3FFF'    ; In-Circuit Debugger disabled, RB6 and RB7 are general purpose I/O pins
    50.  
    51. _CP_ON               EQU  H'1FFF'    ; All program memory code-protected
    52. _CP_ALL              EQU  H'1FFF'    ; All program memory code-protected
    53. _CP_OFF              EQU  H'3FFF'    ; Code protection off
    I attach the complete file here.

    Allen
     
    Last edited: Aug 23, 2013
  11. Maliha

    New Member

    Dec 17, 2013
    2
    0
    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
     
Loading...