Hi,I have problem to start with stm32f051r8t6 on discovery and single controller mc33926.
I read the sf and it always low and motor dont start, adc works, but I dont know about pwm
Here is my code :
#include <stm32f0xx_gpio.h>
#include <stm32f0xx_rcc.h>
#include "stm32f0xx_adc.h"
#include "stm32f0xx_tim.h"
GPIO_InitTypeDef Gp;
ADC_InitTypeDef ADC_INIT;
TIM_TimeBaseInitTypeDef timerInitStructure;
TIM_OCInitTypeDef outputChannelInit;
#define GreenLED_Pin GPIO_Pin_9
#define BlueLED_Pin GPIO_Pin_8
#define przycisk_Pin GPIO_Pin_0
int odczyt_adc;
uint8_t ButtonRead = 0;
uint8_t blad = 1;
#define IN2 GPIO_Pin_14 //PB14
#define IN1 GPIO_Pin_13 //PB13
#define EN_P GPIO_Pin_6 //PC6
#define SLEW GPIO_Pin_7 //PC7
#define SF_pin GPIO_Pin_7 //PA7
#define PWM_pin GPIO_Pin_3 //PA3
void wyjscia_init(void)
{
Gp.GPIO_Pin =GreenLED_Pin | BlueLED_Pin | EN_P | SLEW; //Set pins inside the struct
Gp.GPIO_Mode = GPIO_Mode_OUT; //Set GPIO pins as output
Gp.GPIO_OType = GPIO_OType_PP; //Ensure output is push-pull vs open drain
Gp.GPIO_PuPd = GPIO_PuPd_NOPULL; //No internal pullup resistors required
Gp.GPIO_Speed = GPIO_Speed_Level_1; //Set GPIO speed to lowest
GPIO_Init(GPIOC, &Gp); //Assign struct to LED_GPIO
Gp.GPIO_Pin = IN1 | IN2; //Set pins inside the struct
Gp.GPIO_Mode = GPIO_Mode_OUT; //Set GPIO pins as output
Gp.GPIO_OType = GPIO_OType_PP; //Ensure output is push-pull vs open drain
Gp.GPIO_PuPd = GPIO_PuPd_NOPULL; //No internal pullup resistors required
Gp.GPIO_Speed = GPIO_Speed_Level_1; //Set GPIO speed to lowest
GPIO_Init(GPIOB, &Gp); //Assign struct to LED_GPIO
}
void wejscia_init(void)
{
Gp.GPIO_Pin = SF_pin | przycisk_Pin; //Set pins inside the struct
Gp.GPIO_Mode = GPIO_Mode_IN; //Set GPIO pins as output
Gp.GPIO_PuPd = GPIO_PuPd_NOPULL; //No pullup required as pullup is external
GPIO_Init(GPIOA, &Gp); //Assign struct to LED_GPIO
}
void ADC_init(void)
{
ADC_DeInit(ADC1);
Gp.GPIO_Mode = GPIO_Mode_AN;
Gp.GPIO_OType = GPIO_OType_PP;
Gp.GPIO_Pin = GPIO_Pin_1;
Gp.GPIO_PuPd = GPIO_PuPd_NOPULL;
Gp.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&Gp);
ADC_INIT.ADC_Resolution = ADC_Resolution_12b;
ADC_INIT.ADC_ContinuousConvMode = ENABLE;
ADC_INIT.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_INIT.ADC_DataAlign = ADC_DataAlign_Right;
ADC_INIT.ADC_ScanDirection = ADC_ScanDirection_Upward;
ADC_Init(ADC1, &ADC_INIT);
ADC_Cmd(ADC1, ENABLE);
ADC_ChannelConfig(ADC1,ADC_Channel_1, ADC_SampleTime_71_5Cycles);
ADC_StartOfConversion(ADC1);
}
void PWM_INIT(void)
{
Gp.GPIO_Pin = PWM_pin;
Gp.GPIO_Mode = GPIO_Mode_AF;
Gp.GPIO_OType = GPIO_OType_PP;
Gp.GPIO_PuPd = GPIO_PuPd_NOPULL;
Gp.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&Gp);
GPIO_PinAFConfig(GPIOA, PWM_pin, GPIO_AF_2);
timerInitStructure.TIM_Prescaler = 48000;
timerInitStructure.TIM_CounterMode = TIM_CounterMode_Up;
timerInitStructure.TIM_Period = 4000;
timerInitStructure.TIM_ClockDivision = TIM_CKD_DIV1;
timerInitStructure.TIM_RepetitionCounter = 0;
TIM_TimeBaseInit(TIM2, &timerInitStructure);
outputChannelInit.TIM_OCMode = TIM_OCMode_PWM1;
outputChannelInit.TIM_OutputState = TIM_OutputState_Enable;
outputChannelInit.TIM_Pulse = 1000;
outputChannelInit.TIM_OCPolarity = TIM_OCPolarity_High;
TIM_OC4Init(TIM2, &outputChannelInit);
TIM_OC4PreloadConfig(TIM2, TIM_OCPreload_Enable);
TIM_Cmd(TIM2, ENABLE);
}
int main(void)
{
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC, ENABLE);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
wyjscia_init();
wejscia_init();
ADC_init();
PWM_INIT();
GPIO_SetBits(GPIOB, IN1);
GPIO_ResetBits(GPIOB, IN2);
GPIO_SetBits(GPIOC, EN_P);
GPIO_SetBits(GPIOC, SLEW);
while(1)
{
odczyt_adc=ADC_GetConversionValue(ADC1);
TIM_SetCompare1(TIM2,odczyt_adc);
ButtonRead = GPIO_ReadInputDataBit(GPIOA, przycisk_Pin);
blad= GPIO_ReadInputDataBit(GPIOA, SF_pin);
if(ButtonRead)
GPIO_SetBits(GPIOC, GreenLED_Pin);
if(!blad)
GPIO_SetBits(GPIOC, BlueLED_Pin);
// if(odczyt_adc>2050){
// GPIO_SetBits(GPIOC, GreenLED_Pin);
// GPIO_ResetBits(GPIOC, BlueLED_Pin);
// }
// if(odczyt_adc<1950){
// GPIO_ResetBits(GPIOC, GreenLED_Pin);
// GPIO_SetBits(GPIOC, BlueLED_Pin);
}
}