-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaxbotixs.c
80 lines (72 loc) · 2.01 KB
/
maxbotixs.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
#include "maxbotixs.h"
volatile uint32_t Htick_count;
volatile uint32_t Ltick_count;
volatile uint8_t dataGood;
static float distance;
void init_maxbotix()
{
//The following configures the timer
//Enable TMR_TMR32B0
SCB_SYSAHBCLKCTRL |= 0x200;
TMR_TMR32B0TCR |= 0x1;
//Set the prescaler for 24Mhz
TMR_TMR32B0PR |= 0x2;
//clear the variables
Htick_count = 0;
Ltick_count = 0;
dataGood = 0;
distance = 0;
//Configure the pins and the interupt
//Set port.pin to an input
gpioSetDir(0, 6, gpioDirection_Input);
//Disable the internal pullup/down resistor
gpioSetPullup(&IOCON_PIO0_6, gpioPullupMode_Inactive);
//set up an interupt
gpioSetInterrupt(0,6,
gpioInterruptSense_Edge,
gpioInterruptEdge_Double,
gpioInterruptEvent_ActiveHigh);
gpioIntEnable(0,6);
}
//Returns the distance in inches
float measure_maxbotix_in(void)
{
if (dataGood)
{
if (Ltick_count <= Htick_count)
{
distance = ((((uint64_t)Ltick_count + 0xFFFFFFFF) - Htick_count)/timerSpeed)/uSperInch;
}
else
{
distance = ((Ltick_count - Htick_count)/timerSpeed)/uSperInch;
}
}
return distance;
}
//Returns the distance in cm
float measure_maxbotix_cm(void)
{
return (measure_maxbotix_in())*2.54; //2.54 * inch = cm
}
void PIOINT0_IRQHandler(void)
{
uint32_t regVal;
regVal = gpioIntStatus(0, 6);
if (regVal)
{
uint32_t status = gpioGetValue(0,6);
gpioIntClear(0, 6);
if (status)
{
Htick_count = TMR_TMR32B0TC;
dataGood = 0;
}
else
{
Ltick_count = TMR_TMR32B0TC;
dataGood = 1;
}
}
return;
}