-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathWProgram.c
83 lines (67 loc) · 2.1 KB
/
WProgram.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright (c) 2011 Per Eklund, D.E. Goodman-Wilson
// Arduino stand-in
#include <stdint.h>
#include "LPC17xx.h"
#include "lpc17xx_rit.h"
#define PCRIT 16
//the above is from p. 63, LPC167x User's Manual
#define TIME_INTERVAL 120000
volatile uint32_t _count=0;
volatile uint32_t _millisec=0;
/************************** PRIVATE FUNCTION *************************/
/*----------------- INTERRUPT SERVICE ROUTINES --------------------------*/
/*********************************************************************//**
* @brief RIT interrupt handler sub-routine
* @param[in] None
* @return None
**********************************************************************/
#if defined (__cplusplus)
extern "C" {
#endif
void RIT_IRQHandler(void)
{
//LPC_GPIO0->FIOPIN ^= (1 << 22);
++ _millisec;
LPC_RIT->RICTRL |= RIT_CTRL_INTEN; //clear interrupt flag by writing '1'
//RIT_GetIntStatus(LPC_RIT); //call this to clear interrupt flag
}
void InitTimers(void)
{
//bit of an update for CMSIS V2.0 DEGW 20 August 2011
RIT_CMP_VAL value;
value.CMPVAL = TIME_INTERVAL;
value.COUNTVAL = 0x00000000;
value.MASKVAL = 0x00000000;
RIT_Init(LPC_RIT);
/* Configure time_interval for RIT
* In this case: time_interval = 1 ms
* So, RIT will generate interrupt each 1s
*/
RIT_TimerConfig(LPC_RIT, &value);
RIT_TimerClearCmd(LPC_RIT, ENABLE); //enable automatic clearing of counter at compare match.
//RIT_TimerConfig(LPC_RIT, TIME_INTERVAL);
//RIT_Cmd(LPC_RIT, ENABLE); //shouldn't be needed; timer is enabled by default
NVIC_EnableIRQ(RIT_IRQn);
}
void init(void)
{
InitTimers();
}
uint32_t millis(void)
{
return _millisec;
}
void delay(uint32_t ms)
{
uint32_t currentTime;
currentTime = _millisec; // read current tick counter
// Now loop until required number of ticks passes.
while ((_millisec - currentTime) < ms);
}
long map(long x, long in_min, long in_max, long out_min, long out_max)
{
return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}
#if defined (__cplusplus)
}
#endif