Skip to content
This repository was archived by the owner on Dec 3, 2025. It is now read-only.

Commit 9d6d19f

Browse files
committed
Update main.c
1 parent e699786 commit 9d6d19f

1 file changed

Lines changed: 72 additions & 74 deletions

File tree

Program/main.c

Lines changed: 72 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,17 @@
4242
#define G_led RC0
4343
#define R_led RC1
4444

45+
/**********Global variables**********/
46+
int data1 = 0;
47+
int data2 = 0;
48+
unsigned char newData = 0;
49+
unsigned char AddData = 0;
50+
char ch = 0;
51+
int col = 16;
52+
int flag = 1;
53+
int i = 0;
54+
55+
4556
/***********USER FUNCTIONS***********/
4657

4758
//prototypes
@@ -50,32 +61,19 @@ void loop(void);
5061
char receive(void);
5162
void send_str(char *str);
5263

64+
// 3 version of LCD display
5365
void LCDBegin(void);
5466
void LCDTitle(void);
5567
void LCDEnd(void);
5668

57-
void readLDR_value(void);
69+
// I2C function
70+
void readLDR_data(void);
5871
void Write_data(void);
5972
void Send_data(void);
6073
void receive_data(void);
6174
void Light();
6275

6376

64-
/**********Global variables**********/
65-
unsigned char gMode;
66-
char gSpeed;
67-
unsigned char new_value = 0;
68-
unsigned char ad_value = 0;
69-
int value1 = 0;
70-
int value2 = 0;
71-
int thr = 1;
72-
unsigned char gOutString[16];
73-
int i = 0;
74-
char ch = 0;
75-
int col = 16;
76-
int temp = 1;
77-
78-
7977
/******************************************************************************************/
8078

8179
void main()
@@ -103,7 +101,7 @@ void main()
103101
{
104102
for (;;) // endless loop
105103
{
106-
readLDR_value();
104+
readLDR_data();
107105
loop();
108106
LCDTitle();
109107
send_str("Working_SW2");
@@ -127,16 +125,15 @@ void main()
127125

128126
}
129127

130-
void setup(void) // setup stuff
128+
void setup(void) // Setup stuff
131129
{
132130
PORTD = 0b11111111;
133131
TRISD = 0b00000000;
134-
// TRISC = 0xC0; //RC6 and RC7 must be set to inputs for USART.
135-
TXSTA = 0x24; //Set TXEN bit to enable transmit.
136-
//Set BRGH bit for Baud Rate table selection.
137-
RCSTA = 0x90; //Set CREN bit for continuous read.
132+
TXSTA = 0x24; //Set TXEN bit to enable transmit.
133+
//Set BRGH bit for Baud Rate table selection.
134+
RCSTA = 0x90; //Set CREN bit for continuous read.
138135
//Set SPEN bit to enable serial port.
139-
SPBRG = 0x19; //Set Baud Rate to 9600
136+
SPBRG = 0x19; //Set Baud Rate as 9600
140137

141138
TRISC = 0xd8;
142139
TRISD = 0x00;
@@ -145,73 +142,67 @@ void setup(void) // setup stuff
145142
INTCON = 0xc0;
146143
PIE1 = 0x21;
147144
PIR1 = 0x00;
148-
Lcd8_Init(); // Required initialisation of LCD to 8-bit mode
149-
TRISB = 0x07; // Set PORTB bit 0 as input
145+
Lcd8_Init(); // Required initialisation of LCD to 8-bit mode
146+
TRISB = 0x07; // Set PORTB bit 0 as input
150147

151148
TRISC = 0xd8;
152149
TRISD = 0x00;
153150
PORTD = 0xff;
154151

155152
TXSTA = 0x24;
156153
RCSTA = 0x90;
157-
SPBRG = 0x19; // set baud rate
154+
SPBRG = 0x19; // set Baud Rate
158155

159156
T1CON = 0x21;
160157
INTCON = 0xc0;
161158
PIE1 = 0x21;
162159
PIR1 = 0x00;
163160
//Set the ACD registers
164161

165-
TRISA = 0b00000101; // Set PORTA bits 0 and 2 are output
162+
TRISA = 0b00000101; // Set PORTA bits 0 and 2 are output
166163
//TRISC = 0b00000000; // Set PORTC bit 1 and 0 as output
167164
PORTC = 0b00000010;
168-
ADCON0 = 0b01010001; // Set FOSC/8,RA2 as analog input and A/D converter module is powered up
169-
ADCON1 = 0b00000010; // Set Left justified
165+
ADCON0 = 0b01010001; // Set FOSC/8,RA2 as analog input and A/D converter module is powered up
166+
ADCON1 = 0b00000010; // Set Left justified
170167
OPTION_REG &= 0b01111111;
171168

172169

173170

174-
TRISC = 0b1101100; //RC6 and RC7 must be set to inputs for USART.
175-
TXSTA = 0x24; //Set TXEN bit to enable transmit.
176-
//Set BRGH bit for Baud Rate table selection.
177-
RCSTA = 0x90; //Set CREN bit for continuous read.
171+
TRISC = 0b1101100; // RC6 and RC7 must be set to inputs for USART.
172+
TXSTA = 0x24; // Set TXEN bit to enable transmit.
173+
// Set BRGH bit for Baud Rate table selection.
174+
RCSTA = 0x90; // Set CREN bit for continuous read.
178175
//Set SPEN bit to enable serial port.
179-
SPBRG = 0x19; //Set Baud Rate to 9600
180-
i2c_init();
176+
SPBRG = 0x19; // Set Baud Rate to 9600
177+
i2c_init(); // Required initialisation of I2C
181178

182179

183180

184181
PORTD = 0b11111111;
185182
TRISD = 0b00000000;
186-
//TRISC = 0xC0; //RC6 and RC7 must be set to inputs for USART.
187-
TXSTA = 0x24; //Set TXEN bit to enable transmit.
188-
//Set BRGH bit for Baud Rate table selection.
189-
RCSTA = 0x90; //Set CREN bit for continuous read.
183+
//TRISC = 0xC0; //RC6 and RC7 must be set to inputs for USART.
184+
TXSTA = 0x24; //Set TXEN bit to enable transmit.
185+
//Set BRGH bit for Baud Rate table selection.
186+
RCSTA = 0x90; //Set CREN bit for continuous read.
190187
//Set SPEN bit to enable serial port.
191-
SPBRG = 0x19; //Set Baud Rate to 9600
188+
SPBRG = 0x19; //Set Baud Rate to 9600
192189
}
193190

194191
void loop(void)
195192
{
196-
197-
if (value1 > thr)
193+
if (data1 > flag)
198194
{
199195
T1CON = 0x01;
200-
201-
202196
}
203-
else if (value1 <= thr)
197+
else if (data1 <= flag)
204198
{
205199
T1CON = 0x31;
206-
207-
208200
}
209201
}
210202

211-
/******************************************************************************************/
212203

213-
void __interrupt() // Interrupt identifier
214-
isr(void) // Here is interrupt function
204+
void __interrupt() // Interrupt identifier
205+
isr(void) // Here is interrupt function.
215206
{
216207
if (TMR1IF)
217208
{
@@ -231,6 +222,7 @@ void __interrupt() // Interrupt identifier
231222
}
232223
}
233224

225+
/******************************************************************************************/
234226

235227
char receive(void)
236228
{
@@ -255,8 +247,10 @@ void send_str(char *str)
255247
}
256248
}
257249

