Nothing Special   »   [go: up one dir, main page]

F - Cpu: #Define #Include #Include #Include

Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 8

#define F_CPU 8000000

#include <avr/io.h>
#include <util/delay.h>
#include <avr/pgmspace.h>

/* Pin assignment for control lines on the PCF8574 */


#define RS 0x10
#define RW 0x20
#define EN 0x40
#define BL 0x80
#define START 0x08 // START has been transmitted
#define REP_START 0x10 // Repeated START has been transmitted
#define MTX_ADR_ACK 0x18 // SLA+W has been transmitted and ACK received
#define MTX_DATA_ACK 0x28 // Data byte has been transmitted and ACK
received
#define SUCCESS 0xFF // return code for success
#define LCD_FUNCTION_4BIT_1LINE 0x20 /* 4-bit interface, single line, 5x7 dots */
#define LCD_FUNCTION_4BIT_2LINES 0x28 /* 4-bit interface, dual line, 5x7 dots */
#define LCD_FUNCTION_8BIT_1LINE 0x30 /* 8-bit interface, single line, 5x7 dots */
#define LCD_FUNCTION_8BIT_2LINES 0x38 /* 8-bit interface, dual line, 5x7 dots */
#define LCD_DISP_ON 0x0C /* display on, cursor off */
#define LCD_CLR 0 /* DB0: clear display */

#define PCF8574_ADDR 0x40 // Slave address of PCF8574


#define SCL_CLOCK 10 // Set SCL to 100 KHz at 3.6 MHz xtal
// See application note AVR315, table 1

//#define delay(us) _delayFourCycles( ( ( 1*(F_CPU/4000) )*us)/1000 )

/*************************************************************************
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);
}

unsigned char i2c_write(unsigned char data)


{
Wait_TWI_int();

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;
}

unsigned char i2c_send_start(void)


{
TWCR = ((1<<TWINT)+(1<<TWSTA)+(1<<TWEN));// Send START

Wait_TWI_int();

if((TWSR != START)&&(TWSR != REP_START))// If status other than START


return TWSR; // transmitted (0x08) or Repeated
return SUCCESS; // START transmitted (0x10)
// -> error and return TWSR.
// If success return SUCCESS.
}

void i2c_send_stop(void)
{
TWCR = ((1<<TWEN)+(1<<TWINT)+(1<<TWSTO));// Send STOP condition
}

unsigned char i2c_send_adr(unsigned char adr)


{
Wait_TWI_int();

TWDR = adr;
TWCR = ((1<<TWINT)+(1<<TWEN)); // Clear int flag to send byte

Wait_TWI_int();

if (TWSR != MTX_ADR_ACK) // If NACK received return TWSR


return TWSR;
return SUCCESS; // Else return SUCCESS
}

static void
lcd_write(unsigned char data, unsigned char df)
{
unsigned char dataBits;

i2c_send_start();
i2c_send_adr(PCF8574_ADDR);

dataBits = (data >> 4) & 0x0f; // Output high nibble


if (df)
dataBits |= RS;
if (!OCR1A)
dataBits |= BL;
i2c_write(dataBits);
i2c_write(EN | dataBits);
i2c_write(dataBits);

dataBits = data & 0x0f; // Output low nibble


if (df)
dataBits |= RS;
if (!OCR1A)
dataBits |= BL;
i2c_write(dataBits);
i2c_write(EN | dataBits);
i2c_write(dataBits);
i2c_send_stop();
_delay_ms(40); // Minimum execution time
}

/*
* 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();

_delay_ms(16000); /* wait 16ms or more after power-on */

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();

lcd_command(LCD_FUNCTION_4BIT_2LINES); /* function set: display lines */


lcd_command(LCD_DISP_ON); /* Display on, Cursor on, Blink off */
lcd_clrscr();
}
int main(void)
{
//PORTMUX.CTRLB = PORTMUX_TWI0_ALTERNATE_gc; // PA1, PA2 are alternate pins
for SCL and SDA, so need to use the PORTMUX register bit to enable

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>

void TWI_Init (void)


{
//set_bit(TWCR,6);
TWSR=0;
TWBR=0x07;
TWCR|=(1<<TWEN);

void TWI_Start (void)

TWCR= (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);

while (get_bit(TWCR,TWINT)==0)

void TWI_Stop (void)

TWCR=(1<<TWSTO)|(1<<TWEN)|(1<<TWINT);

void TWI_Write (char data)

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;

void EEPROM_Write (char data, char address)

TWI_Start();

TWI_Write(0xA8); //slave address is 1010.100 and a 0 in the 8th bit to indicate


Writting.

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);

_delay_ms(1000); // You must allow suffcent delay for the EEPROM to


complete the its internal write cycle

EEPROM_Read(0x00,&R);

if (R==0xE0)

PORTD=0b01000000;

You might also like