




版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
1、STM32 配置 IIC 接口通信方式參考源碼全部分類移動(dòng)開發(fā)與應(yīng)用WEB 前端架構(gòu)與運(yùn)維序設(shè)計(jì)數(shù)據(jù)庫(kù)操作系統(tǒng)熱點(diǎn)技術(shù)綜合STM32 配置接口通信方式參考源碼90 閱讀0 評(píng)論 2016-11-30程IICzhuimengcanyang分類:嵌入式最近在讀取SHT3x 系列 sensor 的溫度和濕度,用到的是IIC接口。順便寫了一下STM32的IIC接口。這次配置的是STM32內(nèi)部的IIC接口。注意:讀的時(shí)候,怎么發(fā)送Ack,和NAck信號(hào),參考stm 的設(shè)計(jì)文檔。#include Dev_SHT3X.h點(diǎn)擊 (此處 )折疊或打開#include globalDef.h#include st
2、dio.h#define I2C1_OWN_ADDRESS7 0x0A#define I2C_Speed40000#define SHT3X_ADDRESS0x44/* read out command */#define CMD_READH_SHX0x2c#define CMD_READL_SHX0x06/* SHX device: IIC1* PB6 - SCL* PB7 - SDA*/static void IIC_gpioConfig(void)GPIO_InitTypeDef GPIO_InitStructure;/* 使能與I2C1 有關(guān)的時(shí)鐘*/RCC_APB2PeriphClo
3、ckCmd(RCC_APB2Periph_GPIOB,E NABLE);RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,EN ABLE);/* PB6-I2C1_SCL、 PB7-I2C1_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_Init
4、Structure);static void IIC_modeConfig(void)I2C_InitTypeDef I2C_InitStructure;I2C_InitStructure.I2C_Mode = I2C_Mode_I2C; /* I2C 配置 */I2C_InitStructure.I2C_DutyCycle =I2C_DutyCycle_2; /* 高電平數(shù)據(jù)穩(wěn)定,低電平數(shù)據(jù)變化 SCL 時(shí)鐘線的占空比 */I2C_InitStructure.I2C_OwnAddress1 =I2C1_OWN_ADDRESS7; /* setting master address. */I2
5、C_InitStructure.I2C_Ack = I2C_Ack_Enable ; /* enable ack */I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; /* 7-bit addressing mode*/I2C_InitStructure.I2C_ClockSpeed = I2C_Speed; /* communication speed = 400k */I2C_Init(I2C1, &I2C_InitStructure);I2C_Cmd(I2C1, ENABLE);void S
6、HT3X_InitDevice(void)IIC_gpioConfig();IIC_modeConfig();void sht3x_readTempHumi(uint8_t* pBuffer, uint8_t NumByteToRead)while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY); /* detect IIC busy */I2C_GenerateSTART(I2C1, ENABLE); /* Send START condition */* Test on EV5 and clear it */while(!I2C_CheckEvent(I2C1
7、,I2C_EVENT_MASTER_MODE_SELECT);/* Send slave device address for write */I2C_Send7bitAddress(I2C1, (SHT3X_ADDRESS 1), I2C_Direction_Transmitter);/* Test on EV6 and clear it */while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECT ED);/* Clear EV6 by setting again the PE bit */ I2C_Cmd(I2
8、C1, ENABLE);/* Send the device address to write to */ I2C_SendData(I2C1, CMD_READH_SHX);/* Test on EV8 and clear it */ while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED);/* Send the device address to write to */ I2C_SendData(I2C1, CMD_READL_SHX);/* Test on EV8 and clear it */ while(!I2C_
9、CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED);/-/* Send STRAT condition a second time */ I2C_GenerateSTART(I2C1, ENABLE);/* Test on EV5 and clear it */while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT);/* Send shx address for read */I2C_Send7bitAddress(I2C1, (SHT3X_ADDRESS1) | 0x01), I2C
10、_Direction_Receiver);/* Test on EV6 and clear it */while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED);/* While there is data to be read */while(NumByteToRead)if(NumByteToRead = 1)/* Disable Acknowledgement */I2C_AcknowledgeConfig(I2C1,DISABLE);/* Send STOP Condition */I2C_GenerateST
11、OP(I2C1, ENABLE);/* Test on EV7 and clear it */if(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_RECEIVED)/* Read a byte from the EEPROM */*pBuffer = I2C_ReceiveData(I2C1);/* Point to the next location where the byte read will be saved */pBuffer+;/* Decrement the read bytes counter */NumByteToRead-;/* En
12、able Acknowledgement to be ready for another reception */I2C_AcknowledgeConfig(I2C1, ENABLE);/-static float SHT3X_CalcTemperature(uint16_t rawValue)/ calculate temperature C/ T = -45 + 175 * rawValue / (216-1)return 175.0f * (float)rawValue / 65535.0f - 45.0f;/-static float SHT3X_CalcHumidity(uint16
13、_t rawValue)/ calculate relative humidity %RH/ RH = rawValue / (216-1) * 100return 100.0f * (float)rawValue / 65535.0f;void sht3x_testTask(void)uint8_t recBuffer6;uint16_t rawdata;float fTemp, fHumi;sht3x_readTempHumi(&recBuffer0, 6); rawdata = recBuffer0;rawdata = (rawdata 8) + recBuffer1; fTemp = SHT3X_CalcTemperature(rawdata);rawdata = recBuffer3;rawdata = (rawdata 8) + recBuffer4; fHumi = SHT3X_CalcHumidity(rawdata);enterCriticalSection();printf(T: %0.2f, H: %0.1frn, fTemp, fHumi); exitCriticalSection();點(diǎn)擊 (此處 )折疊或打開#ifndef_DEV_SHT3X_H#define _DEV_SHT3X_H#include b
溫馨提示
- 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
- 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
- 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 2025至2030年中國(guó)船用電加熱器數(shù)據(jù)監(jiān)測(cè)研究報(bào)告
- 2025年度水利工程款抵還住宅房款執(zhí)行協(xié)議
- 二零二五年度素食餐廳轉(zhuǎn)讓及素食文化推廣合同
- 2025年度租賃房屋承租方精裝修合同范本
- 2025年度老年人贍養(yǎng)贍養(yǎng)金及社區(qū)養(yǎng)老支持協(xié)議
- 2025至2030年中國(guó)緩沖葫蘆數(shù)據(jù)監(jiān)測(cè)研究報(bào)告
- 二零二五年度礦業(yè)權(quán)轉(zhuǎn)讓與資源整合合同
- 二零二五年度大型土方工程居間管理與協(xié)調(diào)合同
- 2025至2030年中國(guó)純棉布料數(shù)據(jù)監(jiān)測(cè)研究報(bào)告
- 二零二五年度智能電網(wǎng)工程款代付及進(jìn)度管理合同
- 初一語(yǔ)文下冊(cè):閱讀理解知識(shí)點(diǎn)整理
- 營(yíng)銷部安全生產(chǎn)責(zé)任制
- CSM工法雙輪銑水泥土攪拌墻專項(xiàng)施工方案
- 【講座】高三英語(yǔ)高效二輪備考講座課件
- 定點(diǎn)醫(yī)療機(jī)構(gòu)接入驗(yàn)收申請(qǐng)表
- 小羊詩(shī)歌大全1479首(小羊喝水?dāng)U句)
- 2022-2023學(xué)年遼寧省鞍山市普通高中高一年級(jí)下冊(cè)學(xué)期第一次月考數(shù)學(xué)(A卷)試題【含答案】
- 中國(guó)農(nóng)村居民儲(chǔ)蓄行為研究共3篇
- 華為鴻蒙深度研究
- 心理咨詢師考試題庫(kù)及答案
- 弟子規(guī)42+用人物須明求+教案
評(píng)論
0/150
提交評(píng)論