250+
/******************************************************************************************/
258251

259-
//Display title on LCD
252+
// 3 LCD Display Version
253+
// Beginning of the project, version 1.
260254
void LCDBegin(void)
261255
{
262256
Lcd8_Set_Cursor(1,1); // select line 2 of LCD
@@ -266,7 +260,7 @@ void LCDBegin(void)
266260
__delay_ms(1000);
267261
}
268262

269-
263+
// Once SW2 is closed, LCD version 2.
270264
void LCDTitle(void)
271265
{
272266
if (col < 0)
@@ -282,6 +276,7 @@ void LCDTitle(void)
282276
__delay_ms(300);
283277
}
284278

279+
// Once SW4 is closed, LCD version 3.
285280
void LCDEnd(void)
286281
{
287282
Lcd8_Set_Cursor(1,1); // select line 2 of LCD
@@ -291,28 +286,45 @@ void LCDEnd(void)
291286
__delay_ms(1000);
292287
}
293288

294-
void readLDR_value(void){
289+
void Light(){
290+
291+
if(data1+data2*0.1<1.5){
292+
R_led = HIGH;
293+
G_led = LOW;
294+
}else{
295+
R_led = LOW;
296+
G_led = HIGH;
297+
298+
}
299+
}
300+
301+
/******************************************************************************************/
302+
303+
// ADC function
304+
void readLDR_data(void){
295305
if(1){
296306
__delay_ms(150);
297307
__delay_us(50);
298308
GO_nDONE = 1;
299309
while(GO_nDONE){
300310
continue;
301311
}
302-
if(ADRESH!=new_value){
303-
ad_value = ADRESH;
304-
value1 = (ad_value*5/255);
305-
value2 = (ad_value*10*5/255)%10;
306-
312+
313+
if(ADRESH!=newData){
314+
AddData = ADRESH;
315+
data1 = (AddData*5/255);
316+
data2 = (AddData*10*5/255)%10;
307317
}
308318

309319
Light();
310-
new_value = ad_value;
320+
newData = AddData;
311321
__delay_ms(100);
312322
}
313323
}
314324

325+
/******************************************************************************************/
315326

327+
// I2C function
316328
void Write_data(void)
317329
{
318330
unsigned char address_hi = hi;
@@ -344,19 +356,5 @@ void Send_data(void)
344356
}
345357
}
346358

347-
348-
void Light(){
349-
350-
if(value1+value2*0.1<1.5){
351-
R_led = HIGH;
352-
G_led = LOW;
353-
}else{
354-
R_led = LOW;
355-
G_led = HIGH;
356-
357-
}
358-
}
359-
360-
361-
// Created by Hanlin CAI in 2022/12/15
359+
// This program is created by Hanlin CAI in 2022/12/15
362360
// EE302FZ Final Project.

0 commit comments

Comments
 (0)