|
這個是STM32F103ZET6的串口5測試代碼,內(nèi)部附帶一個簡單的串口協(xié)議測試。
單片機源程序如下:
- /*-------------------------------------------------------------------------------
- 文件名稱:main.c
- 文件描述:通過串口5,使用printf函數(shù)打印信息,編譯時需勾選Use MicroLIB
- 硬件平臺:尼莫M3S開發(fā)板
- 編寫整理:shifang
- 固件庫 :V3.5
- 備 注:通過簡單修改可以移植到其他開發(fā)板,部分資料來源于網(wǎng)絡(luò)。
- ---------------------------------------------------------------------------------*/
- #include <stdio.h>
- #include "stm32f10x.h"
- #include "led.h"
- #include "delay.h"
- #include "key.h"
- #include "timer.h"
- #include "beep.h"
- #ifdef __GNUC__
- /* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
- set to 'Yes') calls __io_putchar() */
- #define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
- #else
- #define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
- #endif /* __GNUC__ */
- int main(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- USART_InitTypeDef USART_InitStructure;
- NVIC_InitTypeDef NVIC_InitStructure;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD, ENABLE); //使能PORTC,PORTD時鐘
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART5,ENABLE); //使能UART5
- USART_DeInit(UART5); //復(fù)位串口5
- //UART5_TX PC.12
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12; //PC.12
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //復(fù)用推挽輸出
- GPIO_Init(GPIOC, &GPIO_InitStructure); //初始化PC12
-
- //UART5_RX PD.2
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//浮空輸入
- GPIO_Init(GPIOD, &GPIO_InitStructure); //初始化PD2
-
- //UART5 NVIC 配置
- NVIC_InitStructure.NVIC_IRQChannel = UART5_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//搶占優(yōu)先級3
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子優(yōu)先級3
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
- NVIC_Init(&NVIC_InitStructure); //根據(jù)指定的參數(shù)初始化VIC寄存器
- /* USARTx configured as follow:
- - BaudRate = 9600 baud 波特率
- - Word Length = 8 Bits 數(shù)據(jù)長度
- - One Stop Bit 停止位
- - No parity 校驗方式
- - Hardware flow control disabled (RTS and CTS signals) 硬件控制流
- - Receive and transmit enabled 使能發(fā)送和接收
- */
- USART_InitStructure.USART_BaudRate = 9600;
- 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(UART5, &USART_InitStructure);
- USART_ITConfig(UART5, USART_IT_RXNE, ENABLE);//開啟中斷
- USART_Cmd(UART5, ENABLE); //使能串口
- LED_Init();//LED初始化
- KEY_Init();//按鍵初始化
- SysTick_Init();//延時初始化
- BEEP_Init(); //蜂鳴器初始化
- printf("\n\rUSART Printf Example: (德飛萊)尼莫M3S開發(fā)板串口測試程序\r輸入任何信息發(fā)送,接收到同樣信息");
- while (1)
- {
- //使用printf函數(shù)循環(huán)發(fā)送固定信息
- Delay_ms(500);
- LED2_REV;
- }
- }
- PUTCHAR_PROTOTYPE
- {
- /* Place your implementation of fputc here */
- /* e.g. write a character to the USART */
- USART_SendData(UART5, (uint8_t) ch);
- /* 循環(huán)等待直到發(fā)送結(jié)束*/
- while (USART_GetFlagStatus(UART5, USART_FLAG_TC) == RESET)
- {}
- return ch;
- }
復(fù)制代碼
所有資料51hei提供下載:
串口5測試協(xié)議.zip
(310.8 KB, 下載次數(shù): 19)
2018-3-13 19:28 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|
評分
-
查看全部評分
|