★ 可以直接複製貼上到 C 語言開發環境中,此程式碼可以直接編譯執行(只要你配置好開發環境與硬體)。
說明:本篇程式碼是網路上資源東拼拼西拼拼、右剪又貼,所以有點亂,不過程式碼是保證可以運行的。晚點我會再重新寫一篇簡潔的程式碼,敬請期待...哈哈 !!
原理:STM32F4 使用 I2C 讀取 TC74 A0 然後再經由 UART 傳送至電腦中查看。
腳位:
I2C 腳位:PB6 = SCL,PB7 = SDA
UART 腳位:PC10 = TX,PC11 = RX
程式碼:
#include "stm32f4xx.h"
#define SLAVE_ADDRESS 72 << 1 // TC74 A0 的 I2C 地址
int i;
void init_I2C1(){
GPIO_InitTypeDef GPIO_InitStruct;
I2C_InitTypeDef I2C_InitStruct;
// 啟用 I2C1 的 RCC 時鐘
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
// 啟用 GPIOB 時鐘,主要是啟用 PB6 腳位的 SCL 時鐘、PB7 腳位的 SDA 時鐘
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
/* 設定 SCL 與 SDA 腳位
*
* 配對腳位:
* SCL = PB6
* SDA = PB7
*/
GPIO_InitStruct.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; // 使用 PB6 與 PB7
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF; // 設定腳位為 覆用 ( AF - Alternate Function )
GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz; // 設定 GPIO 速度為 50 MHz
GPIO_InitStruct.GPIO_OType = GPIO_OType_OD; // set output to open drain --> the line has to be only pulled low, not driven high
GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP; // 使用上拉電阻
GPIO_Init(GPIOB, &GPIO_InitStruct); // 初始化 GPIOB
// 連接 I2C1 到 AF
GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_I2C1); // SCL
GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_I2C1); // SDA
// 設定 I2C1
I2C_InitStruct.I2C_ClockSpeed = 100000; // 設定 I2C 時鐘速度為 100kHz
I2C_InitStruct.I2C_Mode = I2C_Mode_I2C; // I2C 模式
I2C_InitStruct.I2C_DutyCycle = I2C_DutyCycle_2; // 50% duty cycle --> standard
I2C_InitStruct.I2C_OwnAddress1 = 0x00; // own address, not relevant in master mode
I2C_InitStruct.I2C_Ack = I2C_Ack_Disable; // disable acknowledge when reading (can be changed later on)
I2C_InitStruct.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; // 設定 I2C 地址長度為 7 bit
I2C_Init(I2C1, &I2C_InitStruct); // 初始化 I2C1
// 啟用 I2C1
I2C_Cmd(I2C1, ENABLE);
}
void I2C_start(I2C_TypeDef* I2Cx, uint8_t address, uint8_t direction){
// wait until I2C1 is not busy anymore
while(I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY));
// Send I2C1 START condition
I2C_GenerateSTART(I2Cx, ENABLE);
// wait for I2C1 EV5 --> Slave has acknowledged start condition
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT));
// Send slave Address for write
I2C_Send7bitAddress(I2Cx, address, direction);
/* wait for I2C1 EV6, check if
* either Slave has acknowledged Master transmitter or
* Master receiver mode, depending on the transmission
* direction
*/
if(direction == I2C_Direction_Transmitter){
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
} else if(direction == I2C_Direction_Receiver){
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED));
}
}
/* This function transmits one byte to the slave device
* Parameters:
* I2Cx --> the I2C peripheral e.g. I2C1
* data --> the data byte to be transmitted
*/
void I2C_write(I2C_TypeDef* I2Cx, uint8_t data)
{
I2C_SendData(I2Cx, data);
// wait for I2C1 EV8_2 --> byte has been transmitted
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
}
/* This function reads one byte from the slave device
* and acknowledges the byte (requests another byte)
*/
uint8_t I2C_read_ack(I2C_TypeDef* I2Cx){
uint8_t data;
// enable acknowledge of recieved data
I2C_AcknowledgeConfig(I2Cx, ENABLE);
// wait until one byte has been received
while( !I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED) );
// read data from I2C data register and return data byte
data = I2C_ReceiveData(I2Cx);
return data;
}
/* This function reads one byte from the slave device
* and doesn't acknowledge the recieved data
*/
uint8_t I2C_read_nack(I2C_TypeDef* I2Cx){
uint8_t data;
// disabe acknowledge of received data
// nack also generates stop condition after last byte received
// see reference manual for more info
I2C_AcknowledgeConfig(I2Cx, DISABLE);
I2C_GenerateSTOP(I2Cx, ENABLE);
// wait until one byte has been received
while( !I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED) );
// read data from I2C data register and return data byte
data = I2C_ReceiveData(I2Cx);
return data;
}
/* This funtion issues a stop condition and therefore
* releases the bus
*/
void I2C_stop(I2C_TypeDef* I2Cx){
// Send I2C1 STOP Condition
I2C_GenerateSTOP(I2Cx, ENABLE);
}
char My_Usart3_Printf(char *string){
while(*string){
/* 傳送訊息至 USART3 */
USART_SendData(USART3, (unsigned short int) *string++);
/* 等待訊息傳送完畢 */
while (USART_GetFlagStatus(USART3, USART_FLAG_TC) == RESET);
}
}
void Init_UART(){
/******** 宣告 USART、GPIO 結構體 ********/
USART_InitTypeDef USART_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/******** 啟用 GPIOC、USART3 的 RCC 時鐘 ********/
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3, ENABLE);
/******** 將 PC10、PC11 連接至 USART3 ********/
GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_USART3);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_USART3);
/******** 設定 PC10 為 Tx 覆用 ********/
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; // 使用推挽式輸出
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; // 使用上拉電阻
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; // 設置為覆用
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; // 設定第 10 腳
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // 設定 GPIO 速度為 50 MHz
GPIO_Init(GPIOC, &GPIO_InitStructure); // 套用以上 GPIO 設置,並初始化 GPIOC
/******** 設定 PC11 為 Rx 覆用 ********/
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; // 設置為覆用
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; // 設定第 11 腳
GPIO_Init(GPIOC, &GPIO_InitStructure); // 套用以上 GPIO 設置,並初始化 GPIOC
/******** USART 基本參數設定 ********/
USART_InitStructure.USART_BaudRate = 9600; // 設定 USART 包率 (每秒位元數) 為 9600
USART_InitStructure.USART_WordLength = USART_WordLength_8b; // 設定 USART 傳輸的資料位元為 8
USART_InitStructure.USART_StopBits = USART_StopBits_1; // 設定 USART 停止位元為 1
USART_InitStructure.USART_Parity = USART_Parity_No; // 不使用同位元檢查
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // 不使用流量控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; // 設定 USART 模式為 Rx (接收) 、 Tx (傳送)
USART_Init(USART3, &USART_InitStructure); // 套用以上 USART 設置,並初始化UART3
/******** 啟用 USART3 ********/
USART_Cmd(USART3, ENABLE);
}
int main(){
uint8_t received_data[2];
char buff [] = ""; // 宣告字元陣列
Init_UART();
init_I2C1();
while (1){
I2C_start(I2C1, SLAVE_ADDRESS, I2C_Direction_Transmitter); // start a transmission in Master transmitter mode
I2C_write(I2C1, 0x00); // write one byte to the slave
I2C_write(I2C1, 0x00); // write another byte to the slave
I2C_stop(I2C1); // stop the transmission
I2C_start(I2C1, SLAVE_ADDRESS, I2C_Direction_Receiver); // start a transmission in Master receiver mode
received_data[0] = I2C_read_ack(I2C1); // read one byte and request another byte
received_data[1] = I2C_read_nack(I2C1); // read one byte and don't request another byte, stop transmission
sprintf (buff, "%d", received_data[0]); // 將數字轉成字串 (String)
My_Usart3_Printf(buff);
My_Usart3_Printf(" C \n");
for(i=0; i<30000000; i++); // 延遲
}
}
運行結果:
留言列表