用正点原子的精英版作为主控,红外循迹用TCRT5000芯片。
单片机源程序如下:
/* 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; //设置了用来作为TIMx时钟频率除数的预分频值
TIM1_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //选择了计数器模式
TIM1_TimeBaseStructure.TIM_Period = 0xFFFF; //设置了在下一个更新事件装入活动的自动重装载寄存器周期 ARR
TIM1_TimeBaseStructure.TIM_ClockDivision = 0x0; //设置了时钟分割
TIM1_TimeBaseStructure.TIM_RepetitionCounter = 0x0; //设置了周期计数器值
TIM_TimeBaseInit(TIM1,&TIM1_TimeBaseStructure);//根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位(5个参数)
//TIM1基本计数器设置(设置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个参数) //TIM1_OC1模块设置(设置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); //先设置好了TimeBase、OC、BDTR 使能或者失能TIM1外设
/* 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) //左小转
{
TIM_SetCompare1(TIM1,TIM1Compare1[0]);
TIM_SetCompare2(TIM1,TIM1Compare2[1]);
TIM_SetCompare3(TIM1,TIM1Compare3[0]);
TIM_SetCompare4(TIM1,TIM1Compare4[0]);
设计资源 培训 开发板 精华推荐
- 使用 Analog Devices 的 LTM4603HVEV 的参考设计
- inDART-HCS08D: In-Circuit, Real-Time Debugger/Programmer for NXP S08 Family
- LDK120M25R 2.5V低压降稳压器典型应用固定电路
- EVAL-AD7993EBZ,用于 AD7993、4 通道、10 位 ADC 的评估板,具有 I2C 兼容接口
- 基于51单片机的舵机控制器
- 采用 14 引脚 SOIC 封装的四通道高速运算放大器评估板
- 使用 Microchip Technology 的 TC28C46EPE 的参考设计
- ADM00398,用于 16 位 MCU 系统的 ADC 评估板提供评估 MCP3911 双通道 ADC 性能的能力
- DC2397A,基于 LTC3130EUD-1 的演示板,2.4V = VIN = 25V,VOUT = 1.8V/3.3V/5V/12V @ 600mA 同步降压-升压转换器
- NUVOTON(新唐) - MINI58ZDE