標(biāo)題:
stm32使用IO口實(shí)現(xiàn)模擬IIC接口源程序
[打印本頁(yè)]
作者:
emotion
時(shí)間:
2018-4-8 21:02
標(biāo)題:
stm32使用IO口實(shí)現(xiàn)模擬IIC接口源程序
這是在stm32上使用io口模擬實(shí)現(xiàn)i2c的功能,
希望能幫助大家
單片機(jī)源程序如下:
#include "stm32f10x_lib.h"
#include "I2C_Driver.h"
#include "stdio.h"
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
u8 I2cVal = 0xaa;
/* Private Function ---------------------------------------------------------*/
void RccInitialisation(void);
void GpioInitialisation(void);
void Systick_Delay_1ms(u32 nCount);
void UartInitialisation(void);
int main(void)
{
RccInitialisation();
GpioInitialisation();
UartInitialisation();
I2C_GPIO_Config();
printf("\r\nStart IIC test\r\n");
I2C_WriteByte(I2cVal, 0x0000, 0xa0);
printf("\r\nThe Simulation_IIC has sended data:%d\r\n", I2cVal);
for(vu32 i = 0; i < 20000; i++);
I2C_ReadByte(&I2cVal, 1, 0x00 ,0xa0);
printf("\r\nThe Simulation_IIC has received data:%d\r\n", I2cVal);
if(I2cVal == 0xaa)
{
printf("\r\nIIC test success\r\n");
}
else
{
printf("\r\nIIC test fail\r\n");
}
while(1);
}
/*******************************************************************************
* 函數(shù)名 : RCC_Configuration
* 函數(shù)描述 : 設(shè)置系統(tǒng)各部分時(shí)鐘
* 輸入?yún)?shù) : 無(wú)
* 輸出結(jié)果 : 無(wú)
* 返回值 : 無(wú)
*******************************************************************************/
void RccInitialisation(void)
{
/* 定義枚舉類型變量 HSEStartUpStatus */
ErrorStatus HSEStartUpStatus;
/* 復(fù)位系統(tǒng)時(shí)鐘設(shè)置*/
RCC_DeInit();
/* 開(kāi)啟HSE*/
RCC_HSEConfig(RCC_HSE_ON);
/* 等待HSE起振并穩(wěn)定*/
HSEStartUpStatus = RCC_WaitForHSEStartUp();
/* 判斷HSE起是否振成功,是則進(jìn)入if()內(nèi)部 */
if(HSEStartUpStatus == SUCCESS)
{
/* 選擇HCLK(AHB)時(shí)鐘源為SYSCLK 1分頻 */
RCC_HCLKConfig(RCC_SYSCLK_Div1);
/* 選擇PCLK2時(shí)鐘源為 HCLK(AHB) 1分頻 */
RCC_PCLK2Config(RCC_HCLK_Div1);
/* 選擇PCLK1時(shí)鐘源為 HCLK(AHB) 2分頻 */
RCC_PCLK1Config(RCC_HCLK_Div2);
/* 設(shè)置FLASH延時(shí)周期數(shù)為2 */
FLASH_SetLatency(FLASH_Latency_2);
/* 使能FLASH預(yù)取緩存 */
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/* 選擇鎖相環(huán)(PLL)時(shí)鐘源為HSE 1分頻,倍頻數(shù)為9,則PLL輸出頻率為 8MHz * 9 = 72MHz */
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
/* 使能PLL */
RCC_PLLCmd(ENABLE);
/* 等待PLL輸出穩(wěn)定 */
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
/* 選擇SYSCLK時(shí)鐘源為PLL */
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/* 等待PLL成為SYSCLK時(shí)鐘源 */
while(RCC_GetSYSCLKSource() != 0x08);
}
/* 打開(kāi)APB2總線上的GPIOA時(shí)鐘*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOA |RCC_APB2Periph_USART1, ENABLE);
}
void Systick_Delay_1ms(u32 nCount)
{
u32 TimingDelay = nCount * 10000;
while(TimingDelay)
{
TimingDelay--;
}
}
/*******************************************************************************
* 函數(shù)名 : GPIO_Configuration
* 函數(shù)描述 : 設(shè)置各GPIO端口功能
* 輸入?yún)?shù) : 無(wú)
* 輸出結(jié)果 : 無(wú)
* 返回值 : 無(wú)
*******************************************************************************/
void GpioInitialisation(void)
{
/* 定義GPIO初始化結(jié)構(gòu)體 GPIO_InitStructure */
GPIO_InitTypeDef GPIO_InitStructure;
/* 設(shè)置USART1的Tx腳(PA.9)為第二功能推挽輸出功能 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA , &GPIO_InitStructure);
/* 設(shè)置USART1的Rx腳(PA.10)為浮空輸入腳 */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA , &GPIO_InitStructure);
}
/*******************************************************************************
* 函數(shù)名 : USART_Configuration
* 函數(shù)描述 : 設(shè)置USART1
* 輸入?yún)?shù) : None
* 輸出結(jié)果 : None
* 返回值 : None
*******************************************************************************/
void UartInitialisation(void)
{
/* 定義USART初始化結(jié)構(gòu)體 USART_InitStructure */
USART_InitTypeDef USART_InitStructure;
/* 定義USART初始化結(jié)構(gòu)體 USART_ClockInitStructure */
USART_ClockInitTypeDef USART_ClockInitStructure;
/* 波特率為115200bps;
* 8位數(shù)據(jù)長(zhǎng)度;
* 1個(gè)停止位,無(wú)校驗(yàn);
* 禁用硬件流控制;
* 禁止USART時(shí)鐘;
* 時(shí)鐘極性低;
* 在第2個(gè)邊沿捕獲數(shù)據(jù)
* 最后一位數(shù)據(jù)的時(shí)鐘脈沖不從 SCLK 輸出;
*/
USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;
USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;
USART_ClockInit(USART1 , &USART_ClockInitStructure);
USART_InitStructure.USART_BaudRate = 115200;
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);
/* 使能USART1 */
USART_Cmd(USART1 , ENABLE);
}
int fputc(int ch, FILE *f)
{
USART_SendData(USART1, (u8)ch);
while(USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET);
return ch;
}
復(fù)制代碼
所有資料51hei提供下載:
進(jìn)階十 使用IO口實(shí)現(xiàn)模擬IIC接口.zip
(511.57 KB, 下載次數(shù): 56)
2018-4-8 21:01 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
作者:
wangzxnet
時(shí)間:
2019-5-10 14:38
謝謝分享。。。。。。。。。。!
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1