There is no ADC support on Raspberry Pi and only one UART and hardware PWM. If you need to extend it, you can use special chips or 1 PICAXE.
PICAXEYou can imagine PICAXE as Arduino for PIC microcontrollers. It works in similar ways. You use high level functions instead of low level registers. Programming language is BASIC and IDE is available for all platforms. You don’t need to have special HW programming. It is just serial port and two resistors. If you are not familiar with PICAXE start on the Getting Started page.
We will focus on PICAXE X2 family supporting I2C Slave mode.
The idea is very simple. We will connect Raspberry Pi and PICAXE using I2C line and PICAXE will act as I2C slave. We will use ADC and PWM functionality on PICAXE. We can use PICAXE as port expander too. Smallest X2 series PICAXE-20X2 has 18 GPIO, 11 ADC and 4 PWM.
There is special memory area on PICAXE X2 series called scratchpad. If you connect PICAXE as I2C slave you will be able to access this memory same way as 24LCxx series EEPROM.
Raspberry Pi and I2CThere are a lot of articles on how to set up I2C on Raspberry Pi. I will not cover it here. My recommendation is to start with i2cdetect, i2cset and i2cget command line tools. This is the best way to understand how PICAXE X2 works as I2C Slave device.
Read from device 0x34 from memory register 0x01
sudo i2cget -y 1 0x34 0x01
Write value 0x08 to device 0x34 to memory register 0x00
sudo i2cset -y 1 0x34 0x00 0x08
Code shows how to use PICAXE as ADC (memory register 0x01) and port expander (memory register 0x00).
#no_data
#no_table
init:
' set frequency, works better
setfreq m16
' B pins as output
let dirsB = %11111111
' set 7-bit address
hi2csetup i2cslave, %01101000
' incoming i2c communication interrupt
' http://www.picaxe.com/BASIC-Commands/Interrupts-and-Multi-Tasking/setintflags/
setintflags %01000000, %01000000
main:
' read value from ADS and put it to register b1
readadc 22, b1
' copy value from b1 to scratchpad regiter 1
' this value will be available on I2C
put 1, b1
debug
pause 1000
goto main
' called when value is writen on I2C line
interrupt:
' copy value from cratchpad to register b0
get 0, b0
' check value in b0 and write to pinsB (7-segment display)
select case b0
case 0
let pinsB= %11000000
case 1
let pinsB= %11111001
case 2
let pinsB= %10100100
case 3
let pinsB= %10110000
case 4
let pinsB= %10011001
case 5
let pinsB= %10010010
case 6
let pinsB= %10000010
case 7
let pinsB= %11111000
case 8
let pinsB= %10000000
case 9
let pinsB= %10010000
case 46
let pinsB= %01111111
endselect
' debug output can be deleted
debug
'clear interrupt & reset
let hi2cflag = 0
setintflags %01000000,%01000000
return
Comments
Please log in or sign up to comment.