Searching \ for '[AVR:]What is wrong with this TWI code?' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/index.htm?key=what+wrong+with
Search entire site for: 'What is wrong with this TWI code?'.

Exact match. Not showing close matches.
PICList Thread
'[AVR:]What is wrong with this TWI code?'
2004\07\15@023502 by Hopkins

flavicon
face
I can not seem to get this code to give any pulses out on the SCL or SDA pins of a ATMega 8.

;Master TWI using ATMega 8 set to 8mhz internal oscillator

.include "m8def.inc"

; Interrupt service vectors

.org $0000   rjmp Reset ;Reset vector

.org $011    rjmp TWSI  ; Two-wire Serial Interface
;________________________________________________________________
.def SLA_W  = r17;Slave write address
.def DATA  = r18;Data byte

; Constants
.EQU START   =  0;8 A START condition has been transmited
.EQU MT_SLA_ACK = 0;$18 SLA+W have een transmitted
.EQU MT_DATA_ACK = 0;$28 Data Byte has been transmitted.
;________________________________________________________________
; Reset vector - just sets up interrupts and service routines
Reset:
ldi  r16,high(RAMEND); Main program start
out  SPH,r16  ; Set Stack Pointer to top of RAM
ldi  r16,low(RAMEND)
out  SPL,r16
sei   ;Enable Globel Interrupt
;________________________________________________________________
Main:
ldi r16,$20
out DDRC,r16
ldi r16,32  ;100Khz @ 8 Mhz clock
out TWBR,r16
ldi SLA_W,1 ;Dummy slave address to write to  ldi DATA,5 ;Dummy DATA byte to send

ldi r16, (1<<TWINT)|(1<<TWSTA)|(1<<TWEN)  ;Send START condition
out TWCR, r16
wait1:
in r16,TWCR ;Wait for TWINT Flag set. This
  ;indicates that the START condition
  ;has been transmitted
sbrs r16,TWINT  rjmp wait1  in r16,TWSR ;Check value of TWI Status
  ;Register. Mask prescaler bits. If
  ;status different from START go to
  ;ERROR
andi r16, 0xF8
cpi r16, Start
brne ERROR
mov r16, SLA_W ;Load SLA_W into TWDR Register.
  ;Clear TWINT bit in TWCR to start
  ;transmission of address
out TWDR, r16
ldi r16, (1<<TWINT) | (1<<TWEN)
out TWCR, r16
wait2:
in r16,TWCR ;Wait for TWINT Flag set. This
  ;indicates that the SLA+W has been
  ;transmitted, and ACK/NACK has
  ;been received.
sbrs r16,TWINT
rjmp wait2
in r16,TWSR ;Check value of TWI Status
  ;Register. Mask prescaler bits. If
  ;status different from MT_SLA_ACK
  ;go to ERROR
andi r16, 0xF8
cpi r16, MT_SLA_ACK
brne ERROR
mov r16, DATA ;Load DATA into TWDR Register.
  ;Clear TWINT bit in TWCR to start
  ;transmission of data
out TWDR, r16
ldi r16, (1<<TWINT) | (1<<TWEN)
out TWCR, r16
wait3:
in r16,TWCR ;Wait for TWINT Flag set. This
  ;indicates that the DATA has been
  ;transmitted, and ACK/NACK has
  ;been received.
sbrs r16,TWINT
rjmp wait3
in r16,TWSR ;Check value of TWI Status
  ;Register. Mask prescaler bits. If
  ;status different from
  ;MT_DATA_ACK go to ERROR
andi r16, 0xF8
cpi r16, MT_DATA_ACK
brne ERROR
ldi r16, (1<<TWINT)|(1<<TWEN)|(1<<TWSTO)   ;Transmit STOP condition
out TWCR, r16
ERROR:
nop

TWSI:
nop




---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.719 / Virus Database: 475 - Release Date: 12/07/2004

--
http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads

2004\07\15@024122 by David VanHorn

flavicon
face
>
>  rjmp TWSI  ; Two-wire Serial Interface

One fatal flaw, the TWSI routine does not reti!

I've not used the TWI before, so I can only speak generally.
Are you using anything that conflicts with those pins?
Did you mung them after the TWI was set up (SBR DDRx, or similar)

Are you prescaling clock to a legal value?

Are you SURE that it's not in fact talking? (scope or logic analyzer)

--
http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads

2004\07\15@030033 by Hopkins

flavicon
face
> >  rjmp TWSI  ; Two-wire Serial Interface
> One fatal flaw, the TWSI routine does not reti!

ok have added RETI
>
> I've not used the TWI before, so I can only speak generally.
> Are you using anything that conflicts with those pins?

Just testing this code by it self at this stage.

> Did you mung them after the TWI was set up (SBR DDRx, or similar)

This may be the problem area.
>
> Are you prescaling clock to a legal value?
>
YES
> Are you SURE that it's not in fact talking? (scope or logic analyzer)
>
Yes - have used scope. Also does not produce an output in the simulator
STUDIO 4
> --



---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.719 / Virus Database: 475 - Release Date: 12/07/2004

--
http://www.piclist.com hint: PICList Posts must start with ONE topic:
[PIC]:,[SX]:,[AVR]: ->uP ONLY! [EE]:,[OT]: ->Other [BUY]:,[AD]: ->Ads

More... (looser matching)
- Last day of these posts
- In 2004 , 2005 only
- Today
- New search...