/
main.c
194 lines (161 loc) · 4.54 KB
/
main.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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#include <xc.h>
#include <stdio.h>
#include <stdlib.h>
#include <delays.h>
#include <pic18f45k20.h>
#include "buzzer.h"
#include "sonar.h"
#include "uart.h"
#pragma config FOSC=INTIO7, FCMEN=OFF, IESO=OFF, PWRT=OFF, BOREN=OFF
#pragma config BORV=30, WDTEN=ON, WDTPS=15, MCLRE=ON, HFOFST=ON
#pragma config LPT1OSC=OFF, PBADEN=OFF, CCP2MX=PORTBE, STVREN=OFF
#pragma config LVP=OFF, XINST=OFF, CP0=OFF, CP1=OFF, CP2=OFF
#pragma config CP3=OFF, CPB=OFF, CPD=OFF, WRT0=OFF, WRT1=OFF
#pragma config WRT2=OFF, WRT3=OFF, WRTB=OFF, WRTC=OFF, WRTD=OFF
#pragma config EBTRB=OFF
//#include "bit_settings.h"
#include "my_i2c.h"
#include "font.h"
#include "ssd1306.h"
#include "bargraph.h"
char error[] = "ERROR commande \n\r";
char Retour[] = "\n\r";
char EnterMenu[]= "\n\r*Menu options*\n\r Entrez ON/OFF \n\r Pour activer/desactiver le sonar\n\r";
char quit[] = "Exit Menu \n\r";
char ScrollBuffer[10];
int index = 0;
unsigned int distance = 0;
volatile int j=0;
void init();
void interrupt high_isr (void)
{
if ((PIR1bits.TMR1IF==1)&&(PIE1bits.TMR1IE == 1))
{
if (distance<25)
{
TRISBbits.RB3=0;
TMR1H = 172; // preset for timer1 MSB register
TMR1L = 176; // preset for timer1 LSB register
//0 sec
}
else if ((distance>=25)&&(distance<40))
{
TRISBbits.RB3=~TRISBbits.RB3;
TMR1H = 137; // preset for timer1 MSB register
TMR1L = 250; // preset for timer1 LSB register
// 0.1s
}
else if ((distance>=40)&&(distance<70))
{
TRISBbits.RB3=~TRISBbits.RB3;
TMR1H = 58; // preset for timer1 MSB register
TMR1L = 36; // preset for timer1 LSB register
// 0.1s
}
else
{
j=0;
TRISBbits.RB3=0;
do{
j++;
} while (j < 16000);
TRISBbits.RB3=1;
TMR1H = 0; // preset for timer1 MSB register
TMR1L = 222; // preset for timer1 LSB register
}
T1CONbits.TMR1ON = 1;
PIR1bits.TMR1IF = 0; // Timer1 interrupt Flag cleared
}
if(RCIF) {
if(RCREG == '²') {
UART_Write_Text(EnterMenu);
Mode = 0;
index = 0;
}
else if(RCREG == 27 && Mode == 0) {
Mode = 1;
UART_Write_Text(quit);
UART_Write_Text(Retour);
}
else if(RCREG == 0x0D) {
ScrollBuffer[index] = '\0';
index = 0;
BufferReady = 1;
UART_Write_Text(Retour);
}
else {
BufferReady = 0;
ScrollBuffer[index] = RCREG;
TXREG = RCREG;
index++;
if(10 < index) {
UART_Write_Text(error);
index = 0;
}
}
}
}
void main()
{
char* temp;
char number[4];
int count = 0;
init();
while (1) {
//distance = getFilteredData();
if(SonarReady == 0) {
distance = 1000;
}
else {
distance = getData();
}
if (BufferReady == 1) {
DataCommand(ScrollBuffer);
}
setBargraph(distance);
if (distance > 100) {
TRISBbits.RB3 = 1; // Disable RB3 output
PIE1bits.TMR1IE = 0; // Timer1 interrupt disabled
}
else {
PIE1bits.TMR1IE = 1; // Timer1 interrupt enabled
}
++count;
if (2048 < count) {
if (distance == 1000) {
temp = "888";
}
else {
temp = getChar(distance);
}
for (int i = 0; i < 4; ++i) {
number[i] = temp[i];
}
Oled_SetFont(Segment_25x40, 25, 40, 46, 58);
Oled_Text(number, 30, 3);
DataTransfert(number);
count = 0;
}
CLRWDT();
}
}
void init()
{
OSCCON = 0b01100010;
UART_Init();
initBargraph();
initSonar();
initBuzzer();
song();
CLRWDT();
//INIT OLED
I2C_Close(); // Close the I2C Bus
I2C_Init(1); // I2C 400kHz, 20MHz-CRYSTAL
Oled_Init();
Oled_SetFont(Terminal12x16, 12, 16, 32,127);
Oled_FillScreen(0x00, 0, 0);
Oled_ConstText("SONAR", 35, 0);
Oled_Text("!", 110, 0);
Oled_ConstText("L=", 2, 5);
Oled_ConstText("cm", 105, 5);
}