//Author:"泪与汗的缠绵"
//Data:2011-11月
//功能:积累编程的思想与套路
本页所包含的配置函数有:
void SysTick_Configuration(void);
void GPIO_Configuration(void); //端口引脚功能的配置
void EXTI_Configuration(void);//配置外部中断线
void NVIC_Configuration(void); //配置中断的优先级、内部中断向量
void RCC_Configuration(void); //配置系统及其外设的时钟
void IWDG_Configuration(void);//看门狗的配置结构
void TIM_Configuration(void)
void ADC_configuration();
void DMA_Configuration(void);
void DAC_Configuration(void);
//同时需要配置的结构体变量
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
*******//Cortex系统定时器(SysTick)*********************
*******************************************************
*******************************************************
void SysTick_Configuration(void)
{
//选择系统定时器(SysTick)的时钟源
SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8);
SysTick_SetReload(250000);
SysTick_CounterCmd(SysTick_Counter_Enable);
SysTick_ITConfig(ENABLE);
}
*********///GPIO的配置程序***************************************
******************************************************************
*******************************************************************
void GPIO_Configuration(void) void RCC_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
//中断线路初始化
void EXTI_Configuration(void)
{
EXTI_InitStructure.EXTI_Line = EXTI_Line9;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
EXTI_InitStructure.EXTI_Line = EXTI_Line13;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
}
///嵌套中断向量NVIC**********************************
/
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource9);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel=EXTI0_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
//为EXTI_Line9使能并配置中断向量
//为EXTI_Line0使能并配置中断向量
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource9);
NVIC_InitStructure.NVIC_IRQChannel=EXTI9_5_IRQChannel;
NVIC_Init(&NVIC_InitStructure);
//为EXTI_Line9使能并配置中断向量
//为EXTI_Line13使能并配置中断向量
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource9);
NVIC_InitStructure.NVIC_IRQChannel=EXTI15_10_IRQChannel;
NVIC_Init(&NVIC_InitStructure);
}
********************************************************************
**************系统时钟的配置RCC_Configuration**********************
void RCC_Configuration(void)
{
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_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOB |RCC_APB2Periph_AFIO, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
}[page]
*****************//初始化独立看门狗******************************
********************************************************************
********************************************************************
********************************************************************
void IWDG_Configuration(void)
{
//是能或者失能对寄存器IWDG_PR和IWDG_RLR
IWDG_WriteAccessCmd(IWDG_WriteAccess_Enable);
//选定工作频率
IWDG_SetPrescaler(IWDG_Prescaler_32);
//设置重装载值
IWDG_SetReload(230);
//按照重装载器的值重装载IWDG计数器的值-----------此动作相当于“喂狗”
IWDG_ReloadCounter();
//使能IWDG
IWDG_Enable();
}
/ *******************
/
TIM_DeInit(TIM1); //将外设TIMx寄存器重设为缺省值
//2、基础设置
TIM1_TimeBaseStructure.TIM_Prescaler = 0x0; //预分频,此值+1为分频的除数得到计数周期;
TIM1_TimeBaseStructure.TIM_Period = 0xFFFF;//计数周期 (即当计数值达到“计数值”时,刚好用时一个计数周期)
TIM1_TimeBaseStructure.TIM_ClockDivision = 0x0;//时钟因子
TIM1_TimeBaseStructure.TIM_RepetitionCounter = 0x0;
TIM1_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //计数器模式
//TIM_CounterMode_Dowm 向下计数
//TIM_CounterMode_CenterAligned1 中心对齐方式1
// TIM_CounterMode_CenterAligned2 中心对齐方式2
// TIM_CounterMode_CenterAligned3 中心对齐方式3
TIM_TimeBaseInit(TIM1,&TIM1_TimeBaseStructure);
// 3、输出通道设置
TIM1_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
//TIM_OCMode_Timing输出比较时间模式(输出引脚冻结无效)
//TIM_OCMode_Active 输出比较主动模式(匹配时设置输出引脚为有效电平,当计数值为比较/捕获寄存器值相同时,强制输出为高平)
TIM_OCMode_Inactive; 输出比较非主动模式 (匹配时设置输出引脚为无效电平,当计数值为比较/捕获寄存器值相同时,强制输出为低电平)
//TIM_OCMode_Toggle 输出比较触发模式(翻转。当计数值与比较/捕获寄存器值相同时,翻转输出引脚的电平)
//TIM_OCMode_PWM1向上计数时,当TIMx_CNT < TIMx_CCR*时,输出电平有效,否则为无效,
//向下计数时,当TIMx_CNT > TIMx_CCR*时,输出电平无效,否则为有效
//TIM_OCMode_PWM2 ;与PWM1模式相反
TIM1_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM1_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
TIM1_OCInitStructure.TIM_Pulse = CCR1_Val;
TIM1_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
TIM1_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_Low;
TIM1_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
TIM1_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OC1Init(TIM1,&TIM1_OCInitStructure);
TIM1_OCInitStructure.TIM_Pulse = CCR2_Val;
TIM_OC2Init(TIM1,&TIM1_OCInitStructure);
TIM1_OCInitStructure.TIM_Pulse = CCR3_Val;
TIM_OC3Init(TIM1,&TIM1_OCInitStructure);
TIM1_BDTRInitStructure.TIM_OSSRState = TIM_OSSRState_Enable;
TIM1_BDTRInitStructure.TIM_OSSIState = TIM_OSSIState_Enable;
TIM1_BDTRInitStructure.TIM_LOCKLevel = TIM_LOCKLevel_1;
TIM1_BDTRInitStructure.TIM_DeadTime = 0x75;
TIM1_BDTRInitStructure.TIM_Break = TIM_Break_Enable;
TIM1_BDTRInitStructure.TIM_BreakPolarity = TIM_BreakPolarity_High;
TIM1_BDTRInitStructure.TIM_AutomaticOutput = TIM_AutomaticOutput_Enable;
TIM_BDTRConfig(TIM1,&TIM1_BDTRInitStructure);
TIM_Cmd(TIM1,ENABLE);
TIM_CtrlPWMOutputs(TIM1,ENABLE);
}
////////////AD的配置函数/////////////////////////////
//时钟配置:
// ADCCLK = PCLK2/4
//应用配置
void ADC_configuration()
{
ADC_InitTypeDef ADC_InitStructure;
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = ENABLE;
//ADC_ScanConvMode 规定了模数转换工作在扫描模式(多通道)还是单次(单通道)模式。可以设置这个
//问ENABLE 或者 DISABLE。
ADC_InitStructure.ADC_ContinuousConvMode=ENABLE;
//ADC_ContinuousConvMode 规定了模数转换工作在连续还是单次模式。可以设置这个参数为 ENABLE 或
//DISABLE。
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
//ADC_ExternalTrigConv 描述
//ADC_ExternalTrigConv_T1_CC1 选择定时器 1 的捕获比较 1 作为转换外部触发
//ADC_ExternalTrigConv_T1_CC2 选择定时器 1 的捕获比较 2 作为转换外部触发
//ADC_ExternalTrigConv_T1_CC3 选择定时器 1 的捕获比较 3 作为转换外部触发
//ADC_ExternalTrigConv_T2_CC2 选择定时器 2 的捕获比较 2 作为转换外部触发
//ADC_ExternalTrigConv_T3_TRGO 选择定时器 3 的 TRGO作为转换外部触发
//ADC_ExternalTrigConv_T4_CC4 选择定时器 4 的捕获比较 4 作为转换外部触发
//ADC_ExternalTrigConv_Ext_IT11 选择外部中断线 11 事件作为转换外部触发
//ADC_ExternalTrigConv_None 转换由软件而不是外部触Ⅳ?
ADC_InitStructure.ADC_DataAlign=ADC_DataAlign_Right;
//ADC_DataAlign 描述:
//ADC_DataAlign_Right ADC 数据右对齐
//ADC_DataAlign_Left ADC 数据左对齐
ADC_InitStructure.ADC_NbrOfChannel = 1; // 规定了顺序进行规则转换的 ADC 通道的数目。这个数目的取值范围是 1 到16。
//ADC_NbreOfChannel
ADC_Init(ADC1, &ADC_InitStructure);
ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 1, ADC_SampleTime_55Cycles5);
//(ADC1 或 ADC2,ADC_Channel_X,Rank,ADC_SampleTime);
//规则组采样顺序。取值范围 1 到16。
// ADC_SampleTime: //ADC_SampleTime_1Cycles5 采样时间为 1.5 周期
//ADC_SampleTime_7Cycles5 采样时间为 7.5 周期
//ADC_SampleTime_13Cycles5 采样时间为 13.5 周期
//ADC_SampleTime_28Cycles5 采样时间为 28.5 周期
//ADC_SampleTime_41Cycles5 采样时间为 41.5 周期
//ADC_SampleTime_55Cycles5 采样时间为 55.5 周期
//ADC_SampleTime_71Cycles5 采样时间为 71.5 周期
//ADC_SampleTime_239Cycles5 采样时间为 239.5 周期
ADC_DMACmd(ADC1, ENABLE);
ADC_Cmd(ADC1, ENABLE);
ADC_ResetCalibration(ADC1);
while(ADC_GetResetCalibrationStatus(ADC1));//等待ADC的校准寄存器被设置完毕
ADC_StartCalibration(ADC1);
while(ADC_GetCalibrationStatus(ADC1));
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
AD_value=ADC_GetConversionValue(ADC1);//返回最近一次 ADCx 规则组的转换结果
}
void DMA_Configuration(void)
{
DMA_DeInit(DMA_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address; //定义DMA外设基地址
DMA_InitStructure.DMA_MemoryBaseAddr = (u32)&ADC_ConvertedValue; // 定义DMA内存基地址
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; //外设作为数据传输的来源(外设传到内存) DMA_DIR_PeripheralDST——内存传到外设
DMA_InitStructure.DMA_BufferSize = 3; //连续转化3个AD通道值
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; //外设寄存器地址不变
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; //内存寄存器地址递增
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word; //外设数据宽度32bit
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word; //内存数据宽度32bit
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; //DMA模式是循环
DMA_InitStructure.DMA_Priority = DMA_Priority_High; //DMA优先级
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; //DMA没有设置内存到内存传输
DMA_Init(DMA_Channel1, &DMA_InitStructure);
DMA_Cmd(DMA_Channel1, ENABLE);
}
//RCC_APB1PeriphClockCmd(RCC_APB1Periph_DAC, ENABLE);
void DAC_Configuration(void)
{
DAC_InitTypeDef DAC_InitStructure;
DAC_InitStructure.DAC_Trigger=DAC_Trigger_Software; //DAC触发方式为软件控制
DAC_InitStructure.DAC_WaveGeneration=DAC_WaveGeneration_None; //不从DAC端口产生波形
DAC_InitStructure.DAC_LFSRUnmask_TriangleAmplitude=DAC_LFSRUnmask_Bits8_0; //8位DA波形生成模式
DAC_InitStructure.DAC_OutputBuffer=DAC_OutputBuffer_Enable; //使能DAC输出缓冲器
//用上面参数初始化DAC通道1
DAC_Init(DAC_Channel_1, &DAC_InitStructure);
DAC_Cmd(DAC_Channel_1, ENABLE); //使能DAC通道1
DAC_Init(DAC_Channel_2, &DAC_InitStructure); //用上面参数初始化DAC通道1
DAC_Cmd(DAC_Channel_2, ENABLE); //使能DAC通道2
DA的使用方式:
DAC_SetChannel2Data(DAC_Align_12b_L, DAC_data); //设置DAC通道1为12位且数据左对齐模式
DAC_SoftwareTriggerCmd(DAC_Channel_2, ENABLE); //使能DAC通道1的软件触发方式,转换一次
DAC_SetChannel1Data(DAC_Align_12b_L, DAC_data); //设置DAC通道1为12位且数据左对齐模式
DAC_SoftwareTriggerCmd(DAC_Channel_1, ENABLE); //使能DAC通道1的软件触发方式,转换一次
}