There are two main portions of code that we are concerned with:
-LED Output Code
-Tilt Sensor Polling
The whole firmware program for this project can be found below. It consists of a few variable setups and then 4 for loops that make a single LED move back and forth across the LED bar. Everytime the LED moves the tilt sensor is checked to see if it has changed state. If a change is detected the firmware pauses in a forever while loop, until the tilt sensor returns to it's previous logic 0 state.
ASCII to Numbers/Letters
------------« Begin Code »------------
#include «p18f452.h» #include «delays.h» #pragma config OSC = HS #pragma config LVP = OFF #pragma config WDT = OFF void main(void){ unsigned int count=0; unsigned int led_out=0xFE; //PORTA Digital I/O ADCON1 = 0b00000110; //PORTA Used For Inputs TRISA = 0xFF; //PORTD Used For Outputs TRISD = 0x00; PORTD = 0xFF; while(1){ for(count=0;count<7;count++){ led_out = (led_out<<1); PORTD = led_out; Delay10KTCYx(40); while(PORTAbits.RA0 == 1); } for(count=0;count<7;count++){ led_out = (led_out<<1)|0x01; PORTD = led_out; Delay10KTCYx(40); while(PORTAbits.RA0 == 1); } for(count=0;count<7;count++){ led_out = (led_out>>1); PORTD = led_out; Delay10KTCYx(40); while(PORTAbits.RA0 == 1); } for(count=0;count<7;count++){ led_out = (led_out>>1)|0x80; PORTD = led_out; Delay10KTCYx(40); while(PORTAbits.RA0 == 1); } } }
------------« End Code »------------
One small thing to notice, since the LEDs only turn on when PORTD's bits are set to logic 0, a single logic 0 is moving back and forth across PORTD's 8 bits. this is seemingly backwards from the idea that a logic 1 should mean: LED on, but it's just how I designed the system in the schematic so that power consumption came from the 7805 regulator and not through the PIC's I/O ports.