Code:
#include <pic.h> // Standard PIC library
#include <stdio.h> // Required to enable 'sprintf' function
#include <string.h> // Required to enable 'string' functions
__CONFIG(INTIO & WDTDIS & PWRTEN & MCLRDIS & UNPROTECT \
& UNPROTECT & BORDIS & IESODIS & FCMDIS); // Standard configuration words for the PIC //
char TopMessage[17]; // Define an array of characters of length 17 named 'TopMessage'
char BotMessage[17]; // Define an array of characters of length 17 named 'BotMessage'
#define THERM_INPUT RA0 /////////////////////////////////////////////////////////////////////////
#define TILTSWITCH RA1 // //
#define UP RA2 // A series of 'define' statements for shorthand later //
#define DOWN RA3 // //
#define FORWARD RA4 ////////////////////////////////////////////////////////////////////////
#define E RC4 // Define the LCD control Pins
#define RS RC5 // Define the LCD control Pins
const int Twentyms = 1250; //
const int Fivems = 300; // Declare a number of constant integers to be called later
const int TwoHundredus = 10; //
const int Delay = 5000; //
int i, j, k, n; // Declare global variables
void LCDWrite(int LCDData, int RSValue) // Define a function called 'LCDWrite' that can be called later to
// write the input strings to the LCD screen
{ // Start of LCDWrite
PORTC = (LCDData >> 4) & 0x0F; // Get high 4 bits for output
RS = RSValue;
E = 1; E = 0; // Toggle the high 4 bits out
PORTC = LCDData & 0x0F; // Get low 4 bits for output
RS = RSValue;
E = 1; E = 0; // Toggle the low 4 bits out
if ((0 == (LCDData & 0xFC)) && (0 == RSValue))
n = Fivems; // Set delay interval
else
n = TwoHundredus;
for (k = 0; k < n; k++); // Delay for character
} // End of LCDWrite function
int main(void) // Start of 'main' function
{
PORTA = 0; // Set all pins on PORTA to low
PORTC = 0; // Set all pins on PORTC to low
CMCON0 = 7; // Turn off internal comparators
ANSEL = 0; // Turn off ADC
TRISA = 0b00011111; // Pins RA0 - RA4 are inputs
TRISC = 0; // All of PORTC are outputs
int stridelength = 70; // Define a series of integer variables to be used later
int bodyweight = 80; //
int noofsteps = 0; //
int distance = 0; //
int calories = 0; //
int menulevel; //
// [BELOW] Initialize LCD by sending a series of instructions as defined in the datasheet for the Hitachi 44780 //
for (i = 0; i < Twentyms; i++); // Delay to wait for LCD to power up
PORTC = 3; // Start initialization process
E = 1; E = 0; // Send reset command
for (i = 0; i < Fivems; i++); // Delay
E = 1; E = 0; // Repeat reset command
for (i = 0; i < TwoHundredus; i++); // Delay
E = 1; E = 0; // Repeat reset command for a third time
for (i = 0; i < TwoHundredus; i++); // Delay
PORTC = 2; // Initialize LCD 4 bit mode
E = 1; E = 0;
for (i = 0; i < TwoHundredus; i++); // Delay
// [BELOW] Send a series of instructions to the LCD as defined in the datasheet for the Hitachi 44780 //
LCDWrite(0b00101000, 0); // LCD is 4 Bit I/F, 2 Line
LCDWrite(0b00000001, 0); // Clear LCD
LCDWrite(0b00000110, 0); // Move cursor after each character
LCDWrite(0b00001110, 0); // Turn on LCD and enable cursor
// Finished initialising the LCD
// At this point, the LCD screen should read "Input stride" on the top line and "70 cm" on the bottom line
LOCATION1:
strcpy(TopMessage, "Input Stride");
sprintf(BotMessage, "%d cm \0", stridelength);
LCDWrite(0b00000001, 0); // Clear LCD
for (i = 0; i < Delay; i++); // Delay
for (i = 0; TopMessage[i] != 0; i++)
LCDWrite(TopMessage[i], 1);
LCDWrite(0b11000000, 0); // Move Cursor to the Second Line
for (i = 0; BotMessage[i] != 0; i++)
LCDWrite(BotMessage[i], 1);
while (FORWARD !=1)
{
if (UP==1)
{
stridelength +=5;
for (i = 0; i < Delay; i++); // Delay
sprintf(BotMessage, "%d cm \0", stridelength); // Take the value of the integer 'stridelength' and write it into the character array 'BotMessage' in ASCII format
goto LOCATION1;
}
if (DOWN==1)
{
stridelength -=5;
for (i = 0; i < Delay; i++); // Delay
sprintf(BotMessage, "%d cm \0", stridelength); // Take the value of the integer 'stridelength' and write it into the character array 'BotMessage' in ASCII format
goto LOCATION1;
}
}
LOCATION2:
strcpy(TopMessage, "Input Weight");
sprintf(BotMessage, "%d kg \0", bodyweight);
LCDWrite(0b00000001, 0); // Clear LCD
for (i = 0; i < Delay; i++); // Delay
for (i = 0; TopMessage[i] != 0; i++)
LCDWrite(TopMessage[i], 1);
LCDWrite(0b11000000, 0); // Move Cursor to the Second Line
for (i = 0; BotMessage[i] != 0; i++)
LCDWrite(BotMessage[i], 1);
while (FORWARD !=1)
{
if (UP==1)
{
bodyweight ++;
for (i = 0; i < Delay; i++); // Delay
sprintf(BotMessage, "%d kg \0", bodyweight); // Take the value of the integer 'stridelength' and write it into the character array 'BotMessage' in ASCII format
goto LOCATION2;
}
if (DOWN==1)
{
bodyweight --;
for (i = 0; i < Delay; i++) // Delay
sprintf(BotMessage, "%d kg \0", bodyweight); // Take the value of the integer 'stridelength' and write it into the character array 'BotMessage' in ASCII format
goto LOCATION2;
}
} // elihw
menulevel = 0; //
while(1)
{
if (FORWARD == 1)
{
(menulevel += 1) % 2; // this forces the menu level to wrap around when you hit 3
for (i = 0; i < Delay; i++); // Delay
}
if (TILTSWITCH == 1)
{
noofsteps++;
for (i = 0; i < Delay; i++); // Delay
}
switch (menulevel)
{
case 0 : strcpy(TopMessage, "You have taken"); // The character array named 'TopMessage' is filled with the shown text
sprintf(BotMessage, "%d steps", noofsteps); // Take the value of the variable 'noofsteps' (which is currently o) and write it into the character BotMessage in ASCII format
LCDWrite(0b00000001, 0); // Clear LCD
for (i = 0; i < Delay; i++); // Delay
for (i = 0; TopMessage[i] != 0; i++);
LCDWrite(TopMessage[i], 1);
LCDWrite(0b11000000, 0); // Move Cursor to the Second Line
for (i = 0; BotMessage[i] != 0; i++);
LCDWrite(BotMessage[i], 1);
break;
// Run 'LCDOutput' by reading using the current values of 'TopMessage' and 'BotMessage'
case 1 : distance = stridelength*noofsteps;
strcpy(TopMessage, "Distance"); // The character array named 'TopMessage' is filled with the shown text
sprintf(BotMessage, "%d m", distance); // Take the value of the variable 'noofsteps' (which is currently o) and write it into the character BotMessage in ASCII format
LCDWrite(0b00000001, 0); // Clear LCD
for (i = 0; i < Delay; i++) // Delay
for (i = 0; TopMessage[i] != 0; i++);
LCDWrite(TopMessage[i], 1);
LCDWrite(0b11000000, 0); // Move Cursor to the Second Line
for (i = 0; BotMessage[i] != 0; i++);
LCDWrite(BotMessage[i], 1);
break;
// Run 'LCDOutput' by reading using the current values of 'TopMessage' and 'BotMessage'
case 2 : calories = ((noofsteps*stridelength)/123);
strcpy(TopMessage, "Calories"); // The character array named 'TopMessage' is filled with the shown text
sprintf(BotMessage, "%d Kcal", calories); // Take the value of the variable 'noofsteps' (which is currently o) and write it into the character BotMessage in ASCII format
LCDWrite(0b00000001, 0); // Clear LCD
for (i = 0; i < Delay; i++); // Delay
for (i = 0; TopMessage[i] != 0; i++)
LCDWrite(TopMessage[i], 1);
LCDWrite(0b11000000, 0); // Move Cursor to the Second Line
for (i = 0; BotMessage[i] != 0; i++)
LCDWrite(BotMessage[i], 1);
break;
}
if (UP && DOWN == 1)
{
goto LOCATION1;
}
} // elihw
return 0;
} // End of program
However, this is still not compiling due to the above errors.