Skip to content

Commit

Permalink
Update main.c
Browse files Browse the repository at this point in the history
  • Loading branch information
GuangLun2000 committed Dec 18, 2022
1 parent e699786 commit 9d6d19f
Showing 1 changed file with 72 additions and 74 deletions.
146 changes: 72 additions & 74 deletions Program/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@
#define G_led RC0
#define R_led RC1

/**********Global variables**********/
int data1 = 0;
int data2 = 0;
unsigned char newData = 0;
unsigned char AddData = 0;
char ch = 0;
int col = 16;
int flag = 1;
int i = 0;


/***********USER FUNCTIONS***********/

//prototypes
Expand All @@ -50,32 +61,19 @@ void loop(void);
char receive(void);
void send_str(char *str);

// 3 version of LCD display
void LCDBegin(void);
void LCDTitle(void);
void LCDEnd(void);

void readLDR_value(void);
// I2C function
void readLDR_data(void);
void Write_data(void);
void Send_data(void);
void receive_data(void);
void Light();


/**********Global variables**********/
unsigned char gMode;
char gSpeed;
unsigned char new_value = 0;
unsigned char ad_value = 0;
int value1 = 0;
int value2 = 0;
int thr = 1;
unsigned char gOutString[16];
int i = 0;
char ch = 0;
int col = 16;
int temp = 1;


/******************************************************************************************/

void main()
Expand Down Expand Up @@ -103,7 +101,7 @@ void main()
{
for (;;) // endless loop
{
readLDR_value();
readLDR_data();
loop();
LCDTitle();
send_str("Working_SW2");
Expand All @@ -127,16 +125,15 @@ void main()

}

void setup(void) // setup stuff
void setup(void) // Setup stuff
{
PORTD = 0b11111111;
TRISD = 0b00000000;
// TRISC = 0xC0; //RC6 and RC7 must be set to inputs for USART.
TXSTA = 0x24; //Set TXEN bit to enable transmit.
//Set BRGH bit for Baud Rate table selection.
RCSTA = 0x90; //Set CREN bit for continuous read.
TXSTA = 0x24; //Set TXEN bit to enable transmit.
//Set BRGH bit for Baud Rate table selection.
RCSTA = 0x90; //Set CREN bit for continuous read.
//Set SPEN bit to enable serial port.
SPBRG = 0x19; //Set Baud Rate to 9600
SPBRG = 0x19; //Set Baud Rate as 9600

TRISC = 0xd8;
TRISD = 0x00;
Expand All @@ -145,73 +142,67 @@ void setup(void) // setup stuff
INTCON = 0xc0;
PIE1 = 0x21;
PIR1 = 0x00;
Lcd8_Init(); // Required initialisation of LCD to 8-bit mode
TRISB = 0x07; // Set PORTB bit 0 as input
Lcd8_Init(); // Required initialisation of LCD to 8-bit mode
TRISB = 0x07; // Set PORTB bit 0 as input

TRISC = 0xd8;
TRISD = 0x00;
PORTD = 0xff;

TXSTA = 0x24;
RCSTA = 0x90;
SPBRG = 0x19; // set baud rate
SPBRG = 0x19; // set Baud Rate

T1CON = 0x21;
INTCON = 0xc0;
PIE1 = 0x21;
PIR1 = 0x00;
//Set the ACD registers

TRISA = 0b00000101; // Set PORTA bits 0 and 2 are output
TRISA = 0b00000101; // Set PORTA bits 0 and 2 are output
//TRISC = 0b00000000; // Set PORTC bit 1 and 0 as output
PORTC = 0b00000010;
ADCON0 = 0b01010001; // Set FOSC/8,RA2 as analog input and A/D converter module is powered up
ADCON1 = 0b00000010; // Set Left justified
ADCON0 = 0b01010001; // Set FOSC/8,RA2 as analog input and A/D converter module is powered up
ADCON1 = 0b00000010; // Set Left justified
OPTION_REG &= 0b01111111;



TRISC = 0b1101100; //RC6 and RC7 must be set to inputs for USART.
TXSTA = 0x24; //Set TXEN bit to enable transmit.
//Set BRGH bit for Baud Rate table selection.
RCSTA = 0x90; //Set CREN bit for continuous read.
TRISC = 0b1101100; // RC6 and RC7 must be set to inputs for USART.
TXSTA = 0x24; // Set TXEN bit to enable transmit.
// Set BRGH bit for Baud Rate table selection.
RCSTA = 0x90; // Set CREN bit for continuous read.
//Set SPEN bit to enable serial port.
SPBRG = 0x19; //Set Baud Rate to 9600
i2c_init();
SPBRG = 0x19; // Set Baud Rate to 9600
i2c_init(); // Required initialisation of I2C



PORTD = 0b11111111;
TRISD = 0b00000000;
//TRISC = 0xC0; //RC6 and RC7 must be set to inputs for USART.
TXSTA = 0x24; //Set TXEN bit to enable transmit.
//Set BRGH bit for Baud Rate table selection.
RCSTA = 0x90; //Set CREN bit for continuous read.
//TRISC = 0xC0; //RC6 and RC7 must be set to inputs for USART.
TXSTA = 0x24; //Set TXEN bit to enable transmit.
//Set BRGH bit for Baud Rate table selection.
RCSTA = 0x90; //Set CREN bit for continuous read.
//Set SPEN bit to enable serial port.
SPBRG = 0x19; //Set Baud Rate to 9600
SPBRG = 0x19; //Set Baud Rate to 9600
}

void loop(void)
{

if (value1 > thr)
if (data1 > flag)
{
T1CON = 0x01;


}
else if (value1 <= thr)
else if (data1 <= flag)
{
T1CON = 0x31;


}
}

/******************************************************************************************/

void __interrupt() // Interrupt identifier
isr(void) // Here is interrupt function
void __interrupt() // Interrupt identifier
isr(void) // Here is interrupt function.
{
if (TMR1IF)
{
Expand All @@ -231,6 +222,7 @@ void __interrupt() // Interrupt identifier
}
}

/******************************************************************************************/

char receive(void)
{
Expand All @@ -255,8 +247,10 @@ void send_str(char *str)
}
}

