Я использую мини-плату STM8S003F3PU. Мое программное обеспечение - STVD и Cosmi c. Почему мой TIM1 не работает по-настоящему? Он должен измерить входной сигнал HIGH_Duration на TIM1_CH1 (= PC6), и мой светодиод на PA3 выключится. Но это не так. Кроме того, я использую режим отладки, но он не входит в мое состояние (если (duty_cycle_updated))
Main. c:
#include "stm8s.h"
//Pulse width measurment
unsigned char data_value;
unsigned long duty_cycle = 0;
unsigned long duty_cycle_temp = 0;
unsigned long start_time = 0;
unsigned long end_time = 0;
bool duty_cycle_updated=FALSE;
bool state=FALSE;
void clock_setup(void);
void GPIO_setup(void);
void TIM1_setup(void);
void TIM2_setup(void);
void delay_cycles(uint16_t cy);
void main() {
//Set PinMode
GPIOA->DDR |= 0xFF; // PD.0 as Output
GPIOA->CR1 |= 0xFF; // PD.0 as Push Pull Type Output
GPIOA->ODR |=1<<3; // PD.o = 1
clock_setup();
GPIO_setup();
TIM1_setup();
TIM2_setup();
//delay_ms(10);
while(TRUE){
if(duty_cycle_updated){
GPIOA->ODR &= ~(1<<3); // PD.0 = 0
duty_cycle_temp=duty_cycle;
duty_cycle_updated=FALSE;
}
delay_cycles(10);
};
void clock_setup(void){
CLK_DeInit();
CLK_HSECmd(DISABLE);
CLK_LSICmd(DISABLE);
CLK_HSICmd(ENABLE);// eNABLE INTERNAL hIGH SPEED CLK
while(CLK_GetFlagStatus(CLK_FLAG_HSIRDY) == FALSE);
CLK_ClockSwitchCmd(ENABLE);
CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV8);
CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV1);
CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO, CLK_SOURCE_HSI,
DISABLE, CLK_CURRENTCLOCKSTATE_ENABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER1, ENABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER2, ENABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_SPI, DISABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_I2C, DISABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_ADC, DISABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_AWU, DISABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_UART1, DISABLE);
CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER4, DISABLE);
}
void GPIO_setup(void){
GPIO_DeInit(GPIOC);
GPIO_Init(GPIOC, GPIO_PIN_1, GPIO_MODE_IN_FL_NO_IT);
GPIO_DeInit(GPIOD);
GPIO_Init(GPIOD, GPIO_PIN_4, GPIO_MODE_OUT_PP_HIGH_FAST);
}
void TIM1_setup(void){
TIM1_DeInit();
TIM1_TimeBaseInit(2000, TIM1_COUNTERMODE_UP, 55535, 1);
TIM1_CCxCmd(TIM1_CHANNEL_1, ENABLE);
TIM1_ICInit(TIM1_CHANNEL_1, TIM1_ICPOLARITY_RISING, TIM1_ICSELECTION_DIRECTTI, 1, 1);
TIM1_ITConfig(TIM1_IT_CC1, ENABLE);
TIM1_Cmd(ENABLE);
enableInterrupts();
}
void TIM2_setup(void){
TIM2_DeInit();
TIM2_TimeBaseInit(TIM2_PRESCALER_32, 1250);
TIM2_OC1Init(TIM2_OCMODE_PWM2, TIM2_OUTPUTSTATE_ENABLE, 1000, TIM2_OCPOLARITY_LOW);
TIM2_SetCompare1(1125);
TIM2_Cmd(ENABLE);
}
void delay_cycles(uint16_t cy){
do{
--cy;
}while(cy);
}
stm8s_it. c :( Верхняя часть только)
#include "stm8s_it.h"
#include "stm8s_eval.h"
#include "stm8s.h"
static bool state = FALSE;
extern unsigned long duty_cycle;
extern unsigned long start_time;
extern unsigned long end_time;
static bool duty_cycle_updated=FALSE;
unsigned long my_test_cunt=0;
void TIM1_CH1_OVF_IRQHandler(void){
my_test_cunt++;
}
void TIM1_CH1_CCP_IRQHandler(void){
if(TIM1_GetFlagStatus(TIM1_FLAG_CC1)) {
if(state == FALSE) {
start_time = TIM1_GetCapture1();
TIM1_ICInit(TIM1_CHANNEL_1, TIM1_ICPOLARITY_FALLING, TIM1_ICSELECTION_DIRECTTI, 1, 1);
}
else {
end_time = TIM1_GetCapture1();
TIM1_ICInit(TIM1_CHANNEL_1, TIM1_ICPOLARITY_RISING, TIM1_ICSELECTION_DIRECTTI, 1, 1);
duty_cycle = (end_time - start_time);
duty_cycle_updated=TRUE;
}
state = ~state;
}
TIM1_ClearITPendingBit(TIM1_IT_CC1);
TIM1_ClearFlag(TIM1_FLAG_CC1);
}
stm8s_interupt_vector:
#include "stm8s_it.h"
typedef void @far (*interrupt_handler_t)(void);
struct interrupt_vector {
unsigned char interrupt_instruction;
interrupt_handler_t interrupt_handler;
};
@far @interrupt void NonHandledInterrupt (void)
{
/* in order to detect unexpected events during development,
it is recommended to set a breakpoint on the following instruction
*/
return;
}
extern void _stext(); /* startup routine */
struct interrupt_vector const _vectab[] = {
{0x82, (interrupt_handler_t)_stext}, /* reset */
{0x82, NonHandledInterrupt}, /* trap */
{0x82, NonHandledInterrupt}, /* irq0 */
{0x82, NonHandledInterrupt}, /* irq1 */
{0x82, NonHandledInterrupt}, /* irq2 */
{0x82, NonHandledInterrupt}, /* irq3 */
{0x82, NonHandledInterrupt}, /* irq4 */
{0x82, NonHandledInterrupt}, /* irq5 */
{0x82, NonHandledInterrupt}, /* irq6 */
{0x82, NonHandledInterrupt}, /* irq7 */
{0x82, NonHandledInterrupt}, /* irq8 */
{0x82, NonHandledInterrupt}, /* irq9 */
{0x82, NonHandledInterrupt}, /* irq10 */
{0x82, (interrupt_handler_t)TIM1_CH1_OVF_IRQHandler}, /* irq11 */
{0x82, (interrupt_handler_t)TIM1_CH1_CCP_IRQHandler}, /* irq12 */
{0x82, NonHandledInterrupt}, /* irq13 */
{0x82, NonHandledInterrupt}, /* irq14 */
{0x82, NonHandledInterrupt}, /* irq15 */
{0x82, NonHandledInterrupt}, /* irq16 */
{0x82, NonHandledInterrupt}, /* irq17 */
{0x82, NonHandledInterrupt}, /* irq18 */
{0x82, NonHandledInterrupt}, /* irq19 */
{0x82, NonHandledInterrupt}, /* irq20 */
{0x82, NonHandledInterrupt}, /* irq21 */
{0x82, NonHandledInterrupt}, /* irq22 */
{0x82, NonHandledInterrupt}, /* irq23 */
{0x82, NonHandledInterrupt}, /* irq24 */
{0x82, NonHandledInterrupt}, /* irq25 */
{0x82, NonHandledInterrupt}, /* irq26 */
{0x82, NonHandledInterrupt}, /* irq27 */
{0x82, NonHandledInterrupt}, /* irq28 */
{0x82, NonHandledInterrupt}, /* irq29 */
};
stm8s_it.h (только верхняя часть):
#ifndef __STM8S_IT_H
#define __STM8S_IT_H
@far @interrupt void TIM1_CH1_OVF_IRQHandler(void);
@far @interrupt void TIM1_CH1_CCP_IRQHandler(void);
/* Includes ------------------------------------------------------------------*/
#include "stm8s.h"