G
G
Glavryba2014-07-15 09:17:52
Microcontrollers
Glavryba, 2014-07-15 09:17:52

How to implement a pulse generator on STM8?

I am slowly mastering the MK STM8 (STM8S-Discovery), I set out to implement a rectangular pulse generator. I just can not figure out how to display the result on a standard LED. The task of the device is to start issuing rectangular pulses with a certain delay by pressing the button. On the front of the pulses, it is necessary to light the LED.
What does the program look like now?

#define CCR1_Val ((uint16_t)976)
#define CCR2_Val ((uint16_t)488)
#define CCR3_Val  ((uint8_t)244)

void TIM1_Config(void);
void GPIO_Config(void);

//timer up mode, compare toggle mode
__interrupt void Timer_Compare_isr();


u8 edgeNum = 7;
u16 edges[7] = {50,100,50,500,50,100,50};

void main(void)
{
 //Инициализируем CLK
  CLK_ECKR_bit.HSEEN=1;     //Разрешаем работу генератора с внешним кварцем (HSEEN)
  CLK_SWCR_bit.SWEN=1;      //Разрешаем переключение генераторов;
  CLK_SWR=0xB4;             //Выбираем clock от кварцевого генератора (HSE)
  CLK_CKDIVR=0;             //Делители частоты внутреннего и внешнего генератора на 1 - частота ядра максимальная
  while(CLK_CMSR!=0xB4);    //Ждем стабилизации частоты
  CLK_CSSR_bit.CSSEN=1;     //Разрешаем автопереключение источника Clock на внутренний при неисправности генератора
 /* GPIO configuration -----------------------------------------*/
  GPIO_Config();  
//Инициализируем TIM1
  TIM1_DeInit();
 
  TIM1_ClearFlag(TIM1_FLAG_UPDATE);
  TIM1_TimeBaseInit(128,TIM1_COUNTERMODE_UP  
 , 125, 0);
    TIM1_ITConfig(TIM1_IT_UPDATE, ENABLE);
    TIM1_IER_bit.UIE=1;       //Разрешаем генерацию прерывания при переполнеини
    TIM1_CCMR1=1;  

   
  TIM1_CR1_bit.CEN=1;       //Разрешаем счет

  __enable_interrupt();     //Разрешаем прерывания
  u8 key;

  while (1)
   {
 
   key = GPIO_ReadInputPin(GPIOB, GPIO_PIN_6);
   
    if ( !key ){
    TIM1_CR1_bit.CEN=1;
   static u8 idx = 0;
  TIM1_CCR1H += edges[idx++];
 
 if (idx >= edgeNum){
    idx = 0;
    GPIO_WriteHigh(GPIOD, GPIO_PIN_0);
    TIM1_CR1_CEN = 0;
  }
  if (!TIM1_CHANNEL_1) {
    GPIO_WriteHigh(GPIOD, GPIO_PIN_0); }  
 }
} }
static void GPIO_Config(void)
{
 
  GPIO_Init(GPIOD, GPIO_PIN_0, GPIO_MODE_OUT_PP_LOW_FAST);
   GPIO_Init(GPIOB, GPIO_PIN_6, GPIO_MODE_IN_PU_NO_IT );

I realize that I need to work through TIM1_CHx, but I don’t understand at all how to implement it.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Eddy_Em, 2014-07-20
@Glavryba

As an example, I can advise my stepper motor controller . Clock pulses are set by three independent generators.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question