Knight Rider - LED experiment with a PIC16F628A

 

The LEDs light up in a sequence that resembles KITT's lights. The code is self-explanatory, but you might read about the rrf and rlf instructions in the datasheet. Note that the Carry bit is set before the rrf instruction is executed.

  Schematic (png)

  Hex file (hex)

;-----------------------------------------------
; 'Knight-Rider': LED experiment with PIC16F628A
; www.eeng.biz/kr.htm
; Revision: July 29th, 2007
; Assembled with MPASM v5.11
;-----------------------------------------------

list      p=16f628A ; list directive to define processor
  #include <p16F628A.inc> ; 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_ON & INTOSC_OSC_NOCLKOUT 
 
	; '__CONFIG' directive is used to embed configuration word within .asm file.
; The lables following the directive are located in the respective .inc file.
; See data sheet for additional information on configuration word settings.
  
;***** VARIABLE DEFINITIONS 
	cblock  0x20
v1
v2
endc
  
;**********************************************************************
org     0x000 ; processor reset vector
goto    main ; go to beginning of program
 
main
movlw 0x07 
movwf CMCON ; turn comparators off 
clrf PORTB ; clear PORTB
bsf  STATUS, RP0
clrf TRISB ; all PORTB as output
bcf  STATUS, RP0
	bsf  STATUS, C		; set carry
right
rrf  PORTB, f ; rotate right (through carry)
call delay ; delay 
btfss PORTB, 0 ; check if RB0 has been reached
goto right
left
rlf  PORTB, f ; rotate left
call delay ; delay 
btfss PORTB, 7 ; check if RB7 has been reached
goto left
goto right

delay ; 99993 cycles        
movlw 0x1E
movwf v1
movlw 0x4F
movwf v2
delay_0
decfsz v1, f
goto $+2
decfsz v2, f
goto delay_0
	return 
  
	end			; directive 'end of program' 
  
 

 

 
 

 

Si declina ogni responsabilità per errori ed inesattezze contenute in questa documentazione e per danni di qualsiasi natura derivanti dall'uso dei circuiti e dei programmi descritti. Nomi e marchi citati appartengono ai rispettivi proprietari.