Binary Count - PIC16F684 Programming in Assembly

Thread Starter

Glitchezz

Joined Apr 26, 2012
3
Hello,

I have a university project due soon that requires me to count from 0 - 7 in binary.

I'm using the Microchip PICKit1 Flash Starter Circuit board (please see image).

http://i48.tinypic.com/k3qbr6.jpg

I understand this is a fairly simple thing to code but I can't grasp my head around it.

I know how to count in binary, I just can't code it.

I've tried several codes that just don't seem to work.
I've searched on www.mstracey.btinternet.co.uk and used the tutorials but the codes produce errors. I'm just getting frustrated now :(

I would really appreciate if someone could help me complete this and it will not go unnoticed!
 

Markd77

Joined Sep 7, 2009
2,806
The tutorials on that page are for the 16F84 so won't work on the 16F684 without a few changes.
By count in binary, do you mean output binary on 3 pins with a delay between the counts?
If you post the code you have so far and the errors that you are getting then we should be able to help fix them.
 

panic mode

Joined Oct 10, 2011
2,736
first work on the problem, then detail and eventually everything will fit together.
if you are supposed to count in binary, what exactly is counted? is it some internaly generated clock or external event?
and how to count in binary? or any numbering system? how do YOU count? then translate that into something that machine (mcu) does.

when i count, i just keep adding 1 to whatever current value is. if i have to restart, i assume that i have to start from zero. does this help?
 

atferrari

Joined Jan 6, 2004
4,767
Is your problem how to express / show the results of your counting?

Just to be sure, are you required to count one unit at a time?
 
Last edited:

Thread Starter

Glitchezz

Joined Apr 26, 2012
3
Sorry for my late reply.

When I say count in binary I mean the lights need to represent 0 - 7 in binary with a 1 second delay between each.

E.g. 7 in binary would be lights D0, D1 and D2 on.

Here is some sample code which is meant to be close to what I need to do but it doesn't seem to light the lights in the right order.

Again sorry. I'm spending today trying my best to sort this out!

Rich (BB code):
; WRITTEN BY            TA
; DATE                  28/03/11
; FILE SAVED AS         SAMPLE6.ASM
; DEVICE                PIC16F684
; OSCILLATOR            XT (4MHZ)
; WATCHDOG              DISABLED
; FUNCTION              LEDs are switched ON in a binary sequence 
;                       (THIS TEST PROGRAM IS USED IN PIC LABORATORY 1)
	list      p=16f684            ; list directive to define processor
	#include <p16f684.inc>        ; processor specific variable definitions
__CONFIG  _CP_OFF & _WDT_OFF &_PWRTE_ON & _INTRC_OSC_NOCLKOUT & _MCLRE_OFF & _CPD_OFF

;*******************************Equates****************************************
PORTA EQU 05h
COUNT1 EQU 20h; Set up two counters to count down a delay
COUNT2 EQU 21h

;*******************************Defines****************************************
; define input/output designation for LEDs (what TRISA will equal)

#define TRIS_D0_D1	B'00001111'	; TRISIO setting for D0 and D1
#define TRIS_D2_D3	B'00101011'	; TRISIO setting for D2 and D3
#define TRIS_D4_D5	B'00011011'	; TRISIO setting for D4 and D5

; define LED state (what PORTA will equal)

#define D0_ON	B'00010000'		; D0 LED
#define D1_ON	B'00100000'		; D1 LED
#define D2_ON	B'00010000'		; D2 LED

;****************************** Start of Program ******************************

	       org     0x000			   ; processor reset vector

;************* Initialize PortA **********
	       movlw	B'00111111'		; Set all I/O pins of PORTA as inputs
	       TRIS    PORTA				
	       clrf	   PORTA			   ; clear all outputs

;************** LED0 ON *********************
START		 movlw	TRIS_D0_D1      ; move predefined value to TRISA to switch ON LED0
	  TRIS	 PORTA
	  movlw	D0_ON		       ; move predefined value to PORTA TO switch ON LED0
 	  movwf	PORTA
	  decfsz   COUNT1,1       ;Subtract 1 from 00h (this gives 255 or FFh)
          goto     START          ;If COUNT is zero, carry on.
          decfsz   COUNT2,1       ;Subtract 1 from 00h
          goto     START          ;Go back to the start of our loop
;************** LED1 ON *********************
LOOP2   	movlw	D1_ON		       ; move predefined value to PORTA TO switch ON LED1
 	        movwf	PORTA
    		decfsz   COUNT1,1       ;Subtract 1 from 00h (this gives 255 or FFh)
                goto     LOOP2          ;If COUNT is zero, carry on.
                decfsz   COUNT2,1       ;Subtract 1 from 00h
                goto     LOOP2          ;Go back to the start of our loop
;************** LED0 and LED1 ON **************
LOOP3  movlw	D0_ON		       ; move predefined value to PORTA TO switch ON LED0
 	  movwf	PORTA
	  movlw	D1_ON		       ; move predefined value to PORTA TO switch ON LED1
 	  movwf	PORTA
          decfsz  COUNT1,1        ;Subtract 1 from 00h (this gives 255 or FFh)
          goto    LOOP3           ;If COUNT is zero, carry on.
          decfsz  COUNT2,1        ;Subtract 1 from 00h
          goto    LOOP3           ;Go back to the start of our loop
;************** LED2 ON *********************	
LOOP4       movlw	   TRIS_D2_D3      ; move predefined value to TRISA to switch ON LED2
	       TRIS	   PORTA
	       movlw	   D2_ON		       ; move predefined value to PORTA TO switch ON LED2
 	       movwf   PORTA
               decfsz   COUNT1,1       ;Subtract 1 from 00h (this gives 255 or FFh)
               goto     LOOP4          ;If COUNT is zero, carry on.
               decfsz   COUNT2,1       ;Subtract 1 from 00h
               goto     LOOP4          ;Go back to the start of our loop
;************** LED0 and LED2 ON **************
LOOP5       movlw	   TRIS_D0_D1       ; move predefined value to TRISA to switch ON LED0
	       TRIS	   PORTA
	       movlw	   D0_ON		        ; move predefined value to PORTA TO switch ON LED0
 	       movwf	   PORTA
	       movlw	   TRIS_D2_D3       ; move predefined value to TRISA to switch ON LED2
	       TRIS	   PORTA
	       movlw	   D2_ON		        ; move predefined value to PORTA TO switch ON LED2
 	       movwf   PORTA
 	       decfsz  COUNT1,1        ;Subtract 1 from 00h (this gives 255 or FFh)
               goto    LOOP5           ;If COUNT is zero, carry on.
               decfsz  COUNT2,1        ;Subtract 1 from 00h
               goto    LOOP5           ;Go back to the start of our loop
;************** LED1 and LED2 ON **************
LOOP6       clrf	   PORTA			   ; clear all outputs
	       movlw	   TRIS_D0_D1       ; move predefined value to TRISA to switch ON LED1
	       TRIS	   PORTA
	       movlw	   D1_ON		        ; move predefined value to PORTA TO switch ON LED1
 	       movwf   PORTA
	       movlw	  TRIS_D2_D3       ; move predefined value to TRISA to switch ON LED2
	       TRIS	  PORTA
	       movlw	  D2_ON		        ; move predefined value to PORTA TO switch ON LED2
 	       movwf	  PORTA
 	       decfsz  COUNT1,1        ;Subtract 1 from 00h (this gives 255 or FFh)
               goto    LOOP6           ;If COUNT is zero, carry on.
               decfsz  COUNT2,1        ;Subtract 1 from 00h
               goto    LOOP6           ;Go back to the start of our loop
;************** LED0,LED1 and LED2 ON **************
LOOP7       movlw	   TRIS_D0_D1       ; move predefined value to TRISA to switch ON LED0 and LED1
	       TRIS	   PORTA
	       movlw	   D0_ON		        ; move predefined value to PORTA TO switch ON LED0
 	       movwf	   PORTA
	       movlw	   D1_ON		        ; move predefined value to PORTA TO switch ON LED1
 	       movwf	   PORTA
	       movlw	   TRIS_D2_D3       ; move predefined value to TRISA to switch ON LED2
	       TRIS	   PORTA
	       movlw	   D2_ON		        ; move predefined value to PORTA TO switch ON LED2
 	       movwf	   PORTA
 	       decfsz  COUNT1,1        ;Subtract 1 from 00h (this gives 255 or FFh)
               goto    LOOP7           ;If COUNT is zero, carry on.
               decfsz  COUNT2,1        ;Subtract 1 from 00h
               goto    LOOP7           ;Go back to the start of our loop

;**********End of program *****************

          goto    START			  ; indefinite loop
	            END						  ; End instruction is needed by some compilers and also useful in case we miss goto instruction

;*********************************************************************************
 
Last edited:

Markd77

Joined Sep 7, 2009
2,806
I'm confused, are the LEDs wired in an unusual way that requires TRISA to be changed?
If so could you describe it?
If the LEDs were on 3 pins they could be set to output and left that way.
I thought "TRIS PORTA" wouldn't work on the PIC16F684, but I just checked in the simulator and apparently it does. It's not the recommended method but only because it isn't compatible with other PICs.
 

Thread Starter

Glitchezz

Joined Apr 26, 2012
3
I'm confused, are the LEDs wired in an unusual way that requires TRISA to be changed?
If so could you describe it?
If the LEDs were on 3 pins they could be set to output and left that way.
I thought "TRIS PORTA" wouldn't work on the PIC16F684, but I just checked in the simulator and apparently it does. It's not the recommended method but only because it isn't compatible with other PICs.
Yes. According to the poorly explained tutorials that our university supply. The 8 LEDs are not connected in a straight forward way.

"Connecting 8 LEDs to four pins means that connections are not very straight forward."




This results in following settings for TRISA and PORTA
B'00001111' ; TRISA setting for D0 and D1
B'00101011' ; TRISA setting for D2 and D3
B'00011011' ; TRISA setting for D4 and D5
B'00111001' ; TRISA setting for D6 and D7

Data sent to PORTA to turn on corresponding LED
B'00010000' ; D0 LED ON
B'00100000' ; D1 LED ON
B'00010000' ; D2 LED ON
B'00000100' ; D3 LED ON
B'00100000' ; D4 LED ON
B'00000100' ; D5 LED ON
B'00000100' ; D6 LED ON
B'00000010' ; D7 LED ON
 

Markd77

Joined Sep 7, 2009
2,806
Ah, Charlieplexing.
A feature of that is that only one LED is on at a given time. To give the impression of more than one lit, you have to alternately turn them on at a fairly fast rate (1ms per LED would look good). In your code where you aim to turn two on, what actually happens is the first one is turned on for only a few microseconds, then the other is turned on for the rest of the period.
It's more complicated than you need and it's for a different PIC, but if you click on my name, then blog, you should find something that might give you a few ideas.
 
Top