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.
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 );
}
}
}