TA的每日心情 | 奋斗 2013-9-16 09:51 |
---|
签到天数: 16 天 连续签到: 1 天 [LV.4]偶尔看看III
|
本帖最后由 upc_arm 于 2012-12-16 11:09 编辑
USART中断类似外部中断 分别配置 时钟、GPIO、USART、NVIC
配置时钟 使能时钟- /* Enable GPIO clock */
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
- /* Enable USART clock */
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
复制代码 配置USART的GPIO,都配置成上拉使能。- //GPIO
- /* Configure USART Tx as alternate function push-pull */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
- 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(GPIOA, &GPIO_InitStructure);
- <blockquote> GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
复制代码 配置USART- /* Connect PXx to USARTx_Tx */
- GPIO_PinAFConfig(GPIOA, GPIO_PinSource9, GPIO_AF_1);
- /* Connect PXx to USARTx_Rx */
- GPIO_PinAFConfig(GPIOA, GPIO_PinSource10, GPIO_AF_1);
复制代码- 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(USART1, &USART_InitStructure);
- /* Enable USART */
- USART_Cmd(USART1, ENABLE);
复制代码 配置NVIC- //NVIC
- /* Enable and set EXTI6 Interrupt */
- <blockquote> NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
复制代码 main函数- int main(void)
- {
- uint8_t test = 0;
-
- LED_Init();
- MyUSART_Init();
- while (1)
- {
- MyUSART_PutChar(USART1, test + '0');
- test++;
- if(test == 10)
- {
- test = 0;
- }
- SysTick_Delay_nms(500);
- LED_Ctrl_Toggle(LED1);
- }
- }
复制代码 MyUSART_PutChar函数- void MyUSART_PutChar(USART_TypeDef* USARTx, uint8_t ch)
- {
- USART_SendData(USARTx, (uint8_t) ch);
- while (USART_GetFlagStatus(USARTx, USART_FLAG_TXE) == RESET)
- {}
- }
复制代码 PC机用串口连接开发板,打开串口助手,可以收到01234567890等数据。
|
|