2023-07-12 06:35 AM - edited 2023-07-12 06:37 AM
Good day!
Started learning stm8l001 MC with SPL libraries, and actually cant find any examples of PWM for it.
RWM examples for STM8s was taken and changed according l001 timers and pinouts(Timer 3 - Channel 2 ,PD0),
checked bits were installed during live view in MC in IAR, but I am doing something wrong, no signal at any pin.
Also checked that timer working, because it can be connected to CCO and I can read 2MHz on CCO pin.
Here is the code:
#include "stm8l10x.h"
#include "stm8l10x_gpio.h"
#include "stm8l10x_clk.h"
#include "stm8l10x_tim3.h"
#define ASM asm
signed int pwm_duty = 0;
/* This delay should be added just after reset to have access to SWIM pin
and to be able to reprogram the device after power on (otherwise the
device will be locked) */
#define STARTUP_SWIM_DELAY_5S \
{ \
ASM(" PUSHW X \n" \
" PUSH A \n" \
" LDW X, #0xFFFF \n" \
"loop1: LD A, #50 \n" \
\
"loop2: DEC A \n" \
" JRNE loop2 \n" \
\
" DECW X \n" \
" JRNE loop1 \n" \
\
" POP A \n" \
" POPW X " );\
}
/* not connected pins as output low state (the best EMC immunity)
(PA1, PA3, PA5, PB0, PB1, PB2, PB4, PC5, PC6, PD1, PD2, PD3, PD4, PD5,
PD6, PD7)*/
void delay (int ms) //Function Definition
{
int i =0 ;
int j=0;
for (i=0; i<=ms; i++)
{
for (j=0; j<1200; j++) // Nop = Fosc/4
asm("nop"); //Perform no operation //assembly code
}
}
main()
{
/* -------------STM8L001 startup-------------- */
/* configure unbonded pins */
/* delay for SWIM connection: ~5seconds */
STARTUP_SWIM_DELAY_5S;
/* ------------------------------------------- */
uint16_t i;
/* unbonded pins as output low state (the best EMC immunity)
(PA1, PA3, PA5, PB0, PB1, PB2, PB4, PC5, PC6, PD1, PD2, PD3, PD4, PD5,
PD6, PD7)*/
GPIOA->DDR |= GPIO_Pin_1 | GPIO_Pin_3 | GPIO_Pin_5;
GPIOB->DDR |= GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_4;
GPIOC->DDR |= GPIO_Pin_5 | GPIO_Pin_6;
GPIOD->DDR |= GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
/* configure all STM8L001 pins as input with pull up */
GPIO_Init(GPIOA, GPIO_Pin_0, GPIO_Mode_In_PU_No_IT); // pin 1
GPIO_Init(GPIOA, GPIO_Pin_2, GPIO_Mode_In_PU_No_IT); // pin 2
GPIO_Init(GPIOD, GPIO_Pin_0, GPIO_Mode_In_PU_No_IT); // pin 5
GPIO_Init(GPIOB, GPIO_Pin_6, GPIO_Mode_In_PU_No_IT); // pin 6
GPIO_Init(GPIOB, GPIO_Pin_7, GPIO_Mode_In_PU_No_IT); // pin 7
GPIO_Init(GPIOC, GPIO_Pin_2, GPIO_Mode_In_PU_No_IT); // pin 8
/* initialize tested pin */
//GPIO_Init(GPIOB, GPIO_Pin_6, GPIO_Mode_Out_PP_Low_Fast);
TIM3_DeInit();
//* Enable TIM2 clock */
CLK_PeripheralClockConfig(CLK_Peripheral_TIM3, ENABLE);
//TIM3->CR1|=TIM_CR1_CMS;
// Config TIM3 Channel 1 and channel 2 pins
// GPIO_Init(GPIOD, GPIO_Pin_0, GPIO_Mode_Out_PP_Low_Fast);
GPIO_Init(GPIOD, GPIO_Pin_0, GPIO_Mode_Out_PP_Low_Fast);
// GPIOD->ODR |= GPIO_Pin_0;
//CLK_CCOConfig(CLK_Output_ClockMaster); //Try to get 2MHz on CCO, and it works
//* Time base configuration */
TIM3_TimeBaseInit(TIM3_Prescaler_1, TIM3_CounterMode_Up , 500);
TIM3->CCER1|=TIM_CCER1_CC1E;
TIM3->IER|=TIM_IER_UIE;
//* PWM1 Mode configuration: Channel2 */
TIM3_OC2Init(TIM3_OCMode_PWM1, TIM3_OutputState_Enable, 1000, TIM3_OCPolarity_High, TIM3_OCIdleState_Set);
TIM3_OC2PreloadConfig(ENABLE);
TIM3_ARRPreloadConfig(ENABLE);
/* Enable TIM2 outputs */
TIM3_CtrlPWMOutputs(ENABLE);
//TIM3->IER|=TIM_IER_UIE;
//TIM3->IER|=TIM_IER_CC1IE;
// TIM3->IER|=TIM_IER_CC2IE;
TIM3_Cmd(ENABLE);
while(TRUE)
{
for(pwm_duty = 0; pwm_duty < 1000; pwm_duty += 2){// loop through
// GPIO_ResetBits(GPIOD, GPIO_Pin_0);
TIM3_SetCompare1(pwm_duty); //set capture compair for Timer2
delay(1);}
for(pwm_duty = 1000; pwm_duty > 0; pwm_duty -= 2){
TIM3_SetCompare1(pwm_duty);
delay(100);
}
// }
/* toggle with tested pin */
//
// GPIO_ResetBits
/* delay */
//for(i=0; i<64000; i++);
//GPIO_ToggleBits(GPIOB, GPIO_Pin_6);
// delay(100);
}
}
Could Anyone Help with it or provide any working code for STM8l001 to be compared for looking for mistakes I`we done?
I will be very aprishiated!
2023-07-16 11:42 AM
In you code you are using CH1 and CH2. TIM_CCER1_CC1E is for CH1, TIM3_OC2Init is for CH2, TIM3_SetCompare1 is for CH1. Also for time base (ARR) you have value 500 and for CCR in TIM3_OC2Init you have value 1000. These values are exchanged. ARR value must be greater than CCR or the comparison always fails.
2023-07-16 12:27 PM
Examples found on the Internet for using the resources of the STM8: https://github.com/rtek1000/STM8S-examples
Including:
https://embedded-lab.com/blog/starting-stm8-microcontrollers/
2023-07-19 09:18 AM
Thank YOU!
All problems were in TIM3_SetCompare1, just changing it to TIM3_SetCompare2 and everything working now!!