今天學(xué)習(xí)了串口通信,全雙工異步的USART,一個起始位,8個或者9個數(shù)據(jù)位,所以數(shù)據(jù)位傳輸?shù)臅r候設(shè)為16位,一位檢驗位,一位結(jié)束為,分別初始化串口,查詢硬件原理圖串口1掛在AHB1總線上,初始化時鐘,封發(fā)送數(shù)據(jù)的函數(shù),檢驗狀態(tài)標(biāo)志位,程序如下:
#include "main.h"
void UART1_config(void)
{
GPIO_InitTypeDef GPIO_InitStruct;
USART_InitTypeDef USART_InitStruct;
//1.????GPIO1ü?? PA9 PA10 ?′ó?
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
GPIO_InitStruct.GPIO_Pin? = GPIO_Pin_9|GPIO_Pin_10;
GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_Init(GPIOA, &GPIO_InitStruct);
//2.éè??1ü???′ó?°ó?¨
GPIO_PinAFConfig(GPIOA, GPIO_PinSource9,GPIO_AF_USART1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource10,GPIO_AF_USART1);
//3.3?ê??ˉ′??ú1
USART_InitStruct.USART_BaudRate? = 9600;
USART_InitStruct.USART_WordLength = USART_WordLength_8b;
USART_InitStruct.USART_Parity? = USART_Parity_No;
USART_InitStruct.USART_StopBits = USART_StopBits_1;
USART_InitStruct.USART_Mode = USART_Mode_Rx|USART_Mode_Tx;
USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_Init(USART1,&USART_InitStruct);
USART_Cmd(USART1, ENABLE);
}
void uart_send_data(u8 data)
{
USART_SendData(USART1, data);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE) == RESET);
}
void uart_send_str(u8 *buf,u32 n)
{
u32 i;
for(i=0;i
int main(void)
{
u8 *str="hello,world";
UART1_config();
uart_send_str(str,11);
while (1)
{
}
}