list p=16f627A ; list directive to define processor #include ; processor specific variable definitions errorlevel -302 ; suppress message 302 from list file __CONFIG _CP_OFF & _DATA_CP_OFF & _LVP_OFF & _BOREN_OFF & _MCLRE_OFF & _WDT_OFF & _PWRTE_OFF & _INTOSC_OSC_NOCLKOUT ;***** VARIABLE DEFINITIONS d1 EQU 0x70 ;delay variable #1 d2 EQU 0x71 ;delay variable #1 count EQU 0x74 ;Our Tx Count Variable rxTmp EQU 0x75 ;Our Rx Variable ORG 0x000 ; processor reset vector call MyBank1 ;PCON is in bank 0 BSF PCON, OSCF call MyBank0 ;CMCON is in bank 0 MOVLW 0x07 MOVWF CMCON ;Comparators Off call MyBank1 ;TRISB and TXSTA is in bank 1 movlw b'00000110' movwf TRISB ;Make TRISB, 1 and 2 a INPUT pin (Tx - RB2)(Rx - RB1) It will change when in use. movlw b'00000100' ;BRGH = 1 (High Speed) and SYNC = 0 (Asynchronous Mode) movwf TXSTA call MyBank0 ;RCSTA is in bank 0 movlw b'00010000' ;Enables continuous receive movwf RCSTA call MyBank1 ;SPBRG is in bank 1 movlw d'25' movwf SPBRG ;25 and BRGH =1 would make 9600bps call MyBank0 ;RCSTA is in bank 0 bsf RCSTA, SPEN ;Serial Port Enable bit call MyBank1 ;TXSTA is in bank 1 bsf TXSTA, TXEN ;Transmit Enable bit call MyBank0 ;PORTB is in bank 0 as well as many others. Main Bank bcf PORTB, 4 ;Turn off our LED Main: call Delay ;Start with a small delay to make sure all is ready. call Message1 ;Send our Message1 "Press 1=On 0=Off" call RecvRx ;Wait for user input... movwf rxTmp ;Move the input to the rxTmp variable movlw 0x30 ;move 0x30 which will be used to subtract from inpuut since ;the 1 key is a 0x31. if you minus 0x30 you end up with a 1 as the bit. ;now 0x30 = 0 key minus 0x30 = 0 in the first bit. subwf rxTmp,1 ;subtract it from rxTmp and store it in rxTmp btfss rxTmp,0 ;Test bit 0 of rxTmp and goto Off ;if its a 0 goto off else skip this line On: ;if its a 1 do the below call SendOn ;Send out message "On" bsf PORTB, 4 ;Set the LED pin which is PORTB pin 4 to high aka on goto Main ;goto main lol Off: ;if it was a 0 then you should be here call SendOff ;Send our message "Off" bcf PORTB, 4 ;Clear PortB pin 4 aka off goto Main ;goto main lol RecvRx: btfss PIR1, RCIF ;Check if data has been recieved goto $-1 ;If not then go back 1 instruction movf RCREG, W ;If so move the data to the W register return ;return from where i was called SendTx: btfss PIR1, TXIF ;Check if we are busy sending something already goto $-1 ;If so then go back 1 instruction movwf TXREG ;If ready move W reg into TXREG to send data. return ;Return to where i was called Message1: clrf count Message1A: movf count, w ;put counter value in W call Msg01 ;get a character from the text table with count in w xorlw 0x00 ;XOR the data in W with 0x00 btfsc STATUS, Z ;is it a zero? return ;if so return becuase we are done call SendTx ;if not then call sendTx with the data in W incf count, f ;add 1 to count goto Message1A ;next character (repeat) SendOn: clrf count MessageOn: movf count, w ;put counter value in W call MsgOn ;get a character from the text table xorlw 0x00 ;XOR the data in W with 0x00 btfsc STATUS, Z ;is it a zero? return ;if so return becuase we are done call SendTx ;if not then call sendTx with the data in W incf count, f ;add 1 to count goto MessageOn SendOff: clrf count MessageOff: movf count, w ;put counter value in W call MsgOff ;get a character from the text table xorlw 0x00 ;XOR the data in W with 0x00 btfsc STATUS, Z ;is it a zero? return ;if so return becuase we are done call SendTx ;if not then call sendTx with the data in W incf count, f ;add 1 to count goto MessageOff MsgOn: addwf PCL, f ;add the data in W to the Program Counter (PCL) and return with new data retlw 'O' retlw 'n' retlw ' ' retlw 0x0A ;send LF retlw 0x0D ;send CR retlw 0x00 ;Send our 0x00 which is the end MsgOff: addwf PCL, f ;add the data in W to the Program Counter (PCL) and return with new data retlw 'O' retlw 'f' retlw 'f' retlw 0x0A ;send LF retlw 0x0D ;send CR retlw 0x00 ;Send our 0x00 which is the end Msg01: addwf PCL, f ;add the data in W to the Program Counter (PCL) and return with new data retlw 'P' retlw 'r' retlw 'e' retlw 's' retlw 's' retlw ' ' retlw '1' retlw '=' retlw 'O' retlw 'N' retlw ' ' retlw '0' retlw '=' retlw 'O' retlw 'F' retlw 'F' retlw 0x0A ;send LF retlw 0x0D ;send CR retlw 0x00 ;Send our 0x00 which is the end MyBank0: ;Datasheet states to goto bank 0 you would: bcf STATUS,RP0 ;Clear RP0 bcf STATUS,RP1 ;Clear RP1 return ;return back to where i was called MyBank1: ;Datasheet states to goto bank 1 you would: bsf STATUS,RP0 ;Set RP0 bcf STATUS,RP1 ;Clear RP1 return ;return back to where i was called MyBank2: ;Datasheet states to goto bank 2 you would: bcf STATUS,RP0 ;Clear RP0 bsf STATUS,RP1 ;Set RP1 return ;return back to where i was called MyBank3: ;Datasheet states to goto bank 3 you would: bsf STATUS,RP0 ;Set RP0 bsf STATUS,RP1 ;Set RP1 return ;return back to where i was called Delay ;1mS Delay movlw 0xC6 movwf d1 movlw 0x01 movwf d2 Delay_0 decfsz d1, f goto $+2 decfsz d2, f goto Delay_0 goto $+1 nop return END