标签:
USART:Universal Synchronous/Asynchronous Receiver/Transmitter
通用同步/异步接收和发送器
(1)同步通信
双方必须先建立同步,即双方的时钟要调整到一个频率,收发双发不停地发送和接受连续的同步比特流。
(2)异步通信
接收端必须时刻做好接受准备,而发送端则可以选择何时进行发送,但是发送时需要加一个开始标志和一个结束标志,表示一个发送阶段。
异步通信的优势在于简单。
RX:表示是接受端
TX:表示时发送端
①一个状态寄存器(USART_SR)
②一个数据寄存器(USART_DR):它是由两个寄存器组成的,一个给发送用(发送寄存器 TDR),一个给接收用(接收寄存器 RDR)
③一个波特率寄存器(USART_BRR)
④一个控制寄存器1(USART_CR1)
⑤一个控制寄存器2(USART_CR2)
⑥一个控制寄存器3(USART_CR3)
⑦一个保护时间和预分频寄存器(USART_GTPR)
/*---------------- Universal Synchronous Asynchronous Receiver Transmitter --*/ typedef struct { vu16 SR; u16 RESERVED0; vu16 DR; u16 RESERVED1; vu16 BRR; u16 RESERVED2; vu16 CR1; u16 RESERVED3; vu16 CR2; u16 RESERVED4; vu16 CR3; u16 RESERVED5; vu16 GTPR; u16 RESERVED6; } USART_TypeDef; //用结构体USART_TypeDef定义USARTx串口,(即定义 USARTx串口的7个设置寄存器)
(1)编程时,USARTx串口的具体配置是从USARTx寄存器组开始。首先,用结构体USART_TypeDef 定义 USARTx寄存器组。有代码,有封装好了的哈哈哈哈a
#include "bsp_usart1.h" void USART1_Config(void) { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; /* config USART1 clock */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE); /* USART1 GPIO config */ /* Configure USART1 Tx (PA.09) as alternate function push-pull */ 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); /* Configure USART1 Rx (PA.10) as input floating */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); /* USART1 mode config */ 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); }
(2)初始化函数有了的话,我们可以直接进行下一步功能的编程
//×?·?′?·¢?í /* ?èá??ó′??ú£?′ò?a′??ú£?????1152002¨ì??ê£? ???????ú£?????3ìDò£?μ????óê?μ?×?·?′?£? μ???·¢?í×?·?′?£?3¤?èD?óú100£?μ¥???úè?2? ?óê?oó·μ????μ????£ò2?éò?DT??3ìDò£??óê? ò???·μ??ò???£??éò?·¢?íè?òa3¤?èμ?×?·?′?£? ′ú???ú×¢êí2?·?£?·¢?íêy×??ò??×???£???ê? μ?ê?êy×??ó1£??ò??×????ó1. ′??ú·¢?íμ?ê?×?·?μ?ASCII??£?êy×?0?a9ê? 0x30-0x39,a-zê?0x61-0x7a,A-Zê?0x41-0x5a ?éò?????′??ú??ê?·?ê??aHEX??ê?£¨ê?áù??????ê?£? 2é?′?á1??£ */ #include "stm32f10x.h" #include "bsp_usart1.h" void USART1_Config(void); uint_least8_t ch[100],re[100],i,j; #define endchar ‘.‘ //ê?è?μ?×?·?′??áê?·? void delay(void) { uint16_t i=6000; while(i--); } unsigned char display[]="????μ?×óéè??D?á401êμ?éêò?£";//μ¥???ú·¢?í??PC?úμ?×?·?′? /* int main(void) { USART1_Config(); //′??ú3?ê??ˉ for(j=0;display[j]!=‘\0‘;j++) //·¢?í×?·?′? { USART_SendData(USART1,display[j]); //·¢?íò???êy?Y while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); //μè′y·¢?í?áê? } while(1) { while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET); //μè′yPC?úμ?êy?Yμ?à′ i = USART_ReceiveData(USART1); //?óê?êy?Y if(i==‘9‘)i=‘0‘-1; if(i==‘z‘)i=‘a‘-1; if(i==‘Z‘)i=‘A‘-1; USART_SendData(USART1,i+1); //·¢?íò???êy?Y while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);//μè′y·¢?í?áê? } } */ /* */ int main(void) { USART1_Config(); //′??ú3?ê??ˉ for(j=0;display[j]!=‘\0‘;j++) //·¢?í×?·?′? { USART_SendData(USART1,display[j]); //·¢?íò???êy?Y while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); //μè′y·¢?í?áê? } while(1) { while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET); //μè′yPC?úμ?êy?Yμ?à′ re[0] = USART_ReceiveData(USART1); //?óê?êy?Y i=0; while(re[i]!=endchar) //êy?Y?1??·¢?í?áê? { i++; while (USART_GetFlagStatus(USART1, USART_FLAG_RXNE) == RESET);//μè′yêy?Yμ?à′ re[i] = USART_ReceiveData(USART1); //?óê?êy?Y } for(j=0;j<i+1;j++)ch[j]=re[j]; //?′??×?·?′? delay(); //′úì?êμ?êó|ó??Dμ?′|àíoˉêy for(j=0;j<i;j++) //·¢?í??PC?ú { USART_SendData(USART1,ch[j]); //·¢?íò???êy?Y while (USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);//μè′y·¢?í?áê? } } }
标签:
原文地址:http://www.cnblogs.com/BlueMountain-HaggenDazs/p/4419827.html