#include "stm32f10x.h"
#include "delay.h"
#include "sys.h"
#include "usart.h"
#include "pwm.h"
#include "timer.h"
#define ADC1_DR_Address ((uint32_t)0x4001244C)
u32 i;
u32 d=3980;
u32 pwm=1000;
GPIO_InitTypeDef GPIO_InitStructure;
__IO uint16_t ADCConvertedValue[8];
u8 zhongduancishu = 0;
u8 flg_get_senor_data;
volatile u16 NumberOfCircleLeft,NumberOfCircleRight;
volatile u8 SpeedFlag;
volatile u16 Speed;
extern void T1_ETR_InitConfig(void);
extern void T2_ETR_InitConfig(void);
volatile int SpeedOut = 0;
float spl,spr;
void init_GPIOA(void);
void RCC_Configuration(void);
void gpio_config(void);
void DMA(void);
void ADC(void);
void PWM_Init(void);
void qian(void);
void zuo(void);
void you(void);
void zuo1(void);
void you1(void);
void stop(void);
void xunji(void);
void xunhuo(void);
void delay_s(int z);
void EXTI0and1_Config(void);
void T1_ETR_InitConfig(void);
void T2_ETR_InitConfig(void);
int main(void)
{
RCC_Configuration();
gpio_config();
ADC();
DMA();
PWM_Init();
delay_init();
TIM1_PWM_Init(199,7199);//不分频。PWM频率=72000/(899+1)=80Khz
while(1)
{
xunhuo();
// if(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_5)==1)
// {
// stop();
// //delay_s(2);
// }
// else if((ADCConvertedValue[0]<d)&&(ADCConvertedValue[1]<d)&&(ADCConvertedValue[2]<d)&&(ADCConvertedValue[3]<d)&&(ADCConvertedValue[4]<d)&&(ADCConvertedValue[5]<d)&&(ADCConvertedValue[6]<d)&&(ADCConvertedValue[7]<d))
// {
// for(i=5000;i>0;i--)
// {
// xunji();
// }
// }
// else
// {
// GPIO_SetBits(GPIOA, GPIO_Pin_10);
// xunhuo();
// delay_s(2);
// }
}
}
// while (1)
// {
// u32 x=1;
// u32 flag;
// if(x==100){xunji();}
// //if(flag==1){x=2;x++;}
//
//
//// else{x=0;}
//// if(x==1000){;}
//// GPIO_SetBits(GPIOA, GPIO_Pin_12);
//// delay_ms(5000);
//// GPIO_ResetBits(GPIOA, GPIO_Pin_12);
//// delay_ms(5000);
//
// if(ADCConvertedValue[0]>3950|ADCConvertedValue[1]>3950|ADCConvertedValue[2]>3950|ADCConvertedValue[3]>3950|ADCConvertedValue[4]>3950)
// {
// xunhuo();
// x=2;
// if(x>1){stop();GPIO_SetBits(GPIOA, GPIO_Pin_12);x++;}
// }
// else
// {
// //flag=0;
// xunji();
//
// //xunhuo();
// }
// }
//
//}
//int main(void)
// {
// u8 t;
// u8 len;
// u16 times=0;
// RCC_Configuration();
// gpio_config();
// ADC();
// DMA();
// PWM_Init();
// delay_init(); //延时函数初始化
// NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);// 设置中断优先级分组2
// uart_init(9600); //串口初始化为9600
// //LED_Init(); //初始化与LED连接的硬件接口
// T1_ETR_InitConfig();
// T2_ETR_InitConfig();
// timer_init();
// while(1)
// {
// qian();
// if(USART_RX_STA&0x8000)
// {
// len=USART_RX_STA&0x3fff;//得到此次接收到的数据长度
// printf("
您发送的消息为:
");
// for(t=0;t<len;t++)
// {
// USART1->DR=USART_RX_BUF[t];
// while((USART1->SR&0X40)==0);//等待发送结束
// }
// printf("
");//插入换行
// USART_RX_STA=0;
// }else
// {
// times++;
// if(times%5000==0)
// {
// printf("
ALIENTEK MiniSTM32开发板 串口实验
");
// printf("正点原子@ALIENTEK
");
// }
// if(times%200==0)printf("%d,%d,%d,%d,%d
",ADCConvertedValue[0],ADCConvertedValue[1],ADCConvertedValue[2],ADCConvertedValue[3],ADCConvertedValue[4]);
// //if(times%30==0)LED0=!LED0;//闪烁LED,提示系统正在运行.
// delay_ms(10);
// }
// }
//}
void RCC_Configuration(void)
{
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_ADC1, ENABLE);
}
void gpio_config(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5;//驱动左
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//推挽输出
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_6|GPIO_Pin_7;//驱动右
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//推挽输出
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5;//寻火
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5;//寻火
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//模拟输入
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;//舵机
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode =GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//GPIO_ResetBits(GPIOA, GPIO_Pin_12);
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5;//循迹
// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
// GPIO_Init(GPIOC, &GPIO_InitStructure);
// //GPIO_ResetBits(GPIOC, GPIO_Pin_5);
}
void ADC(void)
{
ADC_InitTypeDef ADC_InitStructure;
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = ENABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 8;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 1, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_5, 2, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_10, 3, ADC_SampleTime_55Cycles5);
//ADC_RegularChannelConfig(ADC1, ADC_Channel_3, 4, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_11, 4, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 5, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_13, 6, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 7, ADC_SampleTime_55Cycles5);
ADC_RegularChannelConfig(ADC1, ADC_Channel_15, 8, ADC_SampleTime_55Cycles5);
ADC_DMACmd(ADC1, ENABLE);
ADC_Cmd(ADC1, ENABLE);
ADC_ResetCalibration(ADC1);
while(ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
while(ADC_GetCalibrationStatus(ADC1));
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
}
void DMA(void)
{
DMA_InitTypeDef DMA_InitStructure;
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&ADCConvertedValue;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize =8;
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_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel1, &DMA_InitStructure);
DMA_Cmd(DMA1_Channel1, ENABLE);
}
//void xunji(void)
//{
//GPIO_ResetBits(GPIOA, GPIO_Pin_10);
//if((GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_1)==0)&&(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_3)==0)&&(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_0)==0)&&(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_4)==0))
//{
//zuo();
//}
//// if((GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_1)==1)&&(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_0)==1))
//// {
//// qian();
//// }
////
//// if((GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_4)==1)&&(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_3)==1))
//// {
//// qian();
//// }
//// if((GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_3)==1)&&(GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_1)==1))
//// {
//// qian();
//// }
//else if(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_1)==1)
//{
//you();
//}
//else if(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_3)==1)
//{
//zuo();
//}
//else if(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_0)==1)
//{
//you1();
//}
//else if(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_4)==1)
//{
//zuo1();
//}
//else{qian();}
//}
////void xunhuo(void)
////{
////
//// GPIO_SetBits(GPIOA, GPIO_Pin_12);
//// if((ADCConvertedValue[0]+ADCConvertedValue[1])>(ADCConvertedValue[2]+ADCConvertedValue[1]))
//// {
//// for(i=3;i>0;i--)
//// {
//// stop();
//// TIM_SetCompare1(TIM1,196);
//// delay_ms(1000);
//// TIM_SetCompare1(TIM1,191);
//// delay_ms(1000);
//// }
//// }
//// if(((ADCConvertedValue[1]+ADCConvertedValue[2])>(ADCConvertedValue[0]+ADCConvertedValue[0]))&&((ADCConvertedValue[1]+ADCConvertedValue[2])>(ADCConvertedValue[2]+ADCConvertedValue[3])))
//// {
//// for(i=3;i>3;i--)
//// {
//// stop();
//// TIM_SetCompare1(TIM1,186);
//// delay_ms(1000);
//// TIM_SetCompare1(TIM1,191);
//// delay_ms(1000);
//// }
//// }
//// if(((ADCConvertedValue[3]+ADCConvertedValue[2])>(ADCConvertedValue[2]+ADCConvertedValue[1]))&&((ADCConvertedValue[2]+ADCConvertedValue[3])>(ADCConvertedValue[4]+ADCConvertedValue[3])))
//// {
////
//// for(i=3;i>0;i--)
//// {
//// stop();
//// TIM_SetCompare1(TIM1,186);
//// delay_ms(1000);
//// TIM_SetCompare1(TIM1,181);
//// delay_ms(1000);
//// }
//// }
//// if(ADCConvertedValue[3]+ADCConvertedValue[4]>ADCConvertedValue[2]+ADCConvertedValue[3])
//// {
////
//// for(i=3;i>0;i--)
//// {
//// stop();
//// TIM_SetCompare1(TIM1,181);
//// delay_ms(1000);
//// TIM_SetCompare1(TIM1,176);
//// delay_ms(1000);
//// }
//// }
////}
void xunhuo(void)
{
if((ADCConvertedValue[0]>ADCConvertedValue[1])&&(ADCConvertedValue[0]>ADCConvertedValue[2])&&(ADCConvertedValue[0]>ADCConvertedValue[3])&&(ADCConvertedValue[0]>ADCConvertedValue[3]))
{
stop();
TIM_SetCompare1(TIM1,196);
//GPIO_SetBits(GPIOA, GPIO_Pin_12);
//delay_ms(5000);
}
if((ADCConvertedValue[1]>ADCConvertedValue[0])&&(ADCConvertedValue[1]>ADCConvertedValue[2])&&(ADCConvertedValue[1]>ADCConvertedValue[3])&&(ADCConvertedValue[1]>ADCConvertedValue[4]))
{
stop();
TIM_SetCompare1(TIM1,192);
//GPIO_SetBits(GPIOA, GPIO_Pin_12);
//delay_ms(5000);
}
if((ADCConvertedValue[2]>ADCConvertedValue[0])&&(ADCConvertedValue[2]>ADCConvertedValue[1])&&(ADCConvertedValue[2]>ADCConvertedValue[3])&&(ADCConvertedValue[2]>ADCConvertedValue[4]))
{
stop();
TIM_SetCompare1(TIM1,187);
//GPIO_SetBits(GPIOA, GPIO_Pin_12);
//delay_s(5000);
}
if((ADCConvertedValue[3]>ADCConvertedValue[0])&&(ADCConvertedValue[3]>ADCConvertedValue[1])&&(ADCConvertedValue[3]>ADCConvertedValue[2])&&(ADCConvertedValue[3]>ADCConvertedValue[4]))
{
stop();
TIM_SetCompare1(TIM1,183);
//GPIO_SetBits(GPIOA, GPIO_Pin_12);
//delay_ms(5000);
}
if((ADCConvertedValue[4]>ADCConvertedValue[0])&&(ADCConvertedValue[4]>ADCConvertedValue[1])&&(ADCConvertedValue[4]>ADCConvertedValue[2])&&(ADCConvertedValue[4]>ADCConvertedValue[3]))
{
stop();
TIM_SetCompare1(TIM1,177);
//GPIO_SetBits(GPIOA, GPIO_Pin_12);
//delay_ms(5000);
}
// if((ADCConvertedValue[0]>ADCConvertedValue[1])&&(ADCConvertedValue[0]>ADCConvertedValue[2])&&(ADCConvertedValue[0]>ADCConvertedValue[3])&&(ADCConvertedValue[0]>ADCConvertedValue[4])&&(ADCConvertedValue[0]>ADCConvertedValue[5])&&(ADCConvertedValue[0]>ADCConvertedValue[6])&&(ADCConvertedValue[0]>ADCConvertedValue[7]))
// {
//
// TIM_SetCompare1(TIM1,175);
// //GPIO_SetBits(GPIOA, GPIO_Pin_12);
// delay_ms(5000);
// }
// if((ADCConvertedValue[1]>ADCConvertedValue[0])&&(ADCConvertedValue[1]>ADCConvertedValue[2])&&(ADCConvertedValue[1]>ADCConvertedValue[3])&&(ADCConvertedValue[1]>ADCConvertedValue[4])&&(ADCConvertedValue[1]>ADCConvertedValue[5])&&(ADCConvertedValue[1]>ADCConvertedValue[6])&&(ADCConvertedValue[1]>ADCConvertedValue[7]))
// {
//
// TIM_SetCompare1(TIM1,177);
// //GPIO_SetBits(GPIOA, GPIO_Pin_12);
// delay_ms(5000);
// }
// if((ADCConvertedValue[2]>ADCConvertedValue[1])&&(ADCConvertedValue[2]>ADCConvertedValue[0])&&(ADCConvertedValue[2]>ADCConvertedValue[3])&&(ADCConvertedValue[2]>ADCConvertedValue[4])&&(ADCConvertedValue[2]>ADCConvertedValue[5])&&(ADCConvertedValue[2]>ADCConvertedValue[6])&&(ADCConvertedValue[2]>ADCConvertedValue[7]))
// {
//
// TIM_SetCompare1(TIM1,180);
// //GPIO_SetBits(GPIOA, GPIO_Pin_12);
// delay_s(5000);
// }
// if((ADCConvertedValue[3]>ADCConvertedValue[1])&&(ADCConvertedValue[3]>ADCConvertedValue[2])&&(ADCConvertedValue[3]>ADCConvertedValue[0])&&(ADCConvertedValue[3]>ADCConvertedValue[4])&&(ADCConvertedValue[3]>ADCConvertedValue[5])&&(ADCConvertedValue[3]>ADCConvertedValue[6])&&(ADCConvertedValue[3]>ADCConvertedValue[7]))
// {
//
// TIM_SetCompare1(TIM1,184);
// //GPIO_SetBits(GPIOA, GPIO_Pin_12);
// delay_ms(5000);
// }
// if((ADCConvertedValue[4]>ADCConvertedValue[1])&&(ADCConvertedValue[4]>ADCConvertedValue[2])&&(ADCConvertedValue[4]>ADCConvertedValue[3])&&(ADCConvertedValue[4]>ADCConvertedValue[0])&&(ADCConvertedValue[4]>ADCConvertedValue[5])&&(ADCConvertedValue[4]>ADCConvertedValue[6])&&(ADCConvertedValue[4]>ADCConvertedValue[7]))
// {
//
// TIM_SetCompare1(TIM1,186);
// //GPIO_SetBits(GPIOA, GPIO_Pin_12);
// delay_ms(5000);
// }
// if((ADCConvertedValue[5]>ADCConvertedValue[1])&&(ADCConvertedValue[5]>ADCConvertedValue[2])&&(ADCConvertedValue[5]>ADCConvertedValue[3])&&(ADCConvertedValue[5]>ADCConvertedValue[4])&&(ADCConvertedValue[5]>ADCConvertedValue[0])&&(ADCConvertedValue[0]>ADCConvertedValue[6])&&(ADCConvertedValue[0]>ADCConvertedValue[7]))
// {
//
// TIM_SetCompare1(TIM1,190);
// //GPIO_SetBits(GPIOA, GPIO_Pin_12);
// delay_ms(5000);
// }
// if((ADCConvertedValue[6]>ADCConvertedValue[1])&&(ADCConvertedValue[6]>ADCConvertedValue[2])&&(ADCConvertedValue[6]>ADCConvertedValue[3])&&(ADCConvertedValue[6]>ADCConvertedValue[4])&&(ADCConvertedValue[6]>ADCConvertedValue[5])&&(ADCConvertedValue[6]>ADCConvertedValue[0])&&(ADCConvertedValue[6]>ADCConvertedValue[7]))
// {
//
// TIM_SetCompare1(TIM1,192);
// //GPIO_SetBits(GPIOA, GPIO_Pin_12);
// delay_ms(5000);
// }
// if((ADCConvertedValue[7]>ADCConvertedValue[1])&&(ADCConvertedValue[7]>ADCConvertedValue[2])&&(ADCConvertedValue[7]>ADCConvertedValue[3])&&(ADCConvertedValue[7]>ADCConvertedValue[4])&&(ADCConvertedValue[7]>ADCConvertedValue[5])&&(ADCConvertedValue[7]>ADCConvertedValue[6])&&(ADCConvertedValue[7]>ADCConvertedValue[0]))
// {
//
// TIM_SetCompare1(TIM1,195);
// //GPIO_SetBits(GPIOA, GPIO_Pin_12);
// delay_ms(5000);
// }
}
//void xunhuo(void)
//{
// if(ADCConvertedValue[4]>ADCConvertedValue[3])
// {
// zuo1();--
// }
// if(ADCConvertedValue[3]>ADCConvertedValue[2])
// {
// zuo();
// }
// if(ADCConvertedValue[0]>ADCConvertedValue[1])
// {
// you1();
// }
// if(ADCConvertedValue[1]>ADCConvertedValue[2])
// {
// you();
// }
// if((ADCConvertedValue[2]>ADCConvertedValue[1])&&(ADCConvertedValue[2]>ADCConvertedValue[3])&&(ADCConvertedValue[2]>3900))
// {
// stop();
// }
// else
// {
// qian();
// }
//}
void PWM_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA |RCC_APB2Periph_GPIOB | RCC_APB2Periph_AFIO, ENABLE); //使能GPIO外设和AFIO复用功能模块时钟使能
//用于TIM3的CH2输出的PWM通过该LED显示
// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
// GPIO_Init(GPIOA, &GPIO_InitStructure);
//设置该引脚为复用输出功能,输出TIM3 CH2的PWM脉冲波形
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7; //TIM_CH2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//GPIO_WriteBit(GPIOA, GPIO_Pin_7,Bit_SET); // PA7上拉
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1; //TIM_CH2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Period = 1800; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值 80K
TIM_TimeBaseStructure.TIM_Prescaler =1; //设置用来作为TIMx时钟频率除数的预分频值 不分频
TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //TIM向上计数模式
TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; //选择定时器模式:TIM脉冲宽度调制模式2
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; //比较输出使能
//TIM_OCInitStructure.TIM_Pulse = 0; //设置待装入捕获比较寄存器的脉冲值
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; //输出极性:TIM输出比较极性高
TIM_OC1Init(TIM3, &TIM_OCInitStructure); //根据TIM_OCInitStruct中指定的参数初始化外设TIMx
TIM_OC2Init(TIM3, &TIM_OCInitStructure); //根据TIM_OCInitStruct中指定的参数初始化外设TIMx
TIM_OC3Init(TIM3, &TIM_OCInitStructure); //根据TIM_OCInitStruct中指定的参数初始化外设TIMx
TIM_OC4Init(TIM3, &TIM_OCInitStructure); //根据TIM_OCInitStruct中指定的参数初始化外设TIMx
//TIM_OC2PreloadConfig(TIM3, TIM_OCPreload_Enable); //使能TIMx在CCR2上的预装载寄存器
//TIM_ARRPreloadConfig(TIM3, ENABLE); //使能TIMx在ARR上的预装载寄存器
TIM_Cmd(TIM3, ENABLE); //使能TIMx外设
}
void delay_s(int z)
{
int x;
for(x=z;x>0;x--)
delay_ms(5000);
}
void qian(void)
{
GPIO_SetBits(GPIOA, GPIO_Pin_0);
GPIO_ResetBits(GPIOA, GPIO_Pin_1);
GPIO_SetBits(GPIOA, GPIO_Pin_3);
GPIO_ResetBits(GPIOA, GPIO_Pin_2);
TIM_SetCompare1(TIM3, pwm);
TIM_SetCompare2(TIM3, pwm);
GPIO_SetBits(GPIOB, GPIO_Pin_2);
GPIO_ResetBits(GPIOB, GPIO_Pin_3);
GPIO_SetBits(GPIOB, GPIO_Pin_4);
GPIO_ResetBits(GPIOB, GPIO_Pin_5);
TIM_SetCompare3(TIM3, pwm);
TIM_SetCompare4(TIM3, pwm);
}
void you(void)
{
GPIO_SetBits(GPIOA, GPIO_Pin_0);
GPIO_ResetBits(GPIOA, GPIO_Pin_1);
GPIO_SetBits(GPIOA, GPIO_Pin_3);
GPIO_ResetBits(GPIOA, GPIO_Pin_2);
TIM_SetCompare1(TIM3, 0);
TIM_SetCompare2(TIM3, 0);
GPIO_SetBits(GPIOB, GPIO_Pin_2);
GPIO_ResetBits(GPIOB, GPIO_Pin_3);
GPIO_SetBits(GPIOB, GPIO_Pin_4);
GPIO_ResetBits(GPIOB, GPIO_Pin_5);
TIM_SetCompare3(TIM3, pwm);
TIM_SetCompare4(TIM3, pwm);
}
void zuo(void)
{
GPIO_SetBits(GPIOA, GPIO_Pin_0);
GPIO_ResetBits(GPIOA, GPIO_Pin_1);
GPIO_SetBits(GPIOA, GPIO_Pin_3);
GPIO_ResetBits(GPIOA, GPIO_Pin_2);
TIM_SetCompare1(TIM3, pwm);
TIM_SetCompare2(TIM3, pwm);
GPIO_SetBits(GPIOB, GPIO_Pin_2);
GPIO_ResetBits(GPIOB, GPIO_Pin_3);
GPIO_SetBits(GPIOB, GPIO_Pin_4);
GPIO_ResetBits(GPIOB, GPIO_Pin_5);
TIM_SetCompare3(TIM3, 0);
TIM_SetCompare4(TIM3, 0);
}
void you1(void)
{
GPIO_SetBits(GPIOA, GPIO_Pin_0);
GPIO_ResetBits(GPIOA, GPIO_Pin_1);
GPIO_SetBits(GPIOA, GPIO_Pin_3);
GPIO_ResetBits(GPIOA, GPIO_Pin_2);
TIM_SetCompare1(TIM3, pwm);
TIM_SetCompare2(TIM3, pwm);
GPIO_SetBits(GPIOB, GPIO_Pin_2);
GPIO_ResetBits(GPIOB, GPIO_Pin_3);
GPIO_SetBits(GPIOB, GPIO_Pin_4);
GPIO_ResetBits(GPIOB, GPIO_Pin_5);
TIM_SetCompare3(TIM3, pwm);
TIM_SetCompare4(TIM3, pwm);
}
void zuo1(void)
{
GPIO_SetBits(GPIOA, GPIO_Pin_0);
GPIO_ResetBits(GPIOA, GPIO_Pin_1);
GPIO_SetBits(GPIOA, GPIO_Pin_3);
GPIO_ResetBits(GPIOA, GPIO_Pin_2);
TIM_SetCompare1(TIM3, pwm);
TIM_SetCompare2(TIM3, pwm);
GPIO_SetBits(GPIOB, GPIO_Pin_2);
GPIO_ResetBits(GPIOB, GPIO_Pin_3);
GPIO_SetBits(GPIOB, GPIO_Pin_4);
GPIO_ResetBits(GPIOB, GPIO_Pin_5);
TIM_SetCompare3(TIM3, pwm);
TIM_SetCompare4(TIM3, pwm);
}
void stop(void)
{
GPIO_SetBits(GPIOA, GPIO_Pin_0);
GPIO_ResetBits(GPIOA, GPIO_Pin_1);
GPIO_SetBits(GPIOA, GPIO_Pin_3);
GPIO_ResetBits(GPIOA, GPIO_Pin_2);
TIM_SetCompare1(TIM3, 0);
TIM_SetCompare2(TIM3, 0);
GPIO_SetBits(GPIOB, GPIO_Pin_2);
GPIO_ResetBits(GPIOB, GPIO_Pin_3);
GPIO_SetBits(GPIOB, GPIO_Pin_4);
GPIO_ResetBits(GPIOB, GPIO_Pin_5);
TIM_SetCompare3(TIM3, 0);
TIM_SetCompare4(TIM3, 0);
}
void T1_ETR_InitConfig(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Prescaler = 0;//????
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//??????
TIM_TimeBaseStructure.TIM_Period = 0xFFFF;//?????????????????????????
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;//TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;//?????
TIM_TimeBaseInit(TIM1,&TIM_TimeBaseStructure);//???
TIM_CounterModeConfig(TIM1, TIM_CounterMode_Up);
TIM_SelectInputTrigger(TIM1, TIM_TS_ETRF);
TIM_ETRClockMode2Config(TIM1, TIM_ExtTRGPSC_OFF,TIM_ExtTRGPolarity_Inverted,0x01);
TIM_Cmd(TIM1, ENABLE);
}
void T2_ETR_InitConfig(void)
{
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinRemapConfig(GPIO_PartialRemap1_TIM2, ENABLE);
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);
TIM_TimeBaseStructure.TIM_Prescaler = 0;//????
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;//??????
TIM_TimeBaseStructure.TIM_Period = 0xFFFF;//?????????????????????????
TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;//TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;//?????
TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);//???
TIM_CounterModeConfig(TIM2, TIM_CounterMode_Up);
TIM_SelectInputTrigger(TIM2, TIM_TS_ETRF);
TIM_ETRClockMode2Config(TIM2, TIM_ExtTRGPSC_OFF,TIM_ExtTRGPolarity_Inverted,0x01);
TIM_Cmd(TIM2, ENABLE);
}
void EXTI0and1_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource0);
GPIO_EXTILineConfig(GPIO_PortSourceGPIOC, GPIO_PinSource1);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //???Group0
NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
/*------????????ROM??????------------*/
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
EXTI_InitStructure.EXTI_Line = EXTI_Line0;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
EXTI_InitStructure.EXTI_Line = EXTI_Line1;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
}
一周热门 更多>