Перейти к содержанию
    

Захват сигнала на STM8L001J3

Добрый день!
У меня есть микроконтроллер STM8L001J3, на вход TIM3, CH2 приходит последовательность прямоугольных импульсов с ультразвукового датчика, мне надо отследить когда сигнал с датчика прервётся в случае когда например ограниченную им территорию пересечёт человек. Но я не могу совсем разобраться как это настроить и тем более отследить прерывание сигнала. Я попытался написать код на запутался в каналах контроллера и регистрах.
 

Spoiler
/**
  ******************************************************************************
  * @file     STM8/main.c
  * @author   MCD Application Team
  * @version V1.2.1
  * @date    30-September-2014
  * @brief    This file contains the firmware main function.
  ******************************************************************************
  * @attention
  *
  * <h2><center>&copy; COPYRIGHT 2014 STMicroelectronics</center></h2>
  *
  * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  * You may not use this file except in compliance with the License.
  * You may obtain a copy of the License at:
  *
  *        http://www.st.com/software_license_agreement_liberty_v2
  *
  * Unless required by applicable law or agreed to in writing, software 
  * distributed under the License is distributed on an "AS IS" BASIS, 
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  *
  ******************************************************************************
/* MAIN.C file */
#include "stm8l10x.h"
#include "stm8l10x_gpio.h"
#include "stm8l10x_clk.h"

uint16_t pulse_lenght=0;

#ifdef _COSMIC_
  #define ASM _asm
#endif
#ifdef _IAR_
  #define ASM asm
#endif

/*sturt up delay*/
#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)*/
#define CONFIG_UNUSED_PINS_STM8L001 \
{ \
  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; \
}

main()
{
/* -------------STM8L001 startup-------------- */
/* configure unbonded pins */
  CONFIG_UNUSED_PINS_STM8L001;
/* delay for SWIM connection: ~5seconds */
  STARTUP_SWIM_DELAY_5S;
/* ------------------------------------------- */
/* 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 TIM3 */
  GPIO_Init(GPIOD, GPIO_Pin_0, GPIO_Mode_In_FL_No_IT); // CH2: input
  CLK_PeripheralClockConfig(CLK_Peripheral_TIM3, ENABLE); //initial clock to TIM3
  TIM3_DeInit(); //DeInit TIM3
  TIM3_TimeBaseInit(TIM3_Prescaler_2, TIM3_CounterMode_Up, 24); //configure TIM3
  TIM3_ICInit(TIM3_Channel_1, TIM3_ICPolarity_Rising, TIM3_ICSelection_DirectTI, TIM3_ICPSC_Div1, 0); //configure capture-compare mod on Rising edge
  TIM3_ICInit(TIM3_Channel_2, TIM3_ICPolarity_Falling, TIM3_ICSelection_IndirectTI, TIM3_ICPSC_Div1, 0); //configure capture-compare mod on Falling edge
  TIM3_SelectInputTrigger(TIM3_TRGSelection_TI1FP1);
  TIM3_SelectSlaveMode(TIM3_SlaveMode_Reset); 
  TIM3_CCxCmd(TIM3_Channel_2, ENABLE);
  TIM3_Cmd(ENABLE);
  while (1)
  { 
    pulse_lenght=TIM3_GetCapture2();
  }
}
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/

image.png

image.png

Изменено пользователем haker_fox
STM8 это не 32-битный микроконтроллер. Тему перенёс. Длинный код нужно прятать под спойлер.

Поделиться сообщением


Ссылка на сообщение
Поделиться на другие сайты

Присоединяйтесь к обсуждению

Вы можете написать сейчас и зарегистрироваться позже. Если у вас есть аккаунт, авторизуйтесь, чтобы опубликовать от имени своего аккаунта.

Гость
Ответить в этой теме...

×   Вставлено с форматированием.   Вставить как обычный текст

  Разрешено использовать не более 75 эмодзи.

×   Ваша ссылка была автоматически встроена.   Отображать как обычную ссылку

×   Ваш предыдущий контент был восстановлен.   Очистить редактор

×   Вы не можете вставлять изображения напрямую. Загружайте или вставляйте изображения по ссылке.

×
×
  • Создать...