8 bit binary output from 14M2 - a binary timer??

radiogareth

Senior Member
Just chucking this idea out to see what people think.....

Hardware: 14M2 with potentiometer feed to C4 ADC and digital input on C3, a push to make with pullup enabled.

8 LEDs driven from b0 to b5, plus C0 and C1.

Software:
Power up, LEDs reflect BINARY value set on potentiometer (0-255)
Press button, timer starts counting minutes, LSB lights at 1 minute, etc until they are all lit = over 4 hours.
Piezo on C2 for when time is up.
Be nice if LSB could also flash 100mSec every second to show counting in taking place.

Question: How to combine ports B and C to simply relfct the contets of eg B0 (0-255).
If it was a single port then the let pins=b0 would work....
There must be a better way than a long string of if thens.....

Suggestions please....or shall I go straight to a 20M2 where the whole port is accessible on one side??

TIA
 

nick12ab

Senior Member
Code:
symbol out = b0
dirsB = %0011111
dirsC = %0000011

...

setoutputs:
	pinB.0 = bit0
	pinB.1 = bit1
	pinB.2 = bit2
	pinB.3 = bit3
	pinB.4 = bit4
	pinB.5 = bit5
	pinC.0 = bit6
	pinC.1 = bit7
	return
Use gosub setoutputs every time you want to set the outputs according to the variable out.
 

radiogareth

Senior Member
OK, I'm making progress, I have the basic bones that seems to work as I want BUT the display part does not show the values in B1. Not sure WHERE its getting them from TBH..... I'm out of my depth to know why and the 'out' is not exactly comprehensively exampled in the manual(s).

Any suggestions?

Code:
pullup %000010000000000	'turns on the pullup on c.3
sense:			'waits for you to press the button
	if pinC.3 = 0 then read_adc_value ' if you press it it goes....
	goto sense	

read_adc_value:	'to here and puts the found value into variable b0
	readadc c.4,b0
	b3=b0
	'falls through into the time counting routine
	Time_to_count_up:
	for b2=0 to 5	'should give 1 minute adjust on test
	Pause 900 'Pauses for 0.9 secs adjust for timeing correction
	high c.2	'Flashes the activity LED
	pause 90	'on for a short time
	low c.2	'and off to save battery
	next b2	'for-next loop to count one minute
	b1=b1+1
	gosub display_time	' to update the display
	if b1=b3 then goto finished_noise
	goto Time_to_count_up
	
	Display_time:'Displays the value in b1
	symbol out = b1
	dirsB = %00111111
	dirsC = %00000011
	gosub setoutputs
	return

setoutputs:
	pinB.0 = bit7	'bit layout to suit pcb 
	pinB.1 = bit6	'vertical row of LEDs
	pinB.2 = bit5	'bit 7 top from Pinb0,
	pinB.3 = bit4	'bit 0 from port c pin c0
	pinB.4 = bit3
	pinB.5 = bit2
	pinC.1 = bit1
	pinC.0 = bit0
	return
	
Finished_noise:
	Tune c.4,1,%00000111,($AA,$85,$43,$42,$40,$8A,$C5,$43,$42,$40,$8A,$C5,$43,$42,$43,$C0,$65,$65,$65,$EA,$C5,$43,$42,$40,$8A,$C5,$43,$42,$40,$8A,$C5,$43,$42,$43,$80)
	w0=0	'reset the variables!
	w1=0	'reset the variables!
 	goto sense
And HOW do you make the code scroll in a nice tidy window - I can't find it in the FAQ!!!!
 
Last edited:

hippy

Technical Support
Staff member
OK, I'm making progress, I have the basic bones that seems to work as I want BUT the display part does not show the values in B1. Not sure WHERE its getting them from TBH.....
The 'setoutputs:' routine moves 'bit0' to 'bit7' to the output pins, and those bits are part of byte variable 'b0'. Set 'b0' to the value you want displaying before calling the 'setoutputs' routine, ie, "Let b0 = b1".
 

nick12ab

Senior Member
BUT the display part does not show the values in B1.
Could that be because you changed the variable used for the display ('out') from b0 to b1 without changing the bit variables used in the sub? Try this:
Code:
symbol out = b1
	pullup %000010000000000	'turns on the pullup on c.3
	dirsB = %00111111
	dirsC = %00000011
sense:			'waits for you to press the button
	if pinC.3 = 0 then read_adc_value ' if you press it it goes....
	goto sense	

