Tuesday, September 8, 2020

ATMega32 TWI interfaces to PCF8574A parallel port expanding

PCF8574A is an 8-bit parallel I/O port expander with two-wire serial interface. This TWI peripheral interfaces to the master MCU via two wires. It's I/O pins model similar to the legacy 8051 one's. It operates from a 2.5 to 6 V DC supply voltage. An optional INT (interrupt) triggers an active low signal to the connected MCU pin whenever its digital input changes its logic state.






PCF8574A is similar to its precedence one's PCF8574. In the previous post, I use PIC12F629 to interfaces to PCF8574. However, PCF8574A has a difference reading and writing address, with 0x70 for writing and 0x71 for reading. I don't want to show its full details here.





In this example, the ATMega32 master TWI module reads the digital inputs from lower nibble of PCF8574A I/O. This lower nibble will be wrote back to the higher nibble of PCF8574A repeatedly.

 



ATMega32 TWI interfaces to PCF8574A parallel port expander
A running example

Embedded C source code list below.

/*
 * i2c_pcf8574.c
 *
 * Created: 9/9/2020 9:10:41 AM
 * Author : aki-technical
 */ 

#include <avr/io.h>

#define F_CPU 16000000UL
#include <util/delay.h>

void i2cInit(void){
TWSR=0x03; //Bit Rate Pre-scaler Is 1:64
TWBR=0x0F; 
TWCR=(1<<TWEN); //Enable The TWI Module
PORTC|=(1<<0);
PORTC|=(1<<1);
}

void i2cStart(void){
TWCR=(1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
while((TWCR&(1<<TWINT))==0);
}

void i2cWrite(unsigned char data){
TWDR=data;
TWCR=(1<<TWINT)|(1<<TWEN);
while((TWCR&(1<<TWINT))==0);
}

unsigned char i2cRead(char ACK){
if(ACK==0)
TWCR=(1<<TWINT)|(1<<TWEN)|(1<<TWEA);
else
TWCR=(1<<TWINT)|(1<<TWEN);
while((TWCR&(1<<TWINT))==0);
return TWDR;
}

void i2cStop(){
TWCR=(1<<TWINT)|(1<<TWEN)|(1<<TWSTO);
}

int main(void)
{
unsigned char i2cData=0x00;
i2cInit();
PORTC=0x03;

/*Set the lower nibble high for digital inputs*/
i2cStart();
i2cWrite(0x70);
i2cWrite(0x0F);
i2cStop();

while (1)
{
/*Read the digital inputs*/
i2cStart();
i2cWrite(0x71);
i2cData=i2cRead(1);
i2cStop();

/*Transfer the inputs to outputs*/
i2cData<<=4;

/*Write inputs to outputs*/
i2cStart();
i2cWrite(0x70);
i2cWrite(i2cData|0x0F);
i2cStop();

_delay_ms(10);
}
}

Click here to download the zip file of this programming example.

See also


No comments:

Post a Comment

Search This Blog

Labels

25AA010A (1) 8051 (7) 93AA46B (1) ADC (30) Analog Comparator (1) Arduino (15) ARM (6) AT89C52 (7) ATMega32 (54) AVR (57) CCS PICC (28) DAC (1) DHT11 (2) Display (105) Distance Sensor (3) DS18B20 (3) dsPIC (2) dsPIC30F1010 (2) EEPROM (5) Environment Sensor (4) esp8266 (1) I2C (29) Input/Output (67) Interrupt (19) Keil (5) Keypad (10) LCD (46) Master/Slave (1) MAX7221 (1) MCP23017 (5) MCP23S17 (4) Meter (3) MikroC (2) Motor (15) MPLABX (66) Nokia 5110 LCD (3) OLED (2) One-Wire (6) Oscillator (8) PCB (6) PCD8544 (3) PCF8574 (5) PIC (107) PIC12F (2) PIC16F628A (2) PIC16F630 (1) PIC16F716 (3) PIC16F818 (10) PIC16F818/819 (2) PIC16F84A (15) PIC16F876A (1) PIC16F877A (9) PIC16F88 (1) PIC16F887 (60) PIC18 (19) PIC18F1220 (4) PIC18F2550 (3) PIC18F4550 (12) PWM (11) RTC (8) Sensor (10) SH1106 (1) Shift Register (11) Shift Registers (2) SPI (24) STM32 (6) STM32 Blue Pill (6) STM32CubeIDE (6) STM32F103C8T6 (6) SysTick (3) temperature sensor (11) Thermometer (21) Timer/Counter (30) TM1637 (2) UART (7) Ultrasonic (4) Voltmeter (7) WDT (1) XC16 (2) XC8 (94)