STM32:ADC采集數(shù)據(jù)實(shí)例(采用DMA模式)
SOC:STM32F103RCT6
軟件平臺:STM官方庫V3.5.0
開發(fā)工具:Keil
本文章將給出STM32F103RCT6控制AT24C02的代碼。如有疑問和錯誤,歡迎留言告之。
AT24C02使用I2C進(jìn)行數(shù)據(jù)訪問,板子上使用STM32的I2C控制器2和AT24C02進(jìn)行連接。
具體來說,PB10作為SCL和AT24C02相連,PB11作為SDL和AT24C02相連。
這里就不給出硬件連接圖了,比較簡單。
I2C這里使用輪詢方法,不是中斷,請留意。
首先,給出I2C控制器的初始化代碼,和讀寫代碼,一共三個函數(shù),對應(yīng)三個功能。
注意:這里是使用I2C控制器2。
#include"stm32f10x.h"
#include"stm32f10x_i2c.h"
#include
voidI2C2_Init(I2C_InitTypeDef*I2C_InitStruct)
{
GPIO_InitTypeDefGPIO_InitStruct;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
/*配置IO管腳*/
GPIO_InitStruct.GPIO_Pin=GPIO_Pin_10|GPIO_Pin_11;
GPIO_InitStruct.GPIO_Speed=GPIO_Speed_2MHz;
GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF_OD;
GPIO_Init(GPIOB,&GPIO_InitStruct);
I2C_Init(I2C2,I2C_InitStruct);
I2C_Cmd(I2C2,ENABLE);
//配置NVIC
/*NVIC_InitStructure.NVIC_IRQChannel=I2C2_EV_IRQn;//I2C事件中斷
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0x01;//搶占優(yōu)先級1
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0x02;//子優(yōu)先級2
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;//使能中斷通道
NVIC_Init(&NVIC_InitStructure);//根據(jù)NVIC_InitStruct中指定的參數(shù)初始化外設(shè)NVIC寄存器*/
//I2C_ITConfig(I2C2,I2C_IT_EVT,ENABLE);//打開事件中斷
}
/*i2c寫一個字節(jié)*/
/*ByteWrite*/
voidI2C2_WriteByte(uint8_tDeviceAddress,uint8_tMemoryAddress,uint8_tData)
{
while(I2C_GetFlagStatus(I2C2,I2C_FLAG_BUSY));//等待I2C空閑
//start
I2C_GenerateSTART(I2C2,ENABLE);
while(I2C_CheckEvent(I2C2,I2C_EVENT_MASTER_MODE_SELECT)!=SUCCESS);
//deviceaddress
I2C_Send7bitAddress(I2C2,DeviceAddress,I2C_Direction_Transmitter);//寫模式
while(I2C_CheckEvent(I2C2,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)!=SUCCESS);
//printf("addressoverrn");
//memoryaddress
I2C_SendData(I2C2,MemoryAddress);
while(I2C_CheckEvent(I2C2,I2C_EVENT_MASTER_BYTE_TRANSMITTED)!=SUCCESS);
//data
I2C_SendData(I2C2,Data);
while(I2C_CheckEvent(I2C2,I2C_EVENT_MASTER_BYTE_TRANSMITTED)!=SUCCESS);
//stop
I2C_GenerateSTOP(I2C2,ENABLE);
}
/*i2c讀一個字節(jié)*/
/*RandomRead*/
uint8_tI2C2_ReadByte(uint8_tDeviceAddress,uint8_tMemoryAddress)
{
uint8_tData;
while(I2C_GetFlagStatus(I2C2,I2C_FLAG_BUSY));//等待I2C空閑
//start
I2C_GenerateSTART(I2C2,ENABLE);
while(I2C_CheckEvent(I2C2,I2C_EVENT_MASTER_MODE_SELECT)!=SUCCESS);//ev5
//deviceaddress
I2C_Send7bitAddress(I2C2,DeviceAddress,I2C_Direction_Transmitter);//寫模式
while(I2C_CheckEvent(I2C2,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)!=SUCCESS);//ev6
//memoryaddress
I2C_SendData(I2C2,MemoryAddress);
while(I2C_CheckEvent(I2C2,I2C_EVENT_MASTER_BYTE_TRANSMITTED)!=SUCCESS);
//start
I2C_GenerateSTART(I2C2,ENABLE);
while(I2C_CheckEvent(I2C2,I2C_EVENT_MASTER_MODE_SELECT)!=SUCCESS);
//deviceaddress
I2C_Send7bitAddress(I2C2,DeviceAddress,I2C_Direction_Receiver);//讀模式
while(I2C_CheckEvent(I2C2,I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED)!=SUCCESS);
/*按照manual的圖273,先讀取數(shù)據(jù),關(guān)閉ACK應(yīng)答,最后發(fā)出STOP*/
while(I2C_CheckEvent(I2C2,I2C_EVENT_MASTER_BYTE_RECEIVED)!=SUCCESS);/*等待讀取事件,再讀取數(shù)據(jù)*/
Data=I2C_ReceiveData(I2C2);
I2C_AcknowledgeConfig(I2C2,DISABLE);//關(guān)閉應(yīng)答和停止條件產(chǎn)生
printf("receivedrn");
I2C_GenerateSTOP(I2C2,ENABLE);
I2C_AcknowledgeConfig(I2C2,ENABLE);
returnData;
}
接著給出相應(yīng)的AT24C02訪問代碼
#include"stm32f10x.h"
#include"stm32f10x_i2c.h"
#include"i2c.h"
#include"delay.h"
#include
/*從AT24CXX的指定地址開始讀出一個字節(jié)*/
uint8_tAT24CXX_ReadByte(uint8_tReadAddr)
{
returnI2C2_ReadByte(0xA0,ReadAddr);
}
/*向AT24CXX的指定地址開始寫入一個字節(jié)*/
voidAT24CXX_WriteByte(uint8_tWriteAddr,uint8_tData)
{
I2C2_WriteByte(0xA0,WriteAddr,Data);
}
voidAT24CXX_test(void)
{
uint8_ti;
I2C_InitTypeDefI2C_InitStructure;
I2C_InitStructure.I2C_ClockSpeed=200000;//200kHZ
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;