Extending / reconfiguring Picaxe-18X I/O

kranenborg

Senior Member
[size=+2]Converting 18X output pins to logical inputs[/size]

The following code shows how on the highly configurable PICAXE-18X any of the output pins can be converted to input pins, by poking the "shadow TRISB" register with 1 bits on appropriate locations. Note that the credits for this extension and several others are for hippy.

The possibility to make inputs on PORTB opens a large number of possibilities due to the flexibility of this port, as shown by posts in this thread

The example code shows how Output 6 and Output 7 are converted to logical inputs, and subsequently their state is read continuously from the PORTB data register

Code:
REM Example program
REM Shows how OUT6 and OUT7 on a PICAXE 18X are converted into inputs
REM (any output can be made input).

SYMBOL IO_Direction = b0
SYMBOL Shadow_TRISB = $AE
SYMBOL PORT_B = $06


REM First convert OUT6 and OUT7 into inputs (and leave the state of the others as they were)
REM using hippy's "shadow" TRISB register

PEEK Shadow_TRISB,IO_Direction
LET IO_Direction = IO_Direction OR %11000000             REM Set appropriate bits
POKE Shadow_TRISB,IO_Direction												

DO
	
	PEEK PORT_B,b0				REM Read inputs from PORTB data register
	REM Show PORTB register contents
	SERTXD("PORTB register contents: ", #b0,13,10)
	
	PAUSE 1000


LOOP
 
Last edited:

kranenborg

Senior Member
[size=+2]Converting 18X output pins to ANALOG inputs[/size]

The following code shows how on the highly configurable PICAXE-18X two extra ADC inputs can be created, by reconfiguring Output 6 and Output 7 to two analog inputs. This is done in two steps: 1) convert output to input, 2) configure the AD converter and connect it to the proper PORTB pin (AN5 or AN6 are available, see also the PIC16F88 datasheet).

This approach allows to use max 5 ADC channels, or compensate for the "loss" of a normal ADC input pin due to another, multiplexed function on the same input pin (for example ADC0 cannot be used if infrain is needed at the same time).

The tested example code below shows a continuous read and subsequent print of the measured values at pin 12 and 13 (Output 6 and Output 7).


Code:
REM Example program
REM Shows how OUT6 and OUT7 on a PICAXE 18X can be converted into ADC inputs
REM thus increasing the max number of 10-bits ADC inputs on this device to 5
REM 
REM This program implements continuous AD conversion and 
REM sends the results to the terminal program.



SYMBOL IO_Direction = b0
SYMBOL Shadow_TRISB = $AE
SYMBOL ANSEL = $9B
SYMBOL ADCON0 = $1F
SYMBOL ADCON1 = $9F
SYMBOL ADRESH = $1E
SYMBOL ADRESL = $9E


REM First convert OUT6 and OUT7 into inputs (and leave the state of the others as they were)
REM using hippy's "shadow" TRISB register

PEEK Shadow_TRISB,IO_Direction
LET IO_Direction = IO_Direction OR %11000000
POKE Shadow_TRISB,IO_Direction												

DO
	
	REM Do an ADC read on pin 12 (was OUT6, will be AN5 now).
	REM First enable the ADC functionality
	POKE ANSEL,%01100000				REM Set ANSEL register: Analogue inputs enabled on AN5 (RB6), AN6 (RB7)
	POKE ADCON1,%10000000				REM Set ADCON1 register (
	POKE ADCON0,%11101001				REM Set ADCON0 register (AN6 input); Enable the AD module
	REM Now do the actual ADC conversion 
	POKE ADCON0,%11101101				REM Start actual ADC conversion
	REM Get 10-bit result in w0
	PEEK ADRESH,b1					REM Read upper 2 bits of AD convertion result.
	PEEK ADRESL,b0					REM Read lower 8 bits of AD convertion result.
	POKE ADCON0,%11101000				REM Disable AD module

	POKE ADCON1,%00000000				REM Restore ADCON1 settings for proper READADC functioning
	
	
	REM Do an ADC read on pin 13 (was OUT7, will be AN6 now).
	REM First enable the ADC functionality
	POKE ANSEL,%01100000				REM Set ANSEL register: Analogue inputs enabled on AN5 (RB6), AN6 (RB7)
	POKE ADCON1,%10000000				REM Set ADCON1 register (
	POKE ADCON0,%11110001				REM Set ADCON0 register (AN7 input); Enable the AD module
	REM Now do the actual ADC conversion 
	POKE ADCON0,%11110101				REM Start actual ADC conversion
	REM Get 10-bit result in w1
	PEEK ADRESH,b3					REM Read upper 2 bits of AD convertion result.
	PEEK ADRESL,b2					REM Read lower 8 bits of AD convertion result.
	POKE ADCON0,%11110000				REM Disable AD module

	POKE ADCON1,%00000000				REM Restore ADCON1 settings for proper READADC functioning

	
	REM Show 10-bit result for the two new ADC channels
	SERTXD("ADC pin 12: ", #w0, "  ADC pin 13: ",#w1,13,10)
	
	PAUSE 1000

LOOP
Note the POKE ADCON1,%00000000 instruction after the conversion, which is necessary to guarantee proper READADC functioning after the above routines have been used; it seems that ADCON1 is not initialized explicitly by the PICAXE firmware before a READADC instruction is executed, probably to save instructions. Not initializing ADCON1 properly leads to erroneous READADC behaviour.
 
Last edited:

Dippy

Moderator
Good Stuff.
Can all this be posted on a link somewhere or else it'll be lost in a few months time?
 

kranenborg

Senior Member
I would suggest to use this very link for all picaxe 18x extensions, as this is the code snippets section. Of course several authors can contribute

Regards,
Jurjen
 

MFB

Senior Member
Great contribution! Does anyone know is similar work has been done to provide extra analog inputs on the 28X1?
 

Dippy

Moderator
A warm welcome and goodbye to:-

lkky00245 IP:220.178.42.42 Chinanet Anhui Province Network, Beijing, China
 

Maxpower

New Member
Wow - awesome post. Are there any limitations on this modifcation?
Eg - Can't leave the new ADC's running all the time or processing speed penalties?

Thanks again.
 
Top