★ 可以直接複製貼上到 C 語言開發環境中,此程式碼可以直接編譯執行(只要你配置好開發環境與硬體)。
說明:保證編譯可以通過,實際運行後可以正確輸出,祝大家成功
#include "stm32f10x.h"
原理:STM32F4 使用 I2C 讀取 TC74 A0 然後再經由 UART 傳送至電腦中查看。
腳位:
I2C 腳位:PB6 = SCL,PB7 = SDA
UART 腳位:PC10 = TX,PC11 = RX,(包率 = 9600)
輸出結果:
#include "stm32f10x.h"
#include "stdio.h"
#define SLAVE_ADDRESS 72 << 1 // TC74 A0 的 I2C 地址
void Init_I2C() {
I2C_InitTypeDef I2C_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
/* 啟用 I2C */
I2C_Cmd(I2C1,ENABLE);
/* 啟用 I2C1 RCC 時鐘 */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
/* 設定 I2C1 的 SDA 與 SCL 腳位 */
/* PB6 = SCL ,PB7 = SDA */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_Init(GPIOB, &GPIO_InitStructure);
/* 設定 I2C1 */
I2C_InitStructure.I2C_Mode = I2C_Mode_SMBusHost;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = 0x00; // STM32 自己的 I2C 地址
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = 100000 ; // 設定 I2C 時鐘速度為 100K
I2C_Init(I2C1, &I2C_InitStructure);
}
/* 發送開始信號
* 發送到 "從設備 (Slave Device)" 地址 + R/W bit
*
* 參數:
* I2Cx --> I2C 外設,例如:I2C1
* address --> 7 bit 的 "從設備 (Slave Device)" 地址
* direction --> 設定方向,例如:
* I2C_Direction_Tranmitter 為 主發送模式
* I2C_Direction_Receiver 為 主接收模式
*/
void I2C_start(I2C_TypeDef* I2Cx, uint8_t address, uint8_t direction){
// 等待 I2C1 不忙的時候...
while(I2C_GetFlagStatus(I2Cx, I2C_FLAG_BUSY));
// 對 I2C1 發送啟動信號
I2C_GenerateSTART(I2Cx, ENABLE);
// 等待 I2C1 EV5 --> 從 從設備 (Slave Device) 確認啟動信號
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_MODE_SELECT));
// Send slave Address for write
I2C_Send7bitAddress(I2Cx, address, direction);
/* 等待 I2C1 EV6, 並判斷 I2C 方向:
* 如果是 I2C_Direction_Tranmitter 則設定為 主發送模式
* 如果是 I2C_Direction_Receiver 則設定為 主接收模式
*/
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));
}
}
/* 發送一個 byte 到 "從設備 (Slave Device)"
*
* 參數:
*I2Cx --> I2C 的外設,例如:I2C1
*data --> 要發送的 byte 資料
*/
void I2C_write(I2C_TypeDef* I2Cx, uint8_t data)
{
I2C_SendData(I2Cx, data);
// 等待 I2C1 EV8_2 --> 已發送一個 byte
while(!I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
}
/* 從 "從設備 (Slave Device)" 讀取一個 byte
* 並請求後面一個 byte
*/
uint8_t I2C_read_ack(I2C_TypeDef* I2Cx){
uint8_t data;
// 啟用 確認設定接收資料
I2C_AcknowledgeConfig(I2Cx, ENABLE);
// 等待接收到一個 byte
while( !I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED) );
// 從 I2C 佔存器讀取一個 byte 並回傳
data = I2C_ReceiveData(I2Cx);
return data;
}
/* 從 "從設備 (Slave Device)" 讀取一個 byte
* 且之後就不再接收資料
*/
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);
// 等待收到一個 byte
while( !I2C_CheckEvent(I2Cx, I2C_EVENT_MASTER_BYTE_RECEIVED) );
// 從 I2C 佔存器讀取一個 byte 並回傳
data = I2C_ReceiveData(I2Cx);
return data;
}
/* 發送停止信號
* 釋放 I2C 匯流排
*/
void I2C_stop(I2C_TypeDef* I2Cx){
// 停止 I2C1
I2C_GenerateSTOP(I2Cx, ENABLE);
}
void My_Usart1_Send(char *string){
while(*string){
/* 傳送訊息至 USART1 */
USART_SendData(USART1, (unsigned short int) *string++);
/* 等待訊息傳送完畢 */
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
}
}
void Init_UART(){
/******** 宣告 USART、GPIO 結構體 ********/
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
/******** 啟用 GPIOA、USART1 的 RCC 時鐘 ********/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1, ENABLE);
/******** 設定 PA9 為 Tx 覆用 ********/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure); // 初始化 PA9
/******** 設定 PA10 為 Rx 覆用 ********/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure); // 初始化 PA10
/******** USART 基本參數設定 ********/
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_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_Init(USART1, &USART_InitStructure); // 初始化 UART1
USART_Cmd(USART1, ENABLE); // 啟用 USART1
}
int main(){
int i;
char buff [] = "";
uint8_t received_data[2];
Init_I2C(); // 初始化 I2C
Init_UART(); // 初始化 UART
My_Usart1_Send(" Start \n");
My_Usart1_Send("---------------------------------------------- \n");
while(1){
I2C_start(I2C1, SLAVE_ADDRESS, I2C_Direction_Transmitter); // 在 主發送 模式中 準備開始 發送
I2C_write(I2C1, 0x00); // 寫入一個 byte 到 TC74 感測器
I2C_stop(I2C1); // 停止發送
I2C_start(I2C1, SLAVE_ADDRESS, I2C_Direction_Receiver); // 在 主發送 模式中 準備開始 發送
received_data[0] = I2C_read_ack(I2C1); // 從 TC74 讀取一個 byte 並請求後面一個 byte
received_data[1] = I2C_read_nack(I2C1); // 讀取一個 byet 後,就不再讀取後面一個 byte (停止傳輸)
sprintf (buff, "%d", received_data[0]); // 將數字轉成字串 (String)
My_Usart1_Send(buff); // 輸出溫度值到電腦
My_Usart1_Send(" C \n");
for(i=0; i<1000000; i++); // 延遲
}
}
全站熱搜