新手上路
- 积分
- 48
- 金钱
- 48
- 注册时间
- 2021-3-26
- 在线时间
- 6 小时
|
3金钱
void usart1_Init()
{
GPIO_InitTypeDef GPIO_InitStrue;
USART_InitTypeDef USART_InitStrue;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);
GPIO_InitStrue.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStrue.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStrue.GPIO_Speed = GPIO_Speed_50MHz; //发送串口PB10
GPIO_Init(GPIOA,&GPIO_InitStrue);
GPIO_InitStrue.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStrue.GPIO_Pin = GPIO_Pin_10; //接收串口PB11
GPIO_Init(GPIOA,&GPIO_InitStrue);
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority= 1;//抢占优先级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化NVIC寄存器
USART_InitStrue.USART_BaudRate = 115200; //波特率
USART_InitStrue.USART_WordLength = USART_WordLength_8b; //8位数据
USART_InitStrue.USART_StopBits = USART_StopBits_1; //停止位
USART_InitStrue.USART_Parity = USART_Parity_No; //无奇偶校验
USART_InitStrue.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_InitStrue.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_Init(USART1,&USART_InitStrue);
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE); //接收中断
USART_Cmd(USART1,ENABLE);
}
void USART1_IRQHandler()
{
u8 a;
if(USART_GetITStatus(USART1,USART_IT_RXNE) == 1) //该函数用于中断检测
{
a = USART_ReceiveData(USART1);
if(a == 101)
GPIO_SetBits(GPIOE,GPIO_Pin_5);
if(a == 102)
GPIO_SetBits(GPIOB,GPIO_Pin_5);
if(a == 103)
{
GPIO_ResetBits(GPIOE,GPIO_Pin_5);
GPIO_ResetBits(GPIOB,GPIO_Pin_5);
}
// USART_SendData(USART1,a);
// while(USART_GetFlagStatus(USART1,USART_FLAG_TC) == 0);
}
}
void led1_Init()
{
GPIO_InitTypeDef GPIO_InitStrue;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOE,ENABLE);
GPIO_InitStrue.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStrue.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStrue.GPIO_Pin = GPIO_Pin_5;
GPIO_Init(GPIOB,&GPIO_InitStrue);
GPIO_InitStrue.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStrue.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStrue.GPIO_Pin = GPIO_Pin_5;
GPIO_Init(GPIOE,&GPIO_InitStrue);
//GPIO_SetBits(GPIOB,GPIO_Pin_5);
//GPIO_SetBits(GPIOE,GPIO_Pin_5);
}
int main()
{
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
led1_Init();
// pwm1_Init(200-1,7200-1);
usart1_Init();
while(1);
}
|
|