forked from stancecoke/BMSBattery_S_controllers_firmware
-
Notifications
You must be signed in to change notification settings - Fork 1
/
timers.c
61 lines (47 loc) · 1.33 KB
/
timers.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
/*
* BMSBattery S series motor controllers firmware
*
* Copyright (C) Casainho, 2017.
*
* Released under the GPL License, Version 3
*/
#include <stdint.h>
#include <stdio.h>
#include "stm8s_gpio.h"
#include "stm8s_tim1.h"
#include "stm8s.h"
#include "stm8s_tim2.h"
#include "main.h"
#include "motor.h"
#include "gpio.h"
void timer2_init (void)
{
// TIM2 Peripheral Configuration
TIM2_DeInit();
TIM2_TimeBaseInit(TIM2_PRESCALER_8, 25536); //timetic with 50HZ
TIM2_ITConfig(TIM2_IT_UPDATE,ENABLE);
TIM2_Cmd(ENABLE); // TIM2 counter enable
}
//interrupt routine for slow control loop timing
void TIM2_UPD_OVF_TRG_BRK_IRQHandler(void) __interrupt(TIM2_UPD_OVF_TRG_BRK_IRQHANDLER)
{
ui8_slowloop_flag=1;
//printf("SlowTimetic\n");
// clear the interrupt pending bit for TIM2
TIM2_ClearITPendingBit(TIM2_IT_UPDATE);
}
#define CLOCKFREQ (16000000L)
/* Instructions per millisecond. */
#define INSNS_PER_MS (CLOCKFREQ / 4000L)
/* Delay loop is about 10 cycles per iteration. */
#define LOOPS_PER_MS (INSNS_PER_MS/20L)
void delay_halfms(uint16_t ms) {
uint16_t u;
while (ms--) {
/* Inner loop takes about 10 cycles per iteration + 4 cycles setup. */
for (u = 0; u < LOOPS_PER_MS; u++) {
/* Prevent this loop from being optimized away. */
__asm nop __endasm;
}
}
}