Skip to content

Instantly share code, notes, and snippets.

@ScienceElectronicsFun
Created July 5, 2021 02:23
Show Gist options
  • Save ScienceElectronicsFun/fc68753311d5008b7ae7401c67d91a68 to your computer and use it in GitHub Desktop.
Save ScienceElectronicsFun/fc68753311d5008b7ae7401c67d91a68 to your computer and use it in GitHub Desktop.
Adafruit RGB LED Driver for C8051F410 (SiLabs)
; Used Tom C. Hayes codes as templates (bit flip.a51, serial message receive.a51, int_inc_dec.a51)
; from Learning the Art of Electronics
$NOSYMBOLS ; keeps listing short..
$INCLUDE (C:\MICRO\8051\RAISON\INC\c8051f410.inc)
$INCLUDE (C:\MICRO\8051\RAISON\INC\VECTORS320.INC)
CLOCK EQU P0.0
LATCH EQU P0.1
TIMER96K EQU 96h
ORG 0 ; tells assembler the address at which to place this code
SJMP STARTUP ; here code begins--with just a jump to start of
; real program. ALL our programs will start thus
ORG 80h ; ...and here the program starts
STARTUP:
CLR 0
CLR 1
CLR 2
CLR 3
ACALL USUAL_SETUP
ACALL TIMER_SETUP
ACALL SERIAL_SETUP
ACALL INTERRUPT_SETUP
SETB LATCH ;LATCH IS ACTIVE LOW
CLR CLOCK
RESET:
MOV R3, #00h ; This holds the current byte
MOV P2, #00h ; ADDRESS
MOV R2, #00h ; ADDRESS COUNTER
MOV P1, #00h ; port 1 = GGXXBBRR
MOV R1, #00h; increment for serial
MOV DPTR, #0000h ; initialize the data pointer
;SCRATCH REGISTERS USED
;R0 CLOCK PULSES
;R1 INCREMENT FOR SERIAL DATA INPUT
;R2 ADDRESS COUNTER (LINE ON DISPLAY)
;R3 BYTE INDEX FOR DISPALY
MAIN_LOOP:
JNB RI0, REFRESH_DISPLAY ; await receipt of a byte from UART
CLR RI0 ; ...clear flag when it's asserted
MOV A, SBUF0
CJNE A, #10h, CHECK_NEXT
SJMP RESET
CHECK_NEXT:
CJNE A, #11h, STORE_IT ; PROGRAM REQUEST KEY STATUS
MOV R7, #00h
JNB 0, CHECK_UP_DIR
MOV R7, #02h
CLR 0 ; CLEAR THE FLAG
SJMP SEND_COMMAND
;MOV R7, #41h
CHECK_UP_DIR:
JNB 1, CHECK_ROTATE
MOV R7, #04h
CLR 1
SJMP SEND_COMMAND
CHECK_ROTATE:
JNB 2, CHECK_DROP
MOV R7, #08h
CLR 2
SJMP SEND_COMMAND
CHECK_DROP:
JNB 3, SEND_COMMAND
MOV R7, #10h
CLR 3
SEND_COMMAND:
ACALL SENDIT
SJMP RESET
;SJMP REFRESH_DISPLAY
STORE_IT:
MOVX @R1, A ; get received data
INC R1
REFRESH_DISPLAY:
CLR LATCH
MOV R0, #20h ; R0 holds clock pulses, EACH ADDRESS 4 bytes of clocks (32)
PULSES:
;CLOCK IN PULSES TO R1,2/G1,2/B1,2
ACALL TICKTOCKBYTE
;FOUR BYTES OF PULSES CLOCKED IN TO CURRENT ADDRESS
DJNZ R0, PULSES
;LATCH DATA FROM ADDRESS
SETB LATCH
CLR LATCH
;INCREMENT ADDRESS
INC P2
INC R2
;ALL ADDRESSES COMPLETE?
CJNE R2, #08, REFRESH_DISPLAY ; NO KEEP GOING
;YES, SET ADDRESS BACK TO 0
MOV P2, #00h
MOV R2, #00h
;ANOTHER ROUND
SJMP MAIN_LOOP
USUAL_SETUP:
ANL PCA0MD, #NOT(040h) ; Clear Watchdog Enable bit
MOV OSCICN, #087h
MOV XBR1, #40h ; Enable Crossbar
MOV P0MDOUT, #0DFh
MOV P1MDOUT, #0FFh
MOV P2MDOUT, #0FFh
MOV XBR0, #01h
RET
TIMER_SETUP:
;mov TCON, #040h
MOV TMOD, #20h ; Timer 1: Mode 2 (8-bit reload) (Dallas sample pgm, p. 299)
MOV TH1, #96h ; Reload for 9600, if SMOD = 0 (table, p. 296, again)
;MOV TL1, #TIMER96K ; This is for first-pass only
MOV TL1, #00h
ANL (PCON), #07Fh ; clear SMOD bit (this is default, but let's be thorough):
; prevents baud-rate doubling at Serial Port 0 (p. 21)
RET
SERIAL_SETUP:
mov SCON0, #10h ; set receive enable, 8-bit rather than "9-bit" or what the Dallas pgm calls 10-bit
SETB TR1 ; Start Timer
ret
SENDIT:
MOV SBUF0, R7 ; Put character in buffer (send it)--UART 1
LINGER: JNB TI0, LINGER ; wait here till told it's been sent
CLR TI0
RET
TICKTOCK:
SETB CLOCK
CLR CLOCK
RET
TICKTOCKBYTE:
ACALL GETCODE
MOV P1, A
SETB CLOCK
CLR CLOCK
INC R3
DONE:
RET
; SUBROUTINE GETCODE
; LOAD R3 WITH INDEX OF CODE YOU WANT (0 INDEXED)
; BYTE RETURNS IN ACCUMULATOR
GETCODE:
MOV DPL, R3
MOVX A, @DPTR
RET
INTERRUPT_SETUP:
SETB IT0
;SETB IT1
MOV IT01CF, #02h ; SETS INT0* on P0.2
SETB EX0
;SETB EX1
SETB EA
RET
;BUTTON PUSHED MOVE BLOCK LEFT OR RIGHT
ORG INT0VECTOR
SJMP ISR0
ISR0:
JNB P0.3, BUTTON_A_PRESS
JNB P0.6, BUTTON_B_PRESS
JNB P0.7, BUTTON_C_PRESS
;MUST BE BUTTON_D
SETB 3
RETI
BUTTON_A_PRESS:
SETB 0
RETI
BUTTON_B_PRESS:
SETB 1
RETI
BUTTON_C_PRESS:
SETB 2
RETI
END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment