stm32f103串口程序

来源:互联网 发布:java自定义表单设计器 编辑:程序博客网 时间:2024/05/23 12:48
#include "stm32f10x.h"void Delay(u32);void fputc(u8);void uart_init(u32);int main(void){     GPIO_InitTypeDef  GPIO_InitStructure;NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //ÉèÖÃNVICÖжϷÖ×é2:2λÇÀÕ¼ÓÅÏȼ¶£¬2λÏìÓ¦ÓÅÏȼ¶uart_init(115200); //´®¿Ú³õʼ»¯Îª115200  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;    //LED0-->PB.5 ¶Ë¿ÚÅäÖà  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;  //ÍÆÍìÊä³ö  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO¿ÚËÙ¶ÈΪ50MHz  GPIO_Init(GPIOB, &GPIO_InitStructure);     //³õʼ»¯GPIOB.5  GPIO_ResetBits(GPIOB, GPIO_Pin_5);  while(1){fputc(0x9);Delay(10);   }}void uart_init(u32 bound){  //GPIO¶Ë¿ÚÉèÖà  GPIO_InitTypeDef GPIO_InitStructure;USART_InitTypeDef USART_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOA, ENABLE);//ʹÄÜUSART1£¬GPIOAʱÖÓ  //USART1_TX   GPIOA.9  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA.9  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//¸´ÓÃÍÆÍìÊä³ö  GPIO_Init(GPIOA, &GPIO_InitStructure);//³õʼ»¯GPIOA.9     //USART1_RX  GPIOA.10³õʼ»¯  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//PA10  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//¸¡¿ÕÊäÈë  GPIO_Init(GPIOA, &GPIO_InitStructure);//³õʼ»¯GPIOA.10       //USART ³õʼ»¯ÉèÖÃUSART_InitStructure.USART_BaudRate = bound;//´®¿Ú²¨ÌØÂÊUSART_InitStructure.USART_WordLength = USART_WordLength_8b;//×Ö³¤Îª8λÊý¾Ý¸ñʽ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); //³õʼ»¯´®¿Ú1  USART_Cmd(USART1, ENABLE);                    //ʹÄÜ´®¿Ú1 } void fputc(u8 ch){      USART_SendData(USART1, (unsigned char) ch);      while(USART_GetFlagStatus(USART1,USART_FLAG_TC)!=SET);}void Delay(u32 count){   u32 i=0;for(;i<count;i++);}

原创粉丝点击