|
RT,串口设置了中断,现在返回值一直发个没完,不知道是什么原因!
#include "stm32f10x.h"
void Dis_Init ( void ) //四个LED的初始化,LED1,2,3,4分别接在PD2,PD3,PD4,PD7
{
GPIO_InitTypeDef GPIO_InitStructure;
//RCC_APB2PeriphClockCmd ( RCC_APB2Periph_GPIOD, ENABLE );
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init ( GPIOD, &GPIO_InitStructure );
}
void USART1_Init ( void )
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
//RCC_APB2PeriphClockCmd ( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO | RCC_APB2Periph_USART1, ENABLE ); //使能GPIOD的时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA9设置为输出
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //速度50MHz
GPIO_Init ( GPIOA, &GPIO_InitStructure );
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //PA10设置为输入
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入
GPIO_Init ( GPIOA, &GPIO_InitStructure );
USART_InitStructure.USART_BaudRate = 115200; //设置波特率
USART_InitStructure.USART_WordLength = USART_WordLength_8b; //字符长度为8字节
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_Tx | USART_Mode_Rx; //使用接受和发送功能
USART_Init ( USART1, &USART_InitStructure ); //初始化串口1
USART_Cmd ( USART1, ENABLE ); //使能串口1
USART_ITConfig ( USART1, USART_IT_RXNE, ENABLE ); //使能USART1接受中断
NVIC_PriorityGroupConfig ( NVIC_PriorityGroup_1 ); //分组方式选择第一组
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //使能USART1道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占优先级为0,即抢占优先级为0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //从优先级为0,即响应优先级为0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能
NVIC_Init ( &NVIC_InitStructure );
}
void delay ( u32 count )
{
for ( ; count != 0; count -- );
}
int main ( void )
{
// uint16_t temp = 0x0000;
RCC_APB2PeriphClockCmd ( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOD | RCC_APB2Periph_AFIO | RCC_APB2Periph_USART1, ENABLE );
Dis_Init ();
USART1_Init ();
while (1) //死循环
{
// USART_SendData ( USART1, temp );
// while ( USART_GetFlagStatus ( USART1, USART_FLAG_TXE ) == RESET ); //等待数据发送完毕
// temp ++;
// delay ( 0xeffff );
// GPIO_SetBits ( GPIOD, GPIO_Pin_4 ); //指示灯
// delay ( 0xeffff );
// GPIO_ResetBits ( GPIOD, GPIO_Pin_4 );
}
}
void USART1_IRQHandler ( void )
{
u16 temp = 0;
if ( USART_GetITStatus ( USART1, USART_IT_RXNE ) != RESET )
{
temp = USART_ReceiveData ( USART1 );
USART_SendData ( USART1, 0xf0 );
while ( USART_GetFlagStatus ( USART1, USART_FLAG_TXE ) == RESET ); //等待数据发送完毕
GPIO_SetBits ( GPIOD, GPIO_Pin_3 ); //指示灯
delay ( 0xeffff );
GPIO_ResetBits ( GPIOD, GPIO_Pin_3 );
}
}
|
阿莫论坛20周年了!感谢大家的支持与爱护!!
曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
|