#include <xc.h> #include <stdlib.h> #include <pic18f4550.h> #define Digit PORTBbits.RB1 //variable to sink current to PNP base #define SENSER PORTBbits.RB2 //variable to sink current to PNP base #define LED1 PORTDbits.RD4 #define PROX_SENSOR PORTBbits.RB3 #define Senfwd 1 //pressed is high #define Senbwd 1 //pressed is high #define LEDPin LATDbits.LATD0 // Define LEDPin as PORT D Pin 0 #define LEDTris TRISDbits.TRISD0 // Define LEDTris as TRISD Pin 0 as output mode #define _XTAL_FREQ 8000000 // for __delay_ms() function #pragma config FOSC = INTOSC_HS // Internal oscillator, HS used by USB. #pragma config FOSC = INTOSCIO_EC // Internal oscillator, port function on RA6, EC used by USB. #pragma config WDT = OFF // Disable watchdog timer void initialize(); void segment (void); void Sensor(void); void delay_ms(unsigned int); unsigned char count=0; void main(void){ initialize(); Digit = 0; while(1){ Sensor(); segment(); Digit = 1; return; } } void segment() { static const unsigned char segArray[]= {0b11000000, 0b11111001, 0b00100100, 0b00110000, 0b00011001, 0b00010010, 0b00000010, 0b11111000, 0b00000000, 0b00011000 }; TRISC = 0; //PortC all OUTPUT PORTC = 0xFF; //PortC all HIGH = IDLE = LED_OFF TRISBbits.TRISB0 = 0; //Output unused TRISBbits.TRISB1 = 0; //Output Digit1 TRISBbits.TRISB2 = 0; //Output Digit2 TRISBbits.TRISB4 = 1; //Input: Switch PLUS TRISBbits.TRISB3 = 1; //Input: Switch MINUS PORTB = 0x00; unsigned char count=0; for(;;) { //Handle buttons if (Senfwd && count<9) { ++count; delay_ms(100); } if (Senbwd && count > 0) { --count; delay_ms(100); } else if (Senbwd && count <= 0) { break; } //Write low digit PORTC = segArray[count%10]; Digit = 0; delay_ms(10); // Delay for 10 ms Digit = 1; } } void buzzerled(void) { LATD = 0x00; // send 0x00 to port D. TRISD = 0x00; TRISB = 0XFF; while (1) { LED1 = PROX_SENSOR; } }
var
This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)