Wednesday 25 April 2012

Picaxe i2c as Slave

Not difficult - but there is one pitfall I fell into.

Having set the slave up to be a slave, it needs to keep looping. It doesn't matter what the loop does (and in a simple application at least, I haven't needed to bother with interrupts).

Example:


#picaxe 20x2
#no_table
symbol counter=b5
HI2CSETUP I2CSLAVE, 20 'sets up with address 20, which I find easier to debug than 010100
main:
inc counter  
goto main


The "main" loop could just have a "pause 5000" in it - but it DOES need to loop

Sunday 22 April 2012

VSM and i2c: Beware

Why are companies so reluctant to tell you what their products won't do?


The Picaxe VSM simulator is a great product if it simulates the functions that you want to use. The problem is that it is sold by Revolution Education with broad claims - ie that it will do simulation "for all M, X, X1, X2 PICAXE chips" and "Support for all major protocols, including RS232, spi, i2c, 1-wire, etc."


But - after investing five or six hours learning my way around VSM (which provides reasonable-but-not-brilliant documentation and limited tutorials, but still involves a big learning curve) - I hit problems with an i2c interaction between two 20x2 Picaxe chips not working as I expected.


So I reduced everything to the absolute basic, with one chip acting as Master, trying to update a single byte on the other, set up as Slave.


It failed - and there in the log (as an item of Information, not an Error) was the message:
Background i2c receive (slave mode) cannot be simulated


I've googled for workarounds or updates - and there are none. So everything points to this being a fundamental limitation of the VSM simulation program as supplied by Revolution Education.


I don't expect the world for sixpence - but I do think it would be rather more grown-up for companies selling products that have known limitations to treat their potential customers like adults and say "This product does all these brilliant things, but please be aware before you buy it that it won't do the following".


UPDATE 23 April 2012


Reply from Picaxe Technical Support:


The current PICAXE X2 VSM models have only been recently added, are in beta state and therefore don't support all that a physical PICAXE may offer; I2C master mode is supported but I2C slave mode currently is not. I2C slave emulation is quite complicated (effectively a whole different model to layer on top) and we do not currently have an estimate as to when that may be supported. 



Saturday 21 April 2012

VSM and the AXE033 display


My first test of the VSM simulator didn't go well - I tried to output a simple text message from a 20x2 to an AXE033 LCD component, and nothing happened.


For once, a quick Google sent me in nearly the right direction, and a bit of trial and error got it sorted soon afterwards


Key points:

  • Despite a 2008 promise on the Forum to provide improvements, the VSM will still (in 2012) only write to the AXE033 at T2400 (or T2400_8 if frequency is at m8, eg 20x2)
  • If you are writing to the AXE033 as the first thing your program does, you may need to add a delay: in my case 150 milliseconds is enough: a bit less gets you part of the string, and no pause results in no string!
Here is a program to run on a 20x2 with an AXE033 connected to pin C.0:




'test program to get 033 LCD working 
#define forVSM




#ifdef forVSM
symbol baudRate=T2400_8 'for VSM
#else
symbol baudRate=N2400_8 'for physical chip
#endif


symbol pinForDisplay=C.0




#ifdef forVSM

#rem
VSM needs 150 milliseconds (in my configuration) before it can write to the LCD/OLED. 
This is only needed if writing at the very start of the program, 
so if your LCD/OLED write happens at least 150 msecs after program starts
then you won't need this
You can try a shorter pause (eg 120) - in my case I only got the end of the string displayed


#endrem
pause 150
#endif


serout pinForDisplay,baudRate,(254,1) 'clear screen
pause 30 'give screen clear time to execute


serout pinForDisplay,baudRate,(254,128) 'go to start of line 1
serout pinForDisplay,baudRate,("0123456789abcdef")


serout pinForDisplay,baudRate,(254,192) 'go to start of line 2
serout pinForDisplay,baudRate,("fedcba9876543210")

Sunday 1 April 2012

Timer and settimer

There's a lot of detail on settimer in the manual, but I still struggled how to reset the timer. In fact it is blindlingly obvious:

timer=0

timer is just a word variable, which gets incremented when a second (hidden) "minor tick" variable overflows. The preload value in settimer specifies what value the "minor tick" counter moves to once it has hit 65535: the higher the preload value, the fewer increments needed, and so the sooner the "minor tick" overflows and increments timer.

The pitfall is that wheras the first settimer command will set the timer to zero, any subsequent settimer commands will change the rate at which the timer increments, but will NOT reset the timer