新聞中心

        EEPW首頁 > 嵌入式系統 > 設計應用 > STM32 之 UART1(2)

        STM32 之 UART1(2)

        作者: 時間:2016-12-03 來源:網絡 收藏
        (2)Init_External_Device.c
        C語言:Codee#14663

        #include "includes.h"

        /*******************************************************************************
        == 全局變量 ==
        *******************************************************************************/

        //=== UART1_RX變量,TX變量 ===========================================
        unsignedcharUart1_Rx;// 接受一個字符變量
        unsignedcharUart1_Tx;// 發送一個字符變量

        //=== 精確延時 =======================================================
        vu32TimingDelay;// 精確延時在SysTick中斷里用的計數變量



        /*******************************************************************************
        * Function Name : RCC_Configuration
        * Description : Configures the different system clocks.
        * Input : None
        * Output : None
        * Return : None
        *******************************************************************************/
        voidRCC_Configuration(void)
        {
        ErrorStatusHSEStartUpStatus;

        //將外設 RCC寄存器重設為缺省值
        RCC_DeInit();

        //設置外部高速晶振(HSE)
        RCC_HSEConfig(RCC_HSE_ON);

        //等待 HSE 起振
        HSEStartUpStatus=RCC_WaitForHSEStartUp();

        if(HSEStartUpStatus==SUCCESS)
        {
        //預取指緩存使能
        FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

        //設置代碼延時值
        //FLASH_Latency_2 2 延時周期
        FLASH_SetLatency(FLASH_Latency_2);

        //設置 AHB 時鐘(HCLK)
        //RCC_SYSCLK_Div1 AHB 時鐘 = 系統時鐘
        RCC_HCLKConfig(RCC_SYSCLK_Div1);

        //設置高速 AHB 時鐘(PCLK2)
        //RCC_HCLK_Div2 APB1 時鐘 = HCLK / 2
        RCC_PCLK2Config(RCC_HCLK_Div2);

        //設置低速 AHB 時鐘(PCLK1)
        //RCC_HCLK_Div2 APB1 時鐘 = HCLK / 2
        RCC_PCLK1Config(RCC_HCLK_Div2);

        // PLLCLK = 8MHz * 9 = 72 MHz
        //設置 PLL 時鐘源及倍頻系數
        RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9);

        //使能或者失能 PLL
        RCC_PLLCmd(ENABLE);

        //等待指定的 RCC 標志位設置成功 等待PLL初始化成功
        while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET)
        {
        }


        //設置系統時鐘(SYSCLK) 設置PLL為系統時鐘源
        RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

        //等待PLL成功用作于系統時鐘的時鐘源
        // 0x00:HSI 作為系統時鐘
        // 0x04:HSE作為系統時鐘
        // 0x08:PLL作為系統時鐘
        while(RCC_GetSYSCLKSource()!=0x08)
        {
        }
        }

        //RCC_APB2PeriphClockCmd(RCC_APB2Periph_ALL, ENABLE);


        //使能或者失能 APB2 外設時鐘
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO,ENABLE);
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD,ENABLE);
        // Enable USART1 clocks
        RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);


        }

        /*******************************************************************************
        * Function Name : SysTick_Config SysTick設置
        * Description : Configures SysTick
        * Input : None
        * Output : None
        * Return : None
        *******************************************************************************/
        voidSysTick_Config(void)
        {
        /* Disable SysTick Counter */
        SysTick_CounterCmd(SysTick_Counter_Disable);

        /* Disable the SysTick Interrupt */
        SysTick_ITConfig(DISABLE);

        /* Configure HCLK clock as SysTick clock source */
        SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);

        /* SysTick interrupt each 1000 Hz with HCLK equal to 72MHz */
        SysTick_SetReload(9000);

        /* Enable the SysTick Interrupt */
        SysTick_ITConfig(ENABLE);

        }

        /*******************************************************************************
        * Function Name : NVIC_Configuration
        * Description : Configures NVIC and Vector Table base location.
        * Input : None
        * Output : None
        * Return : None
        *******************************************************************************/
        voidNVIC_Configuration(void)
        {
        NVIC_InitTypeDefNVIC_InitStructure1_UART1;

        //===== Default ======================================================
        #ifdef VECT_TAB_RAM
        /* Set the Vector Table base location at 0x20000000 */
        NVIC_SetVectorTable(NVIC_VectTab_RAM,0x0);
        #else/* VECT_TAB_FLASH */
        /* Set the Vector Table base location at 0x08000000 */
        NVIC_SetVectorTable(NVIC_VectTab_FLASH,0x0);
        #endif

        //===== NVIC_UART1 ===================================================
        /* Configure the NVIC Preemption Priority Bits */
        NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);

        /* Enable the USART1 Interrupt */
        NVIC_InitStructure1_UART1.NVIC_IRQChannel=USART1_IRQChannel;// 配置使能指定的IRQ(Interrupt ReQuest中斷請求)通道
        NVIC_InitStructure1_UART1.NVIC_IRQChannelPreemptionPriority=0;// 配置IRQ的 組 優先級
        NVIC_InitStructure1_UART1.NVIC_IRQChannelSubPriority=0;// 配置IRQ的 從 優先級
        NVIC_InitStructure1_UART1.NVIC_IRQChannelCmd=ENABLE;// 配置IRQ 使能
        NVIC_Init(&NVIC_InitStructure1_UART1);// 初始化 UART1_IRQ
        }

        /*******************************************************************************
        * Function Name : UART1_Configuration
        * Description : Configures the uart1
        * Input : None
        * Output : None
        * Return : None
        *******************************************************************************/
        voidUART1_Configuration(void)
        {
        USART_InitTypeDefUSART_InitStructure_UART1;
        /* USART1 configured as follow:
        - BaudRate = 9600 baud
        - Word Length = 8 Bits
        - One Stop Bit
        - No parity
        - Hardware flow control disabled (RTS and CTS signals)
        - Receive and transmit enabled
        */
        USART_InitStructure_UART1.USART_BaudRate=9600;// 配置UART1 波特率為9600
        USART_InitStructure_UART1.USART_WordLength=USART_WordLength_8b;// 配置UART1 傳輸過程中每一幀的數據位數為 8位
        USART_InitStructure_UART1.USART_StopBits=USART_StopBits_1;// 配置UART1 發送停止位 為1個
        USART_InitStructure_UART1.USART_Parity=USART_Parity_No;// 配置UART1 奇偶效驗模式 為無奇偶效驗
        USART_InitStructure_UART1.USART_HardwareFlowControl=USART_HardwareFlowControl_None;// 配置UART1 硬件流控制 為無硬件流控制
        USART_InitStructure_UART1.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;// 配置UART1 使能發射和接受模式

        /* Configure the USART1*/
        USART_Init(USART1,&USART_InitStructure_UART1);// 初始化UART1

        /* Enable USART1 Receive and Transmit interrupts */
        USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);// 配置UART1接受中斷使能

        /* Enable the USART1 */
        USART_Cmd(USART1,ENABLE);// 使能UART1
        }

        /*******************************************************************************
        * Function Name : GPIO_Configuration
        * Description : Configures the different GPIO ports.
        * Input : None
        * Output : None
        * Return : None
        *******************************************************************************/
        voidGPIO_Configuration(void)
        {
        GPIO_InitTypeDefGPIO_InitStructure_LED_PORTB;
        GPIO_InitTypeDefGPIO_InitStructure_KEY_PORTA;
        GPIO_InitTypeDefGPIO_InitStructure_KEY_PORTB;
        GPIO_InitTypeDefGPIO_InitStructure_KEY_PORTC;
        GPIO_InitTypeDefGPIO_InitStructure_UART1_TX_PORTA;
        GPIO_InitTypeDefGPIO_InitStructure_UART1_RX_PORTA;

        //==== LED =======================================================
        GPIO_InitStructure_LED_PORTB.GPIO_Pin=GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
        GPIO_InitStructure_LED_PORTB.GPIO_Speed=GPIO_Speed_50MHz;
        GPIO_InitStructure_LED_PORTB.GPIO_Mode=GPIO_Mode_Out_PP;//推挽輸出
        GPIO_Init(GPIOB,&GPIO_InitStructure_LED_PORTB);

        //==== KEY =======================================================
        GPIO_InitStructure_KEY_PORTA.GPIO_Pin=GPIO_Pin_0;
        GPIO_InitStructure_KEY_PORTA.GPIO_Speed=GPIO_Speed_50MHz;
        GPIO_InitStructure_KEY_PORTA.GPIO_Mode=GPIO_Mode_IPU;//上拉輸入
        GPIO_Init(GPIOA,&GPIO_InitStructure_KEY_PORTA);

        GPIO_InitStructure_KEY_PORTB.GPIO_Pin=GPIO_Pin_7;
        GPIO_InitStructure_KEY_PORTB.GPIO_Speed=GPIO_Speed_50MHz;
        GPIO_InitStructure_KEY_PORTB.GPIO_Mode=GPIO_Mode_IPU;//上拉輸入
        GPIO_Init(GPIOB,&GPIO_InitStructure_KEY_PORTB);

        GPIO_InitStructure_KEY_PORTC.GPIO_Pin=GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
        GPIO_InitStructure_KEY_PORTC.GPIO_Speed=GPIO_Speed_50MHz;
        GPIO_InitStructure_KEY_PORTC.GPIO_Mode=GPIO_Mode_IPU;//上拉輸入
        GPIO_Init(GPIOC,&GPIO_InitStructure_KEY_PORTC);

        //==== UART1 ======================================================
        // Configure USART1_Tx as alternate function push-pull
        GPIO_InitStructure_UART1_TX_PORTA.GPIO_Pin=GPIO_Pin_9;
        GPIO_InitStructure_UART1_TX_PORTA.GPIO_Speed=GPIO_Speed_50MHz;
        GPIO_InitStructure_UART1_TX_PORTA.GPIO_Mode=GPIO_Mode_AF_PP;//復用推挽輸出
        GPIO_Init(GPIOA,&GPIO_InitStructure_UART1_TX_PORTA);

        // Configure USART1_Rx as input floating
        GPIO_InitStructure_UART1_RX_PORTA.GPIO_Pin=GPIO_Pin_10;
        GPIO_InitStructure_UART1_RX_PORTA.GPIO_Speed=GPIO_Speed_50MHz;
        GPIO_InitStructure_UART1_RX_PORTA.GPIO_Mode=GPIO_Mode_IN_FLOATING;// 懸浮輸入
        GPIO_Init(GPIOA,&GPIO_InitStructure_UART1_RX_PORTA);

        }

        /*******************************************************************************
        * Function Name : 精確延時函數
        *******************************************************************************/
        voidDelay_Ms(u32nTime)
        {
        /* Enable the SysTick Counter */
        SysTick_CounterCmd(SysTick_Counter_Enable);

        TimingDelay=nTime;

        while(TimingDelay!=0);

        /* Disable SysTick Counter */
        SysTick_CounterCmd(SysTick_Counter_Disable);
        /* Clear SysTick Counter */
        SysTick_CounterCmd(SysTick_Counter_Clear);
        }

        本文引用地址:http://www.104case.com/article/201612/325137.htm



        關鍵詞: STM32UART

        評論


        技術專區

        關閉
        主站蜘蛛池模板: 杂多县| 新安县| 壶关县| 呼图壁县| 延吉市| 宿松县| 新疆| 齐齐哈尔市| 四会市| 亚东县| 大兴区| 张家口市| 平江县| 比如县| 南木林县| 沙湾县| 巍山| 沙河市| 晋宁县| 苗栗市| 云浮市| 新余市| 河源市| 绥滨县| 垣曲县| 遂溪县| 阿鲁科尔沁旗| 河北省| 石阡县| 宿松县| 江孜县| 穆棱市| 鄂州市| 榆中县| 山西省| 郑州市| 辉县市| 岚皋县| 合阳县| 石嘴山市| 巴马|