F - Cpu: #Define #Include #Include #Include
F - Cpu: #Define #Include #Include #Include
F - Cpu: #Define #Include #Include #Include
#include <avr/io.h>
#include <util/delay.h>
#include <avr/pgmspace.h>
/*************************************************************************
Sends one byte to the display using I2C bus.
Input: data = byte to be sent, df = flag if data (1) or command (0)
Returns: none
*************************************************************************/
void Wait_TWI_int(void)
{
while (!(TWCR & (1<<TWINT))) // Wait for TWI interrupt flag set
;
}
/****************************************************************************
Public functions
****************************************************************************/
void i2c_init(void)
{
TWBR = SCL_CLOCK; // Set SCL frequency
TWCR = (1<<TWEN);
}
TWDR = data;
TWCR = ((1<<TWINT)+(1<<TWEN)); // Clear int flag to send byte
Wait_TWI_int();
if(TWSR != MTX_DATA_ACK) // If NACK received return TWSR
return TWSR;
return SUCCESS;
}
Wait_TWI_int();
void i2c_send_stop(void)
{
TWCR = ((1<<TWEN)+(1<<TWINT)+(1<<TWSTO));// Send STOP condition
}
TWDR = adr;
TWCR = ((1<<TWINT)+(1<<TWEN)); // Clear int flag to send byte
Wait_TWI_int();
static void
lcd_write(unsigned char data, unsigned char df)
{
unsigned char dataBits;
i2c_send_start();
i2c_send_adr(PCF8574_ADDR);
/*
* PUBLIC FUNCTIONS
*/
/*************************************************************************
Send LCD controller instruction command
Input: instruction to send to LCD controller, see HD44780 data sheet
Returns: none
*************************************************************************/
void lcd_command(unsigned char cmd)
{
lcd_write(cmd, 0);
}
/*************************************************************************
Send data byte to LCD controller
Input: data to send to LCD controller, see HD44780 data sheet
Returns: none
*************************************************************************/
void lcd_data(unsigned char data)
{
lcd_write(data, 1);
}
/*************************************************************************
Clear display
*************************************************************************/
void lcd_clrscr(void)
{
lcd_command(1<<LCD_CLR);
_delay_ms(1500);
}
/*************************************************************************
Display string without auto linefeed
Input: string to be displayed
Returns: none
*************************************************************************/
void lcd_puts(const char *s)
{
while ( (*s) )
lcd_data(*s++);
}
/*************************************************************************
Display string from program memory without auto linefeed
Input: string from program memory be be displayed
Returns: none
*************************************************************************/
void lcd_puts_p(const char *progmem_s)
{
register char c;
while ( (c = pgm_read_byte(progmem_s++)) ) {
lcd_data(c);
}
}
/*************************************************************************
Initialize I2C and display
*************************************************************************/
void lcd_init(void)
{
i2c_init();
i2c_send_start();
i2c_send_adr(PCF8574_ADDR);
/*
* Send 0x03 a couple of times which is the same as
* ((1<<LCD_FUNCTION | 1<<LCD_FUNCTION_8BIT) >> 4)
*/
i2c_write(0x03);
i2c_write(EN | 0x03);
i2c_write(0x03);
_delay_ms(4992);
i2c_write(EN | 0x03);
i2c_write(0x03);
_delay_ms(64);
i2c_write(EN | 0x03);
i2c_write(0x03);
_delay_ms(64);
i2c_write(0x02); /* Switch to 4 bit mode */
i2c_write(EN | 0x02);
i2c_write(0x02);
i2c_send_stop();
DDRB=0XFF;
DDRC=0XFF;
while(1) //continue
{
lcd_init();
_delay_ms(2);
lcd_puts_p("text1");
}
}
#define F_CPU 8000000UL
#define get_bit(reg,bitnum) ((reg & (1<<bitnum))>>bitnum) // get bit macro used to get
the value of a certain bit.
#include <avr/io.h>
#include <util/delay.h>
TWCR= (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
while (get_bit(TWCR,TWINT)==0)
TWCR=(1<<TWSTO)|(1<<TWEN)|(1<<TWINT);
TWDR=data;
TWCR= (1<<TWINT)|(1<<TWEN);
while (get_bit(TWCR,TWINT)==0)
}
void TWI_Read_Nack (char* ptr) // The function argument is a pointer to a memory place
in the MCU to store the received data in
TWCR=(1<<TWINT)|(1<<TWEN);
while (get_bit(TWCR,TWINT)==0)
*ptr=TWDR;
TWI_Start();
TWI_Write(address);
TWI_Write(data);
TWI_Stop();
void EEPROM_Read (char address, char* ptr) // the function arguments are an address in
the EEPROM to read from and a pointer to a memory place in the MCU to store the read
data in
TWI_Start();
TWI_Write(0xA8);
TWI_Write(address);
TWI_Start();
TWI_Write(0xA9);
TWI_Read_Nack(ptr);
TWI_Stop();
}
int main(void)
char R;
DDRD=0b11111111;
TWI_Init();
while(1)
EEPROM_Write(0xE0,0x00);
EEPROM_Read(0x00,&R);
if (R==0xE0)
PORTD=0b01000000;