button triggered up/down counting question

Thread Starter

turningjapanese

Joined Apr 25, 2009
3
Hi all,

This is my first post and i would like to tap into your wealth of knowledge. I did some PIC programming about 10 years ago as part of my Product Design degree though it was only ever meant to be a basic grounding.

I have a velleman p8048 programming board and i write my code in assembler language in notepad.

I am looking to write a program that uses 2 momentary switch inputs to control 4 led outputs. What i would like the buttons to do is to act as up and down scroll buttons.

I would like the 1st led to come on when the system is powered up, then when the scroll up button is pressed, the 1st and second led come on, and so on until all 4 leds are on. When all four are on i want the system to stay in that state until the scroll down button is pressed. (not to run over or loop around to 1 again)

i want to be able to go up and down through the order using the buttons.

e.g. 1 /\ 2 /\ 3 \/ 2 /\ 3 /\ 4 \/ 3 etc etc

i hope this make sense.

oh btw i am looking to use a 16F627-04/p pic


I was thinking of using a subrouting to control the LED's

SCROLL ADDWF GR
RETLW B'00000001' ;1 LED ON
RETLW B'00000011' ;2 LED ON
RETLW B'00000111' ;3 LED ON
RETLW B'00001111' ;4 LED ON

I am however unsure as to how to get the button triggered up down counting code working.

Thanks in Advance

Stacy
 

Thread Starter

turningjapanese

Joined Apr 25, 2009
3
thanks for that, will build a very simple board and program and then develop it from there.


Will probably be posting again when i get stuck but at least there will be some proper code to talk about.
 

Thread Starter

turningjapanese

Joined Apr 25, 2009
3
Hi,

I've modified a program tah came with the programmer kit, i am now using a 16F84A chip. I seem to be missing something, I have created a file called COUNT, i have managed to get it incrementing on 1 button push and resetting to zero when it gets to 3 but when i tried to modify it to down count on a second button it all seemed to go wrong.

Also unsure as to how to get my counter to stop at 3, ignoring any future count up button presses. I am also struggling to get it to stop at 0 on down count and only count up from there.

Anyway here is my code in full, hope someone can make sense of my beginner programming.

Thanks in Advance

Stacy

;**************************************************************************
;* K8048 Microchip PIC(tm) Programmer & experiment board *
;**************************************************************************
;* (C) VELLEMAN Components,2003 All rights reserved *
;**************************************************************************
;* Hardw. Rev: P8048'1 Softw. Rev: SJ002 *
;* OSC.......: XT 4MHz Max. POWER.....: 15V DC *
;**************************************************************************
;==========================================================================
;
; Configuration Bits
;
;==========================================================================
_CP_ON EQU H'000F'
_CP_OFF EQU H'3FFF'
_PWRTE_ON EQU H'3FF7'
_PWRTE_OFF EQU H'3FFF'
_WDT_ON EQU H'3FFF'
_WDT_OFF EQU H'3FFB'
_LP_OSC EQU H'3FFC'
_XT_OSC EQU H'3FFD'
_HS_OSC EQU H'3FFE'
_RC_OSC EQU H'3FFF'
__CONFIG _CP_OFF & _PWRTE_ON & _WDT_OFF & _XT_OSC
;==========================================================================
;
; Register Definitions
;
;==========================================================================
W EQU H'0000'
F EQU H'0001'
;----- Register Files------------------------------------------------------
INDF EQU H'0000'
TMR0 EQU H'0001'
PCL EQU H'0002'
STATUS EQU H'0003'
FSR EQU H'0004'
PORTA EQU H'0005'
PORTB EQU H'0006'
EEDATA EQU H'0008'
EEADR EQU H'0009'
PCLATH EQU H'000A'
INTCON EQU H'000B'
OPTION_REG EQU H'0081'
TRISA EQU H'0085'
TRISB EQU H'0086'
EECON1 EQU H'0088'
EECON2 EQU H'0089'
;----- STATUS Bits --------------------------------------------------------
IRP EQU H'0007'
RP1 EQU H'0006'
RP0 EQU H'0005'
NOT_TO EQU H'0004'
NOT_PD EQU H'0003'
Z EQU H'0002'
DC EQU H'0001'
C EQU H'0000'
;----- INTCON Bits --------------------------------------------------------
GIE EQU H'0007'
EEIE EQU H'0006'
T0IE EQU H'0005'
INTE EQU H'0004'
RBIE EQU H'0003'
T0IF EQU H'0002'
INTF EQU H'0001'
RBIF EQU H'0000'
;----- OPTION_REG Bits ----------------------------------------------------
NOT_RBPU EQU H'0007'
INTEDG EQU H'0006'
T0CS EQU H'0005'
T0SE EQU H'0004'
PSA EQU H'0003'
PS2 EQU H'0002'
PS1 EQU H'0001'
PS0 EQU H'0000'
;----- EECON1 Bits --------------------------------------------------------
EEIF EQU H'0004'
WRERR EQU H'0003'
WREN EQU H'0002'
WR EQU H'0001'
RD EQU H'0000'
;==========================================================================
;
; RAM Definition
;
;==========================================================================
__MAXRAM H'CF'
__BADRAM H'07', H'50'-H'7F', H'87'

