#ifdef VECT_TAB_RAM
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif
usart实验,这几句我没加也能进中断,我同学写的程序中不加就不能进中断,必须加,那哪种情况况下不需要加?
#include "stm32f10x.h"
void RCC_Configuration(void);
void USATR_Configuration(uint32_t USART_BaudRate);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
void Delay(vu32 times);
int main(void)
{
int i;
unsigned char TXBuf[] = "hello welcome to china";
RCC_Configuration(); //RCC是第一个初始化
GPIO_Configuration();
USATR_Configuration(128000);
NVIC_Configuration(); //NVIC是最后一个,应在USART后面,如果在前面则程序不对
GPIO_SetBits(GPIOA,GPIO_Pin_8);
GPIO_SetBits(GPIOD,GPIO_Pin_2);
USART_ClearFlag(USART1,USART_FLAG_TC);
/*for(i=0;TXBuf[i]!='\0';i++)
{
USART_SendData(USART1,TXBuf[i]);
GPIO_ResetBits(GPIOA,GPIO_Pin_8);
Delay(100000);
while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET);
GPIO_SetBits(GPIOA,GPIO_Pin_8);
Delay(100000);
} */
while(1)
{
}
}
void RCC_Configuration(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE); //串口时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO | RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOD,ENABLE); //GPIO时钟
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure; //LED0初始化
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_Out_OD;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; //LED1初始化
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_Out_OD;
GPIO_Init(GPIOD,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //PA10为RXD,初始化 开发板上的RXD和TXD连接
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //PA9为TXD,初始化
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_AF_PP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
}
void USATR_Configuration( uint32_t USART_BaudRate)
{
USART_InitTypeDef USART_InitStructure; //串口初始化
USART_InitStructure.USART_BaudRate = USART_BaudRate;
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_Tx | USART_Mode_Rx;
USART_Init(USART1,&USART_InitStructure);
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE); //接收中断
USART_Cmd(USART1,ENABLE); //打开串口
}
void NVIC_Configuration(void) //中断嵌套向量
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //中断优先级分组
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; //*3.4的库不是使用USART1_IRQChannel,看stm32f10x.h吧*/
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占优先级
NVIC_InitStructure. NVIC_IRQChannelSubPriority = 0; //响应优先级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void Delay(vu32 times)
{
while(times--);
}
void USART1_IRQHandler(void)
{
char RX_Data;
GPIO_ResetBits(GPIOA,GPIO_Pin_8);
if(USART_GetFlagStatus(USART1,USART_FLAG_RXNE)==SET) //接收数据非空 ,使用发生中断也可以,//RX_Flag=USART_GetITStatus(USART1,USART_IT_RXNE),因为一有数据接收就会进入中断。
{
USART_ClearITPendingBit(USART1,USART_IT_RXNE); //清除中断标志
RX_Data=USART_ReceiveData(USART1);
USART_SendData(USART1,RX_Data);
if(USART_GetITStatus(USART1,USART_IT_RXNE)==SET)
{
USART_ClearITPendingBit(USART1,USART_IT_RXNE);
}
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET); //发送收到的数据 ,很多程序用TC来判断,会导致数据收不到,用TXE就不会这种问题。
GPIO_SetBits(GPIOA,GPIO_Pin_8);
GPIO_ResetBits(GPIOD,GPIO_Pin_2);
}
}
上面是我的程序,没加那几句。 |