Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Help needed for pic16f628a

Status
Not open for further replies.

jayalath

New Member
Dear all,
I am very new to Pics and so much happy to learn.recently I started with pic16f628a.And when I browse internet found a code that toggles
four LEDs with four switches.each LED has it's own switch.The code works fine.

;************************************************* ***************
;* Name of Project: Experiment 4 *
;* 4 push-buttons and 4 LEDs *
;************************************************* ***************

list P = 16F628 ;microcontroller identity
; e.g: 0x033 = hex value

__Config 3F18h

;************************************************* ***************
;Equates
;************************************************* ***************
status equ 0x03
cmcon equ 0x1F
rp1 equ 0x06 ;this is bit 6 in file 03
rp0 equ 0x05 ;this is bit 5 in file 03
portA equ 0x05 ;file 5 is Port A
portB equ 0x06 ;file 6 is Port B
flags equ 0x30 ;flag file
decrA equ 0x20 ;file to decrement
decrB equ 0x21 ;file to decrement


;************************************************* ***************
;Beginning of program
;************************************************* ***************
reset: org 0x00 ;reset vector address
goto SetUp ;goto set-up
nop
nop
nop
org 4 ;interrupts go to this location
goto isr1 ;goto to Interrupt Routine - not used
;isr1 must be written at address 004
; otherwise bcf status,rp1 will be
; placed at address 01 by assembler!

;************************************************* ***************
;* Port A and B initialisation *
;************************************************* ***************

SetUp bcf status,rp1 ;select bank 1 (must be = 0)
bsf status,rp0 ; also to select bank 1

movlw 0xFF ;make all Port A inputs
movwf portA
movlw 0x0F ; out out out out in in in in
movwf portB ;

bcf status,rp0 ;select programming area - bank0
movlw 0x07 ;turn comparators off and enable
movwf cmcon ; pins for I/O functions
goto Main

;************************************************* ***************
;* Interrupt Service Routine will go here (not used) *
;************************************************* ***************
isr1

;************************************************* ***************
;Delay sub-routine - approx 130mS
;************************************************* ***************
delay movlw 0x80 ;load 80h into w
movwf decrA ;shift 80h into the file for decrementing
delx nop
decfsz decrB,1 ;decrement the file
goto delx
decfsz decrA,1 ;decrement the file
goto delx
retlw 0x00 ;return

;************************************************* ***************
;* Main *
;************************************************* ***************

Main clrf flags
MainA call delay ;1/10 second delay
btfsc portB,0 ;file06, input bit 0 = red button pushed?
goto Main1 ;Yes
btfsc portB,1 ;file06, input bit 1 = green button pushed?
goto Main2 ;Yes
btfsc portB,2 ;file06, input bit 2 = yellow button pushed?
goto Main3 ;Yes
btfsc portB,3 ;file06, input bit 3 = orange button pushed?
goto Main4 ;Yes
clrf flags ;clear flag file
goto MainA ;No. Loop again

Main1 btfsc flags,0 ;test red flag
goto MainA ;return if flag is SET
movlw 0x10 ;set the bit for the red LED
xorwf portB,1 ;put answer into file 06 to toggle LED
bsf flags,0 ;set the red flag
goto MainA

Main2 btfsc flags,1 ;test green flag
goto MainA ;return if flag is SET
movlw 0x20 ;set the bit for the green LED
xorwf portB,1 ;put answer into file 06 to toggle LED
bsf flags,1 ;set the green flag
goto MainA

Main3 btfsc flags,2 ;test yellow flag
goto MainA ;return if flag is SET
movlw 0x40 ;set the bit for the yellow LED
xorwf portB,1 ;put answer into file 06 to toggle LED
bsf flags,2 ;set the yellow flag
goto MainA

Main4 btfsc flags,3 ;test orange flag
goto MainA ;return if flag is SET
movlw 0x80 ;set the bit for the orange LED
xorwf portB,1 ;put answer into file 06 to toggle LED
bsf flags,3 ;set the orange flag
goto MainA

END

Now Can anybody to help me to devolop this code to be able to go for six LEDs with six switches with toggle action and additional switch that can be pushed onece and all out puts go zero and when again pressed all out puts go high.

randy963@yahoo.com
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top