#include "usart.h"//毫秒级的延时extern u8 rx_done;
extern u8 buf[9];
u8 i;
void init(){GPIO_Config();
NVIC_Configuration();
USART1_Config();
USART2_Config();
}void Reive_data(){buf[0]=rx_buff[0];
buf[1]=rx_buff[1];
buf[2]=rx_buff[2];
buf[3]=rx_buff[3];
buf[4]=rx_buff[4];
buf[5]=rx_buff[6];
buf[6]=rx_buff[6];
buf[7]=rx_buff[7];
buf[8]=rx_buff[8];
}int main() { init();
while(1) { Reive_data();
if(rx_done==1) { rx_done=0;
for(i=0;
i<9;
i++) { USART_SendData(USART1,buf[ i]);
while(USART_GetFlagStatus(USART1,USART_FLAG_TXE)==RESET);
} } }}#ifndef __USART_H#define __USART_H#include "stm32f10x.h"uint8_t rx_buff[9];
//接收缓存uint8_t rx_done =0;
//接收完成标志uint8_t rx_cnt=0;
//接收数据长度u8 buf[9];
void delay_ms(u16 nms){u32 temp;
SysTick->LOAD = 9000*nms;
SysTick->VAL=0X00;
//清空计数器SysTick->CTRL=0X01;
//使能,减到零是无动作,采用外部时钟源do{temp=SysTick->CTRL;
//读取当前倒计数值}while((temp&0x01)&&(!(temp&(1<<16))));
//等待时间到达SysTick->CTRL=0x00;
//关闭计数器SysTick->VAL =0X00;
//清空计数器}void GPIO_Config(void) { GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_13;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);
/*使能SWD 禁用JTAG*/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
// GPIO_Write(GPIOB,x);
2// ReadValue = GPIO_ReadOutputData(GPIOB);
GPIO_Init(GPIOB, &GPIO_InitStructure);
}void TIM3_PWM_Init(u16 arr,u16 psc){ GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
//使能定时器3时钟// RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);
//使能GPIO外设和AFIO复用功能模块时钟使能 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE );
// 不开AFIO也可以 //设置该引脚为复用输出功能,输出TIM2 CH3的PWM脉冲波形 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
//TIM_CH1(通道1就是PA6) GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
//复用推挽输出。这句好关键啊。。。。 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//初始化TIM2 TIM_TimeBaseStructure.TIM_Period = arr;
//设置在下一个更新事件装入活动的自动重装载寄存器周期的值 TIM_TimeBaseStructure.TIM_Prescaler =psc;
//设置用来作为TIMx时钟频率除数的预分频值 TIM_TimeBaseStructure.TIM_ClockDivision = 0;
//设置时钟分割:TDTS = Tck_tim TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
//TIM向上计数模式 TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
//初始化TIM2 Channel3 PWM模式 TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
//选择定时器模式:TIM脉冲宽度调制模式2 必须是2 TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
//比较输出使能,使能PWM输出到端口。// TIM_OCInitStructure.TIM_Pulse=127;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
//输出极性:TIM输出比较极性 TIM_OC2Init(TIM3, &TIM_OCInitStructure);
//使能通道1 TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable);
//使能TIM2在CCR2上的预装载寄存器 TIM_ARRPreloadConfig(TIM3, ENABLE);
//使能TIMx在ARR上的预装载寄存器 TIM_Cmd(TIM3, ENABLE);
//使能TIM3总开关} void steerl(u8 d){ if(d==0){TIM_SetCompare2(TIM3,1500);
}//输出PWM舵机转0度 舵机松开 if(d==1){TIM_SetCompare2(TIM3,7500);
}//输出PWM舵机转180度 舵机夹取} static void NVIC_Configuration(void){NVIC_InitTypeDef NVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
//嵌套向量中断控制器组选择NVIC_InitStructure.NVIC_IRQChannel=USART1_IRQn;
//配置USART为中断源NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
//抢断优先级NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
//子优先级NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
//使能中断NVIC_Init(&NVIC_InitStructure);
//初始化配置NVIC}void USART1_Config(void){GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
//使能串口GPIO的时钟RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
//使能串口外设的时钟GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;
//将USART1?Tx的GPIO配置为推挽复用模式GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
//将USART1?Rx的GPIO配置为浮空输入模式GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA,&GPIO_InitStructure);
USART_InitStructure.USART_BaudRate=115200;
//配置波特率115200USART_InitStructure.USART_WordLength=USART_WordLength_8b;
//配置数据字长8bitUSART_InitStructure.USART_StopBits=USART_StopBits_1;
//配置停止位1bitUSART_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);
//完成串口的初始化配置NVIC_Configuration();
//串口中断优先级配置USART_ITConfig(USART1,USART_IT_RXNE,ENABLE);
//使能串口接收中断USART_ITConfig(USART1,USART_IT_IDLE,ENABLE);
//空闲中断使能USART_Cmd(USART1,ENABLE);
//使能串口}void USART2_Config(void){ //GPIO端口设置 GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
//使能USART2,GPIOB时钟//USART2_TX GPIOB10. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
//复用推挽输出 GPIO_Init(GPIOA, &GPIO_InitStructure);
//初始化PB10 //USART12_RX GPIOB.11初始化 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
//浮空输入 GPIO_Init(GPIOA, &GPIO_InitStructure);
//初始化PB11 //Usart2 NVIC 配置 NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0 ;
//抢占优先级0 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
//子优先级3 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
//IRQ通道使能 NVIC_Init(&NVIC_InitStructure);
//根据指定的参数初始化VIC寄存器 //USART 初始化设置 USART_InitStructure.USART_BaudRate = 115200;
//串口波特率 USART_InitStructure.USART_WordLength = USART_WordLength_8b;
//字长为8位数据格式 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(USART2, &USART_InitStructure);
//初始化串口2 // USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
//开启串口2接受中断 USART_Cmd(USART2, ENABLE);
//使能串口2 }
void USART1_IRQHandler(void){ uint8_t temp;
if(USART_GetITStatus(USART1,USART_IT_RXNE)!=RESET)//接收到一个字节,进入一次接收中断 { USART_ClearITPendingBit(USART1,USART_IT_RXNE);
//清除接收中断标志 rx_buff[rx_cnt++]=USART_ReceiveData(USART1);
//将接收的数据存入rx_buff中 // if(rx_cnt>=9) rx_cnt=0;
//每包数据不能超过接收buff的总长度 }if(USART_GetITStatus(USART1,USART_IT_IDLE)!=RESET)//接收完数据后进入空闲中断{USART_ClearITPendingBit(USART1,USART_IT_IDLE);
//这条语句是无效的temp=USART1->SR;
//先读SR,再读DR才能完成idle中断的清零,否则会一直进入中断。temp=USART1->DR;
rx_done =1;
//检测到空闲状态,置位接收完成位if(rx_done==1) GPIO_SetBits(GPIOC,GPIO_Pin_13);
else{GPIO_ResetBits(GPIOC,GPIO_Pin_13);
}rx_cnt=0;
}}#endif
|