新手入门
- 积分
- 31
- 金钱
- 31
- 注册时间
- 2013-7-11
- 在线时间
- 0 小时
|
#include "stm32f10x.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_usart.h"
void GPIO_Initialization()
{
GPIO_InitTypeDef GPIO_InitStructure; //从GPIO_InitTypeDef 开始找,还要加上GPIO_Init
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA,&GPIO_InitStructure);
}
void USART_Interrupt()
{
#if EN_USART1_RX //如果使能了接收
//Usart1 NVIC 配置
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=3 ;//抢占优先级3
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; //子优先级3
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能
NVIC_Init(&NVIC_InitStructure); //根据指定的参数初始化VIC寄存器
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE); //开启接收中断
USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
#endif
USART_Cmd(USART1, ENABLE);
}
void RCC_Initialization()
{
SystemInit();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1, ENABLE ); //或标志,启动了GPIO B和E的 时钟。控制GPIO口
//先要初始化GPIO口和RCC(时钟)
}
void USART_Initialization()
{
USART_InitTypeDef USART_InitStructure;
USART_InitStructure.USART_BaudRate=9600;
USART_InitStructure.USART_WordLength=USART_WordLength_8b;
USART_InitStructure.USART_StopBits=USART_StopBits_1;
USART_InitStructure.USART_Parity=USART_Parity_No;
USART_InitStructure.USART_Mode=USART_Mode_Rx | USART_Mode_Tx;
USART_InitStructure.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
USART_Init(USART1, &USART_InitStructure);
}
void USART_Reset()
{
USART_DeInit(USART1);
}
void Delay(vu32 n)
{
for(;n!=0;n--);
}
int main()
{
RCC_Initialization();
USART_Reset();
GPIO_Initialization();
USART_Initialization();
USART_Interrupt();
while(1)
{
if(USART_GetITStatus(USART1, USART_IT_RXNE)!= RESET)
{
u8 res;
res=USART_ReceiveData(USART1);
USART_SendData(USART1,res);
Delay(50);
}
}
}
又是按着实验改了,本来不加和中断相关的语句。能写能读,但是一直在读数据。 加了和中断相关的语句以后,写完数据,读不出来。。。串口助手R一直为0,这什么情况。
|
|