;------------------------------------------------------------------------------- ; FILE: 3 Segment LED multiplex ; AUTH: Ed's Projects ; DATE: 23/07/2016 ; DESC: Asynchronous Serial to 3 character segment LED Display ; Note: Oscillator set to 4MHz Internal ; Commone cathode display, pnp between segments and positive, npn between ground and common. ;------------------------------------------------------------------------------- LIST P=16F628A, R=DEC ; All numbers Decimal format unless otherwise stated. include ; Define configurations, registers, etc. __config _CP_OFF & _CPD_OFF & _LVP_OFF & _BOREN_ON & _MCLRE_OFF & _PWRTE_ON & _WDTE_OFF & _INTRC_OSC_NOCLKOUT ;------------------------------------------------------------------------------- ;----------------------------- Port Definitions ------------------------------ ;------------------------------------------------------------------------------- ; PortA - Output - RA1 - Pin 18 - Segment a ; PortA - Output - RA0 - Pin 17 - Segment b ; PortA - Output - RA7 - Pin 16 - Segment c ; PortA - Output - RA6 - Pin 15 - Segment d ; PortB - Output - RB7 - Pin 13 - Segment e ; PortB - Output - RB6 - Pin 12 - Segment f ; PortB - Output - RB5 - Pin 11 - Segment g ; PortB - Output - RB4 - Pin 10 - Segment dot ; PortA - Output - RA2 - Pin 1 - Segment 1 ; PortA - Output - RA3 - Pin 2 - Segment 2 ; PortB - Output - RB0 - Pin 6 - Segment 3 ; PortB - Input - RB1 - Pin 7 - Serial Data In ;------------------------------------------------------------------------------- ;-------------------------------- Variables ---------------------------------- ;------------------------------------------------------------------------------- cblock h'20' delaya delayb w_save ; Save working register on interrupt status_save ; Save status register on interrupt segtemp ; Segment temp digit1 ; Digit 1 display digit2 ; Digit 2 display digit3 ; Digit 3 display sertemp ; Serial input value to dislay on segments tempdig ; For converting to digits orgdig ; For converting to digits endc ;------------------------------------------------------------------------------- ;----------------------------- Define Symbols -------------------------------- ;------------------------------------------------------------------------------- #define SEGA PORTA, 1 #define SEGB PORTA, 0 #define SEGC PORTA, 7 #define SEGD PORTA, 6 #define SEGE PORTB, 7 #define SEGF PORTB, 6 #define SEGG PORTB, 5 #define SEG1 PORTA, 2 #define SEG2 PORTA, 3 #define SEG3 PORTB, 0 ;------------------------------------------------------------------------------- ;------------------------------- Program Code -------------------------------- ;------------------------------------------------------------------------------- ORG 0 goto Init ;------------------------------------------------------------------------------- ;---------------------------- Interrupt Address ------------------------------ ;------------------------------------------------------------------------------- ORG 04 goto Interrupt ;------------------------------------------------------------------------------- ;-------------------------- Initialisation of Ports -------------------------- ;------------------------------------------------------------------------------- Init bsf STATUS,RP0 ; Set RP0 to 1 bcf STATUS,RP1 ; Set RP1 to 0 ; Bank 1 Selected bsf INTCON, PEIE ; Turn on peripheral Interrupts clrf VRCON ; Set Vref to off clrf TRISB ; Set all PortB as Output clrf TRISA ; Set all PortB as Output bcf STATUS,RP0 ; Set RP0 to 0 ; Bank 0 Selected movlw d'7' ; Load working register with "7" movwf CMCON ; Turn off comparators clrf PORTA clrf PORTB ;------------------------------------------------------------------------------- ;----------------- Asynchronous Serial Receive Initialisation ----------------- ;------------------------------------------------------------------------------- serialinit bsf STATUS, RP0 ; Bank 1 selected bsf TRISB, 1 ; Both serial input and output must bsf TRISB, 2 ; be set as inputs. bsf TXSTA, BRGH ; Set high baud rate movlw d'25' movwf SPBRG ; Set baud rate to 9600 bcf TXSTA, SYNC ; Set to asynchronous mode bcf STATUS, RP0 ; Set RP0 to 0 ; Bank 0 Selected bsf RCSTA, SPEN ; Enable Serial Port bsf STATUS, RP0 ; Set RP0 to 1 ; Bank 1 Selected bsf PIE1, RCIE ; Turn on Serial Receive Interrupt bcf STATUS, RP0 ; Set RP0 to 0 ; Bank 0 Selected bsf RCSTA, CREN ; Turns on receive movf RCREG, w movf RCREG, w movf RCREG, w goto Start ;------------------------------------------------------------------------------- ;------------------------------ Segment Lookup ------------------------------- ;------------------------------------------------------------------------------- Segment addwf PCL, f retlw b'00111111' ; 0 retlw b'00000110' ; 1 retlw b'01011011' ; 2 retlw b'01001111' ; 3 retlw b'01100110' ; 4 retlw b'01101101' ; 5 retlw b'01111100' ; 6 retlw b'00000111' ; 7 retlw b'01111111' ; 8 retlw b'01100111' ; 9 ;------------------------------------------------------------------------------- ;----------------------------- Convert to Ports ------------------------------ ;------------------------------------------------------------------------------- Conv bsf SEGA bsf SEGB bsf SEGC bsf SEGD bsf SEGE bsf SEGF bsf SEGG btfsc segtemp, 0 bcf SEGA btfsc segtemp, 1 bcf SEGB btfsc segtemp, 2 bcf SEGC btfsc segtemp, 3 bcf SEGD btfsc segtemp, 4 bcf SEGE btfsc segtemp, 5 bcf SEGF btfsc segtemp, 6 bcf SEGG return ;------------------------------------------------------------------------------- ;------------------------------- Main Program -------------------------------- ;------------------------------------------------------------------------------- Start bsf INTCON, GIE ; Turn on interrupts movlw d'000' call digitcharacter ;------------------------------------------------------------------------------- ;------------------------------- Display Loop -------------------------------- ;------------------------------------------------------------------------------- Loop movf digit1, w call Segment ; Converts to binary movwf segtemp bsf SEG1 call Conv ; Converts binary to port call delay bcf SEG1 movf digit2, w call Segment movwf segtemp bsf SEG2 call Conv call delay bcf SEG2 movf digit3, w call Segment movwf segtemp bsf SEG3 call Conv call delay bcf SEG3 goto Loop ;------------------------------------------------------------------------------- ;------------------------------- Delay Program -------------------------------- ;------------------------------------------------------------------------------- delay ; 5ms delay movlw d'4' movwf delaya label1 movlw d'250' movwf delayb label2 nop ;1us nop ;1us decfsz delayb, f ;1us goto label2 ;2us ;total 1250us label3 decfsz delaya, f goto label1 return ;return from call ;------------------------------------------------------------------------------- ;--------------------------------- Interrupt --------------------------------- ;------------------------------------------------------------------------------- Interrupt movwf w_save movf STATUS, w clrf STATUS movwf status_save movf RCREG, w call digitcharacter movf status_save, w movwf STATUS swapf w_save, f swapf w_save, w retfie ;------------------------------------------------------------------------------- ;---------------------- Convert variable to characters ----------------------- ;------------------------------------------------------------------------------- digitcharacter movwf tempdig ; temp variable movwf orgdig clrf digit3 clrf digit2 clrf digit1 dlabel1 movlw d'100' subwf tempdig btfss STATUS, C goto dlabel3 movlw d'100' subwf orgdig incf digit3, f goto dlabel1 dlabel3 movf orgdig, w movwf tempdig dlabel2 movlw d'10' subwf tempdig btfss STATUS, C goto dlabel4 movlw d'10' subwf orgdig incf digit2, f goto dlabel2 dlabel4 movf orgdig, w movwf digit1 return end