PIC as 8-bit counter

Basrad

Member
I need an 8-bit counter asap, so i thought, Why not use a 18M??

Was thinking if i made a program with 8 stages,
B:
if pin1 = 1 let pins= 11111110
goto B
C:
if pin1 = 1 let pins= 11111101
goto C

It seems to work, but at slow freqs say 5Hz to 20Hz, it seems to be missing stages??

Is there a better command to use for this?
 

Attachments

westaust55

Moderator
Is it a counter or shift register that you are trying to achieve ?

Looking at the .bas file you attached, the "0" seems to just ripple through from bit 0 to bit 7.

Maybe some clarification (better description) would help us understand you requirement

Here is some fundamental code examples for counters and a shift register. Niether checks for overflow.

Counter

Code:
init:
	b0 = 0
main:
	IF pin1 = 1 THEN
  	  INC b0
	ENDIF
	GOTO main

Shift Register
Code:
init:
	b0 = 0
main:
	IF pin1 = 1 THEN
	  b0 = b0 * 2 OR 1
	ENDIF
	GOTO main
 
Last edited:

Basrad

Member
Thats right, im looking for a ripple to power LED banks on and off for multiplexing..

so thats a shift register then?

Why is it the code i've made doesnt work?

Its like sometimes, 1 high pluse is trigering many stages...
 

knight

Member
okay, remember that your picaxe is running at or greater than 4MHz, and the code is executing faster than you can move. IE: your pressing the button, but before you've lifted your finger its already got to the next check if the button is being pressed.

You have several options to deal with this.
The first, simpliest and easiest is to simply add a pause statement after each of the let pins lines.
This will mean that the code stops running long enough for you to remove your finger.

The other method you can use would be to write a seperate routine that keeps track of where you are up to, the state of the switch and updates the display only once every time the switch is pressed.

the code would be something like this:

Code:
updateDisp:
	b0 = b0 * 2 OR 1
        let pins = b0
        previousState = 1
	goto checkInput

checkInput:
        if pin1 = 1 and previousState = 0 then updateDisp
        if pin1 = 0 and previousState = 1 then previousState = 0
        goto checkInput
Disclaimer is that this code was constructed in 5 minutes at midnight, i'm tired and it hasn't been tested, but that should be what you are looking for.

It continually checks the input, when the button is pressed it updates the display, but also updates a variable to indicate that the button has recently been pressed.
Once returing to the check input routine it then will not accept another button push until the button has been released. When it determines that the button has been released it resets the previous state variable and the system is ready to go again.
 

hippy

Ex-Staff (retired)
As knight notes it's probably that you get a lot of high-speed changes as soon as you push the button and hold it.

Rather than looking fo a 'state' change it may be simpler to look for signal going high and signal going low ...

Do
Do : Loop Until pin1 = 1
-- update pins --
Do : Loop Until pin1 = 0
Loop
 
Top