ATmega8 and TWI :-( Help !

Go To Last Post
7 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello all.
For the last 2 days I can't get I2C communication with DS1307 RTC working.

My program hangs when it needs to wait for TWINT to be set by hardware. Don't know what to do.
I'm 99.99% sure hardware is OK. I tried internal pull ups and external later. No change.
IC2_wait is where it all stops, endless loop (tested with LCD display, it read some wierd stuff for TWCR)

Here is some of my code
************

reset:	ldi r16, 0x04
	out SPH, r16
	rcall LCDInit
		
	rcall I2Cinit
	ldi r16, 0xD0
	rcall I2C_start
	ldi r16, 0x07
	rcall I2C_write
	ldi r16, 0x10
	rcall I2C_write
	rcall I2C_stop
loop:	rjmp loop

; VOID || VOID
I2Cinit:  push r16
	;Clock <100kHz
	ldi r16, 20
	out TWBR, r16
	clr r16
	out TWSR, r16
	pop r16
	ret

; R16=SLA_W or SLA_R || VOID
I2C_start:
	push r17
	;START
	ldi r17, (1<<TWINT) | (1<<TWSTA) | (1<<TWEN)
	out TWCR, r17
	rcall I2C_wait
	;START OK?
	in r17, TWSR
	andi r17, 0xF8
	cpi r17, START
	brne I2C_error
	;ADDRESS
	out TWDR, r16
	ldi r17, (1<<TWINT) | (1<<TWEN)
	out TWCR, r17
	rcall I2C_wait
	;ADDRESS OK ?
	in r17, TWSR
	andi r17, 0xF8
	cpi r17, MT_SLA_ACK
	brne I2C_error
	pop r17
	ret

; VOID || VOID
I2C_wait:	push r16
loop69:	in r16, TWCR
	sbrs r16, TWINT
	rjmp loop69
	pop r16
	ret
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You call I2C_wait in a few places. Where exactly it hangs?

I had similar problems caused by connection errors. For example damaged cable. Are you sure that this is ok?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

spock wrote:
You call I2C_wait in a few places. Where exactly it hangs?

I had similar problems caused by connection errors. For example damaged cable. Are you sure that this is ok?

It never passed the first call :-(
Something is very wrong, but I can't find out what.
Cable is OK 100%, I checked it 10x times.

Is there any I2C simulator available for AVR ?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

As the fault is probably hardware related I'd have thought the best diagnostic tool to use was actually an oscilloscope?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

clawson wrote:
As the fault is probably hardware related I'd have thought the best diagnostic tool to use was actually an oscilloscope?

Thanks. This is my last solution because I don't have a scope.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well if you really bump up the TWPS and TWBR settings in order to REALLY slow the operation down you might be able to catch the activity using one of the free download PC Soundcard based scopes. (they are good up to about 20KHz, htough 5-10kHz is a more realistic upper limit). As the input to the soundcard is basically for a microphone you will need to run the SDA signal through some kind of divider to get it down to an acceptable level.

Cliff

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Problem solved. Hardware problem :'( All OK now.

Ivan