You are on page 1of 6

/* Includes ------------------------------------------------------------------*/

#include "main.h"
#include "stm32f4xx.h"
#include <stdio.h>
#include <math.h>
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
#define LED1 GPIOD , GPIO_Pin_12
#define LED2 GPIOD , GPIO_Pin_13
#define LED3 GPIOD , GPIO_Pin_14
#define LED4 GPIOD , GPIO_Pin_15
#define LED5 GPIOD , GPIO_Pin_5
#define LED6 GPIOA , GPIO_Pin_9
#define OFF GPIO_ResetBits
#define ON GPIO_SetBits
/* Private variables ---------------------------------------------------------*/
__IO uint32_t TimeOut = 10;
uint8_t i;
uint8_t MSB, LSB;
int16_t Xg, Yg ,Zg , Counter; // 16-bit values f
rom accelerometer
uint16_t CCR1_Val = 1;
uint16_t CCR2_Val = 1;
uint16_t CCR3_Val = 1;
uint16_t CCR4_Val = 1;
uint16_t PrescalerValue = 0;
SPI_InitTypeDef SPI_InitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
/* Private function prototypes -----------------------------------------------*/
static void SysTickConfig(void);
static void Led_Config(void);
void mySPI_SendData(uint8_t adress, uint8_t data);
uint8_t mySPI_GetData(uint8_t adress);
void mySPI_Init(void);
void TIM4_PWM_Config(void);
void GPIO_PWM_Config(void);
/* Private functions ---------------------------------------------------------*/
int main(void)
{
Led_Config();
TIM4_PWM_Config();
GPIO_PWM_Config();
/* SysTick configuration */
SysTickConfig();
// Turn off led5
ON(LED5);
uint32_t m = 5000000;
while(m--);
mySPI_Init();
mySPI_SendData(0x23, 0xc9); // resetting the acceler
ometer internal circuit
mySPI_SendData(0x20, 0x67); // 100Hz data update rat
e, block data update disable, x/y/z enabled
mySPI_SendData(0x24, 0x20); // Anti aliasing filter
bandwidth 800Hz, 16G (very sensitive), no self-test, 4-wire interface
mySPI_SendData(0x10, 0x00); // Output(X) = Measureme
nt(X) - OFFSET(X) * 32;
mySPI_SendData(0x11, 0x00); // Output(Y) = Measureme
nt(Y) - OFFSET(Y) * 32;
mySPI_SendData(0x12, 0x00); // Output(Z) = Measureme
nt(Z) - OFFSET(Z) * 32;
while(1) // X and Z axes example
{
MSB = mySPI_GetData(0x29); // X-axis MSB
LSB = mySPI_GetData(0x28); // X-axis LSB
Xg = (MSB << 8) | (LSB); // Merging
MSB = mySPI_GetData(0x2B); // y-axis MSB
LSB = mySPI_GetData(0x2A); // y-axis LSB
Yg = (MSB << 8) | (LSB); // Merging

MSB = mySPI_GetData(0x2D); // y-axis MSB


LSB = mySPI_GetData(0x2C); // y-axis LSB
Zg = (MSB << 8) | (LSB); // Merging
if(Xg < 0)CCR1_Val = ((-Xg)*664/1500);
else CCR1_Val = 0;
TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
TIM_OC1Init(TIM4, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);
if(Yg > 0)CCR2_Val = ((Yg)*664/1500);
else CCR2_Val = 0;
TIM_OCInitStructure.TIM_Pulse = CCR2_Val;
TIM_OC2Init(TIM4, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable);
if( Xg > 0 && Xg < 600) Xg /= 2;
if(Xg > 0) CCR3_Val = ((Xg)*664/1500);
else CCR3_Val = 0;
TIM_OCInitStructure.TIM_Pulse = CCR3_Val;
TIM_OC3Init(TIM4, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable);
if( Yg > 0 && Yg < 1300) Yg += 300;
if(Yg < 0)CCR4_Val = ((-Yg)*664/1500);
else CCR4_Val = 0;
TIM_OCInitStructure.TIM_Pulse = CCR4_Val;
TIM_OC4Init(TIM4, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Enable);
if (Counter < 500){
Counter++;
}
else{
Counter = 0;
if (fabs(Yg) > 100 || fabs(Xg) >100)
{
// ON LED6
OFF(LED6);
}
else
{
//OFF LED6
GPIO_ToggleBits(LED6);
}
}
}
}
void GPIO_PWM_Config(void){
GPIO_InitTypeDef GPIO_InitStructure;
// GPIO clock enable
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
// GPIOC Configuration: TIM4 CH1 (PD12), TIM4 CH2 (PD13), TIM4 CH3 (PD14) an
d TIM4 CH4 (PD15)
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_14 | GPIO_Pin_15 | GPIO
_Pin_13;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
// Connect TIM4 pins to AF2
GPIO_PinAFConfig(GPIOD, GPIO_PinSource12, GPIO_AF_TIM4);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource13, GPIO_AF_TIM4);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource14, GPIO_AF_TIM4);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource15, GPIO_AF_TIM4);
}

