源码的目录结构如下:
STM32LIB
USER
USER目录如下
hardware_dri
main
software_module
hardware_dri目录
Adc_hard_dri.c
//uiAdData:存放AD采样的数据
//ucSampleNum:存放AD采样的次数
//ucRemoveNum:存放最低值与最高值的个数
//去掉的数值放在数组0-(cRemoveNum-1)及(cSampleNum-ucRemoveNum)-(ucSampleNum-1),即最大值与最小值均放在两端
void choise(u16 *uiAdData,u8 ucSampleNum,u8 ucRemoveNum)
{
u32 i,j,k,temp;
for(i=0;i<ucRemoveNum;i++)//将最小的ucRemoveNum值排在数组的前ucRemoveNum位
{
k=i; /*给记号赋值*/
for(j=i+1;j<ucSampleNum;j++)
if(uiAdData[k]>uiAdData[j]) k=j; /*是k总是指向最小元素*/
if(i!=k)
{ /*当k!=i是才交换,否则a[i]即为最小*/
temp=uiAdData[i];
uiAdData[i]=uiAdData[k];
uiAdData[k]=temp;
}
}
for(i=ucSampleNum-1;i>ucSampleNum-ucRemoveNum-1;i--)//将最大的ucRemoveNum排在数组的后ucRemoveNum位
{
k=i; /*给记号赋值*/
for(j=i-1;j>1;j--)
if(uiAdData[k]<uiAdData[j]) k=j; /*是k总是指向最大元素*/
if(i!=k)
{
temp=uiAdData[i];
uiAdData[i]=uiAdData[k];
uiAdData[k]=temp;
}
}
}
//初始化 ADC
//这里我们仅以规则通道为例
//我们默认将开启通道 0~3
void Adc_Init(void)
{
ADC_InitTypeDef ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |RCC_APB2Periph_ADC1 , ENABLE ); //使能 ADC1 通道时钟
RCC_ADCCLKConfig(RCC_PCLK2_Div6); //设置 ADC 分频因子 6
//72M/6=12,ADC 最大时间不能超过 14M
//PA1 作为模拟通道输入引脚
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;//模拟输入
GPIO_Init(GPIOA, &GPIO_InitStructure); //初始化 GPIOA
ADC_DeInit(ADC1); //复位 ADC1,将外设 ADC1 的全部寄存器重设为缺省值
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; //ADC 独立模式
ADC_InitStructure.ADC_ScanConvMode = DISABLE; //单通道模式
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; //单次转换模式
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;//转换由
//软件而不是外部触发启动
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //ADC 数据右对齐
ADC_InitStructure.ADC_NbrOfChannel = 1; //顺序进行规则转换的 ADC 通道的数目
ADC_Init(ADC1, &ADC_InitStructure); //根据指定的参数初始化外设 ADCx 器
ADC_Cmd(ADC1, ENABLE); //使能指定的 ADC1
ADC_ResetCalibration(ADC1); //开启复位校准
while(ADC_GetResetCalibrationStatus(ADC1)); //等待复位校准结束
ADC_StartCalibration(ADC1); //开启 AD 校准
while(ADC_GetCalibrationStatus(ADC1)); //等待校准结束
}
//获得 ADC 值
//ch:通道值 0~3
u16 Get_Adc(u8 ch)
{
//设置指定 ADC 的规则组通道,设置它们的转化顺序和采样时间
ADC_RegularChannelConfig(ADC1, ch, 1, ADC_SampleTime_239Cycles5 ); //通道 1
//规则采样顺序值为 1,采样时间为 239.5 周期
ADC_SoftwareStartConvCmd(ADC1, ENABLE); //使能指定的 ADC1 的
//软件转换启动功能
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC ));//等待转换结束
return ADC_GetConversionValue(ADC1); //返回最近一次 ADC1 规则组的转换结果
}
//求平均函数
u16 Get_Adc_Average(u8 ch)
{
u16 uiAdd=0;
u8 ucSampleNum=16;
u8 ucRemoveNum=2;
u16 uiAdData[16]={0};
u8 i;
//连续多次次采样
for(i=0;i<ucSampleNum;i++)
uiAdData[i]=Get_Adc(ch); //读取AD转换结果,赋予数组
choise(uiAdData,ucSampleNum,ucRemoveNum) ;//去掉俩个最大值、去掉两个最小值
for(i=ucRemoveNum;i<ucSampleNum-ucRemoveNum;i++)
{
uiAdd+=uiAdData[i];
}
return (uiAdd/((u16)(ucSampleNum-2*ucRemoveNum))); //返回平均值
}
#include "stm32f10x.h"/*
1,初始化GPIO位输入和时钟
2,初始化复用时钟
3,将gpio和外部中断挂钩起来
4,设置外部中断参数、
5,设置中断向量参数
stm32外部中断线16个 可以匹配到任意的io口,
但外部中断函数只有6个
EXPORT EXTI0_IRQHandler
EXPORT EXTI1_IRQHandler
EXPORT EXTI2_IRQHandler
EXPORT EXTI3_IRQHandler
EXPORT EXTI4_IRQHandler
EXPORT EXTI9_5_IRQHandler
EXPORT EXTI15_10_IRQHandler
中断线 0-4 每个中断线对应一个中断函数,
中断线 5-9 共用中断函数 EXTI9_5_IRQHandler,
中断线 10-15 共用中断函数 EXTI15_10_IRQHandler
*/
void Exit_0_Init(u8 IRQChannelPreemptionPriority_exit0, u8 IRQChannelSubPriority_exit0)
{
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_AFIO,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //要设置的PIN
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //下拉输入
GPIO_Init(GPIOA,&GPIO_InitStructure); //配置 IO
//将gpioA 和外步中断0连接起来 就是gpioA.0和外部中断0
GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource0);
//外部中断参数
EXTI_InitStructure.EXTI_Line= EXTI_Line0; //选中断0
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; //模式是中断触发
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising ; //上升沿电平触发
EXTI_InitStructure.EXTI_LineCmd = ENABLE; //中断使能
EXTI_Init(&EXTI_InitStructure); //初始化
//中断向量参数
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn; //使能按键所在的外部中断通道
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = IRQChannelPreemptionPriority_exit0; //抢占优先级 2,
NVIC_InitStructure.NVIC_IRQChannelSubPriority = IRQChannelSubPriority_exit0; //子优先级 2
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能外部中断通道
NVIC_Init(&NVIC_InitStructure); //初始化
}
#include "stm32f10x.h"
//定时器 5 通道 1 输入捕获配置
TIM_ICInitTypeDef TIM4_ICInitStructure;
void TIM4_Cap_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //①使能 TIM4 时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); //①使能 GPIOB 时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; //PB6 清除之前设置
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD; //PB6 输入
GPIO_Init(GPIOB, &GPIO_InitStructure);//初始化 GPIOB.6 ①
GPIO_ResetBits(GPIOB,GPIO_Pin_6); //PB6 下拉
TIM_TimeBaseStructure.TIM_Period = arr; //设定计数器自动重装值
TIM_TimeBaseStructure.TIM_Prescaler =psc; //预分频器
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; // TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM 向上计数模式
TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure); //②根据指定的参数初始化 TIMx
TIM4_ICInitStructure.TIM_Channel = TIM_Channel_1; // 选择输入端 IC1 映射到 TI1 上
TIM4_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; //上升沿捕获
TIM4_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; //映射到 TI1 上
TIM4_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; //配置输入分频,不分频
TIM4_ICInitStructure.TIM_ICFilter = 0x00;//IC1F=0000 配置输入滤波器 不滤波
TIM_ICInit(TIM4, &TIM4_ICInitStructure);//③初始化 TIM4 输入捕获通道 1
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn; //TIM3 中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2; //先占优先级 2 级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //从优先级 0 级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ 通道被使能
NVIC_Init(&NVIC_InitStructure); //⑤根据指定的参数初始化 NVIC
TIM_ITConfig( TIM4,TIM_IT_Update|TIM_IT_CC1,ENABLE);//④允许更新中断捕获中断
TIM_Cmd(TIM4,ENABLE ); //⑥使能定时器 4
}
#include "stm32f10x.h"
#include "Iwdg_hard_dri.h"
//初始化独立看门狗
//prer:分频数:0~7(只有低 3 位有效!)
//分频因子=4*2^prer.但最大值只能是 256!
//rlr:重装载寄存器值:低 11 位有效.
//时间计算(大概):Tout=((4*2^prer)*rlr)/40 (ms).
//prer 4 rlr 625 时间是1s
void IWDG_Init(u8 prer,u16 rlr)
{
IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable); //①使能对寄存器 I 写操作
IWDG_SetPrescaler(prer); //②设置 IWDG 预分频值:设置 IWDG 预分频值
IWDG_SetReload(rlr); //②设置 IWDG 重装载值
IWDG_ReloadCounter(); //③按照 IWDG 重装载寄存器的值重装载 IWDG 计数器
IWDG_Enable(); //④使能 IWDG
}
//喂独立看门狗
void IWDG_Feed(void)
{
IWDG_ReloadCounter();//reload
}
#include "stm32f10x.h"
#include "Pwm_hard_dri.h"
//TIM2 -----TIM5都一样
//TIM3 PWM 部分初始化
//PWM 输出初始化
//arr:自动重装值
//psc:时钟预分频数
//pwm频率= 72000000/((arr+1)*(psc+1)) hz
//周期 = ((arr+1)*(psc+1))/72000000 s
// arr 为计数值
//通用定时器每个都可以产生TIMX_CH1---4 路pwm
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_GPIOB|RCC_APB2Periph_AFIO, ENABLE); //①使能 GPIO 和 AFIO 复用功能时钟
GPIO_PinRemapConfig(GPIO_PartialRemap_TIM3, ENABLE); //②重映射 TIM3_CH2->PB5
//设置该引脚为复用输出功能,输出 TIM3 CH2 的 PWM 脉冲波形 GPIOB.5
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //TIM_CH2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);//①初始化 GPIO
//初始化 TIM3
TIM_TimeBaseStructure.TIM_Period = arr; //设置在自动重装载周期值
TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM 向上计数模式
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //③初始化 TIMx
//初始化 TIM3 Channel2 PWM 模式 一般有4个通道
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //选择 PWM 模式 2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; //输出极性高
TIM_OC2Init(TIM3, &TIM_OCInitStructure); //④初始化外设 TIM3 OC2
TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable); //使能预装载寄存器
TIM_Cmd(TIM3, ENABLE); //⑤使能 TIM3
}
// pwmval越大,正脉宽越小
void TIM3_pwm_out(u16 pwmval)
{
TIM_SetCompare2(TIM3, pwmval);
}
#include "stm32f10x.h"
#include "Time_hard_dri.h"
//@
//通用定时器 3 中断初始化
//这里时钟选择为 APB1 的 2 倍,而 APB1 为 36M
//arr:自动重装值。
//psc:时钟预分频数
//这里使用的是定时器 3! Tout= ((arr+1)*(psc+1))/Tclk?
// Tout= ((4999+1)*( 7199+1))/72=500000us=500ms。
/*
Tclk:TIM3 的输入时钟频率(单位为 Mhz)。
Tout:TIM3 溢出时间(单位为 us)。
TIM2---TIM5 都一样
*/
void TIM2_User_Init(u16 arr,u16 psc,u8 IRQChannelPreemptionPriority_TIM2, u8 IRQChannelSubPriority_TIM2)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //①时钟 TIM3 使能
//定时器 TIM3 初始化
TIM_TimeBaseStructure.TIM_Period = arr; //设置自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler = psc;//设置时钟频率除数的预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1 ; //设置时钟分割
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM 向上计数
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //②初始化 TIM3
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE ); //③允许更新中断
//中断优先级 NVIC 设置
NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; //TIM3 中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = IRQChannelPreemptionPriority_TIM2; //先占优先级 0 级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = IRQChannelSubPriority_TIM2; //从优先级 3 级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ 通道被使能
NVIC_Init(&NVIC_InitStructure); //④初始化 NVIC 寄存器
TIM_Cmd(TIM2, ENABLE); //⑤使能 TIM3
}
void TIM2_Stop(void)
{
TIM_Cmd(TIM2, DISABLE);
}
//@
//通用定时器 3 中断初始化
//这里时钟选择为 APB1 的 2 倍,而 APB1 为 36M
//arr:自动重装值。
//psc:时钟预分频数
//这里使用的是定时器 3! Tout= ((arr+1)*(psc+1))/Tclk?
// Tout= ((4999+1)*( 7199+1))/72=500000us=500ms。
/*
Tclk:TIM3 的输入时钟频率(单位为 Mhz)。
Tout:TIM3 溢出时间(单位为 us)。
TIM2---TIM5 都一样
*/
void TIM3_User_Init(u16 arr,u16 psc,u8 IRQChannelPreemptionPriority_TIM3, u8 IRQChannelSubPriority_TIM3)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); //①时钟 TIM3 使能
//定时器 TIM3 初始化
TIM_TimeBaseStructure.TIM_Period = arr; //设置自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler = psc;//设置时钟频率除数的预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1 ; //设置时钟分割
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM 向上计数
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //②初始化 TIM3
TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE ); //③允许更新中断
//中断优先级 NVIC 设置
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; //TIM3 中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = IRQChannelPreemptionPriority_TIM3; //先占优先级 0 级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = IRQChannelSubPriority_TIM3; //从优先级 3 级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ 通道被使能
NVIC_Init(&NVIC_InitStructure); //④初始化 NVIC 寄存器
TIM_Cmd(TIM3, ENABLE); //⑤使能 TIM3
}
void TIM3_Stop(void)
{
TIM_Cmd(TIM3, DISABLE);
}
void TIM4_User_Init(u16 arr,u16 psc,u8 IRQChannelPreemptionPriority_TIM4, u8 IRQChannelSubPriority_TIM4)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //①时钟 TIM3 使能
//定时器 TIM3 初始化
TIM_TimeBaseStructure.TIM_Period = arr; //设置自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler = psc;//设置时钟频率除数的预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1 ; //设置时钟分割
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM 向上计数
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //②初始化 TIM3
TIM_ITConfig(TIM4,TIM_IT_Update,ENABLE ); //③允许更新中断
//中断优先级 NVIC 设置
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn; //TIM3 中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = IRQChannelPreemptionPriority_TIM4; //先占优先级 0 级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = IRQChannelSubPriority_TIM4; //从优先级 3 级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ 通道被使能
NVIC_Init(&NVIC_InitStructure); //④初始化 NVIC 寄存器
TIM_Cmd(TIM4, ENABLE); //⑤使能 TIM3
}
void TIM4_Stop(void)
{
TIM_Cmd(TIM4, DISABLE);
}
void TIM5_User_Init(u16 arr,u16 psc,u8 IRQChannelPreemptionPriority_TIM5, u8 IRQChannelSubPriority_TIM5)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5, ENABLE); //①时钟 TIM3 使能
//定时器 TIM3 初始化
TIM_TimeBaseStructure.TIM_Period = arr; //设置自动重装载寄存器周期的值
TIM_TimeBaseStructure.TIM_Prescaler = psc;//设置时钟频率除数的预分频值
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1 ; //设置时钟分割
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM 向上计数
TIM_TimeBaseInit(TIM5, &TIM_TimeBaseStructure); //②初始化 TIM3
TIM_ITConfig(TIM5,TIM_IT_Update,ENABLE ); //③允许更新中断
//中断优先级 NVIC 设置
NVIC_InitStructure.NVIC_IRQChannel = TIM5_IRQn; //TIM3 中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = IRQChannelPreemptionPriority_TIM5; //先占优先级 0 级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = IRQChannelSubPriority_TIM5; //从优先级 3 级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ 通道被使能
NVIC_Init(&NVIC_InitStructure); //④初始化 NVIC 寄存器
TIM_Cmd(TIM5, ENABLE); //⑤使能 TIM3
}
void TIM5_Stop(void)
{
TIM_Cmd(TIM5, DISABLE);
}
#include "stm32f10x.h"
#include "Uart_hard_dri.h"
/*
1:使能串口和gpio时钟
2:串口复位
3:初始化gpio和串口参数
4:串口中断使能
5:向量中断设置
*/
void User_Uart1_init(u32 BAUD1 ,u8 IRQChannelPreemptionPriority_uart1, u8 IRQChannelSubPriority_uart1 )
{
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_USART1,ENABLE);
//打开时钟
USART_DeInit(USART1);
//串口复位
/* USART1 GPIO config */
/* Configure USART1 Tx (PA.09) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9; //要设置的PIN
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; //输出速度
GPIO_Init(GPIOA,&GPIO_InitStructure); //配置 IO
/* Configure USART1 Rx (PA.10) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; //RX 接收引脚
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入模式
GPIO_Init(GPIOA, &GPIO_InitStructure); //配置IO口
/* USART1 mode config */
USART_InitStructure.USART_BaudRate= BAUD1; //设定波特率
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;//无硬件控制 CTS RTS
USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx; //发送接收 使能
USART_Init(USART1,&USART_InitStructure); //配置USART1
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);//串口接收中断使能
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn ;//通道设置为串口1中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = IRQChannelPreemptionPriority_uart1; //中断占先等级0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = IRQChannelSubPriority_uart1; //中断响应优先级0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //打开中断
NVIC_Init(&NVIC_InitStructure); //初始化
USART_Cmd(USART1,ENABLE); //使能 串口1
}
void User_Uart1_Send_Byte(u8 byte) //串口发送一个字节
{
USART_SendData(USART1, byte);
while( USART_GetFlagStatus(USART1,USART_FLAG_TC)!= SET);
//等待发送完成。 检测 USART_FLAG_TC 是否置1;
}
void User_Uart2_init(u32 BAUD2, u8 IRQChannelPreemptionPriority_uart2, u8 IRQChannelSubPriority_uart2)
{
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOG,ENABLE); //485收发控制端口
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
//打开时钟
USART_DeInit(USART2);
//串口复位
//485使能端
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; //PG9端口配置
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* USART1 GPIO config */
/* Configure USART2 Tx (PA.02) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2; //要设置的PIN
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; //输出速度
GPIO_Init(GPIOA,&GPIO_InitStructure); //配置 IO
/* Configure USART2 Rx (PA.03) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; //RX 接收引脚
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入模式
GPIO_Init(GPIOA, &GPIO_InitStructure); //配置IO口
/* USART2 mode config */
USART_InitStructure.USART_BaudRate=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;//无硬件控制 CTS RTS
USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx; //发送接收 使能
USART_Init(USART2,&USART_InitStructure); //配置USART1
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//串口接收中断使能
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn ;//通道设置为串口2中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = IRQChannelPreemptionPriority_uart2; //中断占先等级0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = IRQChannelSubPriority_uart2; //中断响应优先级0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //打开中断
NVIC_Init(&NVIC_InitStructure); //初始化
USART_Cmd(USART2,ENABLE); //使能 串口1
}
void User_Uart2_Send_Byte(u8 byte) //串口发送一个字节
{
USART_SendData(USART2, byte);
while( USART_GetFlagStatus(USART2,USART_FLAG_TC)!= SET);
//等待发送完成。 检测 USART_FLAG_TC 是否置1;
}
// USART_SendData(USART2,buf[t]);
void User_Uart4_init(u32 BAUD4, u8 IRQChannelPreemptionPriority_uart4, u8 IRQChannelSubPriority_uart4)
{
NVIC_InitTypeDef NVIC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_UART4,ENABLE);
//打开时钟
USART_DeInit(UART4);
//串口复位
/* USART1 GPIO config */
/* Configure USART4 Tx (Pc.10) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10; //要设置的PIN
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; //输出速度
GPIO_Init(GPIOC,&GPIO_InitStructure); //配置 IO
/* Configure USART4 Rx (Pc.11) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11; //RX 接收引脚
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入模式
GPIO_Init(GPIOC, &GPIO_InitStructure); //配置IO口
/* USART2 mode config */
USART_InitStructure.USART_BaudRate=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;//无硬件控制 CTS RTS
USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx; //发送接收 使能
USART_Init(UART4,&USART_InitStructure); //配置USART1
USART_ITConfig(UART4, USART_IT_RXNE, ENABLE);//串口接收中断使能
NVIC_InitStructure.NVIC_IRQChannel = UART4_IRQn ;//通道设置为串口2中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = IRQChannelPreemptionPriority_uart4; //中断占先等级0
NVIC_InitStructure.NVIC_IRQChannelSubPriority = IRQChannelSubPriority_uart4; //中断响应优先级0
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //打开中断
NVIC_Init(&NVIC_InitStructure); //初始化
USART_Cmd(UART4,ENABLE); //使能 串口1
}
void User_Uart4_Send_Byte(u8 byte) //串口发送一个字节
{
USART_SendData(UART4, byte);
while( USART_GetFlagStatus(UART4,USART_FLAG_TC)!= SET);
//等待发送完成。 检测 USART_FLAG_TC 是否置1;
}