I2CSLAVE OR Background serial between 20X2 and 20M2

nz-tdm

New Member
Basically, I have a sensor unit and a display unit.
The sensor unit gets temperature from DS18B20, light from TSL235R, and humidity from HH10D and stores the values for retrieval from the display unit.
The display unit retrieves the values from the sensor unit and displays them on a 16x2 serial OLED.

However, I want the display unit to be able to be controlled by 3 buttons, Up Down and Select. I have the menu system on the display working all fine, its just a matter of making the two PICAXEs communicate WITHOUT disrupting user input into the display unit. i.e. no SERIN or long PAUSEs.

I have successfully connected the two chips via I2C. The 20X2 supports being an I2C slave so I used that as the sensor. However, I cannot read from the 20X2 via I2C if the 20X2 is currently reading one of the sensors. (each sensor takes about 0.8seconds to read). I just get all 255 (I2C error) if I try to read at this time (99% of the time). Only if I add a pause after every read (thats fine btw) can I get data through. Sometimes the display unit trys to read the sensor unit whilst not in this pause timeframe and I get 255s.

There is a way I could solve this, is to use background serial, however, I can never get any data to come through, I have tried half a dozen guides on this forum. When using background serial, the 20M2 becomes the sensor unit and the 20X2 with background serial becomes the display unit and the sensor unit sends values to the display unit whenever it reads the sensors.

The problem is, the scratchpad is always empty...


Anyway, this is the program for the I2C method, which would be most ideal as it is simpler. (only the relevant code is included, no menu system or sensor read subroutines)

Sensor unit (20X2):

Code:
setfreq m4
hi2csetup i2cslave, %10100000

start:
gosub getlight
gosub gethumid
gosub gettemp

put 0,Whole,Fract,humidity,lightlvl

goto start

Display unit (20M2):

Code:
hi2csetup i2cmaster,%10100000,i2cfast,i2cbyte

await:
if s1 = 1 or s2 = 1 or s3 = 1 then
	if s1 = 1 and s2 = 1 then await
	if s3 = 1 then
		rc3:
		if s3 = 0 then
			if current > 3 then
				gosub beepsel
			endif
			gosub sel
			goto await
		endif
		goto rc3
	elseif s1 = 1 then
		rc1:
		if s1 = 0 then
			if current > 1 then
				gosub beepup
			endif
			gosub up
			goto await
		endif
		goto rc1
	elseif s2 = 1 then
		rc2:
		if s2 = 0 then
			if current < 6 then
				gosub beepdown
			endif
			gosub down
			goto await
		endif
		goto rc2
	endif
endif
hi2cin 0,(Whole,Fract,humidity,lightlvl)
gosub update
goto await
^^^ That loop just checks whether one of the three buttons is pressed, and on release, goes to the respective subroutine, I figured that that would be a good place to download new values from the sensor unit as it loops over and over when the device is idle, which is what I want because the temperature will keep updating.

Here is a youtube of the almost-finished menu system to help you understand what I mean. The device is a mini-weather station type deal with a sensor located somewhere else connected by a cable.
http://www.youtube.com/watch?v=B0t9FZlkkT4
 
Welcome to the PICAXE Forum.

Background serial would IMO be best as the sensor unit can push data to the display unit when it has some, the display unit can then check if it has new data or not which should make things fairly easy.

It shouldn't be too hard to get background receive implemented but best to start with simple code to prove it is working.
 
Perhaps if you post your code that uses background serial then we can help you find the problem. It should not be that difficult.
 
However, I want the display unit to be able to be controlled by 3 buttons, Up Down and Select. I have the menu system on the display working all fine, its just a matter of making the two PICAXEs communicate WITHOUT disrupting user input into the display unit. i.e. no SERIN or long PAUSEs.

If you reprioritize your master slave arrangement so that the input buttons are also a slave device then you can send the master key press inputs via hserout/hserin and act on them when the master is ready.
 
Thanks for the quick response.

I will try background serial again and will post my program. But first I will try another idea that will use the I2C method, but use the 20X2 (slave) as the display module and have the sensor module HI2COUT to the display module. Since the display module has not many long pauses, just a few SEROUTs to the OLED.

Btw, will SEROUT disrupt incoming I2C data if it happens to occur at this time? If so, will HSEROUT be any different since it uses different physical parts?
 
Btw, will SEROUT disrupt incoming I2C data if it happens to occur at this time? If so, will HSEROUT be any different since it uses different physical parts?
Slave i2c devices can be read into variables while hserin background received into scratchpad or vise versa to avoid conflict.

Master updates OLED. Make a map of OLED screen characters in user RAM "poke" and refresh entire screen each update. Do not relinquish control during updates. Button press debounce pause will give sufficient time for OLED refresh.

Button press has next highest priority.

Multipliable slaves:

One for buttons
One or more for sensors
 
Last edited:
Back
Top