2020-09-02 10:13 PM
I am using STM8AF6213 MCU in my project. I am using PD6 (AIN6) pin to read analog value. I use PD4 (TIM5_CH1) to generate PWM. But the stm8_adc1.h and stm8_tim5.h libraries that I included in my project do not work properly. It does not see the functions in the library.
#include <stm8s.h>
#include <stm8s_gpio.h>
#include <stm8s_conf.h>
#include <stm8s_tim5.h>
#include <stm8s_clk.h>
#include <stm8s_adc1.h>
uint16_t CCR1_Val1 = 5533;
uint16_t CCR1_Val2 = 4800;
uint16_t CCR1_Val3 = 4000;
static void TIM5_Config1(void);
static void TIM5_Config2(void);
static void TIM5_Config3(void);
uint16_t Conversion_Value = 0;
static void ADC_Config(void);
static void GPIO_Config(void);
void main(void)
{
CLK_DeInit();
CLK_HSECmd(DISABLE);
CLK_LSICmd(DISABLE);
CLK_HSICmd(ENABLE);
while(CLK_GetFlagStatus(CLK_FLAG_HSIRDY)==0);
CLK_ClockSwitchCmd(ENABLE);
CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV1);
CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV1);
GPIO_Init(GPIOC,GPIO_PIN_4,GPIO_MODE_OUT_PP_LOW_FAST);
CLK_CCOConfig(CLK_OUTPUT_CPU);
CLK_CCOCmd(ENABLE);
GPIO_Init(GPIOD, GPIO_PIN_6, GPIO_MODE_IN_FL_NO_IT);
ADC1_DeInit();
ADC1_Init(ADC1_CONVERSIONMODE_SINGLE, ADC1_CHANNEL_6, ADC1_PRESSEL_FCPU_D2, \
ADC1_EXTTRIG_TIM, DISABLE, ADC1_ALIGN_RIGHT, ADC1_SCHMITTTRIG_CHANNEL8,\
DISABLE);
ADC1_ITConfig(ENABLE,DISABLE);
ADC1_StartConversion();
GPIO_Init(GPIOC, GPIO_PIN_3, GPIO_MODE_OUT_PP_LOW_FAST);
Conversion_Value=ADC1_GetConversionValue();
if(Conversion_Value <= 400 && 0 <= Conversion_Value)
{
TIM5_Config1();
while (1){
}
}
else if(Conversion_Value < 800 && 400<= Conversion_Value)
{
TIM5_Config2();
while (1){
}
}
else if(Conversion_Value < 1024 && 800<= Conversion_Value)
{
TIM5_Config3();
while (1){
}
}
else{
TIM5_Config2();
while (1){
}
}}
static void TIM5_Config1(void)
{
TIM5_TimeBaseInit(TIM5_PRESCALER_8, 6665);
/* PWM1 Mode configuration: Channel1 */
TIM5_OC2Init(TIM5_OCMODE_PWM1, TIM5_OUTPUTSTATE_ENABLE,CCR1_Val1, TIM5_OCPOLARITY_HIGH);
TIM5_OC2PreloadConfig(ENABLE);
/* TIM5 enable counter */
TIM5_Cmd(ENABLE);
}
static void TIM5_Config2(void)
{
TIM5_TimeBaseInit(TIM5_PRESCALER_8, 6665);
/* PWM1 Mode configuration: Channel1 */
TIM5_OC2Init(TIM5_OCMODE_PWM1, TIM5_OUTPUTSTATE_ENABLE,CCR1_Val2, TIM5_OCPOLARITY_HIGH);
TIM5_OC2PreloadConfig(ENABLE);
/* TIM5 enable counter */
TIM5_Cmd(ENABLE);
}
static void TIM5_Config3(void)
{
TIM5_TimeBaseInit(TIM5_PRESCALER_8, 6665);
/* PWM1 Mode configuration: Channel1 */
TIM5_OC2Init(TIM5_OCMODE_PWM1, TIM5_OUTPUTSTATE_ENABLE,CCR1_Val3, TIM5_OCPOLARITY_HIGH);
TIM5_OC2PreloadConfig(ENABLE);
/* TIM5 enable counter */
TIM5_Cmd(ENABLE);
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval : None
*/
void assert_failed(u8* file, u32 line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif