- #include "delay.h"
- #include "sys.h"
- #include "usart.h"
- #include "chao.h"
- #include "remote.h"
- #include "timer.h"
- #define L2 PBin(14)
- #define R2 PBin(15)
- uint16_t spd=300;
- u8 last;
- u8 last1;
- u8 last2;
- u8 last3;
- u16 pp=400;
- void dingyi()
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOA, ENABLE);
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
-
-
-
- //红外
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_15;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPD;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- }
- void shizhong_Init()
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_OCInitTypeDef TIM_OCInitStructure;
-
- dingyi();
-
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);
-
- TIM_TimeBaseStructure.TIM_Period = 889; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值 80K
- TIM_TimeBaseStructure.TIM_Prescaler =0; //设置用来作为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_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
-
- 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_High; //输出极性:TIM输出比较极性高
-
- TIM_OC1Init(TIM3, &TIM_OCInitStructure); //根据TIM_OCInitStruct中指定的参数初始化外设TIMx
- TIM_OC2Init(TIM3, &TIM_OCInitStructure);
- TIM_OC3Init(TIM4, &TIM_OCInitStructure);
- TIM_OC4Init(TIM4, &TIM_OCInitStructure);
- TIM_CtrlPWMOutputs(TIM3,ENABLE); //MOE 主输出使能
- TIM_CtrlPWMOutputs(TIM4,ENABLE);
-
-
- TIM_ARRPreloadConfig(TIM3, ENABLE); //使能TIMx在ARR上的预装载寄存器
- TIM_ARRPreloadConfig(TIM4, ENABLE);
-
- TIM_Cmd(TIM3, ENABLE); //使能TIM1
- TIM_Cmd(TIM4, ENABLE);
- }
- void qianjin(uint16_t spd)
- {
- TIM_SetCompare1(TIM3,899); //INT1
- TIM_SetCompare2(TIM3,spd); //ibt2
-
- TIM_SetCompare3(TIM4,spd);
- TIM_SetCompare4(TIM4,899);
-
- }
- void zuozhuan(uint16_t spd)
- {
- TIM_SetCompare1(TIM3,spd);
- TIM_SetCompare2(TIM3,899);
-
- TIM_SetCompare3(TIM4,spd);
- TIM_SetCompare4(TIM4,899);
- }
- void youzhuan(uint16_t spd)
- {
- TIM_SetCompare1(TIM3,899);
- TIM_SetCompare2(TIM3,spd);
-
- TIM_SetCompare3(TIM4,899);
- TIM_SetCompare4(TIM4,spd);
- }
- void shache(uint16_t spd)
- {
- TIM_SetCompare1(TIM3,899);
- TIM_SetCompare2(TIM3,899);
-
- TIM_SetCompare3(TIM4,899);
- TIM_SetCompare4(TIM4,899);
- }
- void back(uint16_t spd)
- {
- TIM_SetCompare1(TIM3,spd);
- TIM_SetCompare2(TIM3,899);
-
- TIM_SetCompare3(TIM4,899);
- TIM_SetCompare4(TIM4,spd);
- }
- //红外壁障
- void hongzhang(uint16_t spd)
- {
- if(L2==1 && R2==1) //没有障碍
- qianjin(spd);
- else if(L2 ==0 && R2 == 1) //左边有障碍
- {
- back(spd);
- delay_ms(spd/2);
- youzhuan(spd);
- delay_ms(spd/2);
- }
- else if(L2 ==1 && R2 == 0) //右边有障碍
- {
- back(spd);
- delay_ms(spd/2);
- zuozhuan(spd);
- delay_ms(spd/2);
- }
- else //左右都有障碍
- {
- back(spd);
- delay_ms(spd/2);
- youzhuan(spd);
- delay_ms(spd/2);
- }
- }
- int main(void)//
- {
- u8 key;
- u8 key1;
- u8 key2;
- u8 key3;
- //u8 t=0;
- u8 *str=0;
- u8 x=0;
- delay_init(); //延时函数初始?
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);// 设置中断优先级分组2
- uart_init(9600); //串口初始化为9600
- shizhong_Init();
- TIM2_Cap_Init(0XFFFF,72-1); //以1Mhz的频率计数 ;
- CSB_Init();
- Remote_Init();
- while(1)
- {
- key=Remote_Scan();
- if(!key){key=last;}
- if(key)
- {
- switch(key)
- {
- case 152:str="2";
- {
- hongzhang(spd);
- key2=Remote_Scan();
- if(!key2){key2=last2;}
- if(key2==66)
- {
-
- shache(spd);
- last2=key2;
- }
- else
- last=key;
- break;
-
- }
- case 176:str="3";
- {
- chaosheng();
- key3=Remote_Scan();
- if(!key3){key3=last3;}
- if(key3==66)
- {
-
- shache(spd);
- last3=key3;
- }
- else
- last=key;
- break;
- }
-
- }
- }
-
-
- }
- }
复制代码
所有资料51hei提供下载:
红外+超声波.rar
(331.61 KB, 下载次数: 11)
|