read_adc_value:	'to here and puts the found value into variable b0
	readadc c.4,b0
	b3=b0
	'falls through into the time counting routine
Time_to_count_up:
	for b2=0 to 5	'should give 1 minute adjust on test
	Pause 900 'Pauses for 0.9 secs adjust for timeing correction
	high c.2	'Flashes the activity LED
	pause 90	'on for a short time
	low c.2	'and off to save battery
	next b2	'for-next loop to count one minute
	b1=b1+1
	gosub display_time	' to update the display
	if b1=b3 then goto finished_noise
	goto Time_to_count_up
	
Display_time:'Displays the value in b1
	gosub setoutputs
	return

setoutputs:
	pinB.0 = bit15	'bit layout to suit pcb 
	pinB.1 = bit14	'vertical row of LEDs
	pinB.2 = bit13	'bit 7 top from Pinb0,
	pinB.3 = bit12	'bit 0 from port c pin c0
	pinB.4 = bit11
	pinB.5 = bit10
	pinC.1 = bit9
	pinC.0 = bit8
	return
	
Finished_noise:
	Tune c.4,1,%00000111,($AA,$85,$43,$42,$40,$8A,$C5,$43,$42,$40,$8A,$C5,$43,$42,$43,$C0,$65,$65,$65,$EA,$C5,$43,$42,$40,$8A,$C5,$43,$42,$40,$8A,$C5,$43,$42,$43,$80)
	w0=0	'reset the variables!
	w1=0	'reset the variables!
 	goto sense
 

radiogareth

Senior Member
The 'setoutputs:' routine moves 'bit0' to 'bit7' to the output pins, and those bits are part of byte variable 'b0'. Set 'b0' to the value you want displaying before calling the 'setoutputs' routine, ie, "Let b0 = b1".
Yes, that fixes it Hippy and the post afterwards by nick12ab sheds light on what I was doing wr0ng. Works nicely now....binary is fascinating to watch "filling up".

Thanks all.
 

trinity

New Member
After years of being happy with my binary timer I suddenly had a need to re-use the code.
I'm hoping to control an 8 bit RF attenuator based on this board https://www.ebay.co.uk/itm/195699736917.
I've cut out all the surplus code and am just reading the A/D and outputting the data as above.

But it looks as if the bit pattern is being placed one bit at a time,ie, with a tiny pause, as the attenuation drops to max at every change in A/D reading. Once set (and the attenuation follows the expected path in general) its stable. I'm wondering if there is a POKE command (or similar) that could dump the new bit pattern 'all in one go' or might I have to just go for an 8 bit port Picaxe?
Thoughts welcome.....for some reason I had to reset my PW and its picked up my old school account (trinity). I am however, the real radiogareth as above....

Gareth
 
Last edited:

Buzby

Senior Member
It would help if you posted your exact code.

It's impossible for anyone to tell what you've 'cut out'.
 

radiogareth

Senior Member
Indeed it would...
Code:
pullup %000100000000000    'turns on the pullup on C.3
#no_data            'saves downloading blank stuff
    
sense:'    
    readadc c.4,b0    'reads the adc on C.4
    let b0=255-b0
    'pause 100
    'debug
    gosub display_time 'shows what you have adjusted it to

    goto sense         'checks again...and again....
    
    
    Display_time:'Displays the value in b0
    
    symbol out = b0
    dirsB = %00111111
    dirsC = %00000011
    gosub setoutputs
    return

setoutputs:
    pinB.0 = bit7    'bit layout to suit pcb 
    pinB.1 = bit6    'vertical row of LEDs
    pinB.2 = bit5    'bit 7 top from Pinb0,
    pinB.3 = bit4    'bit 0 from port c pin c0
    pinB.4 = bit3
    pinB.5 = bit2
    pinC.1 = bit1
    pinC.0 = bit0
    return
 

Buzby

Senior Member
I can't see any big delays in there, but there are a couple of thing it might be.

The dirsB = %00111111 and dirsC = %00000011 should only be needed once. Move them to before the pullup command.

The setoutputs: routine is setting the MSB first and the LSB last. Although I do not think this is usually a problem, it might be with audio. I would try reversing the execution order of the lines. i.e. Put pinC.0 = bit0 first, and pinB.0 = bit7 last.

Even better, get a bigger Picaxe, wire the portC pins to attenuator pins sequentialy, .0 to .0, ... .7 to .7, then write the whole port with pinsC = b0.

Cheers,

Buzby
 

Flenser

Senior Member
I'm hoping to control an 8 bit RF attenuator based on this board https://www.ebay.co.uk/itm/195699736917
1) When I go to that link I don't see any connections for connecting the PICAXE parallel port to the attenuator board.
Pls post a picture of how you have your PICAXE connected to the attenuator module or post a picture of the circuit so we know how you have connected them.

2) I found this datasheet for the PE43703 chip PE43703-70-0245-05.pub - PE43703.pdf
If this is the right datasheet then:
- This chip has only 7 bit of attenuation, not 8, and the 7 bits are set by the switches marked 0.25 to 16. This is another reason why I would like to see how you have connected the 8 bits from the PICAXE chip to the attenuator module.
- The switch marked P/S is to choose between the parallel and serial modes that the chips supports. If you are using parallel you would need to leave it permanently set at P.

But it looks as if the bit pattern is being placed one bit at a time,ie, with a tiny pause, as the attenuation drops to max at every change in A/D reading.
- This issue is dealt with by the chip so that you don't get this attenuation drop every time you change one of the switches on the board. - The manual describes how you hold the pin marked LE low before making any changes, then you change the parallel bits, and finally you pulse the LE pin high for a minimum of 30nS to latch the new attenuation setting into the PE43703 chip .
 

hippy

Technical Support
Staff member
But it looks as if the bit pattern is being placed one bit at a time,ie, with a tiny pause
Not surprisingly if using something like-
Code:
setoutputs:
    pinB.0 = bit7
    pinB.1 = bit6
    pinB.2 = bit5
    pinB.3 = bit4
    pinB.4 = bit3
    pinB.5 = bit2
    pinC.1 = bit1
    pinC.0 = bit0
    return
There will be a delay between setting each individual bit of output.

Things can be improved by writing whole bytes at a time, for example 'outpinsB = b0', but you will still have some delay between bits being set if your outputs are spread across multiple ports.

And there can also be some delay, albeit much shorter, if the output pins of the native PICmicro don't all map to the same port of the PICAXE.
 

radiogareth

Senior Member
Thanks for the patient and enlightening comments above. I really should have curbed my enthusiasm to get it working and read the data sheet....now an update and how it was solved, before I actually even READ the comments above! After trying the bit order suggestion (no change that I could tell) and stopping short of using a single port Picaxe I first tried the highest clock speed (m32) but the problem persisted. I then realised there was an enable pin on the attenuator and having a spare pin on my 14M2 I added in a pause 50 after setting the ports, then enabled the attenuator, another pause 50, low on the attenuator enable pin and back round to measure the pot setting again. That worked nicely, no jumping around and a perfectly responsive smooth progression result (pause 50's x2 not being a real 100mseconds at m32 clock speed).
The Picaxe ports are connected by ribbon cable to the bottom side of the 8way DIP switch by soldering to the SM connections. Including the P/S select pin (! how it worked at all...). That is now disabled in the listing.
So basically solved...another useful Picaxe project :)
Code:
pullup %000100000000000    'turns on the pullup on C.3
#no_data            'saves downloading blank stuff
setfreq m32
    
sense:'    
    readadc c.4,b0    'reads the adc on C.4
    let b0=255-b0
    'pause 100
    'debug
    gosub display_time 'shows what you have adjusted it to

    goto sense         'checks again...and again....
    
    
    Display_time:'Displays the value in b0
    
    symbol out = b0
    dirsB = %00111111
    dirsC = %00000011
    gosub setoutputs
    return

setoutputs:
    pinB.0 = bit7    'bit layout to suit pcb 
    pinB.1 = bit6    'vertical row of LEDs
    pinB.2 = bit5    'bit 7 top from Pinb0,
    pinB.3 = bit4    'bit 0 from port c pin c0
    pinB.4 = bit3
    pinB.5 = bit2
    pinC.1 = bit1
    'pinC.0 = bit0    'not needed
    pause 50
    high c.2 'attenuator latch enable
    pause 50
    low c.2    'Attenuator latch disable
    return
 

inglewoodpete

Senior Member
As mentioned previously, the "dirsB" and "dirsC" could be moved to the line just before the "sense:" label, since it only needs to execute once, at start up.

For clarity "symbol out = b0", being a definition and non-executable, is best moved to just before the "setfreq m32" line.
 
Top