int main(void)
{
delay_init();
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA,ENABLE);
led_conf();
//中断管理
NVIC_uConfigura
tion();
//USART1和USART2初始化配置
//USART1_uConfiguration(); //9600,n,8,1 使能接收中断
USART2_uConfiguration(); //9600,n,8,1 使能接收中断
//_3485_ENABLE_CONFIG();
led_on();
delay_us(700);
// 开中断标志位
while(1)
{
// USART_SendData(USART1,String);
USART_SendData(USART2,String);
delay_ms(3000);
}
}
void USART2_uConfiguration(void)
{
GPIO_InitTypeDef GPIO_uInitStructure,GPIO_uInitStr;
USART_InitTypeDef USART_uInitStructure;
// USART2_TX -> PA2
// USART2_RX -> PA3
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_1);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_1);
GPIO_uInitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_uInitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_uInitStructure.GPIO_OType = GPIO_OType_OD;
GPIO_uInitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_uInitStructure.GPIO_Speed = GPIO_Speed_50MHz; // 50M
GPIO_Init(GPIOA,&GPIO_uInitStructure);
GPIO_uInitStr.GPIO_Pin = GPIO_Pin_3;
GPIO_uInitStr.GPIO_Mode = GPIO_Mode_AF;
GPIO_uInitStr.GPIO_OType = GPIO_OType_OD;
GPIO_uInitStr.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_uInitStr.GPIO_Speed = GPIO_Speed_50MHz; // 50M
GPIO_Init(GPIOA,&GPIO_uInitStr);
USART_uInitStructure.USART_BaudRate = 9600;
USART_uInitStructure.USART_Parity = USART_Parity_No;
USART_uInitStructure.USART_StopBits = USART_StopBits_1;
USART_uInitStructure.USART_WordLength = USART_WordLength_8b;
USART_uInitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_uInitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_Init(USART2,&USART_uInitStructure);
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
USART_Cmd(USART2,ENABLE);
}
void NVIC_uConfiguration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure1;
// NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
// NVIC_InitStructure.NVIC_IRQChannelPriority = 1;
// NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
// NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure1.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure1.NVIC_IRQChannelPriority = 0;
NVIC_InitStructure1.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure1);
}
void USART2_IRQHandler(void)
{
led_off();
if(USART_GetITStatus(USART2,USART_IT_RXNE)==SET)
{
USART_ClearITPendingBit(USART2,USART_IT_RXNE);
String=USART_ReceiveData(USART2);
}
}
主程序里一直发送0x55,串口调试助手也可以接到的,说明发送没问题。我在中断服务函数写了led_off(),如果中断,led灯会熄灭,可是使用串口调试助手发送数据led并没有熄灭,发送字符也没有改变
我用的是stm32f030c8,好像没有这个函数。
一周热门 更多>