这是主程序:#include "pbdata.h"
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
void USART_Configuration(void);
int fputc(int ch,FILE *f) //printf 重定向
{
USART_SendData(USART1,(u8)ch);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
return ch;
}
int main(void)
{
RCC_Configuration(); //系统时钟初始化
GPIO_Configuration();//端口初始化
USART_Configuration();//串口初始化
NVIC_Configuration();//中断初始化
while(1)
{
u8 i=0;
for(i=0;i<14;i++)
{
USART_SendData(USART3,txd[i]);
while( USART_GetFlagStatus(USART1,USART_FLAG_TC)!= SET); //串口3发送完成标志检测
}
while( USART_GetFlagStatus(USART3,USART_FLAG_RXNE)== RESET)//串口3接收数据标志检测
{
printf(" RxData[0]=%10x
",RxData[0]);
delay_ms(1000);
delay_ms(1000);
printf(" RxData[1]=%10x
",RxData[1]);
delay_ms(1000);
delay_ms(1000);
printf(" RxData[2]=%10x
",RxData[2]);
delay_ms(1000);
delay_ms(1000);
printf(" RxData[3]=%10x
",RxData[3]);
delay_ms(1000);
delay_ms(1000);
printf(" RxData[4]=%10x
",RxData[4]);
delay_ms(1000);
delay_ms(1000);
}
}
//d1=123456;
//printf("10进制整型输出 d1=%10d
",d1);
//d1=123456;
//printf("16进制输出 d1=%X
",d1);//X 大写输出 x小写输出
//d2=12.345;
//printf("浮点数输出 d2=%f
",d2);
///printf("浮点数输出 d2=%.2f
",d2);
//printf("浮点数输出 d2=%10.2f
",d2);
//d3='a';
//printf("字符输出 d3=%c
",d3);
}
void RCC_Configuration(void)
{
SystemInit();//72m
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//管脚A时钟打开
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD,ENABLE); //管脚D时钟打开
RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE);//串口1时钟打开
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);//串口3时钟打开
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);//复用时钟打开
GPIO_PinRemapConfig(GPIO_FullRemap_USART3,ENABLE);//串口3端口重映射
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
//串口1管脚配置
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;//TX
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;//RX
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA,&GPIO_InitStructure);
//串口3管脚配置
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8;//TX
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_Init(GPIOD,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;//RX
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOD,&GPIO_InitStructure);
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
//串口1中断配置
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
//串口3中断配置
NVIC_InitStructure.NVIC_IRQChannel = USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void USART_Configuration(void)
{
//串口1初始化
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_HardwareFlowControl=USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
USART_Init(USART1,&USART_InitStructure);
USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
USART_Cmd(USART1,ENABLE);
USART_ClearFlag(USART1,USART_FLAG_TC);
//串口1初始化3初始化
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(USART3,&USART_InitStructure);
USART_ITConfig(USART3,USART_IT_RXNE,ENABLE);
USART_Cmd(USART3,ENABLE);
USART_ClearFlag(USART3,USART_FLAG_TC);
}
中断程序:
#include "stm32f10x_it.h"
#include "stm32f10x_exti.h"
#include "stm32f10x_rcc.h"
#include "misc.h"
#include "pbdata.h"
void NMI_Handler(void)
{
}
void USART1_IRQHandler(void)
{
if(USART_GetITStatus(USART1,USART_IT_RXNE)!=RESET)//串口1接收中断检测
{
USART_SendData(USART1,USART_ReceiveData(USART1));
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
}
}
void USART3_IRQHandler(void)
{
if(USART_GetITStatus(USART3,USART_IT_RXNE)!=RESET)//串口3接收中断检测
{
while( USART_GetFlagStatus(USART3,USART_FLAG_RXNE)!= RESET)//串口3接收数据标志检测
{
RxData[flag]=USART_ReceiveData(USART3);
flag++;
}
flag=0;
}
}
公共函数:
#ifndef _pbdata_H
#define _pbdata_H
#include "stm32f10x.h"
#include "misc.h"
#include "stm32f10x_exti.h"
#include "stm32f10x_tim.h"
#include "stm32f10x_usart.h"
#include "stdio.h"
//定义变量
extern u8 dt;
extern u8 flag;
extern u8 RxData[14];
extern u8 txd[14];
//定义函数
void RCC_HSE_Configuration(void);
void delay(u32 nCount);
void delay_us(u32 nus);
void delay_ms(u16 nms);
#endif
这是要发送的数据数组:u8 RxData[14]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
这是要接收的数据存放数组u8 txd[14]={0xFF,0xFF,0x0B,0x00,0x00,0x7B,0x81,0x01,0x00,0x00,0x02,0x00,0x00,0x0A};
求哪位大侠伸出援手,帮帮小弟,小弟真心感谢,搞了两天已经人废了,都搞不出,百度论坛各种找,查不出原因!
一周热门 更多>