|
現(xiàn)在提供STM32F030C8T6串口例程,適合初學(xué)上手
單片機源程序如下:
- /**
- ******************************************************************************
- * @file USART/USART_Printf/main.c
- * @author MCD Application Team
- * @version V1.4.0
- * @date 24-July-2014
- * @brief Main program body
- ******************************************************************************
- * @attention
- *
- * <h2><center>© COPYRIGHT 2014 STMicroelectronics</center></h2>
- *
- * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
- * You may not use this file except in compliance with the License.
- * You may obtain a copy of the License at:
- *
- * http://www.st.com/software_license_agreement_liberty_v2
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- *
- ******************************************************************************
- */
- /* Includes ------------------------------------------------------------------*/
- #include "main.h"
- /** @addtogroup STM32F0xx_StdPeriph_Examples
- * @{
- */
- /** @addtogroup USART_Printf
- * @{
- */
- /* Private typedef -----------------------------------------------------------*/
- /* Private define ------------------------------------------------------------*/
- /* Private macro -------------------------------------------------------------*/
- /* Private variables ---------------------------------------------------------*/
- /* Private function prototypes -----------------------------------------------*/
- void USART_Config(void);
- // 重定義
- int fputc(int ch, FILE *f)
- {
- while (!(USART1->ISR & USART_FLAG_TXE));
- USART1->TDR = ch;
-
- return (ch);
- }
- void USART1_Send_Char(unsigned char c)
- {
- while(!((USART1->ISR)&USART_FLAG_TXE));
- USART1->TDR=c;
- }
- void USART1_Send_String(char *s )
- {
- while (*s)
- USART1_Send_Char(*s++);
- }
-
- void SYSTICK_INIT(void)
- {
- SysTick_Config(SystemCoreClock / 1000); //Set SysTick Timer for 1ms interrupts
- //SysTick_Config(SystemCoreClock / 200); //Set SysTick Timer for 5ms interrupts
- //SysTick_Config(SystemCoreClock / 100); //Set SysTick Timer for 10ms interrupts
- //SysTick_Config(SystemCoreClock / 10); //Set SysTick Timer for 100ms interrupts
- }
-
-
- /**
- * @brief Main program
- * @param None
- * @retval None
- */
- int main(void)
- {
- /*!< At this stage the microcontroller clock setting is already configured,
- this is done through SystemInit() function which is called from startup
- file (startup_stm32f0xx.s) before to branch to application main.
- To reconfigure the default setting of SystemInit() function, refer to
- system_stm32f0xx.c file
- */
-
- // SYSTICK_INIT();
-
- /* USART configuration */
- USART_Config();
- USART1_Send_String("Fresh Persimmon all right reserved!\r\n");
- printf("USART Printf Example: retarget the C library printf function to the USART\r\n");
- printf("ASD=%.3f\r\n",9.88987);
-
- while (1)
- {
-
- }
- }
- /**
- * @brief Configure the USART Device
- * @param None
- * @retval None
- */
- void USART_Config(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
-
- NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPriority = 2;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
-
- RCC_AHBPeriphClockCmd( RCC_AHBPeriph_GPIOA, ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE );
- GPIO_PinAFConfig(GPIOA,GPIO_PinSource9,GPIO_AF_1);
- GPIO_PinAFConfig(GPIOA,GPIO_PinSource10,GPIO_AF_1);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9|GPIO_Pin_10;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- 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);
-
- USART_Cmd(USART1, ENABLE);
- USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
-
- }
-
- #ifdef USE_FULL_ASSERT
- /**
- * @brief Reports the name of the source file and the source line number
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復(fù)制代碼
所有資料51hei提供下載:
STM32F030C8T6串口例程.zip
(487.8 KB, 下載次數(shù): 549)
2017-11-28 10:30 上傳
點擊文件名下載附件
|
評分
-
查看全部評分
|