這是在stm32上使用io口模擬實現i2c的功能,
希望能幫助大家
單片機源程序如下:
- #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);
- }
- /*******************************************************************************
- * 函數名 : RCC_Configuration
- * 函數描述 : 設置系統各部分時鐘
- * 輸入參數 : 無
- * 輸出結果 : 無
- * 返回值 : 無
- *******************************************************************************/
- void RccInitialisation(void)
- {
- /* 定義枚舉類型變量 HSEStartUpStatus */
- ErrorStatus HSEStartUpStatus;
- /* 復位系統時鐘設置*/
- RCC_DeInit();
- /* 開啟HSE*/
- RCC_HSEConfig(RCC_HSE_ON);
- /* 等待HSE起振并穩定*/
- HSEStartUpStatus = RCC_WaitForHSEStartUp();
- /* 判斷HSE起是否振成功,是則進入if()內部 */
- if(HSEStartUpStatus == SUCCESS)
- {
- /* 選擇HCLK(AHB)時鐘源為SYSCLK 1分頻 */
- RCC_HCLKConfig(RCC_SYSCLK_Div1);
- /* 選擇PCLK2時鐘源為 HCLK(AHB) 1分頻 */
- RCC_PCLK2Config(RCC_HCLK_Div1);
- /* 選擇PCLK1時鐘源為 HCLK(AHB) 2分頻 */
- RCC_PCLK1Config(RCC_HCLK_Div2);
- /* 設置FLASH延時周期數為2 */
- FLASH_SetLatency(FLASH_Latency_2);
- /* 使能FLASH預取緩存 */
- FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
- /* 選擇鎖相環(PLL)時鐘源為HSE 1分頻,倍頻數為9,則PLL輸出頻率為 8MHz * 9 = 72MHz */
- RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
- /* 使能PLL */
- RCC_PLLCmd(ENABLE);
- /* 等待PLL輸出穩定 */
- while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
- /* 選擇SYSCLK時鐘源為PLL */
- RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
- /* 等待PLL成為SYSCLK時鐘源 */
- while(RCC_GetSYSCLKSource() != 0x08);
- }
- /* 打開APB2總線上的GPIOA時鐘*/
- 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--;
- }
- }
- /*******************************************************************************
- * 函數名 : GPIO_Configuration
- * 函數描述 : 設置各GPIO端口功能
- * 輸入參數 : 無
- * 輸出結果 : 無
- * 返回值 : 無
- *******************************************************************************/
- void GpioInitialisation(void)
- {
- /* 定義GPIO初始化結構體 GPIO_InitStructure */
- GPIO_InitTypeDef GPIO_InitStructure;
- /* 設置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);
-
- /* 設置USART1的Rx腳(PA.10)為浮空輸入腳 */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOA , &GPIO_InitStructure);
- }
- /*******************************************************************************
- * 函數名 : USART_Configuration
- * 函數描述 : 設置USART1
- * 輸入參數 : None
- * 輸出結果 : None
- * 返回值 : None
- *******************************************************************************/
- void UartInitialisation(void)
- {
- /* 定義USART初始化結構體 USART_InitStructure */
- USART_InitTypeDef USART_InitStructure;
- /* 定義USART初始化結構體 USART_ClockInitStructure */
- USART_ClockInitTypeDef USART_ClockInitStructure;
- /* 波特率為115200bps;
- * 8位數據長度;
- * 1個停止位,無校驗;
- * 禁用硬件流控制;
- * 禁止USART時鐘;
- * 時鐘極性低;
- * 在第2個邊沿捕獲數據
- * 最后一位數據的時鐘脈沖不從 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;
- }
復制代碼
所有資料51hei提供下載:
進階十 使用IO口實現模擬IIC接口.zip
(511.57 KB, 下載次數: 56)
2018-4-8 21:01 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|