;==========================================================================
;
; EQUATES Section
;
;==========================================================================

TMR0 EQU 1 ;means TMR0 is file 1.
PC EQU 2 ;program counter is file2
STATUS EQU 3 ;means STATUS is file 3.
PORTA EQU 5 ;PORTA is file 5
PORTB EQU 6 ;PORTB is file 6
ZEROBIT EQU 2 ;ZEROBIT is file 2
COUNT EQU 0CH ;COUNT is file 0C hex

;==========================================================================
;
; Variables
;
;==========================================================================
TIMER1 EQU H'0D' ;Timer 1, used for general delay.
TIMER2 EQU H'0E' ;Timer 2, used for delay !
;==========================================================================
; PORTA & PORTB Bits.
;==========================================================================
;INPUTS
SW1 EQU H'00' ;SW1 is triggering RA0
SW2 EQU H'01' ;SW2 is triggering RA1
SW3 EQU H'02' ;SW3 is triggering RA2
SW4 EQU H'03' ;SW4 is triggering RA3
;OUTPUTS
LD1 EQU H'00' ;LD1 is connected to RB0
LD2 EQU H'01' ;LD2 is connected to RB1
LD3 EQU H'02' ;LD3 is connected to RB2
LD4 EQU H'03' ;LD4 is connected to RB3

ORG 0 ;Reset vector address for PIC16F84
GOTO RESET ;Jump to RESET routine when boot PIC.

; **************************
; * delay routine: *
; **************************
DELAY MOVLW D'50' ;Put 50 decimal in the 'TIMER1' register.
MOVWF TIMER1
;
DELAY2 MOVLW D'50' ;Put 50 decimal in the 'TIMER2' register.
MOVWF TIMER2
DECFSZ TIMER2,F ;Timer2 = Timer2 -1, skip next instruction if
Timer2 = 0.
GOTO $-1 ;Jump back 1 instruction.

DECFSZ TIMER1,F ;Timer1 = Timer1 - 1, skip next instruction if
Timer1 = 0
GOTO DELAY2 ;Jump to 'DELAY2' routine
RETLW 0 ;Return (jump back to main) and load W with 0.
; **************************
; * PRESS1 routine: *
; **************************
PRESS1 INCF COUNT ;Increment count file +1
MOVF COUNT,W ;Move count file into working reg.
SUBLW .4 ;Subtract w from .4
BTFSC STATUS,ZEROBIT ;if 4-w=0 zerobit will be set so skip next
CLRF COUNT ;clear count, reset count to 0 again
MOVF COUNT,W ;Move count into working reg
CALL CONVERT ;Use convert table to turn count into output
MOVWF PORTB ;Send convert table output to portB
RELEASE1 BTFSS PORTA,SW1 ;Test SW1 to see if it has been released
GOTO RELEASE1 ;loop to check for release again
CALL DELAY ;delay
GOTO START ;goto START to begin testing again
; **************************
; * PRESS2 routine: *
; **************************
PRESS2 DECF COUNT
MOVF COUNT,W
CALL CONVERT
MOVWF PORTB
RELEASE2 BTFSS PORTA,SW2
GOTO RELEASE2
CALL DELAY
GOTO START

CONVERT ADDWF PC
RETLW B'00000001' ;0
RETLW B'00000010' ;1
RETLW B'00000100' ;2
RETLW B'00001000' ;3


; **************************
; * main routine: *
; **************************
RESET

BSF STATUS,RP0 ;Switch to program memory bank 1 of PIC
MOVLW B'00001111' ;All available PORTA I/O are set as inputs.
MOVWF TRISA ;Config here PORTA
MOVLW B'11000000' ;RB0...RB5 are outputs, RB6&7 are set as input.
MOVWF TRISB ;RB6&7 are used by K8048 for programming PIC.
MOVLW B'00000111' ;Prescaler is /256
OPTION ;TIMER is 1/32 secs.

BCF STATUS,RP0 ;Switch back to program memory bank 0 of PIC.
CLRF PORTA ;Clear PORTA
CLRF PORTB ;Clear all PORTB outputs before start mainloop.
CLRF COUNT ;Clear COUNT

START BTFSC PORTA,SW1 ;Test SW1, inc count if pressed else test sw2
GOTO PRESS1 ;Goto PRESS1
CALL DELAY ;
BTFSC PORTA,SW2 ;Test SW2, decrease count by 1 if pressed
GOTO PRESS2 ;
CALL DELAY ;
GOTO START ;Return to start

END
 

DonQ

Joined May 6, 2009
321
Did you start with a flow-chart, pseudo-code, or some other way of documenting what you wanted?

A pseudo-code version of what you want is:

IF (count < 3 AND UpButtonPressed) THEN count = count+1;

... then do the equivalent for down counting.

Implement this in assembly and it will work.
 
Last edited:
Top