PIC18F2550 Memory game PIC c18 programming led off

Discussion in 'Embedded Systems and Microcontrollers' started by elec2010, Dec 18, 2012.

  1. elec2010

    Thread Starter New Member

    Mar 1, 2012
    4
    0
    I am using MPLAB C18 compiler to write c code for a PIC18F2550 which is to control a memory game. The user is to repeat a sequence of flashing LEDs by pressing corresponding buttons. A bit like a old Simon game but with a sequence just happening once.
    I have been struggling so far and I have only managed to get a number of LEDs to turn on, but the buttons only turn off the LEDs for a split second then they go on again.
    Here's my code so far:

    Code ( (Unknown Language)):
    1.  
    2.  
    3. #include <stdio.h>
    4. #include <stdlib.h>
    5. #include <p18f2550.h>
    6. #include <delays.h>
    7. //#pragma config WDT = OFF //Disable watchdog timer
    8. // LED Configuration
    9. #define LED_PIN_0 LATAbits.LATA0 //RA0
    10. #define LED_PIN_1 LATAbits.LATA1 //RA1
    11. #define LED_PIN_2 LATAbits.LATA2 //RA2
    12. #define LED_PIN_3 LATAbits.LATA3 //RA3
    13. #define LED_PIN_4 LATAbits.LATA4 //RA4
    14. #define LED_PIN_5 LATAbits.LATA5 //RA5
    15. #define LED_PIN_6 LATAbits.LATA6 //RA6
    16. // Push Button Configuration
    17. #define PUSH_BUTTON_0 PORTBbits.RB0 //RB0
    18. #define PUSH_BUTTON_1 PORTBbits.RB1 //RB1
    19. #define PUSH_BUTTON_2 PORTBbits.RB2 //RB2
    20. #define PUSH_BUTTON_3 PORTBbits.RB3 //RB3
    21. #define PUSH_BUTTON_4 PORTBbits.RB4 //RB4
    22. #define PUSH_BUTTON_5 PORTBbits.RB5 //RB5
    23. #define PUSH_BUTTON_6 PORTBbits.RB6 //RB6
    24. // Global Variables
    25. int led_array[7] ; // array storing the pin outs
    26. int button_array[7] ;
    27. void main()
    28. {
    29. int i;
    30. ADCON1 = 0b11111111;
    31. TRISA = 0; // sets the LED pins to output
    32. TRISB = 1; // sets pushbutton pins to input
    33. PORTA = 0;
    34. PORTB = 0;
    35. //sequence
    36. led_array[0] = 1 ;
    37. led_array[1] = 0 ;
    38. led_array[2] = 1 ;
    39. led_array[3] = 0 ;
    40. led_array[4] = 1 ;
    41. led_array[5] = 1 ;
    42. led_array[6] = 0 ;
    43. // Configure the LEDs
    44. LED_PIN_0 = led_array[0] ;
    45. LED_PIN_1 = led_array[1] ;
    46. LED_PIN_2 = led_array[2] ;
    47. LED_PIN_3 = led_array[3] ;
    48. LED_PIN_4 = led_array[4] ;
    49. LED_PIN_5 = led_array[5] ;
    50. LED_PIN_6 = led_array[6] ;
    51. // Configure the push button array
    52. PUSH_BUTTON_0 = button_array[0] ;
    53. PUSH_BUTTON_1 = button_array[1] ;
    54. PUSH_BUTTON_2 = button_array[2] ;
    55. PUSH_BUTTON_3 = button_array[3] ;
    56. PUSH_BUTTON_4 = button_array[4] ;
    57. PUSH_BUTTON_5 = button_array[5] ;
    58. PUSH_BUTTON_6 = button_array[6] ;
    59. if ( PUSH_BUTTON_0 == 1 )
    60. {
    61. led_array[0] = 0;
    62. }
    63. if ( PUSH_BUTTON_1 == 1 )
    64. {
    65. led_array[1] = 1;
    66. }
    67. if ( PUSH_BUTTON_2 == 1 )
    68. {
    69. led_array[2] = 0;
    70. }
    71. if ( PUSH_BUTTON_3 == 1 )
    72. {
    73. led_array[3] = 1;
    74. }
    75. if ( PUSH_BUTTON_4 == 1 )
    76. {
    77. led_array[4] = 0;
    78. }
    79. if ( PUSH_BUTTON_5 == 1 )
    80. {
    81. led_array[5] = 0;
    82. }
    83. if ( PUSH_BUTTON_6 == 1 )
    84. {
    85. led_array[6] = 1;
    86. }
    87. }
    88.  
     
  2. JohnInTX

    Moderator

    Jun 26, 2012
    2,348
    1,029
    This is backwards, you are assigning array values to inputs.

    I don't know about your other logic either.
     
  3. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,016
    And what have you done to debug this on your own? Have you stepped through the code with your debugger?
     
  4. Ian Rogers

    Member

    Dec 12, 2012
    158
    29
    There is no super loop... It just gets to the end and reboots...
     
  5. elec2010

    Thread Starter New Member

    Mar 1, 2012
    4
    0
    I know that the button configurations are backwards but I am using Proteus for simulation and its the only way that i get an output. The code was very different and written properly before i started the simulation but nothing worked there so i removed everything and started again with this basic code trying to get there step by step. Thank you all for your responses.
     
  6. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    You may get an output, but you aren't going to get what you want. How do you know everything was working properly? If it was working properly, why dismantle it and simulate a different function?
     
  7. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,395
    1,607
    And when it reboots it resets the LEDs. You need that "super loop" to retain whatever state information you want on the LEDs.
     
  8. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,016
    In other words.

    while(1);


    at the very end of your code.
     
  9. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,395
    1,607
    Well, perhaps a bit more, unless the player is supposed to remember a sequence of LEDs before they are shown, simultaneously press 1 to 7 buttons before even turning on the power... then guessing if he got it correct.

    I'd thing something like this is in order:

    - Display random LED sequence
    - wait till any key pressed
    - blank all LEDs
    - start timer
    - loop
    - if button pressed toggle that LED, reset timer
    - if timer expires, exit this loop
    - compare lit LEDs to goal sequence and display results
    - wait till any key pressed, then do it all over again.
     
  10. elec2010

    Thread Starter New Member

    Mar 1, 2012
    4
    0
    I made some changes based on what you said. I added a super loop and two counters. The first counter (keypress) checks if a button is pressed an turns the sequence off. The second counter (a) is supposed to check if the correct buttons were pressed but it doesn't. I want it to check if the correct buttons are pressed and if yes to exit the program else to start again from the begining. This is my code so far :
     
  11. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    Your TRISB is being set to 0x01, I think you mean to set it to 0xFF, otherwise, RB0 is an input, while all others are output...
     
  12. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    Just friendly tip. Then a program do not work as expected. We all have to do some debugging. Here in the first post(link). You will find some info on using the MPLAB software simulator. 5 minutes work with the debugger. May save you days in project time. Feel free to ask for help using the debugger.
    http://forum.allaboutcircuits.com/showthread.php?t=44852
     
Loading...