查询方式接收都行呢,但是中断没反应
我看了iar例程和技术手册
手册上有个这东西
RXNEIE: RXNE Interrupt Enable.
This bit is set and cleared by software.
0: Interrupt is inhibited
1: An USART interrupt is generated whenever ORE=1 or RXNE=1 in the USART_SR register
我不知道在哪设定,还是它包含在什么地方了,我没找到,例程的文件里也没有
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
看来是别的问题了,实在弄不懂,程序如下
/******************** (C) COPYRIGHT 2007 STMicroelectronics ********************
* File Name : main.c
* Author : MCD Application Team
* Date First Issued : 02/05/2007
* Description : Main program body
********************************************************************************
* History:
* 05/21/2007: V0.3
* 04/02/2007: V0.2
* 02/05/2007: V0.1
********************************************************************************
* THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME.
* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT,
* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE
* CONTENT OF SUCH SOFTWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING
* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*******************************************************************************/
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x_lib.h"
void RCC_Config(void);
void GPIO_Config(void);
void USART_Config(void);
void NVIC_Config(void);
int main(void)
{
RCC_Config();
GPIO_Config();
NVIC_Config();
USART_Config();
USART_SendData(USART2, 0x55);
while(1)
{
/*if(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == SET)
{
USART_SendData(USART2, USART_ReceiveData(USART2));
}*/
}
}
/*************************************************
函数: void RCC_Config(void)
功能: 配置系统时钟
参数: 无
返回: 无
**************************************************/
void RCC_Config(void)
{
ErrorStatus HSEStartUpStatus;//定义外部高速晶体启动状态枚举变量
/* RCC system reset(for debug purpose) */
RCC_DeInit();
/* Enable HSE */
RCC_HSEConfig(RCC_HSE_ON);
/* Wait till HSE is ready */
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS)
{
/* HCLK = SYSCLK */
RCC_HCLKConfig(RCC_SYSCLK_Div1);
/* PCLK2 = HCLK */
RCC_PCLK2Config(RCC_HCLK_Div1);
/* PCLK1 = HCLK/2 */
RCC_PCLK1Config(RCC_HCLK_Div2);
/* Flash 2 wait state */
FLASH_SetLatency(FLASH_Latency_2);
/* Enable Prefetch Buffer */
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/* PLLCLK = 8MHz * 9 = 72 MHz */
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
/* Enable PLL */
RCC_PLLCmd(ENABLE);
/* Wait till PLL is ready */
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{
}
/* Select PLL as system clock source */
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/* Wait till PLL is used as system clock source */
while(RCC_GetSYSCLKSource() != 0x08)
{
}
}
/* Enable GPIOA, GPIOB and AFIO clocks */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE |
RCC_APB2Periph_AFIO, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
}
/*************************************************
函数: void GPIO_Config(void)
功能: GPIO配置
参数: 无
返回: 无
**************************************************/
void GPIO_Config(void)
{
//设置RTS(PD.04),Tx(PD.05)为推拉输出模式
GPIO_InitTypeDef GPIO_InitStructure; //定义GPIO初始化结构体
GPIO_PinRemapConfig(GPIO_Remap_USART2, ENABLE);//使能GPIO端口映射USART2
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;//选择PIN4 PIN5
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //引脚频率50M
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;//引脚设置推拉输出
GPIO_Init(GPIOD, &GPIO_InitStructure);//初始化GPIOD
GPIO_Init(GPIOE, &GPIO_InitStructure);//初始化GPIOD
//GPIO_Init(GPIOD, &GPIO_InitStructure);//初始化GPIOD
//配置CTS (PD.03),USART2 Rx (PD.06)为浮点输入模式
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOD, &GPIO_InitStructure);
}
/*************************************************
函数: void NVIC_Config(void)
功能: USART配置
参数: 无
返回: 无
**************************************************/
void NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
#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
/* Configure the NVIC Preemption Priority Bits */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/*************************************************
函数: void USART_Config(void)
功能: USART配置
参数: 无
返回: 无
**************************************************/
void USART_Config(void)
{
USART_InitTypeDef USART_InitStructure; //定义串口初始化结构体
USART_InitStructure.USART_BaudRate = 9600;//波特率9600
USART_InitStructure.USART_WordLength = USART_WordLength_8b;//8位数据
USART_InitStructure.USART_StopBits = USART_StopBits_1;//1个停止位
USART_InitStructure.USART_Parity = USART_Parity_No ; //无校验位
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
//禁用RTSCTS硬件流控制
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//使能发送接收
USART_InitStructure.USART_Clock = USART_Clock_Disable; //串口时钟禁止
USART_InitStructure.USART_CPOL = USART_CPOL_Low; //时钟下降沿有效
USART_InitStructure.USART_CPHA = USART_CPHA_2Edge;//数据在第二个时钟沿捕捉
USART_InitStructure.USART_LastBit = USART_LastBit_Disable;
//最后数据位的时钟脉冲不输出到SCLK引脚
USART_Init(USART2, &USART_InitStructure);//初始化串口2
/* Enable the USART Receive interrupt: this interrupt is generated when the
USART1 receive data register is not empty */
USART_ITConfig(USART2, USART_IT_RXNE , ENABLE);
USART_Cmd(USART2, ENABLE);//串口2使能
}
/******************* (C) COPYRIGHT 2007 STMicroelectronics *****END OF FILE****/
中断
void USART2_IRQHandler(void)
{
u8 ttt;
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
{
/* Read one byte from the receive data register */
ttt = USART_ReceiveData(USART2);
/* Clear the USART2 Receive interrupt */
// USART_ClearITPendingBit(USART2, USART_IT_RXNE);
}
if(USART_GetITStatus(USART2, USART_IT_TXE) != RESET)
{
/* Write one byte to the transmit data register */
USART_SendData(USART2, ttt);
/* Clear the USART2 transmit interrupt */
//USART_ClearITPendingBit(USART2, USART_IT_TXE);
}
}
usart2时钟没打开呀
/* Enable USART2 clocks */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
没看清,时钟没问题,好象设断点不行
我开始在中断设断点,始终检测不到USART_IT_RXNE != RESET,后来干脆把断点全去掉了,还是可以接收到数据的
什么断点呀,说的是仿真么,我是直接下载程序的呀
我怀疑是我的其他文件有问题,但我都是从例程里烤过来的,那个
stm32f10x_vector.c
不用改吧
楼上的大虾的程序是i*a*r4.42的么,能否参考一下?最好是整个工程
我的邮箱ilymyhk@sohu.com
经过hkap大虾的提醒,把这句
if(USART_GetITStatus(USART2, USART_IT_TXE) != RESET)
屏蔽后,可以了,但是我该怎么改呢
我上面那句是照抄的,判断错了是么
RESET应该为0是吧,表示发送区为空,原来的例程是说,发送区不为空则继续发
真晕,我也每仔细看
已经可以了,非常感谢hkap的帮助
把!=改为==就可以了,我可真苯啊