/******************************************************************************************/

//Display title on LCD
// 3 LCD Display Version
// Beginning of the project, version 1.
void LCDBegin(void)
{
Lcd8_Set_Cursor(1,1); // select line 2 of LCD
Expand All @@ -266,7 +260,7 @@ void LCDBegin(void)
__delay_ms(1000);
}


// Once SW2 is closed, LCD version 2.
void LCDTitle(void)
{
if (col < 0)
Expand All @@ -282,6 +276,7 @@ void LCDTitle(void)
__delay_ms(300);
}

// Once SW4 is closed, LCD version 3.
void LCDEnd(void)
{
Lcd8_Set_Cursor(1,1); // select line 2 of LCD
Expand All @@ -291,28 +286,45 @@ void LCDEnd(void)
__delay_ms(1000);
}

void readLDR_value(void){
void Light(){

if(data1+data2*0.1<1.5){
R_led = HIGH;
G_led = LOW;
}else{
R_led = LOW;
G_led = HIGH;

}
}

/******************************************************************************************/

// ADC function
void readLDR_data(void){
if(1){
__delay_ms(150);
__delay_us(50);
GO_nDONE = 1;
while(GO_nDONE){
continue;
}
if(ADRESH!=new_value){
ad_value = ADRESH;
value1 = (ad_value*5/255);
value2 = (ad_value*10*5/255)%10;


if(ADRESH!=newData){
AddData = ADRESH;
data1 = (AddData*5/255);
data2 = (AddData*10*5/255)%10;
}

Light();
new_value = ad_value;
newData = AddData;
__delay_ms(100);
}
}

/******************************************************************************************/

// I2C function
void Write_data(void)
{
unsigned char address_hi = hi;
Expand Down Expand Up @@ -344,19 +356,5 @@ void Send_data(void)
}
}


void Light(){

if(value1+value2*0.1<1.5){
R_led = HIGH;
G_led = LOW;
}else{
R_led = LOW;
G_led = HIGH;

}
}


// Created by Hanlin CAI in 2022/12/15
// This program is created by Hanlin CAI in 2022/12/15
// EE302FZ Final Project.

0 comments on commit 9d6d19f

Please sign in to comment.