void TIM4_PWM_Config(void){
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
/* TIM4 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE);
/* Compute the prescaler value */
PrescalerValue = (uint16_t) ((SystemCoreClock )/8400000);
/* Time base configuration */
TIM_TimeBaseStructure.TIM_Period = 665;
TIM_TimeBaseStructure.TIM_Prescaler = PrescalerValue;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
/* PWM1 Mode configuration: Channel 1 */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OCInitStructure.TIM_Pulse = CCR1_Val;
TIM_OC1Init(TIM4, &TIM_OCInitStructure);
TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);
/* PWM1 Mode configuration: Channel 2 */
TIM_OCInitStructure.TIM_Pulse = CCR2_Val;
TIM_OC2Init(TIM4, &TIM_OCInitStructure);
TIM_OC2PreloadConfig(TIM4, TIM_OCPreload_Enable);
/* PWM1 Mode configuration: Channel 3 */
TIM_OCInitStructure.TIM_Pulse = CCR3_Val;
TIM_OC3Init(TIM4, &TIM_OCInitStructure);
TIM_OC3PreloadConfig(TIM4, TIM_OCPreload_Enable);
/* PWM1 Mode configuration: Channel 4 */
TIM_OCInitStructure.TIM_Pulse = CCR4_Val;
TIM_OC4Init(TIM4, &TIM_OCInitStructure);
TIM_OC4PreloadConfig(TIM4, TIM_OCPreload_Enable);
TIM_ARRPreloadConfig(TIM4, ENABLE);
/* TIM4 enable counter */
TIM_Cmd(TIM4, ENABLE);
}
uint8_t mySPI_GetData(uint8_t adress){
GPIO_ResetBits(GPIOE, GPIO_Pin_3);
adress = 0x80 | adress;
//transmit buffer empty?
while(!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE));
SPI_I2S_SendData(SPI1, adress);
//data received?
while(!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE));
//Clear RXNE bit
SPI_I2S_ReceiveData(SPI1);
//transmit buffer empty?
while(!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE));
//Dummy byte to generate clock
SPI_I2S_SendData(SPI1, 0x00);
//data received?
while(!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE));
GPIO_SetBits(GPIOE, GPIO_Pin_3);
//return reveiced data
return SPI_I2S_ReceiveData(SPI1);
}
void mySPI_SendData(uint8_t adress, uint8_t data){
GPIO_ResetBits(GPIOE, GPIO_Pin_3);
//transmit buffer empty?
while(!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE));
SPI_I2S_SendData(SPI1, adress);
//data received?
while(!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE));
SPI_I2S_ReceiveData(SPI1);
//transmit buffer empty?
while(!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE));
SPI_I2S_SendData(SPI1, data);
//data received?
while(!SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_RXNE));
SPI_I2S_ReceiveData(SPI1);
GPIO_SetBits(GPIOE, GPIO_Pin_3);
}
void mySPI_Init(void){
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOE , ENABLE)
;
GPIO_InitTypeDef GPIO_InitTypeDefStruct;
GPIO_InitTypeDefStruct.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_7 | GPIO_Pin_6;
GPIO_InitTypeDefStruct.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitTypeDefStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitTypeDefStruct.GPIO_OType = GPIO_OType_PP;
GPIO_InitTypeDefStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitTypeDefStruct);
GPIO_InitTypeDefStruct.GPIO_Pin = GPIO_Pin_3;
GPIO_InitTypeDefStruct.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitTypeDefStruct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitTypeDefStruct.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_InitTypeDefStruct.GPIO_OType = GPIO_OType_PP;
GPIO_Init(GPIOE, &GPIO_InitTypeDefStruct);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_SPI1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_SPI1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_SPI1);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SPI1, ENABLE);
SPI_InitTypeDef SPI_InitTypeDefStruct;
SPI_InitTypeDefStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;
SPI_InitTypeDefStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitTypeDefStruct.SPI_Mode = SPI_Mode_Master;
SPI_InitTypeDefStruct.SPI_DataSize = SPI_DataSize_8b;
SPI_InitTypeDefStruct.SPI_NSS = SPI_NSS_Soft;
SPI_InitTypeDefStruct.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitTypeDefStruct.SPI_CPOL = SPI_CPOL_High;
SPI_InitTypeDefStruct.SPI_CPHA = SPI_CPHA_2Edge;
SPI_InitStructure.SPI_CRCPolynomial = 7; //?
SPI_Init(SPI1, &SPI_InitTypeDefStruct);
SPI_Cmd(SPI1, ENABLE);
}
static void Led_Config(void){
// Enable LED
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO
_Pin_15 | GPIO_Pin_5;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
static void SysTickConfig(void){
/* Setup SysTick Timer for 10ms interrupts */
if (SysTick_Config(SystemCoreClock / 100)){
/* Capture error */
while (1);
}
/* Configure the SysTick handler priority */
NVIC_SetPriority(SysTick_IRQn, 0);
}
#ifdef USE_FULL_ASSERT
void assert_failed(uint8_t* file, uint32_t line){
while (1){
}
}
#endif
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/