Initialize variables: DataByte (Transmit data) SwitchVar (SwitchVariable) RXData (Received data) ChargeState (State of battery charge) CharDischar (Charge/Discharge status) LampStatus (Lamp status) DutyCycle (Duty Cycle) ERR (Testing error) Set the following constants: Period = 194 for PWM period of 6.25ms SPI_Mode = 00110010 to set SSPEN=1|CKP=1 TRISASetup = 11111111 to set RA0,1,2 as inputs for Lamp status (inputs = 1) TRISBSetup = 00010000 to set I/O for PortB TRISCSetup = 00000010 to set I/O for PortC CCPsetup = 00001100 to set Compare mode to set output on match T2Setup = 00000101 to set Timer 2 prescaler to 4 Main: ;Initialize Timers/Clock/PWM/SPI/IO and Variables INIT: ;OSCCON - Set int osc speed to 500 kHz Set OSCCON,IRCF0 to 1 Set OSCCON,IRCF1 to 1 Set OSCCON,IRCF0 to 0 ; ANSEL - not using any analog inputs Clear ANSEL Clear ANSELH ; TRIS - set up input/outputs for Ports Set TRISA = TRISASetup Set TRISB = TRISBSetup Set TRISC = TRISCSetup ; SPI - set up SPI communication Clear SSPSTAT Set SSPCON = SPI_Mode ; PWM - Set PR2 = Period Set CCP1CON = CCPSetup Initialize PWM duty cycle to 50% ; Timer - Set T0CS to use internal instruction clock for TM0 and Timer 2 for PWM Clear T0CS bit of OPTION_REG Set T2CON = T2Setup Clear PortA Clear PortB Clear PortC Initialize SwitchVar = 0 Initialize DataByte = 0 Initialize ERR = 0 START_LOOP: ; Start the loop If PortC bit 1 is hi, Set DataByte to 0x3F Else Set DataByte to (PortA & 0x07)|0xF0 Switch(SwitchVar) Case 1: If T0IF is set Write DataByte to SSPBUF Increment SwitchVar Case 2: If BF (Buffer Full) is set Read SSPBUF Write 0x00 to SSPBUF Increment SwitchVar Case 3: If BF is set If SSPBUF is not 0xFF Set ERR=1 End Write 0x00 to SSPBUF Increment SwitchVar Case 4: If BF is set Read SSPBUF to working reg If PortC = 1 Call ChargeStat Write 0x00 to SSPBUF Increment SwitchVar Case 5: If BF is set If PORTC = 1 Set CharDischar = SSPBUF If CharDischar = 0 Clear PortC bit 0 Else if CharDischar = 0xFE Set PortC bit 0 (hi) Else Set LampStatus = SSPBUF LampStatus &= 00000111 Left shift LampStatus bits twice Set PortC = (PortC & 11100011)|LampStatus Set PortC bit 6 hi (raise SS line) Increment SwitchVar If SwitchVar > 5, Set SwitchVar = 0 Clear T0IF flag Clear Timer0 Start Loop again ***Functions**** ChargeStat SSPBUF/255 * 780 = Duty Cycle for PWM (hard code for 5% intervals) END