|
//usart.c
#include "sys.h"
#include "usart.h"
#include "includes.h"
void My_USART_Init(void)
{
GPIO_InitTypeDef GPIO_InitStrue;
USART_InitTypeDef USART_Initstrue;
NVIC_InitTypeDef NVIC_Initstrue;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);
GPIO_InitStrue.GPIO_Mode=GPIO_Mode_AF_PP;//有時(shí)需注意這里的模式
GPIO_InitStrue.GPIO_Pin=GPIO_Pin_10;
GPIO_InitStrue.GPIO_Speed=GPIO_Speed_10MHz;
GPIO_Init(GPIOB,&GPIO_InitStrue); //每個(gè)引腳配置都需要
GPIO_InitStrue.GPIO_Mode=GPIO_Mode_IN_FLOATING
GPIO_InitStrue.GPIO_Pin=GPIO_Pin_11;
GPIO_InitStrue.GPIO_Speed=GPIO_Speed_10MHz;
GPIO_Init(GPIOB,&GPIO_InitStrue);
USART_Initstrue.USART_BaudRate=115200;
USART_Initstrue.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART_Initstrue.USART_Mode=USART_Mode_Rx | USART_Mode_Tx;
USART_Initstrue.USART_Parity=USART_Parity_No;
USART_Initstrue.USART_StopBits=USART_StopBits_1;
USART_Initstrue.USART_WordLength=USART_WordLength_8b;
USART_Init(USART3,&USART_Initstrue);
USART_ITConfig(USART3,USART_IT_RXNE,ENABLE);
USART_Cmd(USART3,ENABLE);
NVIC_Initstrue.NVIC_IRQChannel=USART3_IRQn;
NVIC_Initstrue.NVIC_IRQChannelCmd=ENABLE;
NVIC_Initstrue.NVIC_IRQChannelPreemptionPriority=1;
NVIC_Initstrue.NVIC_IRQChannelSubPriority=1;
NVIC_Init(&NVIC_Initstrue);
}
//中斷服務(wù)函數(shù):如果是UART,如UART4,則注意是void UART4_IRQHandler(void)
void USART3_IRQHandler(void)
{
u8 res;
if(USART_GetITStatus(USART3,USART_IT_RXNE))
{
res=USART_ReceiveData(USART3);
USART_SendData(USART3,res);
}
}
//main.c
typedef int u16_t;
#include "stm32f10x.h"
#include "usart.h"
int main(void)
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
My_USART_Init();
LED_Init();
while(1)
{ }
}
|
評(píng)分
-
查看全部評(píng)分
|