|
用正點(diǎn)原子的精英版作為主控,紅外循跡用TCRT5000芯片。
單片機(jī)源程序如下:
- /* Includes ------------------------------------------------------------------*/
- #include "stm32f10x.h"
- #include "stm32f10x_tim.h"
- #include "stm32f10x_flash.h"
- #include "stm32f10x_exti.h"
- #include "stm32f10x_rcc.h"
- #include "systick.h"
- #include "stdio.h"
- #include "misc.h"
- #define infra1 GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_7);
- #define infra2 GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_8);
- #define tube1 GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_0);
- #define tube2 GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_1);
- #define tube3 GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_2);
- #define tube4 GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_3);
-
- TIM_OCInitTypeDef TIM1_OCInitStructure;
- extern FlagStatus Status;
- extern u16 TIM1Compare1[3] = {0,0x3FFF,0x1FFF};
- extern u16 TIM1Compare2[3] = {0x7FFF,0x3FFF,0x1FFF};
- extern u16 TIM1Compare3[3] = {0,0x3FFF,0x1FFF};
- extern u16 TIM1Compare4[3] = {0x7FFF,0x3FFF,0x1FFF};
- extern u16 tubecnt;
- extern u16 flag;
- extern int add;
- void GPIO_Configuration(void);
- void TIM_Configuration(void);
- void tim2_Configuration(void);
- void exti_Configuration(void);
- void nvic_Configuration(void);
- void zhi1(void);
- void zhi2(void);
- void zhi3(void);
- void zuoxiao(void);
- void zuoda(void);
- void youxiao(void);
- void youda(void);
- void xunji(void);
- u16 ceju(void);
- int main(void)
- {
-
- RCC_Configuration();
- GPIO_Configuration();
- TIM_Configuration();
- delay_init();
- while(1)
- {
- GPIO_ResetBits(GPIOE,GPIO_Pin_15);
- GPIO_ResetBits(GPIOB,GPIO_Pin_11);
- }
-
- }
- void GPIO_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
- /*TIM1 PWM 輸出:A9:CH1/A10:CH2 */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_9;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- /*LED顯示:LED5:PE15 / LED6:PB11 / LED7:PE14 / LED8:PB10 */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10|GPIO_Pin_11;
- GPIO_InitStructure.GPIO_Mode =GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- GPIO_SetBits(GPIOB,GPIO_Pin_10);
- GPIO_SetBits(GPIOB,GPIO_Pin_11);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14|GPIO_Pin_15;
- GPIO_InitStructure.GPIO_Mode =GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOE, &GPIO_InitStructure);
- GPIO_SetBits(GPIOE,GPIO_Pin_14);
- GPIO_SetBits(GPIOE,GPIO_Pin_15);
-
- /*紅外管:infra1-> PC7, infra2-> PC8 浮空輸入 */
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8 ;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- /*光電管:tube1-> PC0, tube2-> PC1, tube3-> PC2, tube4-> PC3 浮空輸入*/
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1|GPIO_Pin_2 | GPIO_Pin_3;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOC, &GPIO_InitStructure);
- /*超聲波:Trig:PB9 Echo:PB8 */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- GPIO_ResetBits( GPIOB, GPIO_Pin_9);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //PB1接受采集上升沿 PB6低電平
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- GPIO_ResetBits(GPIOB,GPIO_Pin_8);
- }
- void TIM_Configuration(void)
- {
- TIM_TimeBaseInitTypeDef TIM1_TimeBaseStructure;
- TIM_OCInitTypeDef TIM1_OCInitStructure;
-
- TIM_DeInit(TIM1);
- /* Time Base configuration */
- TIM1_TimeBaseStructure.TIM_Prescaler = 0x0; //設(shè)置了用來(lái)作為TIMx時(shí)鐘頻率除數(shù)的預(yù)分頻值
- TIM1_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //選擇了計(jì)數(shù)器模式
- TIM1_TimeBaseStructure.TIM_Period = 0xFFFF; //設(shè)置了在下一個(gè)更新事件裝入活動(dòng)的自動(dòng)重裝載寄存器周期 ARR
- TIM1_TimeBaseStructure.TIM_ClockDivision = 0x0; //設(shè)置了時(shí)鐘分割
- TIM1_TimeBaseStructure.TIM_RepetitionCounter = 0x0; //設(shè)置了周期計(jì)數(shù)器值
- TIM_TimeBaseInit(TIM1,&TIM1_TimeBaseStructure);//根據(jù)TIM_TimeBaseInitStruct中指定的參數(shù)初始化TIMx的時(shí)間基數(shù)單位(5個(gè)參數(shù))
- //TIM1基本計(jì)數(shù)器設(shè)置(設(shè)置PWM頻率) //頻率=TIM1_CLK/(ARR+1)
- /* Channel 1, 2,3 and 4 Configuration in PWM mode */
- TIM1_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
- TIM1_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM1_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
- TIM1_OCInitStructure.TIM_Pulse = 0x0;
- 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); //同TimeBaseStructure (8個(gè)參數(shù)) //TIM1_OC1模塊設(shè)置(設(shè)置1通道占空比)
-
- TIM1_OCInitStructure.TIM_Pulse = 0x7fff;
- TIM_OC2Init(TIM1,&TIM1_OCInitStructure); //同TimeBaseStructure
-
- TIM1_OCInitStructure.TIM_Pulse = 0x0;
- TIM_OC3Init(TIM1,&TIM1_OCInitStructure); //同TimeBaseStructure
-
- TIM1_OCInitStructure.TIM_Pulse = 0x7fff;
- TIM_OC4Init(TIM1,&TIM1_OCInitStructure); //同TimeBaseStructure
-
- TIM_Cmd(TIM1,ENABLE); //先設(shè)置好了TimeBase、OC、BDTR 使能或者失能TIM1外設(shè)
-
- /* Main Output Enable */
- TIM_CtrlPWMOutputs(TIM1,DISABLE); //TIM1_CtrlPWMOutputs(ENABLE); 使能或者失能TIM1的主輸出
- }
- void zhi1(void) //直走速度1
- {
- TIM_SetCompare1(TIM1,TIM1Compare1[0]);
- TIM_SetCompare2(TIM1,TIM1Compare2[0]);
- TIM_SetCompare3(TIM1,TIM1Compare3[0]);
- TIM_SetCompare4(TIM1,TIM1Compare4[0]);
- TIM_CtrlPWMOutputs(TIM1,ENABLE);
- }
- void zhi2(void) //直走速度2(慢)
- {
- TIM_SetCompare1(TIM1,TIM1Compare1[0]);
- TIM_SetCompare2(TIM1,TIM1Compare2[1]);
- TIM_SetCompare3(TIM1,TIM1Compare3[0]);
- TIM_SetCompare4(TIM1,TIM1Compare4[1]);
- TIM_CtrlPWMOutputs(TIM1,ENABLE);
- }
- void zhi(void) //直走速度3(很慢)
- {
- TIM_SetCompare1(TIM1,TIM1Compare1[0]);
- TIM_SetCompare2(TIM1,TIM1Compare2[2]);
- TIM_SetCompare3(TIM1,TIM1Compare3[0]);
- TIM_SetCompare4(TIM1,TIM1Compare4[2]);
- TIM_CtrlPWMOutputs(TIM1,ENABLE);
- }
- void zuoxiao(void) //左小轉(zhuǎn)
- {
- TIM_SetCompare1(TIM1,TIM1Compare1[0]);
- TIM_SetCompare2(TIM1,TIM1Compare2[1]);
- TIM_SetCompare3(TIM1,TIM1Compare3[0]);
- TIM_SetCompare4(TIM1,TIM1Compare4[0]);
- TIM_CtrlPWMOutputs(TIM1,ENABLE);
- }
- void zuoda(void) //左大轉(zhuǎn)
- {
- TIM_SetCompare1(TIM1,TIM1Compare1[0]);
- TIM_SetCompare2(TIM1,0);
- TIM_SetCompare3(TIM1,TIM1Compare3[0]);
- TIM_SetCompare4(TIM1,TIM1Compare4[0]);
- TIM_CtrlPWMOutputs(TIM1,ENABLE);
- }
- void youxiao(void) //右小轉(zhuǎn)
- {
- TIM_SetCompare1(TIM1,TIM1Compare1[0]);
- TIM_SetCompare2(TIM1,TIM1Compare2[1]);
- TIM_SetCompare3(TIM1,TIM1Compare3[0]);
- TIM_SetCompare4(TIM1,TIM1Compare4[0]);
- TIM_CtrlPWMOutputs(TIM1,ENABLE);
- }
- void youda(void) //右大轉(zhuǎn)
- {
- TIM_SetCompare1(TIM1,TIM1Compare1[0]);
- TIM_SetCompare2(TIM1,0);
- TIM_SetCompare3(TIM1,TIM1Compare3[0]);
- TIM_SetCompare4(TIM1,TIM1Compare4[0]);
- TIM_CtrlPWMOutputs(TIM1,ENABLE);
- }
- void xunji(void) //循跡
- {
- zhi1();
- if(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_7)) youxiao();
- if(GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_8)) zuoxiao();
- if(1)
- {
- tubecnt++;
- zhi1();
- delay_ms(100);
- zuoda();
- delay_ms(100);
- }
-
- }
- u16 ceju(void)
- {
- u16 juli;
- flag=0;
- GPIO_SetBits( GPIOB, GPIO_Pin_9);
- TIM_Cmd(TIM2,ENABLE);
- while(juli==0);
- juli=add*3.4;;
- flag=0;
- return juli;
- }
- void tim2_Configuration(void)
- {
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2|RCC_APB1Periph_TIM3, ENABLE);
- TIM_DeInit(TIM2);
- TIM_TimeBaseStructure.TIM_Period=72-1;
- TIM_TimeBaseStructure.TIM_Prescaler=15-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,DISABLE);
-
- TIM_DeInit(TIM3);
- TIM_TimeBaseStructure.TIM_Prescaler =72-1; //設(shè)置了用來(lái)作為TIMx時(shí)鐘頻率除數(shù)的預(yù)分頻值
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //選擇了計(jì)數(shù)器模式
- TIM_TimeBaseStructure.TIM_Period = 20-1; //設(shè)置了15us在下一個(gè)更新事件裝入活動(dòng)的自動(dòng)重裝載寄存器周期 ARR
- TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1 ; //設(shè)置了時(shí)鐘分割
- TIM_TimeBaseInit(TIM3, & TIM_TimeBaseStructure);
- TIM_ClearFlag(TIM3,TIM_FLAG_Update);
- TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);
- TIM_Cmd(TIM3,DISABLE);
- }
- void exti_Configuration(void)
- {
- EXTI_InitTypeDef EXTI_InitStructure;
- EXTI_DeInit();
- 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_Rising_Falling;
- EXTI_InitStructure.EXTI_LineCmd = ENABLE;
- EXTI_Init(&EXTI_InitStructure);
- }
- void nvic_Configuration(void)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
- #ifdef VECT_TAB_RAM
- /* Set the Vector Table base location at 0x20000000 */
- NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
- #else /* VECT_TAB_FLASH */
- /* Set the Vector Table base location at 0x08000000 */
-
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
-
- NVIC_InitStructure.NVIC_IRQChannel =TIM2_IRQChannel;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 2;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- NVIC_InitStructure.NVIC_IRQChannel =TIM3_IRQChannel;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- NVIC_InitStructure.NVIC_IRQChannel =EXTI9_5_IRQChannel ;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority =1;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- #endif
- ……………………
- …………限于本文篇幅 余下代碼請(qǐng)從51黑下載附件…………
復(fù)制代碼
所有資料51hei提供下載:
STM32_小車_V1.7z
(137.63 KB, 下載次數(shù): 107)
2019-4-22 15:33 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
|
|