希望一次就可以调通。
代码如下
头文件
#include "stm32f10x.h"
#include "stm32_eval.h"
#include "stm32f10x_it.h"
#include
void RCC_Configuration(void);
void NVIC_Configuration(void);
void USART_Config(void);
unsigned char USART1_ReceiveChar(void);//接收一个字符
char tmpvv=0;
char rbuff[100];
配置函数
void RCC_Configuration(void)
{
ErrorStatus HSEStartUpStatus;
RCC_ClocksTypeDef RccClocks;
RCC_DeInit();
RCC_HSEConfig(RCC_HSE_ON);
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS)
{
RCC_HCLKConfig(RCC_SYSCLK_Div1);
RCC_PCLK2Config(RCC_HCLK_Div1);
RCC_PCLK1Config(RCC_HCLK_Div2);
FLASH_SetLatency(FLASH_Latency_2);
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
RCC_PLLCmd(ENABLE);
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
while(RCC_GetSYSCLKSource() != 0x08);
}
RCC_GetClocksFreq(&RccClocks);
}
void USART_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure; //io口初始化声明
USART_InitTypeDef USART_InitStructure; //串口初始化声明
USART_ClockInitTypeDef USART_ClockInitStructure; //定义USART时钟初始化结构体声明
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 , ENABLE);
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);
//包括所有串口的配置
USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;
//提升USART时钟时使能还是失能,钟低电平活动
USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
//指定SLCK引脚上时钟的极性
USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
//时钟第二个边缘进行数据捕获
USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;
//在SCLK引脚上输出最后发送的那个数据字的脉冲不从SCLK输出
USART_ClockInit(USART1, &USART_ClockInitStructure);
//USART工作在异步模式下 包括所有串口
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_HardwareFlowControl = USART_HardwareFlowControl_None;//无硬件控制流
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//发送接收均使能
USART_Init(USART1, &USART_InitStructure);//初始化串口1 可以在这打开串口2
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);//打开串口1的接收中断
// USART_ITConfig(USART1,USART_IT_TXE,ENABLE); //打开串口1的发送中断
USART_Cmd(USART1, ENABLE); //打开串口1中断
}
uint8_t uart_putchar(uint8_t ch)
{
USART_SendData(USART1, (uint8_t) ch);
while (!(USART1->SR & USART_FLAG_TXE));
}
uint32_t uart_send( uint8_t *pdata, uint16_t length)
{
while(length--)
{
uart_putchar(*pdata++);
}
return length;
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
#ifdef VECT_TAB_RAM
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else
NVIC_SetVectorTable(NVIC_VectTab_FLASH+ 0x4000 , 0x4000);
#endif
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);//配置优先级组为第1组(抢占式优先级有1位,副优先级有3位)
//配置USART中断
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;//中断通道为RTC全局中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;//抢占优先级为1
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;//副优先级为0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;//中断通道使能
NVIC_Init(&NVIC_InitStructure);//初始化NVIC结构?
}
main函数如下
int main(void)
{
uint16_t uid[]={0};
RCC_Configuration();
NVIC_Configuration();
USART_Config();
while (1)
{
// uart_send(rbuff,5);
}
}
中断函数在it.c中添加
#include "stm32f10x_it.h"
extern void USART1_Putc(char c);//输出一个字符
extern unsigned char USART1_ReceiveChar(void);//接收一个字符
extern char rbuff[100];
extern char tmpvv;
void USART1_IRQHandler(void)
{
if ( USART_GetITStatus(USART1, USART_IT_RXNE)!= RESET)//查询是否为秒中断标志
{
char i=0,j;
char t=0x00;
USART_ClearITPendingBit(USART1,USART_IT_RXNE); //清中断标识
tmpvv= USART_ReceiveData(USART1);
USART_SendData(USART1,t);
// USART_SendData(USART1,tmp );
// rbuff[i++] = USART_ReceiveData(USART1); //全局变量
}
}