0% found this document useful (0 votes)
62 views2 pages

PCF8574 C

This document contains code for initializing and communicating with an I2C GPIO expander chip. It defines functions for setting up the I2C interface and GPIO pins, initializing the chip, reading a byte of data from the chip, and writing a byte of data to the chip. These functions handle the I2C communication and protocol to transfer data to and from the chip located at a 7-bit address over the I2C bus.

Uploaded by

Roberto Dias
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views2 pages

PCF8574 C

This document contains code for initializing and communicating with an I2C GPIO expander chip. It defines functions for setting up the I2C interface and GPIO pins, initializing the chip, reading a byte of data from the chip, and writing a byte of data to the chip. These functions handle the I2C communication and protocol to transfer data to and from the chip located at a 7-bit address over the I2C bus.

Uploaded by

Roberto Dias
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

1 #include "PCF8574.

h"
2
3
4 void I2C_GPIO_setup(void)
5 {
6 GPIO_Init(I2C_PORT,
7 ((GPIO_Pin_TypeDef)(SCL_pin | SDA_pin)),
8 GPIO_MODE_OUT_PP_HIGH_FAST);
9 }
10
11
12 void I2C_setup(void)
13 {
14 I2C_DeInit();
15 I2C_Init(100000,
16 PCF8574_address,
17 I2C_DUTYCYCLE_2,
18 I2C_ACK_CURR,
19 I2C_ADDMODE_7BIT,
20 (CLK_GetClockFreq() / 1000000));
21 I2C_Cmd(ENABLE);
22 }
23
24
25 void PCF8574_init(void)
26 {
27 I2C_GPIO_setup();
28 I2C_setup();
29 }
30
31
32 unsigned char PCF8574_read(void)
33 {
34 unsigned char port_byte = 0x00;
35 unsigned char num_of_bytes = 0x01;
36
37 while(I2C_GetFlagStatus(I2C_FLAG_BUSBUSY));
38
39 I2C_GenerateSTART(ENABLE);
40 while(!I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT));
41
42 I2C_Send7bitAddress(PCF8574_address, I2C_DIRECTION_RX);
43 while(!I2C_CheckEvent(I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
44
45 while(num_of_bytes)
46 {
47 if(I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_RECEIVED))
48 {
49 if(num_of_bytes == 0)
50 {
51 I2C_AcknowledgeConfig(I2C_ACK_NONE);
52 I2C_GenerateSTOP(ENABLE);
53 }
54
55 port_byte = I2C_ReceiveData();
56
57 num_of_bytes--;
58 }
59 };
60
61 return port_byte;
62 }
63
64
65 void PCF8574_write(unsigned char data_byte)
66 {
67 I2C_GenerateSTART(ENABLE);
68 while(!I2C_CheckEvent(I2C_EVENT_MASTER_MODE_SELECT));
69
70 I2C_Send7bitAddress(PCF8574_address, I2C_DIRECTION_TX);
71 while(!I2C_CheckEvent(I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
72
73 I2C_SendData(data_byte);
74 while(!I2C_CheckEvent(I2C_EVENT_MASTER_BYTE_TRANSMITTED));
75
76 I2C_GenerateSTOP(ENABLE);
77 }
78

You might also like