You are on page 1of 12

3/10/2016

tiny2313andtwi|AVRFreaks

Signup
Login
Whatareyouinterestedin?
EntireSite

Search

tiny2313andtwi
Log inor registerto post comments Go To Last Post
Author

15 posts / 0 new

Message
Posted by

#1

geoil(http://community.atmel.com/users/geoil1879): Fri. Mar


geoil(http://community.atmel.com/users/geoil1879)
28, 2008 12:21 PM

Fivestar widget
Total votes: 0

(http://community.atmel.com/users/geoil1879)

Level: New Member


Attiny2313 supports TWI interface? Be couse in the datasheet this
Joined: Sun. May 21, 2006
thing is not described, but he looks like he have TWI interface.
Posts: 10 View
posts(http://community.atmel.com/user/52600/content)
Location: Romania
Where i can find a code example for TWI interface in assembler? I am
not interested in C. Where I can find a code example for interfacind an
24C16 eeprom with an avr microcontroller without TWI interface?

Tags:

AVR Microcontrollers (http://community.atmel.com/search/site?


f[0]=im_taxonomy_forums%3A968&f[1]=bundle%3Aforum),
megaAVR and
tinyAVR(http://community.atmel.com/search/site?
f[0]=im_taxonomy_forums%3A591&f[1]=bundle%3Aforum),
ATtiny2313(http://community.atmel.com/search/site?
f[0]=im_field_avr_forum_devices%3A937&f[1]=bundle%3Aforum)

Log In/ Registerto post comments

Top

Posted by
#2
david.prentice(http://community.atmel.com/users/davidprentice)
david.prentice(http://community.atmel.com/users/davidprentice):
http://www.avrfreaks.net/forum/tiny2313andtwi

1/12

3/10/2016

tiny2313andtwi|AVRFreaks

Fri. Mar 28, 2008 12:53 PM

Total votes: 0

The Tiny2313 does not have a TWI interface. You have to use the USI

(http://community.atmel.com/users/davidprentice)
interface. Look for Atmel app note for using the USI as an I2C Master.
Level: 10k+ Postman
Joined: Sat. Feb 12, 2005
You will have to translate it into Assembler yourself.
Posts: 25744 View
posts(http://community.atmel.com/user/3320/content)
Location: Wormshill, England
David.

Log Inor Registerto post comments

Posted by

Top

#3

melexis(http://community.atmel.com/users/melexis8211): Fri.

melexis(http://community.atmel.com/users/melexis8211)
Mar 28, 2008 03:30 PM

Total votes: 0

Afaik no tiny has native I2C support. But to save code you should use
the USI in two wire mode (provides basic support like shift register,

(http://community.atmel.com/users/melexis8211)
overflow interrupts, start/stop condition detection...) as David already
Level: New Member
posted (see Atmel application note AVR310).
Joined: Sat. Mar 22, 2008
Posts: 3 View
posts(http://community.atmel.com/user/229682/content)
Do you only have to interface an E2PROM? If so you maybe should
think about using a SPI EEPROM.
Best regards,
Tom

Log Inor Registerto post comments

Posted by

Top

#4

geoil(http://community.atmel.com/users/geoil1879): Fri. Mar

geoil(http://community.atmel.com/users/geoil1879)
28, 2008 05:42 PM

Total votes: 0

I only have attiny2313 and 24C16. And is necesary to do the projects


with this components. I can not use other components. That's sucks. If
http://www.avrfreaks.net/forum/tiny2313andtwi

2/12

3/10/2016

tiny2313andtwi|AVRFreaks

i could choose than i could make the project with an atmega88. But i
(http://community.atmel.com/users/geoil1879)
Level: New Member
can not.
Joined: Sun. May 21, 2006
Posts: 10 View
posts(http://community.atmel.com/user/52600/content)
Location: Romania

Log Inor Registerto post comments

Posted by
david.prentice(http://community.atmel.com/users/davidprentice)

Top

#5

david.prentice(http://community.atmel.com/users/davidprentice):
Fri. Mar 28, 2008 06:40 PM

Total votes: 0

A bitbang i2c would be perfectly adequate, and there are plenty of C


(http://community.atmel.com/users/davidprentice)
Level: 10k+ Postman
examples of this. Otherwise translate the Atmel C USI code.
Joined: Sat. Feb 12, 2005
Posts: 25744 View
If you must do assembler then change PORTC, PINC and DDRC
posts(http://community.atmel.com/user/3320/content)
Location: Wormshill, England
references:

; parameter: data
; destroys: counter1
; .define DDRC,PINC,PORTC SCL,SDA
;
;****************************
; i2c_init MUST call first
;****************************
i2c_init:

cbi

PORTC,SCL

cbi

PORTC,SDA

sbi

DDRC,SCL

;sink SCL

cbi

DDRC,SDA

;float SDA

ret

;/***************************
; I2C start pulse .cs if free
;/***************************
I2C_Start:

http://www.avrfreaks.net/forum/tiny2313andtwi

cbi

DDRC,SDA

;float SDA

cbi

DDRC,SCL

;float SCL

rcall I2C_delay

clc

sbis

PINC,SDA

;free ?

ret

;no

sbis

PINC,SCL

;free ?

ret

;no

sbi

DDRC,SDA

rcall I2C_delay

sbi

DDRC,SCL

;sink SDA
;sink SCL and hold bus
3/12

3/10/2016

tiny2313andtwi|AVRFreaks

sec
i2c_xit: ret

;ours now

I2C_Stop: sbi DDRC,SDA

;sink SDA

rcall I2C_delay

sbi

rcall I2C_delay

cbi

DDRC,SCL

;float SCL

cbi

DDRC,SDA

;float SDA

rcall I2C_delay

ret

DDRC,SCL

;sink SCL

;/*******************************
; write data 8 bits .cs if ACK
;/*******************************
i2c_write: pushdata

ldi

counter1,8

i2c_write1: cbiDDRC,SDA

;8 bit data
;float SDA

rol

data

brcs

i2c_write2

;if c flag=1 set SDA line else clea

sbi

DDRC,SDA

;sink SDA

i2c_write2:

rcall I2C_delay

cbi

rcall I2C_delay

sbi

DDRC,SCL

dec

counter1

brne

i2c_write1

cbi

DDRC,SDA

;float SDA

cbi

DDRC,SCL

;float SCL

rcall I2C_delay

clc

sbis

sec

DDRC,SCL

;float SCL
;sink SCL

PINC,SDA

;if SDA line=1,begin write old data

sbi

DDRC,SCL

;sink SCL

pop

data

ret

;/**************************************
; receive data from SDA line, always NAK
;/**************************************
i2c_read: push counter1

ldi

counter1,8

;8 bit data

DDRC,SCL

;float SCL

i2c_read1: nop

http://www.avrfreaks.net/forum/tiny2313andtwi

cbi

rcall I2C_delay

sec

sbis

clc

rcall I2C_delay

dec

PINC,SDA

;set c flag when SDA line = 1

rol

data

;rotate left

sbi

DDRC,SCL

;sink SCL

counter1
4/12

3/10/2016

tiny2313andtwi|AVRFreaks

brne

i2c_read1

cbi

DDRC,SDA

;NAK

cbi

DDRC,SCL

;float SCL

rcall I2C_delay

sbi

rcall I2C_delay

cbi

DDRC,SDA

pop

counter1

ret

DDRC,SCL

;sink SCL
;leave bus hi

;/***********************
; delay time destroy temp
;/***********************
I2C_delay: ldi temp,10

;1 ((n1)*3 + 5)+5 = 5uS

I2C_delay_1:

dec

temp

brne

I2C_delay_1

;2

ret

;4

;1

Good luck. You would be well advised to learn C.


David.

Log Inor Registerto post comments

Top

Posted by
#6
theusch (http://community.atmel.com/users/theusch)
theusch (http://community.atmel.com/users/theusch): Fri. Mar
28, 2008 06:55 PM

Total votes: 0

Quote:
(http://community.atmel.com/users/theusch)

Level: 10k+ Postman


Joined: Mon. Feb 19, 2001
Posts: 34565 View
Good luck. You would be well advised to learn C.
posts(http://community.atmel.com/user/654/content)
Location: Wisconsin USA

Yeah, reallyCodeVisionAVR, eminently suited for professional as well as


student and hobbyist use, has the bitbanged I2C interface to a 24C02
right in the Help as a sample program. Should be identical, or nearly so,
to 24C16 if the addresses are the same width.
Lee

http://www.avrfreaks.net/forum/tiny2313andtwi

5/12

3/10/2016

tiny2313andtwi|AVRFreaks

You can put lipstick on a pig, but it is still a pig.


I've never met a pig I didn't like, as long as you have some salt and pepper.

Log Inor Registerto post comments

Posted by

Top

#7

melexis(http://community.atmel.com/users/melexis8211): Fri.
melexis(http://community.atmel.com/users/melexis8211)
Mar 28, 2008 08:19 PM

Total votes: 0

look here:
http://avrasm.tripod.com/twi26....(http://avr
(http://community.atmel.com/users/melexis8211)
Level: New Member
asm.tripod.com/twi26.html)

Joined: Sat. Mar 22, 2008


Posts: 3 View
There's some TWI USI Master code for AVR, all written in assembler.
posts(http://community.atmel.com/user/229682/content)
But read the application note from Atmel, it really helps to understand
what's going on :wink:
Tom

Log Inor Registerto post comments

Posted by

Top

#8

geoil(http://community.atmel.com/users/geoil1879): Fri. Mar


geoil(http://community.atmel.com/users/geoil1879)
28, 2008 08:29 PM

Total votes: 0

I know 'C'.Usualy I work in CovdeVisionAVR. But this is a college project


and the demand for this is verry clear: attiny2313, 24C16 and

(http://community.atmel.com/users/geoil1879)
microcontroller software in assembler. Thanks a lot!
Level: New Member
Joined: Sun. May 21, 2006
Posts: 10 View
posts(http://community.atmel.com/user/52600/content)
Location: Romania

Log Inor Registerto post comments

http://www.avrfreaks.net/forum/tiny2313andtwi

Top

6/12

3/10/2016

tiny2313andtwi|AVRFreaks

david.prentice(http://community.atmel.com/users/davidprentice)

(http://community.atmel.com/users/davidprentice)

Level: 10k+ Postman


Joined: Sat. Feb 12, 2005
Posts: 25744 View
posts(http://community.atmel.com/user/3320/content)
Location: Wormshill, England
Posted by

#9

david.prentice(http://community.atmel.com/users/davidprentice): Fri. Mar 28, 2008


08:31 PM

Total votes: 0

Write in C, so you understand your logic. Then edit the generated ASM code so that your teacher
thinks it is all your own work.
David.

Log Inor Registerto post comments

Posted by
theusch (http://community.atmel.com/users/theusch)

Top

#10

theusch (http://community.atmel.com/users/theusch): Fri. Mar


28, 2008 08:56 PM

Total votes: 0

Quote:
(http://community.atmel.com/users/theusch)

Level: 10k+ Postman


Joined: Mon. Feb 19, 2001
Posts: 34565 View
Write in C, so you understand your logic. Then edit the
posts(http://community.atmel.com/user/654/content)
Location: Wisconsin USA
generated ASM code so that your teacher thinks it is all

your own work.

I don't know exactly what geoil wants us to do for him/her. The USI
app note reference wasn't good enough. Nor was the CV suggestion,
and already a CV user (not entirely unexpected given the Romania
location). You'd think a hungry student would be devouring Google hits
and Forum hits and User Projects hits, but apparently not enough time
between handwringing sessions.

http://www.avrfreaks.net/forum/tiny2313andtwi

7/12

3/10/2016

tiny2313andtwi|AVRFreaks

The C code in AVR310 is almost entirely bitbanging, delays, and "wait


for bit". Sigh. That hard to translate to ASM? Now I know why the
teacher gave the assignment.
Even more pertinent to your post, David, is the CV bitbang I2C, which
looks like

_i2c_init:

cbi __i2c_port,__scl_bit

cbi __i2c_port,__sda_bit

sbi __i2c_dir,__scl_bit

cbi __i2c_dir,__sda_bit

rjmp __i2c_delay2

_i2c_start:

cbi __i2c_dir,__sda_bit

cbi __i2c_dir,__scl_bit

clr r30

...

You can put lipstick on a pig, but it is still a pig.


I've never met a pig I didn't like, as long as you have some salt and pepper.

Log Inor Registerto post comments

Top

Posted by
#11
david.prentice(http://community.atmel.com/users/davidprentice)
david.prentice(http://community.atmel.com/users/davidprentice):
Fri. Mar 28, 2008 09:15 PM

Total votes: 0

Lee,
(http://community.atmel.com/users/davidprentice)

Level: 10k+ Postman


Joined: Sat. Feb 12, 2005
I cannot remember exactly how my assembly code was derived. I think
Posts: 25744 View
it was by editing example ASM from ETT / futurlec.
posts(http://community.atmel.com/user/3320/content)
Location: Wormshill, England
I found this example code to be fairly indecipherable. I rearranged it to
suit myself. Which since I am used to CV, meant that I would choose
to have similar primitive functions.
So there is no surprise as to the similarity of code sequences. I also
seem to remember that the original code drove the lines instead of
being "common collector".

http://www.avrfreaks.net/forum/tiny2313andtwi

8/12

3/10/2016

tiny2313andtwi|AVRFreaks

Whereas I can probably read and follow widely differing C code, ASM is a
very personal thing. If it is not laid out in my style I am completely
lost.
So in fact I did not just edit generated ASM in this case. I wanted to
see if I could make sense of some Thai code. Yes, it is possible but it
required far more rearrangement than starting with C generated code.
David.

Log Inor Registerto post comments

Posted by
theusch (http://community.atmel.com/users/theusch)

Top

#12

theusch (http://community.atmel.com/users/theusch): Fri. Mar


28, 2008 09:34 PM

Total votes: 0

the point I was trying to make is that if OP had a little more initiative,
(http://community.atmel.com/users/theusch)

Level: 10k+ Postman


that the CV "driver" is already straight ASM and it could be massaged
Joined: Mon. Feb 19, 2001
as seen fit. I only used your quote to make that response, and not a a
Posts: 34565 View
posts(http://community.atmel.com/user/654/content)
reflection on your graciouslyposted driver which I didn't really examine.
Location: Wisconsin USA
Lee

You can put lipstick on a pig, but it is still a pig.


I've never met a pig I didn't like, as long as you have some salt and pepper.

Log Inor Registerto post comments

Posted by
ka7ehk(http://community.atmel.com/users/ka7ehk)

Top

#13

ka7ehk(http://community.atmel.com/users/ka7ehk): Fri. Mar


28, 2008 09:55 PM

Total votes: 0

Ahhh, this sounds like an "assignment" I would have given back "in the
(http://community.atmel.com/users/ka7ehk)
Level: 10k+ Postman
day".
Joined: Fri. Nov 22, 2002
Posts: 16272 View
Goal: learn how to use timing diagrams and generate code that will
posts(http://community.atmel.com/user/7050/content)
Location: Tangent, OR, USA
implement that diagram. TWI is somewhat challenging (more complex)

http://www.avrfreaks.net/forum/tiny2313andtwi

9/12

3/10/2016

tiny2313andtwi|AVRFreaks

than I would have chosen, but it gets the job done.


If the whole task is to communicate with a 27C16, then you don't have
to implement the FULL I2C state machine (master, multimaster, and
slave). Be thankful for that!
Jim

Jim Wagner Oregon Research Electronics, Consulting Div. Tangent, OR, USA
https://sites.google.com/site/or...

Log Inor Registerto post comments

Posted by

Top

#14

geoil(http://community.atmel.com/users/geoil1879): Fri. Mar


geoil(http://community.atmel.com/users/geoil1879)
28, 2008 10:37 PM

Total votes: 0

theusch wrote:
Quote:
(http://community.atmel.com/users/geoil1879)

Level: New Member


Joined: Sun. May 21, 2006
Posts: 10 View
I don't know exactly what geoil wants us to do for
posts(http://community.atmel.com/user/52600/content)
Location: Romania
him/her.

I allready sad this Thank a lot!! My english is not verry good. I am


from Romania. But..is not my fault this boys still post mesage. Problem
resolved, after first post of david.prentice .
I wrote:
Quote:

I only have attiny2313 and 24C16. And is necesary to


do the projects with this components. I can not use
other components. That's sucks. If i could choose than i
could make the project with an atmega88. But i can
not.

just be couse I want everybody to understand why I need to build the


program in assembler.
P.S. If somebody need a verry small and compact program for the
microcontroller than he must write the program in assembler.
I am not interested of other posts. Thank you again!
http://www.avrfreaks.net/forum/tiny2313andtwi

10/12

3/10/2016

tiny2313andtwi|AVRFreaks

Log Inor Registerto post comments

Top

Posted by
#15
theusch (http://community.atmel.com/users/theusch)
theusch (http://community.atmel.com/users/theusch): Fri. Mar
28, 2008 11:19 PM

Total votes: 0

Quote:
(http://community.atmel.com/users/theusch)

Level: 10k+ Postman


Joined: Mon. Feb 19, 2001
Posts: 34565 View
I am not interested of other posts.
posts(http://community.atmel.com/user/654/content)
Location: Wisconsin USA

Even though this won't be read...


Quote:

P.S. If somebody need a verry small and compact


program for the microcontroller than he must write the
program in assembler.

...geoil has a LOT to learn if he thinks we can't match in C something


simple like the I2C driver or similar that is nothing more than I/O
register banging. Heck, if given free reign to solve the defined
assignment shortest/fastest we'd probably beat half of the ASM
submissions.
Lee

You can put lipstick on a pig, but it is still a pig.


I've never met a pig I didn't like, as long as you have some salt and pepper.

Log Inor Registerto post comments

Top

Jump To
megaAVRandtinyAVR

http://www.avrfreaks.net/forum/tiny2313andtwi

11/12

3/10/2016

tiny2313andtwi|AVRFreaks

2015 Atmel Corporation


Privacy (http://www.atmel.com/About/privacy.aspx)
Contact (http://www.atmel.com/About/contact/distributors/default.aspx?
contactType=Online%20Directory)

Site Use Terms (http://www.atmel.com/About/legal.aspx)


Cookies (http://www.atmel.com/About/cookies.aspx)

http://www.avrfreaks.net/forum/tiny2313andtwi

12/12

You might also like