Firstly, let me explain what I'am doing, I am working with PCF8574 I2C I/O expander, with Arduino WIRE.h it just worked very well. In the others hand, I was trying to use AVR TWI in a common way. Unfortunately, It just doesn't work. I studied data sheet, textbook and write it accordingly though.
Arduino Code which it just worked easily
#include<Wire.h>
void setup() {
Wire.begin();
}
void I2C_write()
{
unsigned char i=0;
while(i<8){
Wire.beginTransmission(0x20);
Wire.write(i);
Wire.endTransmission();
i=i+1;
delay(100);
}
}
void loop() {
I2C_write();
}
AVR Code
#include<avr/io.h>
void setup() {
i2c_init();
i2c_start();
i2c_writeAddress(0x20);
i2c_writeData(0x00);
i2c_stop();
while(1);
}
void i2c_init(void)
{
TWSR=0x00; // no prescaler.
TWBR=79; // Set frequency of SCL to 100Khz
TWCR=(1<<TWEN); //Enable TWI
}
void i2c_stop(void)
{
TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO);
}
void i2c_start(void)
{
TWCR=(1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
//while(~(TWCR&(1<<TWINT)));
while ((TWCR&(1<<TWINT))==0);
}
void i2c_writeAddress(unsigned char data)
{
TWDR = data;
TWCR = (1<<TWINT)|(1<<TWEN);
while ((TWCR&(1<<TWINT))==0);
}
void i2c_writeData(unsigned char data)
{
TWDR = data;
TWCR = (1<<TWINT)|(1<<TWEN);
while ((TWCR&(1<<TWINT))==0);
}
void loop() {
}
to be honest, I litterally want to use AVR becuase optimizing memory,
information
1.PCF8574 I/O expander
2.Arudino UNO R3 (ATMEGA328P)
Would you please kindly suggest, making this C AVR compatible with Arduino UNO R3
Thank you
Please edit your post to add code tags (see the "How to get the best out of the forum" post for instructions).
Please explain what "doesn't work" means. What do you expect to happen, and what happens instead?
Note that the two programs should do very different things.
Ok, I will
With Arduino library It can trasmit data and start counting from 0x00-0x07
With AVR It can't transmit any data. with comon address
Please edit your post to add code tags.
With AVR It can't transmit any data. with comon address
How do you know that? Explain what you expected the program to do, and what it did instead.
b707
September 8, 2022, 4:51pm
5
If first is works, why do you need the second?
first I notice the memory both Global variable and programm memory using in very low.
Besides, I keen to learn a register level programming.
My expectation is quite simple, I just want to transmit a 8 bit data and display via LED
I send a data with Wire.h which it can transmit a data and display via LED , answering you question.
In case on AVR, It did noting comparing with Wire.h
I just really want to know what I did wrong with AVR?
gfvalvo
September 9, 2022, 1:34am
8
Have you studied the source code for the Wire library? It uses the TWI library internally. So, it can obviously be done.
I'm rookie, It would interesting, how do I access ?
The "AVR" program, as written, sends a single zero. Most people consider that nothing, so your program actually is working correctly.
It hardware configuration, LED are connected to PCF8574 with sink-current that's why I send 0x00
system
Closed
March 12, 2023, 6:33am
14
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.