I am getting compilation errors with my code. Maybe some could help here.
//###################################################################################
//Needs to Fix:
//Alarm for EN sounds 3x2 secs before oven shutdown- Fixed
//SW for Spisvakt PCB 5.0 and 4.0
//1) Date: 2021-07-29 SW version Spisvakt 7.0
//###################################################################################
#include <NHD_Character_LCD.h>
#include <SPI.h>
#include <Wire.h>
#include <stdint.h>
#include <stdlib.h>
#define STARTUP_DELAY 500
#define I2C_DELAY 100
#define SPI_DELAY 100
#define SLAVE_ADDRESS 0x28
//SPI LCD module
//sbit SPI_SS at RA5_bit; //chipselact
//sbit SPI_SCK at RC3_bit;
//sbit SPI_SDI at RC4_bit;
//sbit SPI_SDO at RC5_bit;
//Inputs:Relays
int Relay_0 = 11; // Relay_0
int Relay_1 = 12; // Relay_1
int Relay_2 = 13; // Relay_2
//Outputs:LEDs
const int led_RED = 1; // led Red for Start/Stop of timers
const int led_GREEN = 2; // led Green for 30 mins
const int led_YELLOW = 3; // led Yellow for 3 hrs
const int led_BLUE = 4; // led Blue for 5 hrs
//Outputs: Buzzer
const int buzzer = 5; //Buzzer
//Inputs: Tact Switches and external signal
const int Start_Stop_Button = 6;
const int Min30_Button = 7;
const int Hour2_Button = 8;
const int Hour5_Button = 9;
const int Oven_OFF = 10;
// Messages
char Message1[]= "30min-5hr:Timer OFF";
char Message2[]= "Timer OFF";
char Message3[]= "Timer ON: HH:MM";
char Message4[]= "Set Time: ";
char Message5[]= "Time Left: ";
char* digit[5] = {"0:00"};
unsigned int i; // Minutes counter
unsigned int j; // Seconds counter
unsigned char unit = 0; // unit of minutes counter
unsigned char ten = 0; // Tens of minutes counter
unsigned char cent = 0; // Cents of minutes counter
unsigned char clear; // Variable to keep track of Start_Stop_Button
unsigned char timerFlag; // Variable to know if timer is running
unsigned int time; // Amount of minutes to run
unsigned char LEDBlink; // Flag that states which timer was enabled
// 1 - 30 Min
// 2 - 2Hs
// 3 - 5Hs
// SPI Interface
uint8_t _SCL; // 13
uint8_t _SDI; // 12
uint8_t _CS; // 10
enum Interface{
SPI
};
Interface _interface;
void setup(){
//SPI pins SCK,SDI and CS
initLCD_SPI(13,12,10);
//initialize the digitals led pins as outputs
pinMode(led_RED, OUTPUT);
pinMode(led_GREEN, OUTPUT);
pinMode(led_YELLOW, OUTPUT);
pinMode(led_BLUE, OUTPUT);
pinMode(buzzer, OUTPUT);
pinMode(Relay_0, OUTPUT);
pinMode(Relay_1, OUTPUT);
pinMode(Relay_2, OUTPUT);
//initialize the button pins as inputs
pinMode(Start_Stop_Button, INPUT);
pinMode(Min30_Button, INPUT);
pinMode(Hour2_Button, INPUT);
pinMode(Hour5_Button, INPUT);
//initialize the signal pin to SHUTDOWN OVEN as inputs
pinMode(Oven_OFF, INPUT);
}
void initLCD_SPI(uint8_t SCL, uint8_t SDI, uint8_t CS)
{
_interface = SPI;
// Store pin assignments globally
_SCL = SCL;
_SDI = SDI;
_CS = CS;
// Set IO modes
pinMode(CS, OUTPUT);
pinMode(SCL, OUTPUT);
pinMode(SDI, OUTPUT);
// Set pin states
digitalWrite(CS, HIGH);
digitalWrite(SCL, HIGH);
// Wait for display to power ON
delay(STARTUP_DELAY);
clearScreen();
}
/**
- @brief Set chip/slave select HIGH and wait for 1ms.
- @return none
*/
void setCS()
{
digitalWrite(_CS, HIGH);
delay(1);
}
/**
- @brief Clear chip/slave select and wait for 1ms.
- @return none
*/
void clearCS()
{
digitalWrite(_CS, LOW);
delay(1);
}
/**
- @brief Set the SDA/SDI pin high on the I2C/SPI bus.
- @return none
*/
void setSDI()
{
digitalWrite(_SDI, HIGH);
delayMicroseconds(SPI_DELAY);
}
/**
- @brief Clear the SDA/SDI pin on the I2C/SPI bus.
- @return none
*/
void clearSDI()
{
digitalWrite(_SDI, LOW);
delayMicroseconds(SPI_DELAY);
}
/**
- @brief Set the SCL/SCK pin on the I2C/SPI bus.
- @return none
*/
void setSCL()
{
digitalWrite(_SCL, HIGH);
if(_interface == SPI)
{
delayMicroseconds(SPI_DELAY);
}
}
/**
- @brief Clear the SCL/SCK pin on the I2C/SPI bus.
- @return none
*/
void clearSCL()
{
digitalWrite(_SCL, LOW);
if(_interface == SPI)
{
delayMicroseconds(SPI_DELAY);
}
}
/**
- @brief Write 1 byte of data to the display.
- @param data Byte of data to be written.
- @return none
*/
void write(uint8_t data)
{
switch(_interface)
{
case SPI:
clearCS();
putData_SPI(data);
setCS();
break;
}
delayMicroseconds(150);
}
/**
- @brief Write an array of characters to the display.
- @param data Pointer to the array of characters.
- @return none
/
void writeString(unsigned char data)
{
// Iterate through data until null terminator is found.
while(*data != '\0')
{
write(*data);
data++; // Increment pointer.
}
}
/**
-
@brief Put each bit of data on the SPI data bus.
-
This function sends MSB (D7) first and LSB (D0) last.
-
@param data Byte of data to be put on the SPI data bus.
-
@return none
*/
void putData_SPI(uint8_t data)
{
// Write data byte MSB first -> LSB last
for(int i = 7; i >= 0; i--)
{
clearSCL();digitalWrite(_SDI, (data >> i) & 0x01);
setSCL();
}
}
/**
- @brief Send the prefix data byte (0xFE).
- @return none
*/
void prefix()
{
write(0xFE);
}
/**
- @brief Turn the display ON.
- Display is turned ON by default.
- @return none
*/
void displayON()
{
prefix();
write(0x41);
}
/**
- @brief Turn the display OFF.
- Display is turned ON by default.
- @return none
*/
void displayOFF()
{
prefix();
write(0x42);
}
/**
- @brief Set the display cursor position via DDRAM address.
- @param position Desired DDRAM address.
- @return none
*/
void setCursor(uint8_t position)
{
prefix();
write(0x45);
write(position);
}
/**
- @brief Move the cursor to line 1, column 1.
- @return none
*/
void home()
{
prefix();
write(0x46);
}
/**
- @brief Clear the display screen.
- @return none
*/
void clearScreen()
{
prefix();
write(0x51);
delay(2);
}
/**
- @brief Set the display's contrast.
- 0x00 <= contrast <= 0x32
- Default: 0x28
- @param contrast Desired contrast setting.
- @return none
*/
void setContrast(uint8_t contrast)
{
prefix();
write(0x52);
write(contrast);
}
/**
- @brief Set the display's brightness.
- 0x01 <= brightness <= 0x08
- brightness = 0x01 | Backlight OFF
- brightness = 0x08 | Backlight ON (100%)
- @param brightness Desired brightness setting.
- @return none
*/
void setBrightness(uint8_t brightness)
{
prefix();
write(0x53);
write(brightness);
}
/**
- @brief Turn the underline cursor ON.
- @return none
*/
void underlineCursorON()
{
prefix();
write(0x47);
}
// 100ms Delay
void Delay_100(void)
{
Delay_ms(100);
}
// Debounce function
void Debounce(void)
{
Delay_ms(100);
}
// convert minutes to ASCII hours an d minutes
void Display_Hours(void)
{
unsigned int dminutes;
dminutes = (unit + (ten * 10) + (100 * cent)); // put minutes into one varible
digit[0] = (dminutes / 60) + 48; // hours as ascii
dminutes %= 60;
digit[1] = ':';
digit[2] = (dminutes / 10) + 48;
digit[3] = (dminutes % 10) + 48;
digit[4] = '\0'; // null terminator
Lcd_Out(2,11,digit);
}
//Display digits functions
void Display_Digits(void)
{
digit[2] = unit + 48;
digit[1] = ten + 48;
digit[0] = cent + 48;
Lcd_Out(2,11,digit);
}
// Buzzer sound
void Play_Sound()
{
//Play_Sound();
Buzzer=1;
Delay_ms(500);
Buzzer=0;
}
void Sound()
{
//Play Sound2();
Sound_Play(500,5000); //1kHz@1 sec
}
void stop_timer(void)
{
// Turn off relays
Relay_1 = 0;
Relay_2 = 0;
Relay_3 = 0;
// Turn off LEDs
led_BLUE = 0;
led_RED = 0;
led_GREEN = 0;
led_YELLOW = 0;
// Reset timer counters
unit = 0;
ten = 0;
cent = 0;
// Stops TMR1
T1CON &= 0xFE;
clear = 1;
Play_Sound(); //plays sound_1
// Timer is not counting anymore
timerFlag = 0;
Lcd_Out(1,1,Message2);
}
//Timer function
void start_timer(unsigned int MinVal)
{
unsigned int dminutes;
unsigned char blink = 0;
unsigned char flag = 0;
unsigned char Clear = 0;
Play_Sound(); //plays sound_2
// Activates relays
Relay_1 = 1;
Relay_2 = 1;
Relay_3 = 1;
// Activates Red LED and timerFlag to indicate that Timer is running
led_RED = 1;
timerFlag = 1;
Lcd_Cmd(_LCD_CLEAR); // Clear LCD
Lcd_Out(1,1,Message3); // "Timer ON: HH:MM"
Lcd_Out(2,1,Message5); // "Time Left: "
for (i=0; i<MinVal; i++)
{
// Put minutes into one variable
dminutes = MinVal-i;
if(dminutes == 5) // 5minute before end plays sound
Play_Sound();
// Hours as ASCII
Lcd_Chr(2, 12, (dminutes / 60) + 48);
// Colon character
Lcd_Chr(2, 13, ':');
// Minutes as ASCII
dminutes %= 60;
Lcd_Chr(2, 14, (dminutes / 10) + 48);
Lcd_Chr(2, 15, (dminutes % 10) + 48);
// Start counter
j = 1;
do
{
// Increment counter
j++;
// Increment blinking counter
blink++;
// If a second has passed
if(blink == 10)
{
// If colon is on, then turn it off and viceversa
if(flag == 1)
{
flag = 0;
Lcd_Out(2,13,":");
}
else
{
flag = 1;
Lcd_Out(2,13," ");
}
if(LEDBlink == 1)
led_GREEN = ~led_GREEN;
else if(LEDBlink == 2)
led_YELLOW = ~led_YELLOW;
else if(LEDBlink == 3)
led_BLUE = ~led_BLUE;
// Reset counter
blink = 0;
}
// Check if temperature is too high. If not, check Start_Stop_Button
if(Oven_OFF == 1)
{
Clear = 1;
Sound_Play(500,2000);
Delay_ms(100);
Sound_Play(500,2000);
Delay_ms(100);
Sound_Play(500,2000);
}
else
{
if(Start_Stop_Button == 0)
{
Debounce();
if(Start_Stop_Button == 0)
{
Clear = 1;
}
} // end if !Start_Stop_Button
else
{
Delay_ms(100);
}
}
} while(((j<=600) && (Clear == 0)));
// If the oven must be shutdown
if (Clear == 1)
{
Delay_ms(500);
Lcd_Out(1,1,Message2); // Show message: Timer OFF
stop_timer();
break;
} // end if
} // end for
// Put minutes into one variable
dminutes = MinVal-i;
// Hours as ASCII
Lcd_Chr(2, 12, (dminutes / 60) + 48);
// Colon as ASCII
Lcd_Chr(2, 13, ':');
// Minutes as ASCII
dminutes %= 60;
Lcd_Chr(2, 14, (dminutes / 10) + 48);
Lcd_Chr(2, 15, (dminutes % 10) + 48);
stop_timer();
} // end function
void Start_Stop(void)
{
if(timerFlag == 0)
{
time = cent100 + ten10 + unit ; //unit
if(time > 0)
{
start_timer(time);
}
}
else
{
stop_timer();
}
}
//Interupt
void interrupt(void)
{
if (INTCON.INTF == 1) // Check if INTF flag is set
{
//Clear = 1;
INTCON.INTF = 0; // Clear interrupt flag before exiting ISR
}
}
int main(void)
{
Relay_1 = 0; // RA0
Relay_2 = 0; // RA1
Relay_3 = 0; // RA2
led_BLUE = 0; // RB0
led_RED = 0; // RC5
led_GREEN = 0; // RA4
led_YELLOW = 0; // RA5
timerFlag = 0; // Initialize variable
Sound_Init(&PORTB,1); // Initialize Buzzer pin #1
Lcd_Init(); // Initialize LCD
// Show Init message
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
Lcd_Out(1,1, "Hello Again");
Lcd_Out(2,1, "Hello Once Again");
i=0;
while(i<4){
Delay_100();
i ++;
}
while(1)
{
// Restart Variable
clear = 0;
// 300ms delay
Delay_100();
Delay_100();
Delay_100();
// Show initial message
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Out(1,1, "Hello Again");
Lcd_Out(2,1, "Hello Once Again");
i=0;
while(i<6){
Delay_100();
i ++;
}
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Out(1,1,Message1); // "30min-5hr:Timer OFF"
Lcd_Out(2,1,Message4); // "Set Time: "
cent = 0;
ten = 0; // Set time to zero
unit = 0;
Display_Hours(); // Display_Digits
do
{
if(Min30_Button == 0) // RC1 - 30 minute button is pressed
{
Debounce();
if(Min30_Button == 0)
{
led_GREEN = 1; // RA4
led_YELLOW = 0; // RA5
led_BLUE = 0; // RB0
cent = 0;
ten = 3;
unit = 0; // 3 minutes time for test purpose
LEDBlink = 1;
Display_Hours();
}
} // end if !Min30_Button
if(Hour2_Button == 0) // RC2 - 2 hours(120 min) button is pressed
{
Debounce();
if(Hour2_Button == 0)
{
led_GREEN = 0; // RA4
led_YELLOW = 1; // RA5
led_BLUE = 0; // RB0
cent = 1;
ten = 2;
unit = 0;
LEDBlink = 2;
Display_Hours();
}
} // end if !Hour2_Button
if(Hour5_Button == 0) // RA3 - 5 hour(300 min) button is pressed
{
Debounce();
if(Hour5_Button == 0)
{
led_GREEN = 0; // RA4
led_YELLOW = 0; // RA5
led_BLUE = 1; // RB0
cent = 3;
ten = 0;
unit = 0;
LEDBlink = 3;
Display_Hours();
}
} // end if !Hour5_Button
if(Start_Stop_Button == 0) // RC0 - Start_Stop button is pressed
{
Debounce();
if(Start_Stop_Button == 0)
{
Start_Stop();
}
} // end if !Start_Stop_Button
} while(clear == 0); // End of do...while
}
}
//################################## //Use code tags to format code for the forum