This is the code running on the segment driver controller boards:
Main.c
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
#include <htc.h> #define _XTAL_FREQ 8000000 #include "main.h" int address = 0x40; //address 0x10, 0x20, 0x30 or 0x40 __CONFIG(FOSC_INTOSCIO & WDTE_OFF); //internal osc. WDT off void interrupt isr(void) { if(RCIF) //handle incomming bytes { menu(RCREG); RCIF=0; //clear flag } } void main(void) { init(); //Setup ports test(); //Run segment tests number(7); //set value on display while(1) //let interrupts do the rest... {} } |
and Main.h (This is not the right way to do it)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 |
extern int address; int status=0; void number(int val) { switch(val) { case 0: PORTA=0x05; PORTC=0x0F; break; case 1: PORTA=0x05; PORTC=0x00; break; case 2: PORTA=0x06; PORTC=0x0D; break; case 3: PORTA=0x07; PORTC=0x09; break; case 4: PORTA=0x07; PORTC=0x02; break; case 5: PORTA=0x03; PORTC=0x0B; break; case 6: PORTA=0x03; PORTC=0x0F; break; case 7: PORTA=0x05; PORTC=0x01; break; case 8: PORTA=0x07; PORTC=0x0F; break; case 9: PORTA=0x07; PORTC=0x03; break; case 10: PORTA=0x00; PORTC=0x00; break; default: break; } } void test(void) { int j=0; for(j=0;j<11;j++) { number(j); __delay_ms(300); } PORTA=0x01; __delay_ms(100); PORTA=0x02; __delay_ms(100); PORTA=0x04; __delay_ms(100); PORTA=0x00; PORTC=0x01; __delay_ms(100); PORTC=0x02; __delay_ms(100); PORTC=0x04; __delay_ms(100); PORTC=0x08; __delay_ms(100); } void putch(unsigned char ch) { TXREG=ch; while((TXSTA&0x02)==0){} } void menu(char ch) { if (ch==0xAF) //packet start byte status=1; else if (status==1 && ch==address) status=2; else if (status==2) { number(ch); status=0; } else status=0; } void init(void) { //Osc OSCCON=0x70; //Ports TRISA=0xF8; //0,1,2 is output TRISC=0xF0; //0,1,2,3 is output //UART TXSTA=0x24; //Enable TX RCSTA=0x90; //Enable RX BRGH=1; BRG16=1; SPBRG=16; GIE=1; //Enable interrups PEIE=1; //Peripheral interrups RCIE=1; //receive interrupts } |