-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdelay.c
40 lines (35 loc) · 845 Bytes
/
delay.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
/*
* delay.c
*
* Delay functions for embedded programming.
*/
#include <inttypes.h>
#include "delay.h"
void delay_1ms(uint32_t n){
for (int i = 0; i < n; i++) {
// Load number of cycles for 1 ms
// 16 MHz clock
*(STK_LOAD) = 16000;
// set 1 to enable and clock source
*(STK_CTRL) = (1 << EN) | (1 << CLKSOURCE);
// Wait for countflag to be set
uint32_t countFlag = 0;
while (countFlag == 0) {
countFlag = *(STK_CTRL) & (1 << COUNTFLAG);
}
}
}
void delay_1us(uint32_t n){
for (int i = 0; i < n; i++) {
// Load number of cycles for 1 us
// 16 MHz clock
*(STK_LOAD) = 16;
// set 1 to enable and clock source
*(STK_CTRL) = (1 << EN) | (1 << CLKSOURCE);
// Wait for countflag to be set
uint32_t countFlag = 0;
while (countFlag == 0) {
countFlag = *(STK_CTRL) & (1 << COUNTFLAG);
}
}
}