display using pic18f4520

Discussion in 'Embedded Systems and Microcontrollers' started by harami, Feb 22, 2012.

  1. harami

    Thread Starter Member

    Jun 20, 2011
    66
    0
    can anyone check if the code is right.
    if button 1 pressed followed by button 2, increament n by 1 and save.. and if button 2 is pressed followed by button 1, decrement n by 1 and save. Also display the value of n on the led display.

    Code ( (Unknown Language)):
    1.  
    2. #include <p18f4520.h>
    3. #include <delays.h>
    4.  
    5.     #pragma config OSC = HS                              
    6.     #pragma config WDT = OFF
    7.     #pragma config LVP = OFF
    8.      
    9.     #define btn_on     PORTBbits.RB4
    10.     #define btn_off    PORTBbits.RB5
    11.     #define led        PORTAbits.RA0
    12.      
    13.     #endif  /* HOST */
    14.      
    15.  
    16. void SevenSegment(uint8_t n,uint8_t dp)
    17. {
    18.   static const uint8_t segBits = { 0b00000011, 0b10011111, 0b00100101, 0b00001101, 0b10011001, 0b01001001, 0b01000001, 0b00011111, 0b00000001, 0b00001001, 0b11111101 };
    19.  
    20.   if (n > 10)
    21.     n = 10;
    22.  
    23.   SEVEN_SEGMENT_PORT = segBits[n];
    24.  
    25.   if ((n<10) && dp)
    26.     SEVEN_SEGMENT_PORT &= 0b11111110;
    27. }
    28.  
    29.  
    30.     void led( int state );
    31.     int  pressed( int button, int state );
    32.      
    33.     int main ( ) {
    34.         int btn1 = 0, btn2 = 0;
    35.      
    36.     #if !defined(HOST)
    37.         TRISA = 0;  // set Port A(LED) as output
    38.         n = 0;  
    39.     #endif
    40.      
    41.         for ( ; ; ) {
    42.             btn1 = pressed( 1, btn1 );
    43.             btn2 = pressed( 2, btn2 );
    44.          if ( btn1 && !btn2 ) {
    45.                 n( ++1 );
    46.             } else
    47.             if ( btn2 && !btn1 ) {
    48.                 n( --1 );
    49.             }
    50.         }
    51.     }
    52.  
     
  2. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,016
    What compiler is this? C18? Looks like most of your code is missing. There is no code for pressed. You aren't setting your TRIS registers, except for TRISA and with that #define it is not being compiled. Are any of your input pins have analog input? If so you need to set them as binary input to be sure.


    Have you tried using your debugger to step through the code?


    Comments would be useful. I have no idea what you are trying to do with SevenSegment. Can anyone else figure that one out?
     
  3. harami

    Thread Starter Member

    Jun 20, 2011
    66
    0
    yea im using c18. its my first time using C language therefore im reallly struggling.. any chance u could help. HELP NEEDED!! :(
     
  4. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,016
    You have done next to nothing. We are willing to help but you need to first be willing to help yourself. Don't expect anyone here to write your code for you.

    You need to first define where you need help.
     
Loading...