display using pic18f4520

Thread Starter

harami

Joined Jun 20, 2011
66
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.

Rich (BB code):
#include <p18f4520.h>
#include <delays.h>

	#pragma config OSC = HS                              
	#pragma config WDT = OFF
	#pragma config LVP = OFF
	 
	#define btn_on     PORTBbits.RB4
	#define btn_off    PORTBbits.RB5
	#define led        PORTAbits.RA0
	 
	#endif  /* HOST */
	 

void SevenSegment(uint8_t n,uint8_t dp)
{
  static const uint8_t segBits = { 0b00000011, 0b10011111, 0b00100101, 0b00001101, 0b10011001, 0b01001001, 0b01000001, 0b00011111, 0b00000001, 0b00001001, 0b11111101 };

  if (n > 10)
    n = 10;

  SEVEN_SEGMENT_PORT = segBits[n];

  if ((n<10) && dp)
    SEVEN_SEGMENT_PORT &= 0b11111110;
}


	void led( int state );
	int  pressed( int button, int state );
	 
	int main ( ) {
	    int btn1 = 0, btn2 = 0;
	 
	#if !defined(HOST)
	    TRISA = 0;  // set Port A(LED) as output
	    n = 0;   
	#endif
	 
	    for ( ; ; ) {
	        btn1 = pressed( 1, btn1 );
	        btn2 = pressed( 2, btn2 );
         if ( btn1 && !btn2 ) {
	            n( ++1 );
	        } else
	        if ( btn2 && !btn1 ) {
	            n( --1 );
	        }
	    }
	}
 

spinnaker

Joined Oct 29, 2009
7,830
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?
 

Thread Starter

harami

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

spinnaker

Joined Oct 29, 2009
7,830
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.
 
Top