STM32初始化程序参考.txt
《STM32初始化程序参考.txt》由会员分享,可在线阅读,更多相关《STM32初始化程序参考.txt(12页珍藏版)》请在冰豆网上搜索。
/*
**--------------FileInfo---------------------------------------------------------------------------------
**Filename:
sys_init.c
**Descriptions:
ϵͳ��ʼ��.c�ļ���
**
**--------------------------------------------------------------------------------------------------------
*/
#include"includes.h"
/**************************************************************/
//����������GPIO�˿����á�
//�������ޡ�
//����ֵ���ޡ�
//�����ã�mian.c
/***************************************************************/
voidGPIO_Configuration(void)
{
GPIO_InitTypeDefGPIO_InitStructure;
/*-------��ֹJTAGʹ��SWD-------*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE);
/*---------PORTA---------*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|
GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7|
GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11|
GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin= GPIO_Pin_8;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
//GPIO_InitStructure.GPIO_Pin=GPIO_Pin_11|GPIO_Pin_12|GPIO_Pin_15;
//GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
//GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
//GPIO_Init(GPIOA,&GPIO_InitStructure);
/*---------PORTB---------*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|
GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7|
GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11|
GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
GPIO_Init(GPIOB,&GPIO_InitStructure);
/*---------PORTC---------*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|
GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7|
GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11|
GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOC,&GPIO_InitStructure);
/*---------PORTD---------*/
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_Init(GPIOD,&GPIO_InitStructure);
}
voidEXTI_Configuration(void)
{
EXTI_InitTypeDefEXTI_InitStructure;
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource6);
EXTI_InitStructure.EXTI_Line=EXTI_Line6;
EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd=ENABLE;
EXTI_Init(&EXTI_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource7);
EXTI_InitStructure.EXTI_Line=EXTI_Line7;
EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd=ENABLE;
EXTI_Init(&EXTI_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource8);
EXTI_InitStructure.EXTI_Line=EXTI_Line8;
EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Falling;
EXTI_InitStructure.EXTI_LineCmd=ENABLE;
EXTI_Init(&EXTI_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB,GPIO_PinSource9);
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);
}
/**************************************************************/
//��������������ͨѶ����.
//�������ޡ�
//����ֵ���ޡ�
//�����ã�mian.c
/***************************************************************/
voidUSART_Configuration(void)
{
GPIO_InitTypeDefGPIO_InitStructure;
USART_InitTypeDefUSART_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);
/*
*USART3_TX->PB10,USART3_RX->PA11
*/
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_11;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
USART_InitStructure.USART_BaudRate=19200;
USART_InitStructure.USART_WordLength=USART_WordLength_9b;
USART_InitStructure.USART_StopBits=USART_StopBits_1;
USART_InitStructure.USART_Parity=USART_Parity_Even;
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_ITConfig(USART3,USART_IT_TXE,ENABLE);
USART_ClearFlag(USART3,USART_FLAG_TC);
//USART_ClearFlag(USART3,USART_FLAG_TXE);
USART_Cmd(USART3,ENABLE);
}
/**************************************************************/
//����������ADC�������á�
//�������ޡ�
//����ֵ���ޡ�
//�����ã�mian.c
/***************************************************************/
voidADC_Configuration(void)
{
ADC_InitTypeDefADC_InitStructure;
DMA_InitTypeDefDMA_InitStructure;
GPIO_InitTypeDefGPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1|RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOC,ENABLE);//|RCC_APB2Periph_AFIO,ENABLE);
/*ConfigureGPIOasanaloginput-------------------------*/
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|
GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AIN;
GPIO_Init(GPIOA,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AIN;
GPIO_Init(GPIOC,&GPIO_InitStructure);
/*DMAchannel1configuration----------------------------------------------*/
DMA_DeInit(DMA1_Channel1);
/*-------DMA�����ַ-------*/
DMA_InitStructure.DMA_PeripheralBaseAddr=ADC1_DR_Address;
/*-------�洢����ַ-------*/
DMA_InitStructure.DMA_MemoryBaseAddr=(u32)ADC_ConvertedValue;
/*-------������Ϊ���ݴ������Դ-------*/
DMA_InitStructure.DMA_DIR=DMA_DIR_PeripheralSRC;
/*-------����DMA�ڴ��仺������С����λ�ɴ��䷽�����-------*/
DMA_InitStructure.DMA_BufferSize=12;
/*-------����DMA�������ַ�������-------*/
DMA_InitStructure.DMA_PeripheralInc=DMA_PeripheralInc_Disable;
/*-------�����ڼĴ�����ַ�������-------*/
DMA_InitStructure.DMA_MemoryInc=DMA_MemoryInc_Enable;
/*-------�趨�������ݿ��-------*/
DMA_InitStructure.DMA_PeripheralDataSize=DMA_PeripheralDataSize_HalfWord;
/*-------�趨�洢�����ݿ��-------*/
DMA_InitStructure.DMA_MemoryDataSize=DMA_MemoryDataSize_HalfWord;
/*-------�趨DMAΪѭ������ģʽ-------*/
DMA_InitStructure.DMA_Mode=DMA_Mode_Circular;
/*-------���ø�ͨ��DMAΪ�����ȼ�-------*/
DMA_InitStructure.DMA_Priority=DMA_Priority_High;
/*-------��ֹ�ڴ浽�ڴ�ģʽ------*/
DMA_InitStructure.DMA_M2M=DMA_M2M_Disable;
DMA_Init(DMA1_Channel1,&DMA_InitStructure);
/*EnableDMA1channel1*/
DMA_Cmd(DMA1_Channel1,ENABLE);
/*ADC1configuration------------------------------------------------------*/
/*----ADC�����ڶ���ģʽ��˫ADCģʽ-----*/
ADC_InitStructure.ADC_Mode=ADC_Mode_Independent;
/*----ADCɨ��ģʽ�ǵ�ͨ�����Ƕ�ͨ��-----*/
ADC_InitStructure.ADC_ScanConvMode=ENABLE;
/*----ADCת���ǵ��λ�������-----*/
ADC_InitStructure.ADC_ContinuousConvMode=ENABLE;
/*----�涨�ⲿ���������ADת��-----*/
ADC_InitStructure.ADC_ExternalTrigConv=ADC_ExternalTrigConv_None;
/*----ADC������뷽ʽ-----*/
ADC_InitStructure.ADC_DataAlign=ADC_DataAlign_Right;
/*----ADCת������Ŀ1����16-----*/
ADC_InitStructure.ADC_NbrOfChannel=12;
ADC_Init(ADC1,&ADC_InitStructure);
/*----�趨ָ��ADC�����飬�趨���ǵ�ת��˳��IJ���ʱ��-----*/
ADC_RegularChannelConfig(ADC1,ADC_Channel_10,1,ADC_SampleTime_41Cycles5);
ADC_RegularChannelConfig(ADC1,ADC_Channel_11,2,ADC_SampleTime_28Cycles5);
ADC_RegularChannelConfig(ADC1,ADC_Channel_12,3,ADC_SampleTime_28Cycles5);
ADC_RegularChannelConfig(ADC1,ADC_Channel_13,4,ADC_SampleTime_41Cycles5);
ADC_RegularChannelConfig(ADC1,ADC_Channel_0,5,ADC_SampleTime_28Cycles5);
ADC_RegularChannelConfig(ADC1,ADC_Channel_1,6,ADC_SampleTime_28Cycles5);
ADC_RegularChannelConfig(ADC1,ADC_Channel_2,7,ADC_SampleTime_41Cycles5);
ADC_RegularChannelConfig(ADC1,ADC_Channel_3,8,ADC_SampleTime_28Cycles5);
ADC_RegularChannelConfig(ADC1,ADC_Channel_4,9,ADC_SampleTime_28Cycles5);
ADC_RegularChannelConfig(ADC1,ADC_Channel_5,10,ADC_SampleTime_41Cycles5);
ADC_RegularChannelConfig(ADC1,ADC_Channel_6,11,ADC_SampleTime_28Cycles5);
ADC_RegularChannelConfig(ADC1,ADC_Channel_7,12,ADC_SampleTime_28Cycles5);
/*EnableADC1DMA*/
ADC_DMACmd(ADC1,ENABLE);
/*EnableADC1*/
ADC_Cmd(ADC1,ENABLE);
/*EnableADC1resetcalibarationregister*/
ADC_ResetCalibration(ADC1);
/*ChecktheendofADC1resetcalibrationregister*/
while(ADC_GetResetCalibrationStatus(ADC1));
/*StartADC1calibaration*/
ADC_StartCalibration(ADC1);
/*ChecktheendofADC1calibration*/
while(ADC_GetCalibrationStatus(ADC1));
/*StartADC1SoftwareConversion*/
ADC_SoftwareStartConvCmd(ADC1,ENABLE);
}
/**************************************************************/
//��������:
��ʱ����������
//�������ޡ�
//����ֵ���ޡ�
//�����ã�mian.c
/***************************************************************/
voidinit_timer(void)
{
TIM_TimeBaseInitTypeDefTIM_TimeBaseStructure;
TIM_OCInitTypeDefTIM_OCInitStructure;
GPIO_InitTypeDefGPIO_InitStructure;
/*------------PWM����ܽ�����----------------------*/
//RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
//GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8;
//GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
//GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
//GPIO_Init(GPIOC,&GPIO_InitStructure);
/*------------tim1��PWM���----------------------*/
///*--tim��������--*/
//RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1,ENABLE);
////TimƵ��=Tim_clk/��TIM_Period+1��
//TIM_TimeBaseStructure.TIM_Period=7200-1;
////Timʱ��Ƶ�ʵ�Ԥ��Ƶֵ
//TIM_TimeBaseStructure.TIM_Prescaler=0;
////ʱ�ӷָ�ֵ
//TIM_TimeBaseStructure.TIM_ClockDivision=0;
////���ϼ���
//TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
///*TIM1����*/
//TIM_TimeBaseStructure.TIM_RepetitionCounter=0;
///*-----*/
////��ʼ��Tim��ʱ���������
//TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure);
////����ARRӰ�ӼĴ�����ʹ�ܵ�ʱ��Ҫ��дҪ�ȵ��и����¼�ʱ�ſ��ԡ�
//TIM_ARRPreloadConfig(TIM1,ENABLE);
///*--PWMģʽ����--*/
///*--CH1--*/
////ѡ��ʱ������Ƚ�Ϊpwmģʽ2
//TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM2;
///*TIM1����*/
//TIM_OCInitStructure.TIM_OutputNState=TIM_OutputNState_Enable;
//TIM_OCInitStructure.TIM_OCNPolarity=TIM_OCNPolarity_Low;
///*------*/
///*��ͨTIM����*/
////TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
////TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_Low;
///*------*/
////����ռ�ձ�=TIM_CCRx/(TIM_Period+1)
//TIM_OCInitStructure.TIM_Pulse=0;
//////����ͨ��1
//TIM_OC1Init(TIM1,&TIM_OCInitStructure);
////����CCRӰ�ӼĴ�����ʹ�ܵ�ʱ��Ҫ��дҪ�ȵ��и����¼�ʱ�ſ��ԡ�
//TIM_OC1PreloadConfig(TIM1,TIM_OCPreload_Enable);
//
//
///*--CH2--*/
////ѡ��ʱ������Ƚ�Ϊpwmģʽ2
//TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM2;
///*TIM1����*/
//TIM_OCInitStructure.TIM_OutputNState=TIM_OutputNState_Enable;
//TIM_OCInitStructure.TIM_OCNPolarity=TIM_OCNPolarity_Low;
///*------*/
///*��ͨTIM����*/
////TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
////TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_Low;
///*------*/
////����ռ�ձ�=TIM_CCRx/(TIM_Period+1)
//TIM_OCInitStructure.TIM_Pulse=0;
////����ͨ��2
//TIM_OC2Init(TIM1,&TIM_OCInitStructure);
////����CCRӰ�ӼĴ�����ʹ�ܵ�ʱ��Ҫ��дҪ�ȵ��и����¼�ʱ�ſ��ԡ�
//TIM_OC2PreloadConfig(TIM1,TIM_OCPreload_Enable);
//
//
///*--CH3--*/
////ѡ��ʱ������Ƚ�Ϊpwmģʽ2
//TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM2;
///*TIM1����*/
//TIM_OCInitStructure.TIM_OutputNState=TIM_OutputNState_Enable;
//TIM_OCInitStructure.TIM_OCNPolarity=TIM_OCNPolarity_Low;
///*------*/
///*��ͨTIM����*/
////TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
////TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_Low;
///*------*/
////����ռ�ձ�=TIM_CCRx/(TIM_Period+1)
//TIM_OCInitStructure.TIM_Pulse=0;
////�������
////����ͨ3
//TIM_OC3Init(TIM1,&TIM_OCInitStructure);
////����CCRӰ�ӼĴ�����ʹ�ܵ�ʱ��Ҫ��дҪ�ȵ��и����¼�ʱ�ſ��ԡ�
//TIM_OC3PreloadConfig(TIM1,TIM_OCPreload_Enable);
//
////�����ʱ��
//TIM_Cmd(TIM1,ENABLE);
////���PWM
//TIM_CtrlPWMOutputs(TIM1,ENABLE);
/*------------tim3��PWM�����ʹ����ӳ�书�ܣ�-------------------*/
//����ӳ��ʱ�Ӻ���ӳ����IO��ʱ�ӡ�
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_AFIO,ENABLE);
//IO��ӳ�书�ܿ����GPIO_PartialRemap_TIM3
GPIO_PinRemapConfig(GPIO_FullRemap_TIM3,ENABLE);
//������ӳ�����ţ�ֻ������ӳ����IO��ԭ���IJ���Ҫ���á�
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOC,&GPIO_InitStructure);
/*--tim��������--*/
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
//TimƵ��=Tim_clk/��TIM_Period+1��
TIM_TimeBaseStructure.TIM_Period=7200-1;
//Timʱ��Ƶ�ʵ�Ԥ��Ƶֵ
TIM_TimeBaseStructure.TIM_Prescaler=0;
//ʱ�ӷָ�ֵ
TIM_TimeBaseStructure.TIM_ClockDivision=0;
//���ϼ���
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
//��ʼ��Tim��ʱ���������
TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);
//����ARRӰ�ӼĴ�����ʹ�ܵ�ʱ��Ҫ��дҪ�ȵ��и����¼�ʱ�ſ��ԡ�
TIM_ARRPreloadConfig(TIM3,ENABLE);
/*--PWMģʽ����--*/
/*--CH1--*/
//ѡ��ʱ������Ƚ�Ϊpwmģʽ2
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM2;
///*TIM1����*/
//TIM_OCInitStructure.TIM_OutputNState=TIM_OutputNState_Enable;
//TIM_OCInitStructure.TIM_OCNPolarity=TIM_OCNPolarity_Low;
///*------*/
/*��ͨTIM����*/
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_Low;
/*------*/
//����ռ�ձ�=TIM_CCRx/(TIM_Period+1)
TIM_OCInitStructure.TIM_Pulse=0;
////����ͨ��1
TIM_OC1Init(TIM3,&TIM_OCInitStructure);
//����CCRӰ�ӼĴ�����ʹ�ܵ�ʱ��Ҫ��дҪ�ȵ��и����¼�ʱ�ſ��ԡ�
TIM_OC1PreloadConfig(TIM3,TIM_OCPreload_Enable);
/*--CH2--*/
//ѡ��ʱ������Ƚ�Ϊpwmģʽ2
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM2;
///*TIM1����*/
//TIM_OCInitStructure.TIM_OutputNState=TIM_OutputNState_Enable;
//TIM_OCInitStructure.TIM_OCNPolarity=TIM_OCNPolarity_Low;
///*------*/
/*��ͨTIM����*/
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_Low;
/*------*/
//����ռ�ձ�=TIM_CCRx/(TIM_Period+1)
TIM_OCInitStructure.TIM_Pulse=0;
////����ͨ��1
TIM_OC2Init(TIM3,&TIM_OCInitStructure);
//����CCRӰ�ӼĴ�����ʹ�ܵ�ʱ��Ҫ��дҪ�ȵ��и����¼�ʱ�ſ��ԡ�
TIM_OC2PreloadConfig(TIM3,TIM_OCPreload_Enable);
/*--CH3--*/
//ѡ��ʱ������Ƚ�Ϊpwmģʽ2
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM2;
///*TIM1����*/
//TIM_OCInitStructure.TIM_OutputNState=TIM_OutputNState_Enable;
//TIM_OCInitStructure.TIM_OCNPolarity=TIM_OCNPolarity_Low;
///*------*/
/*��ͨTIM����*/
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_Low;
/*------*/
//����ռ�ձ�=TIM_CCRx/(TIM_Period+1)
TIM_OCInitStructure.TIM_Pulse=0;
//����ͨ��2
TIM_OC3Init(TIM3,&TIM_OCInitStructure);
//����CCRӰ�ӼĴ�����ʹ�ܵ�ʱ��Ҫ��дҪ�ȵ��и����¼�ʱ�ſ��ԡ�
TIM_OC3PreloadConfig(TIM3,TIM_OCPreload_Enable);
//�����ʱ��
TIM_Cmd(TIM3,ENABLE);
//���PWM
TIM_CtrlPWMOutputs(TIM3,ENABLE);
/*---------------TIM2����ʱ��----------------*/
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
TIM_DeInit(TIM2);
TIM_TimeBaseStructure.TIM_Period=1; /*�Զ���װ�ؼĴ������ڵ�ֵ(����ֵ)*/
/*�ۼ�TIM_Period��Ƶ�ʺ����һ�����»����ж�*/
TIM_TimeBaseStructure.TIM_Prescaler=(36000-1); /*ʱ��Ԥ��Ƶ�����磺ʱ��Ƶ��=72MHZ/(ʱ��Ԥ��Ƶ+1)*/
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1; /*������Ƶ*/
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up; /*���ϼ���ģʽ*/
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);
TIM_ClearFlag(TIM2,TIM_FLAG_Update); /*�������жϱ�־*/
TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE);
TIM_Cmd(TIM2,ENABLE); /*����ʱ��*/
}
voidNVIC_Configuration(void)
{
NVIC_InitTypeDefNVIC_InitStructure;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);
NVIC_InitStructure.NVIC_IRQChannel=TIM2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=5;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel=USART3_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel=EXTI9_5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority=4;
NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
/**************************************************************/
//Modbus��ַ����
//��������
//����ֵ����
//�����ã�main.c
/***************************************************************/
uint8_tModbusAdd(void)
{
unionMODBUSADD
{
struct
{
uint8_taddr0:
1;
uint8_taddr1:
1;
uint8_taddr2:
1;
uint8_taddr3:
1;
uint8_taddr4:
1;
uint8_taddr5:
1;
uint8_t:
2;
}ModbusAdd_bit;
uint8_tModbusAdd_byte;
}ModbusAdd;
ModbusAdd.ModbusAdd_byte=0;
if(!
GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_4))
ModbusAdd.ModbusAdd_bit.addr0=0;
else
ModbusAdd.ModbusAdd_bit.addr0=1;
if(!
GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_5))
ModbusAdd.ModbusAdd_bit.addr1=0;
else
ModbusAdd.ModbusAdd_bit.addr1=1;
if(!
GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_6))
ModbusAdd.ModbusAdd_bit.addr2=0;
else
ModbusAdd.ModbusAdd_bit.addr2=1;
if(!
GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_7))
ModbusAdd.ModbusAdd_bit.addr3=0;
else
ModbusAdd.ModbusAdd_bit.addr3=1;
if(!
GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_4))
ModbusAdd.ModbusAdd_bit.addr4=0;
else
ModbusAdd.ModbusAdd_bit.addr4=1;
if(!
GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_5))
ModbusAdd.ModbusAdd_bit.addr5=0;
else
ModbusAdd.ModbusAdd_bit.addr5=1;
returnModbusAdd.ModbusAdd_byte;
}
/**************************************************************/
//Modbus����������
//��������
//����ֵ����
//�����ã�main.c
/***************************************************************/
uint32_tModbusBaud(void)
{
unionMODBUSBAUD
{
struct
{
uint8_tbaud0:
1;
uint8_tbaud1:
1;
uint8_t:
6;
}ModbusBaud_bit;
uint8_tModbusBaud_byte;
}ModbusBaud;
uint32_tBaudRate;
ModbusBaud.ModbusBaud_byte=0;
if(!
GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_0))
ModbusBaud.ModbusBaud_bit.baud0=0;
else
ModbusBaud.ModbusBaud_bit.baud0=1;
if(!
GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1))
ModbusBaud.ModbusBaud_bit.baud1=0;
else
ModbusBaud.ModbusBaud_bit.baud1=1;
switch(ModbusBaud.ModbusBaud_byte)
{
case0:
BaudRate=4800;break;
case1:
BaudRate=9600;break;
case2:
BaudRate=14400;break;
case3:
BaudRate=19200;break;
}
returnBaudRate;
}
/**************************************************************/
//ϵͳ��ʼ��
//��������
//����ֵ����
//�����ã�main.c
/***************************************************************/
voidsys_init(void)
{
GPIO_Configuration();
EXTI_Configuration();
ADC_Configuration();
init_timer();
USART_Configuration();
NVIC_Configuration();
Init_global_var();
eMBInit(MB_RTU,2,0x01,9600,MB_PAR_NONE);
eMBEnable();
}
/*--------------------------------------------------------------*/
/*---------------------endoffile------------------------------*/
/*--------------------------------------------------------------*/