用mini版做的循迹小车,其中TIM4产生4路PWM,GPIOC作为读取Sensor状态;程序如下:
Sensor.c
#include "sensor.h"
#include "delay.h"
void SenRead_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOC,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOC,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOC,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_3;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOC,&GPIO_InitStructure);
}
u8 Sen_Scan(u8 mode)
{
static u8 sen_up=1;
if(mode) sen_up=1;
if(sen_up&(Sen1==0||Sen2==0||Sen3==0|Sen4==0))
{
delay_ms(10);
sen_up=0;
if(Sen1==0) return Sen1_State;
else if(Sen1==0&&Sen2==0) return Sen2_State;
else if(Sen2==0) return Sen3_State;
else if(Sen2==0&&Sen3==0) return Sen4_State;
else if(Sen3==0) return Sen5_State;
else if(Sen3==0&&Sen4==0) return Sen6_State;
else if(Sen4==0) return Sen7_State;
}
else if(Sen1==1&&Sen2==1&&Sen3==1&&Sen4==1)
sen_up=1;
retrun 0;
}
sensor.h
#ifdef __SENSOR_H
#define __SENSOR_H
#define Sen1 GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_0)
#define Sen2 GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_1)
#define Sen3 GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_2)
#define Sen4 GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_3)
#define Sen1_State 1
#define Sen2_State 2
#define Sen3_State 3
#define Sen4_State 4
#define Sen5_State 5
#define Sen6_State 6
#define Sen7_State 7
void SenRead_Init(void);
u8 Sen_Scan();
#endif
PWM.c
#include "pwm.h"
#include "sensor.h"
void TIM4_PWM_Init(u16 arr,u16 psc)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6;
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_7;
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_8;
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_9;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,GPIO_InitStructure);
TIM_TimeBaseStructure.TIM_Period=arr;
TIM_TimeBaseStructure.TIM_Prescaler=psc;
TIM_TimeBaseStructure.TIM_ClockDivision=0;
TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM4,&TIM_TimeBaseStructure);
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
TIM_OC1Init(TIM4,&TIM_OCInitStructure);
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
TIM_OC2Init(TIM4,&TIM_OCInitStructure);
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
TIM_OC3Init(TIM4,&TIM_OCInitStructure);
TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;
TIM_OC4Init(TIM4,&TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM4,TIM_OCPreload_Enable);
TIM_OC2PreloadConfig(TIM4,TIM_OCPreload_Enable);
TIM_OC3PreloadConfig(TIM4,TIM_OCPreload_Enable);
TIM_OC4PreloadConfig(TIM4,TIM_OCPreload_Enable);
TIM_Cmd(TIM4,ENABLE);
}
PWM.h
#ifndef __PWM_H
#define __PWM_H
#include "sys.h"
void TIM4_PWM_Init(u16 arr,u16 psc);
#endif
报错代码如下:
compiling pwm.c...
..HARDWAREPWMpwm.c(16): error: #167: argument of type "GPIO_InitTypeDef" is incompatible with parameter of type "GPIO_InitTypeDef *"
..HARDWAREPWMpwm.c(21): error: #167: argument of type "GPIO_InitTypeDef" is incompatible with parameter of type "GPIO_InitTypeDef *"
..HARDWAREPWMpwm.c(26): error: #167: argument of type "GPIO_InitTypeDef" is incompatible with parameter of type "GPIO_InitTypeDef *"
..HARDWAREPWMpwm.c(31): error: #167: argument of type "GPIO_InitTypeDef" is incompatible with parameter of type "GPIO_InitTypeDef *"
compiling sensor.c...
..HARDWARESENSORsensor.c(36): error: #20: identifier "Sen1" is undefined
..HARDWARESENSORsensor.c(36): error: #20: identifier "Sen2" is undefined
..HARDWARESENSORsensor.c(36): error: #20: identifier "Sen3" is undefined
..HARDWARESENSORsensor.c(36): error: #20: identifier "Sen4" is undefined
..HARDWARESENSORsensor.c(40): error: #20: identifier "Sen1_State" is undefined
..HARDWARESENSORsensor.c(41): error: #20: identifier "Sen2_State" is undefined
..HARDWARESENSORsensor.c(42): error: #20: identifier "Sen3_State" is undefined
..HARDWARESENSORsensor.c(43): error: #20: identifier "Sen4_State" is undefined
..HARDWARESENSORsensor.c(44): error: #20: identifier "Sen5_State" is undefined
..HARDWARESENSORsensor.c(45): error: #20: identifier "Sen6_State" is undefined
..HARDWARESENSORsensor.c(46): error: #20: identifier "Sen7_State" is undefined
..HARDWARESENSORsensor.c(50): error: #20: identifier "retrun" is undefined
..HARDWARESENSORsensor.c(50): error: #65: expected a ";"
..HARDWARESENSORsensor.c(52): warning: #940-D: missing return statement at end of non-void function "Sen_Scan"
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
一周热门 更多>