1 | /******************************************************************************************
|
2 | * *
|
3 | * +---+---+---+---+---+---+---+---+ *
|
4 | * | 0 | 0 | 0 | 1 | 1 | 1 | 1 | 1 | *
|
5 | * +---+---+---+---+---+---+---+---+ *
|
6 | * | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 | *
|
7 | * *
|
8 | * *
|
9 | * *
|
10 | *******************************************************************************************/
|
11 |
|
12 |
|
13 | #include <LPC23xx.h>
|
14 | #include <stdio.h>
|
15 | #include <stdbool.h>
|
16 | #include "timing.h"
|
17 | #include "LCD.h"
|
18 |
|
19 |
|
20 | /************** Globale Variables **********/
|
21 |
|
22 | bool flag = false;
|
23 |
|
24 | /************** Timer1 Init ****************/
|
25 |
|
26 | void T1_Init(void)
|
27 | {
|
28 | PINSEL3 = 0x000000C0; // Set P1.19 for function CAP1.1 PIN.Nr= 47
|
29 | PINMODE3 = 0x000000C0; // enable pull down resistor mode 11
|
30 |
|
31 | T1TCR = 0x02; // Reset Timer 1
|
32 | T1CCR= 0x1B; // Count every edge
|
33 | T1TCR = 0x01; // Enable Timer 1
|
34 | }
|
35 |
|
36 |
|
37 | /************** Pin Config Init ****************/
|
38 | void PIN_Init(void)
|
39 | {
|
40 | PINSEL4 = 0x00000000; // Set P2.13 from Pinsel4 as GPIO
|
41 | FIO2DIR = 0x000020FF; // Set P2.13 as Output
|
42 | FIO2MASK = 0x00000000;
|
43 | }
|
44 |
|
45 | /********** P2.13 Boolean statment ***********/
|
46 | void PIN_State(bool state)
|
47 | {
|
48 | if(state == true)
|
49 | {
|
50 | FIO2SET = (1 << 13);
|
51 | }
|
52 | else
|
53 | {
|
54 | FIO2CLR = (1 << 13);
|
55 | }
|
56 | }
|
57 |
|
58 |
|
59 | /* Function that initializes LEDs */
|
60 | void LED_Init(void) {
|
61 | PINSEL10 = 0; /* Disable ETM interface, enable LEDs */
|
62 | FIO2DIR = 0x000020FF; /* P2.0..7 defined as Outputs */
|
63 | FIO2MASK = 0x00000000;
|
64 | }
|
65 |
|
66 | /* Function that turns on requested LED */
|
67 | void LED_On (unsigned int num)
|
68 | {
|
69 | FIO2SET = (1 << num);
|
70 | }
|
71 |
|
72 | /* Function that turns off requested LED */
|
73 | void LED_Off (unsigned int num)
|
74 | {
|
75 | FIO2CLR = (1 << num);
|
76 | }
|
77 |
|
78 |
|
79 | int main(void)
|
80 | {
|
81 | int stop = 0;
|
82 | int start = 0;
|
83 | double value=0;
|
84 | char charValue[10];
|
85 | int i = 0;
|
86 |
|
87 |
|
88 | LED_Init();
|
89 | T0_init(1); // Delay-function for ms and us
|
90 | T1_Init();
|
91 | lcd_init();
|
92 | PIN_Init();
|
93 | lcd_clear();
|
94 |
|
95 | while(1)
|
96 | {
|
97 | for(i = 0; i <= 7; i++)
|
98 | {
|
99 | LED_On(i);
|
100 | delay_ms(20);
|
101 | LED_Off(i);
|
102 | delay_ms(20);
|
103 | }
|
104 | i = 0;
|
105 |
|
106 | PIN_State(false);
|
107 | delay_us(2);
|
108 | PIN_State(true);
|
109 | delay_us(10);
|
110 | PIN_State(false);
|
111 |
|
112 |
|
113 | while (!(FIO1PIN & (1<<19)));
|
114 | start = T1CR1;
|
115 | while ( FIO1PIN & (1<<19) );
|
116 | stop = T1CR1;
|
117 |
|
118 | if(start>>stop)
|
119 | value=0xFFFFFFFF-start+stop; // FFFFFFFF = 4294967295 the biggest value of timercounter
|
120 | else
|
121 | value=stop-start;
|
122 |
|
123 | value=value*34/20000; // Calculate the value
|
124 |
|
125 |
|
126 |
|
127 |
|
128 |
|
129 | lcd_clear();
|
130 | set_cursor (0,0);
|
131 | sprintf(charValue,"%d",(int)value);
|
132 | lcd_print ((unsigned char const *)"Distance: ");
|
133 | lcd_print((unsigned char const *)charValue);
|
134 | lcd_print ((unsigned char const *)"cm ");
|
135 |
|
136 | delay_ms(1000);
|
137 |
|
138 | }
|
139 |
|
140 |
|
141 |
|
142 | }
|