Newbie Hi2cin issues.

VRAndy

New Member
Hi everyone, I was hoping I could find someone who could tell me what stupid thing I'm doing wrong. I'm a newbie to PICAXE (though I've used straight PICs one or twice.) so I'm probably doing something really stupid.

Here's my issue. I'm trying to interface my 28x1 PICAXE with the DS1307 realtime clock, (Mounted on the sparkfun breakout board) and display some text to a small lcd.

My problem is that every time my program gets to the hi2cin call my program restarts. It does this regardless of whether the clock chip is even connected! The only time the code EVER gets past the hi2cin call is if the clock chip is connected, but its ground isn't. (This strikes me as very telling, but I don't know what it's telling me.)

Here is the code I'm trying to use :
Code:
' Set the chip type
#picaxe 28x1

main: 
	' Set the chip to operate at 8hz
	setfreq m8

	if b8 = 42  then fakeReset
	b8 = 42
	
	wait 1
	serout 0, T9600_8, (" Setting up I2C..." )
	
	
	HI2CSETUP I2CMASTER, %11010000, i2cslow_8, i2cbyte
	wait 1
	

mainloop:
	serout 0, T9600_8, (" Getting time ..." )
	wait 1

	gosub getTime
	wait 1
	
	serout 0, T9600_8, ("Seconds ", b2, b3)
	
	'Blink the LED after sucessfully getting and printing the time.
	wait 1
	pin1 = 1
	wait 1
	pin1 = 0
	wait 3
	goto mainloop

getTime: 
	hi2cin 0, (b1) 	   ' Pull in the seconds from the clock chip.
	'b1 = %01000010
	bcdtoascii b1,b2,b3  ' Convert the seconds to asci seconds	
	return
	
fakeReset:
	serout 0, T9600_8, ("FAKE RESET!")
	goto mainloop
And here is the output I get off of the LCD (Notice how the first five characters are garbage. I don't know if that's a related issue or not.)

#n]vww√ng up I2C... Getting time... #n]vww√ng up I2C... Getting time... #n]vww√ng up I2C... Getting time...
(That's not actually a square root, but some similar symbol I don't recognize.)

It never blinks the LED, or prints the "Seconds" line. (Unless, like I said earlier, I connect the RTC, but disconnect its ground.)

The "FAKE RESET!" line also never prints. (I had the idea that some undefined jump or interrupt might be jumping back to main.)


So... I would greatly appreciate it if someone could point me in the right direction. I have tried this with both the SparkFun breadboard power supply and with a simpler arrangement of a some batteries and a power regulator. I'm sort of operating under the assumption that there's some setting that I've forgotten to set or some switch I've forgotten to throw, but I've got no idea what that would be.

Thank you.

(Here's a photograph here if that's of any help. (2.2mb) )

-Andy
 

Technical

Technical Support
Staff member
The first few bytes of data is corrupt because of the T9600 polarity, at the start of your program the output pin polarity is wrong, so try a 'high 0' as the very first line of the program.

Then your i2c bus hardware is incorrect, because the chip is trying to 'talk i2c' and failing, and so after 2.1 seconds of no i2c activety it times out and resets. Check all the DS1307 setup carefully (e.g. are SDA/SCL reversed), including the 4k7 pull ups (which appear to be missing from your breadboard...)
 
Last edited:

VRAndy

New Member
Then your i2c bus hardware is incorrect, because the chip is trying to 'talk i2c' and failing, and so after 2.1 seconds of no i2c activety it times out and resets. Check all the DS1307 setup carefully (e.g. are SDA/SCL reversed), including the 4k7 pull ups (which appear to be missing from your breadboard...)
Aha! I knew I was doing something stupid! It doesn't get much stupider than not looking at the wiring diagram. I completely didn't realize I needed those!

I added those in and the program progresses like I expect. It's not getting the right data yet, (It's always "80".) but some sort of communication is happening!

The first few bytes of data is corrupt because of the T9600 polarity, at the start of your program the output pin polarity is wrong, so try a 'high 0' as the very first line of the program.
This also fixed that problem!

Thank you very much! I had felt like I'd hit a wall and didn't know what to do next. Now I'm making progress again! Thanks!

PICAXE Technical Support
Tech support? On a Sunday afternoon? Now that's service. (Or is it evening in UK, now?)

-Andy
 

VRAndy

New Member
It's not getting the right data yet, (It's always "80".) but some sort of communication is happening!
Woo! It works now. I just had to set the clock. (I must have jostled the button battery.)

Thank you very much!

-Andy
 
Top