Quellcode |
* Informationstechnik
Labor SS14 Kamerabefestigung VC25-C
* Teammitglieder:
* Thorsten
Ahlers ahth1011@hs-karlsruhe.de
* David
Zagolla zada1012@hs-karlsruhe.de
*
* Web-Dokumentation:
* http://info.hit-karlsruhe.de/info-ss14/KamerabefestigungVC25C/
*
*
*/
/*
* Dieses Beispielprogramm dient der Ansteuerung von zwei Servos an einer Kamerabefestigung
*/
#include "stm32f4xx.h"
#include "stm32f4xx_rcc.h" //
Keil::Device:StdPeriph Drivers:RCC
#include "stm32f4xx_rtc.h" //
Keil::Device:StdPeriph Drivers:RTC
#include "stm32f4xx_tim.h" //
Keil::Device:StdPeriph Drivers:TIM
#include "stm32f4xx_gpio.h" //
Keil::Device:StdPeriph Drivers:GPIO
#include <stdio.h>
#include <stdint.h>
//Servo1
#define MOTORPWMTIMER1 TIM4
#define MOTORPWMTIMCLOCK1
RCC_APB1Periph_TIM4
#define MOTORPWMPORTCLOCK1 RCC_AHB1Periph_GPIOD
#define MOTORPWMAF1 GPIO_AF_TIM4
#define MOTORPWMPORT1 GPIOD
#define
MOTORPWMBIT1 GPIO_Pin_12
#define MOTORPWMTIMBIT1 GPIO_PinSource12
//Servo2
#define MOTORPWMTIMER2 TIM5
#define MOTORPWMTIMCLOCK2 RCC_APB1Periph_TIM5
#define MOTORPWMPORTCLOCK2 RCC_AHB1Periph_GPIOA
#define MOTORPWMAF2
GPIO_AF_TIM5
#define MOTORPWMPORT2 GPIOA
#define MOTORPWMBIT2 GPIO_Pin_13
#define MOTORPWMTIMBIT2 GPIO_PinSource13
// Private typedefs
----------------------------------------------------------
GPIO_InitTypeDef
GPIO_InitStructureLED;
GPIO_InitTypeDef GPIO_InitStructureTimer;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_OCInitTypeDef
TIM_OCInitStructure;
// Private variables
---------------------------------------------------------
uint32_t
TimerCounterClock = 0;
uint32_t TimerOutputClock = 0;
uint16_t
PrescalerValue = 0;
uint32_t PulseDurationInMicroSeconds = 0;
int i;
// the prototypes
------------------------------------------------------------
int main(void);
// Timer init for PWM
void TimerInit1(void);
void TimerInit2(void);
void Delay(__IO uint32_t nCount);
int main(void)
{
//
aufrufen der Timer für PWM
TimerInit1();
TimerInit2();
// Timer
Gundconfiguration
TIM_TimeBaseStructure.TIM_Period = (uint16_t) (480);
TIM_TimeBaseStructure.TIM_Prescaler = (uint16_t) 168;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
// basic
timer init
TIM_TimeBaseInit(MOTORPWMTIMER1, &TIM_TimeBaseStructure);
// configure PWM mode and duration
TIM_OCInitStructure.TIM_OCMode =
TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState =
TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse =
PulseDurationInMicroSeconds; // set the duty cycle / pulse here!
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(MOTORPWMTIMER1, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(MOTORPWMTIMER1, TIM_OCPreload_Enable);
// preload
timer config
TIM_ARRPreloadConfig(MOTORPWMTIMER1, ENABLE);
TIM_ARRPreloadConfig(MOTORPWMTIMER2, ENABLE);
// enable timer / counter
TIM_Cmd(MOTORPWMTIMER1, ENABLE);
TIM_Cmd(MOTORPWMTIMER2, ENABLE);
//
PWM Durchlauf
while(1)
{
//for (i=0; i<=8; i++)
//{
Delay(0xFFFFFF);
TIM_Cmd(MOTORPWMTIMER1, DISABLE);
//PulseDurationInMicroSeconds = i;
TIM_OCInitStructure.TIM_OCMode =
TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState =
TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 35;//+i; //
Pulsbreite min 1ms max 2ms
TIM_OCInitStructure.TIM_OCPolarity =
TIM_OCPolarity_High;
TIM_OC1Init(MOTORPWMTIMER1, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(MOTORPWMTIMER1, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(MOTORPWMTIMER1, ENABLE);
TIM_Cmd(MOTORPWMTIMER1,
ENABLE);
//}
//for (i=8; i>=0; i--)
//{
Delay(0xFFFFFF);
TIM_Cmd(MOTORPWMTIMER2, DISABLE);
//PulseDurationInMicroSeconds = i;
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_Pulse = 35;//+i; // Pulsbreite min 1ms max 2ms
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC1Init(MOTORPWMTIMER2, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(MOTORPWMTIMER2, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(MOTORPWMTIMER2, ENABLE);
TIM_Cmd(MOTORPWMTIMER2,
ENABLE);
i=0;
//}
}
}
void TimerInit1(void)
{
// set timer frequencies
TimerCounterClock = 1000000; // 1 MHz
TimerOutputClock = 10; // 10 kHz = 100 µs period
// set pulse duration in
mili seconds (HIGH time)
// can be up to from 0 to 99 (due to a
TimerOutputClock of 10 kHz)
PulseDurationInMicroSeconds = 50;
// Timer
clock enable
RCC_APB1PeriphClockCmd(MOTORPWMTIMCLOCK1, ENABLE);
//
Port clock enable
RCC_AHB1PeriphClockCmd(MOTORPWMPORTCLOCK1, ENABLE);
// Set PWM Port, Pin and method
GPIO_InitStructureTimer.GPIO_Pin =
MOTORPWMBIT1;
GPIO_InitStructureTimer.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructureTimer.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructureTimer.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructureTimer.GPIO_PuPd = GPIO_PuPd_UP ;
GPIO_Init(MOTORPWMPORT1,
&GPIO_InitStructureTimer);
// Connect TIM pin to AF
GPIO_PinAFConfig(MOTORPWMPORT1, MOTORPWMTIMBIT1, MOTORPWMAF1);
}
void
TimerInit2(void)
{
// set timer frequencies
TimerCounterClock =
1000000; // 1 MHz
TimerOutputClock = 10; // 10 kHz = 100 µs period
//
set pulse duration in mili seconds (HIGH time)
// can be up to from 0 to 99
(due to a TimerOutputClock of 10 kHz)
PulseDurationInMicroSeconds = 50;
// Timer clock enable
RCC_APB1PeriphClockCmd(MOTORPWMTIMCLOCK2, ENABLE);
// Port clock enable
RCC_AHB1PeriphClockCmd(MOTORPWMPORTCLOCK2, ENABLE);
// Set PWM Port, Pin and method
GPIO_InitStructureTimer.GPIO_Pin =
MOTORPWMBIT2;
GPIO_InitStructureTimer.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructureTimer.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructureTimer.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructureTimer.GPIO_PuPd = GPIO_PuPd_UP ;
GPIO_Init(MOTORPWMPORT2,
&GPIO_InitStructureTimer);
// Connect TIM pin to AF
GPIO_PinAFConfig(MOTORPWMPORT2, MOTORPWMTIMBIT2, MOTORPWMAF2);
}
void
Delay(__IO uint32_t nCount)
{
while(nCount--)
{
}
}