TITLE "rgb.asm: Color-Checker with RS232 interface" LIST p=16C71 ERRORLEVEL 0, -302 ;IBM-PC interface: 9-pin D-shell connector f. ;(2) rxd B1 direct to host (no RS232 driver) ;(3) txd from host over 10k to B0 (no RS232 receiver) ;(4) dtr from host back to (1) dcd, (6) dsr and (8) cts ;(5) common ground ;(7) rts from host cleared for +12V power supply (buffered 1000uF) ;(9) ring indicator left unconnected ; red, green and blue led at B4, 5/6, 7, resistors to VCC in series ; photo diode at vcc, grounded by 330k, over source follower to A0 ; xtal 3.6864MHz RTCC EQU 1 PC EQU 2 STATUS EQU 3 PORTB EQU 6 ADCON0 EQU H'08' ADRES EQU H'09' PCLATH EQU H'0A' INTCON EQU H'0B' _OPTION EQU H'81' ; OPTION register in page 1 _TRISA EQU H'85' ; TRISA in page 1 _TRISB EQU H'86' ; TRISB in page 1 _ADCON1 EQU H'88' ; ADCON1 in page 1 C EQU 0 DC EQU 1 Z EQU 2 P EQU 5 rxavl EQU 7 ; rx data available (IRP bit in STATUS) tmp0 EQU H'0C' tmp1 EQU H'0D' tmp2 EQU H'0E' tmp3 EQU H'0F' sav0 EQU H'10' sav1 EQU H'11' rxdata EQU H'12' ; receive data rxshft EQU H'13' ; receiver shift register bitcnt EQU H'14' ; receiver bit counter sum0 EQU H'15' sum1 EQU H'16' ORG 0 ; reset NOP NOP GOTO main ORG 4 ; interrupt GOTO intserv ORG 8 intserv MOVWF sav0 ; save working register MOVF STATUS,W ; save status register MOVWF sav1 BCF STATUS,P ; set page 0 MOVF bitcnt,F ; bitcounter 0? (startbit) BTFSS STATUS,Z GOTO datab startb MOVLW -D'130' ; rtc interrupt in middle of first bit MOVWF RTCC CLRF INTCON ; clear all flags and enable-bits BSF INTCON,5 ; enable rtc interrupt INCF bitcnt,F GOTO intend datab RRF rxshft,F ; shift in new bit BCF rxshft,7 BTFSS PORTB,0 ; poll rx-line BSF rxshft,7 BTFSC bitcnt,3 ; test if this was the eight bit GOTO lastbit INCF bitcnt,F ; prepare polling for next bit MOVLW -D'93' ; tmr0-(cycles-3) ADDWF RTCC,F CLRF INTCON ; clear all flags and enable-bits BSF INTCON,5 ; enable rtc interrupt GOTO intend lastbit CLRF bitcnt MOVF rxshft,W ; copy to data register MOVWF rxdata BSF sav1,rxavl ; status is restored from sav1 CLRF INTCON ; clear all flags and enable-bits BSF INTCON,4 ; enable int for next startbit intend MOVF sav1,W ; restore status register MOVWF STATUS MOVF sav0,W ; restore working register BCF STATUS,Z BTFSC sav1,Z ; restore zero bit BSF STATUS,Z RETFIE ; program memory up to 10D destroyed while debugging ORG H'10D' dly100u MOVLW D'29' ; [3] delay about 100us MOVWF tmp0 ; [4] dly100x DECFSZ tmp0,F ; [29 * 3 -1] GOTO dly100x ; RETLW 0 ; [93] ORG H'124' txchar BSF PORTB,1 ; [1] startbit MOVWF tmp2 ; [2] save char MOVLW H'08' ; [3] bitcounter MOVWF tmp1 ; [4] MOVLW D'29' ; [5] startbit delay MOVWF tmp0 ; [6] txchara DECFSZ tmp0,F ; [29 * 3 - 1] GOTO txchara ; [92] txcharb RRF tmp2,F ; [93] data bit BTFSS STATUS,C ; [94] GOTO txchar0 ; [96] NOP ; [96] txchar1 BCF PORTB,1 ; [1] 1 bit GOTO txcharc ; [3] txchar0 BSF PORTB,1 ; [1] 0 bit GOTO txcharc ; [3] txcharc MOVLW D'28' ; [4] databit delay MOVWF tmp0 ; [5] txchard DECFSZ tmp0,F ; [28 * 3 - 1] GOTO txchard ; [88] NOP ; [89] DECFSZ tmp1,F ; [90] done? GOTO txcharb ; [92] GOTO txchare ; [93] txchare GOTO txcharf ; [95] txcharf NOP ; [96] BCF PORTB,1 ; [1] stopbit MOVLW D'30' ; [2] stopbit delay MOVWF tmp0 ; [3] txcharg DECFSZ tmp0,F ; [30 * 3 - 1] GOTO txcharg ; [92] RETLW 0 ; [94] ORG H'160' ; main MOVLW H'F0' ; all leds off MOVWF PORTB BSF STATUS,P ; set page 1 MOVLW B'11011111' ; int on rising edge, no prescaler for rtc MOVWF _OPTION MOVLW B'00011111' ; all inputs MOVWF _TRISA MOVLW B'00000001' ; 0 data input, others outputs MOVWF _TRISB MOVLW B'00000010' ; 1,0 analog inputs MOVWF _ADCON1 BCF STATUS,P ; set page 0 MOVLW B'11101001' ; internal osc, channel 1, power on MOVWF ADCON0 CLRF bitcnt MOVLW B'10010000' ; int enable, global interrupt enable MOVWF INTCON loop BTFSS STATUS,rxavl ; wait here until char received GOTO loop MOVF rxdata,W ; get data BCF STATUS,rxavl ; reset flag tstrgb MOVWF tmp0 XORLW ' ' BTFSC STATUS,Z GOTO smplrgb tstr MOVF tmp0,W XORLW 'r' BTFSC STATUS,Z GOTO smplr tstg MOVF tmp0,W XORLW 'g' BTFSC STATUS,Z GOTO smplg tstb MOVF tmp0,W XORLW 'b' BTFSC STATUS,Z GOTO smplb GOTO loop ; command not available smplr BCF PORTB,4 ; red led on CALL sample GOTO loop smplg BCF PORTB,5 ; green led on CALL sample GOTO loop smplb BCF PORTB,6 ; blue leds on BCF PORTB,7 CALL sample GOTO loop smplrgb BCF PORTB,4 ; red led on CALL sample BCF PORTB,5 ; green led on BCF PORTB,6 ; green led on CALL sample BCF PORTB,7 ; blue led on CALL sample GOTO loop sample MOVLW D'10' ; 1 ms delay MOVWF tmp1 sampl0 CALL dly100u DECFSZ tmp1,F GOTO sampl0 CLRF sum0 CLRF sum1 MOVLW D'255' ; reserved MOVLW D'12' ; average of n samples MOVWF tmp2 sampl1 BSF ADCON0,2 ; start ad conversion sampl2 BTFSC ADCON0,2 ; wait for conversion complete GOTO sampl2 MOVF ADRES,W ; get A/D result ADDLW 0xB3 ; subtract offset for better resolution ADDWF sum0,F ; add to sum BTFSC STATUS,C INCF sum1,F DECFSZ tmp2,F GOTO sampl1 MOVLW 0xF0 ; all leds off MOVWF PORTB MOVLW D'255' ; reserved MOVLW D'3' ; div 8 MOVWF tmp2 sampl3 RRF sum0,F ; sum div 2 BCF sum0,7 RRF sum1,F BTFSC STATUS,C BSF sum0,7 BCF sum1,7 DECFSZ tmp2,F GOTO sampl3 MOVF sum0,W ; A/D result complete CALL txchar RETLW 0 END