-
Notifications
You must be signed in to change notification settings - Fork 0
/
node3.c
318 lines (263 loc) · 13.9 KB
/
node3.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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
#define _XTAL_FREQ 4000000
#include <xc.h>
// CONFIG1H
#pragma config OSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
#pragma config IESO = OFF // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)
// CONFIG2L
#pragma config PWRT = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = SBORDIS // Brown-out Reset Enable bits (Brown-out Reset enabled in hardware only (SBOREN is disabled))
#pragma config BORV = 3 // Brown Out Reset Voltage bits (Minimum setting)
// CONFIG2H
#pragma config WDT = OFF // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit))
#pragma config WDTPS = 32768 // Watchdog Timer Postscale Select bits (1:32768)
// CONFIG3H
#pragma config CCP2MX = PORTC // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
#pragma config PBADEN = ON // PORTB A/D Enable bit (PORTB<4:0> pins are configured as analog input channels on Reset)
#pragma config LPT1OSC = OFF // Low-Power Timer1 Oscillator Enable bit (Timer1 configured for higher power operation)
#pragma config MCLRE = ON // MCLR Pin Enable bit (MCLR pin enabled; RE3 input pin disabled)
// CONFIG4L
#pragma config STVREN = ON // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
#pragma config LVP = OFF // Single-Supply ICSP Enable bit (Single-Supply ICSP disabled)
#pragma config XINST = OFF // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))
// CONFIG5L
#pragma config CP0 = OFF // Code Protection bit (Block 0 (000800-003FFFh) not code-protected)
#pragma config CP1 = OFF // Code Protection bit (Block 1 (004000-007FFFh) not code-protected)
#pragma config CP2 = OFF // Code Protection bit (Block 2 (008000-00BFFFh) not code-protected)
#pragma config CP3 = OFF // Code Protection bit (Block 3 (00C000-00FFFFh) not code-protected)
// CONFIG5H
#pragma config CPB = OFF // Boot Block Code Protection bit (Boot block (000000-0007FFh) not code-protected)
#pragma config CPD = OFF // Data EEPROM Code Protection bit (Data EEPROM not code-protected)
// CONFIG6L
#pragma config WRT0 = OFF // Write Protection bit (Block 0 (000800-003FFFh) not write-protected)
#pragma config WRT1 = OFF // Write Protection bit (Block 1 (004000-007FFFh) not write-protected)
#pragma config WRT2 = OFF // Write Protection bit (Block 2 (008000-00BFFFh) not write-protected)
#pragma config WRT3 = OFF // Write Protection bit (Block 3 (00C000-00FFFFh) not write-protected)
// CONFIG6H
#pragma config WRTC = OFF // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) not write-protected)
#pragma config WRTB = OFF // Boot Block Write Protection bit (Boot Block (000000-0007FFh) not write-protected)
#pragma config WRTD = OFF // Data EEPROM Write Protection bit (Data EEPROM not write-protected)
// CONFIG7L
#pragma config EBTR0 = OFF // Table Read Protection bit (Block 0 (000800-003FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF // Table Read Protection bit (Block 1 (004000-007FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF // Table Read Protection bit (Block 2 (008000-00BFFFh) not protected from table reads executed in other blocks)
#pragma config EBTR3 = OFF // Table Read Protection bit (Block 3 (00C000-00FFFFh) not protected from table reads executed in other blocks)
// CONFIG7H
#pragma config EBTRB = OFF // Boot Block Table Read Protection bit (Boot Block (000000-0007FFh) not protected from table reads executed in other blocks)
#include <stdlib.h> //standard library
#include <stdio.h>
#include <spi.h> //serial peripheral interface functions
#include <delays.h> //time delay functions
#include <usart.h> //USART functions
#include <string.h> //string functions
#include <adc.h>
#include <timers.h>
#include "MRF24J40.h" //driver function definitions for MRF24J40 RF transceiver
#include <p18c452.h>
void Init_IO(void) {
PORTA = 0x04; //PORTA initially all zeros except RA2 (TC77 chip select)
TRISA = 0xF8; //RA0 and RA1 outputs (LEDs), RA2 Output (TC77 CS), rest inputs
TRISB = 0x00; //PORTB all inputs (RB0 is interrupt, RB4 and RB5 are push buttons)
// TRISB = 0xFF; //PORTB all inputs (RB0 is interrupt, RB4 and RB5 are push buttons) INTCON2bits.RBPU = 0; //enable pull up resistors on PORTB
ADCON0 = 0x1C; //turn off analog input
PORTCbits.RC0 = 1; //Chip select (/CS) initially set high (MRF24J40)
TRISCbits.TRISC0 = 0; //Output: /CS
PORTCbits.RC1 = 1; //WAKE initially set high (MRF24J40)
TRISCbits.TRISC0 = 0; //Output: WAKE
PORTCbits.RC2 = 1; //RESETn initially set high (MRF24J40)
TRISCbits.TRISC2 = 0; //output: RESETn
INTCONbits.INT0IF = 0; //clear the interrupt flag (INT0 = RB0)
INTCONbits.INT0IE = 1; //enable INT0
RCONbits.IPEN = 1; //enable interrupt priorities
INTCONbits.GIEH = 1; //global interrupt enable
OSCCONbits.IDLEN = 1; //enable idle mode (when Sleep() is executed)
OpenUSART(USART_TX_INT_OFF & USART_RX_INT_OFF & USART_ASYNCH_MODE &
USART_EIGHT_BIT & USART_CONT_RX & USART_BRGH_HIGH, 25); //setup USART @ 9600 Baud
OpenSPI(SPI_FOSC_4, MODE_00, SMPMID); //setup SPI bus (SPI mode 00, 1MHz SCLK) (MRF24J40)
RB5 = 0;
}
void USARTOut(char *data, char bytes) {
int i;
for (i = 0; i < bytes; i++) {
while (BusyUSART());
WriteUSART(data[i]);
}
}
typedef struct {
unsigned char GID;
unsigned char NID; //unique identifier
float Data1, Data2; //data byte
unsigned int crc;
}
PacketType;
PacketType TxPacket, RxPacket; //transmitted and received packets
char Text[128];
unsigned int ADCresult1, ADCresult2, Strength, CRCRight;
float Voltage1, Voltage2;
//float Temperature, Light;
float Temperature;
float Lux;
unsigned char FirstFlag = 1;
int Num_of_person = 0;
int cont = 0;
void ADC(void) {
OpenADC(ADC_FOSC_32 & ADC_RIGHT_JUST & ADC_8_TAD,
ADC_CH3 & ADC_INT_OFF & ADC_REF_VDD_VSS, 0);
Delay10TCYx(5);
OpenADC(ADC_FOSC_32 & ADC_RIGHT_JUST & ADC_8_TAD,
ADC_CH4 & ADC_INT_OFF & ADC_REF_VDD_VSS, 0);
Delay10TCYx(5);
SetChanADC(ADC_CH3);
ConvertADC();
while (BusyADC());
ADCresult1 = ReadADC();
SetChanADC(ADC_CH4);
ConvertADC();
while (BusyADC());
ADCresult2 = ReadADC();
CloseADC();
}
const unsigned char code_Table[256] = {
0x00, 0x07, 0x0E, 0x09, 0x1C, 0x1B, 0x12, 0x15, 0x38, 0x3F, 0x36, 0x31, 0x24, 0x23, 0x2A, 0x2D,
0x70, 0x77, 0x7E, 0x79, 0x6C, 0x6B, 0x62, 0x65, 0x48, 0x4F, 0x46, 0x41, 0x54, 0x53, 0x5A, 0x5D,
0xE0, 0xE7, 0xEE, 0xE9, 0xFC, 0xFB,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 0x1D, 0x14, 0x13,
0xAE, 0xA9, 0xA0, 0xA7, 0xB2, 0xB5, 0xBC, 0xBB, 0x96, 0x91, 0x98, 0x9F, 0x8A, 0x8D, 0x84, 0x83,
0xDE, 0xD9, 0xD0, 0xD7, 0xC2, 0xC5, 0xCC, 0xCB, 0xE6, 0xE1, 0xE8, 0xEF, 0xFA, 0xFD, 0xF4, 0xF3
};
/*****************function to caluculate CRC************************/
unsigned char CalculateCRC(unsigned char *message, unsigned char length) {
unsigned char i, crc = 0;
for (i = 0; i < length; i++) {
crc = code_Table[crc ^ message[i]];
}
return crc;
}
void cont_people(void) {
if (Lux > 50) {
// sprintf(Text, "Light, noo-one is using \r\n");
// USARTOut(Text, strlen(Text));
cont = 0;
}
if (Lux < 50) {
cont = cont + 1;
Delay10TCYx(40);
// sprintf(Text, "cont =: %d \r\n", cont);
// USARTOut(Text, strlen(Text));
if (cont > 5) {
// sprintf(Text, "cont > 5, someone is using \r\n");
// USARTOut(Text, strlen(Text));
// Delay10TCYx(40);
if (cont == 6) {
Num_of_person = Num_of_person + 1;
// sprintf(Text, "number of person from cont_person %d \r\n", Num_of_person);
// USARTOut(Text, strlen(Text));
}
}
}
// return Num_of_person;
}
void main(void) {
Init_IO();
MRF24J40Init(); //initialise IEEE 802.15.4 transceiver
SetChannel(CHANNEL_16); //set RF channel CHANNEL_11-CHANNEL_26 (EACH GROUP MUST HAVE UNIQUE CHANNEL)
OpenTimer0(TIMER_INT_ON & T0_16BIT & T0_SOURCE_INT & T0_PS_1_16); //setup timer 0 with prescaler x8
// OpenTimer0(TIMER_INT_ON & T0_16BIT & T0_SOURCE_INT & T0_PS_1_8); //setup timer 0 with prescaler x8 WriteTimer0(3035); //TDMA
FirstFlag = 1;
float a2 = 133.13;
float b2 = -40.07;
float c2 = 43.52;
while (1) {
RB5= 0;
ADC();
Voltage1 = (float) ADCresult1 * 3.3 / 1023;
// //Temperature=a*Voltage1+b;
Voltage2 = (float) ADCresult2 * 3.3 / 1023;
//
// float a1 = -38.4615;
// float b1 = 21.9231;
// float c1 = 25.7000;
// float offset = -0.3;
//
// Temperature = a1 * (float) Voltage1 * Voltage1 + b1 * Voltage1 + c1 + offset;
Lux = a2 * Voltage2 * Voltage2 + b2 * Voltage2 + c2;
//Light=a*Voltage2+b;
if(Lux < 2)
Lux = 1;
Delay10TCYx(20);
cont_people();
TxPacket.GID = 6;
TxPacket.NID = 3;
TxPacket.Data1 = Num_of_person;
TxPacket.Data2 = Lux;
TxPacket.crc = CalculateCRC(&TxPacket, sizeof(TxPacket) - sizeof(TxPacket.crc));
//--------------------send data wirelessly-----------------------
if (INTCONbits.TMR0IF) {
INTCONbits.TMR0IF = 0;
PORTA = 0x05;
PHYTransmit((char *) &TxPacket, sizeof(PacketType)); //Transmit RF data packet
// sprintf(Text, "Group ID=%u Node ID=%u Data1=%.2f Data2=%.2f RSSI =%u CRCRight=%u \r\n",
// TxPacket.GID, TxPacket.NID, TxPacket.Data1, TxPacket.Data2, Strength, CRCRight);
// USARTOut(Text, strlen(Text));
PORTA = 0x04;
RB5= 1;
}
//----------------receive data-------------------------
if (PHYReceive((char *) &RxPacket, &Strength) == sizeof(PacketType)) {
PORTA = 0x06;
if (0 == CalculateCRC(&RxPacket, sizeof(PacketType)))
CRCRight = 1;
else
CRCRight = 0;
if (RxPacket.NID == 1) {
if (FirstFlag == 1) {
switch (TxPacket.NID) {
case 2:
Delay10KTCYx(10);
break;
case 3:
Delay10KTCYx(20);
break;
case 4:
Delay10KTCYx(40);
// case 2:
// Delay10KTCYx(10);
// break;
// case 3:
// Delay10KTCYx(20);
// break;
// case 4:
// Delay10KTCYx(40);
// Delay10KTCYx(3);
// break;
}
FirstFlag = 0;
WriteTimer0(3035); //TDMA
}
}
if (CRCRight == 1) {
// if (((RxPacket.Data1 < -100) || (RxPacket.Data1 > 1000)) ||
// ((RxPacket.Data2 < -100) || (RxPacket.Data2 > 1000))) {
// RB6 = 1;
// } else
// sprintf(Text, "Group ID=%u Node ID=%u Data1=%.2f Data2=%.2f RSSI =%u CRCRight=%u \r\n",
// RxPacket.GID, RxPacket.NID, RxPacket.Data1, RxPacket.Data2, Strength, CRCRight);
// USARTOut(Text, strlen(Text));
PORTA = 0x04;
} else {
// sprintf(Text, "CRC equal to zero, Data rejected \r\n");
// USARTOut(Text, strlen(Text));
}
}
}
}