分享到plurk 分享到twitter 分享到facebook

版本 31fd626567435bc21037be833c809336dfe4d936

embedded/USART

CODE SECTION ………………. main() { //初始化GPIO GPIO_InitTypeDef GPIO_InitStructure;

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOB, &GPIO_InitStructure);

GPIO_PinAFConfig(GPIOB, GPIO_PinSource6, GPIO_AF_USART1); GPIO_PinAFConfig(GPIOB, GPIO_PinSource7, GPIO_AF_USART1);

//初始化UART SART_InitStructure.USART_BaudRate = 115200;//設定baudrate USART_InitStructure.USART_WordLength = USART_WordLength_8b;//設定word長度為8位 USART_InitStructure.USART_StopBits = USART_StopBits_1;//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;//開啟Rx接收和Tx發送功能

USART_Init(USART1, &USART_InitStructure); USART_Cmd(USART1, ENABLE);//啟動usart1

while(1) { while(i < 100){ send_byte(‘G’); i++; } while(USART_GetFlagStatus(USART1, USART_FLAG_RXNE) != RESET)//等到有data才變為RESET b = (USART_ReceiveData(USART1) & 0x7F);

}

} void send_byte(uint8_t b) {

USART_SendData(USART1, b);

while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET);//直到USART1是空

}

……………….