PIC16F684 Interfacing with Hitachi 44780

This is a discussion on PIC16F684 Interfacing with Hitachi 44780 within the C Programming forums, part of the General Programming Boards category; Of 10 characters. The last element has to be null (0) to terminate the string. The sprintf function writes the ...

  1. #16
    Registered User
    Join Date
    Dec 2006
    Location
    Canada
    Posts
    3,183
    Of 10 characters. The last element has to be null (0) to terminate the string.

    The sprintf function writes the value of the integer named 'noofsteps' into the character variable named 'TopMessage'. I.e. take the integer value of 'noofsteps' (say, 14,351 steps for example) then write this to 'TopMessage' as a character string i.e. binary code for 1, binary code for 4, binary code for 3, binary code for 5, binary code for 1. Is that what's going on here?
    Yeap.

    From what I remember (I did a school project with it long ago), for 44780, the internal encoding is mostly just ASCII, so you can just clock the string out the data bus 1 byte at a time, and you'll get the right chars on the LCD.

  2. #17
    Deleted Account
    Join Date
    Nov 2010
    Posts
    16
    So, in my case it would make sense to put

    Code:
    TopMessage[17]
    Since the 44780 display that I'm using can only display a max of 16 characters on each line anyway. Thus the max length of anything going into TopMessage will have to be 16 characters or less, plus the one for the end of the string.

    Is this logic correct?

    If I put down some of the rest of the code, would you mind having a look for me?

  3. #18
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,629
    Quote Originally Posted by JTerry View Post
    Thanks very much for your help. I find this kind of resource amazing, that people are actually willing to help out total strangers just as an act of kindness. It's great to see
    You're very welcome.

    Code:
    char TopMessage[11];
    This is defining a character string named 'TopMessage' which has a length of eleven characters?
    More or less. It's actually defining an array of characters of length 11. It's important to note that C doesn't have a string as a basic type. It uses an array of characters, but treats it as a string (instead of just an array or a plain buffer) by putting a null character ('\0', which has an ASCII value of zero) at the end. This is how all the strXXX functions know when they reached the end of the string.

    Code:
    sprintf(TopMessage, "%d", noofsteps);
    The sprintf function writes the value of the integer named 'noofsteps' into the character variable named 'TopMessage'. I.e. take the integer value of 'noofsteps' (say, 14,351 steps for example) then write this to 'TopMessage' as a character string i.e. binary code for 1, binary code for 4, binary code for 3, binary code for 5, binary code for 1. Is that what's going on here?
    If, by "binary code", you mean the ASCII characters of each digit ('1', '4', etc), instead of the plain numeric values of each digit (1, 4, etc which correspond to non-printable characters in ASCII), then yes, you're right.

    Sorry to ask such basic questions
    Don't be! This is how you learn.

  4. #19
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,629
    Quote Originally Posted by JTerry View Post
    So, in my case it would make sense to put

    Code:
    TopMessage[17]
    Since the 44780 display that I'm using can only display a max of 16 characters on each line anyway. Thus the max length of anything going into TopMessage will have to be 16 characters or less, plus the one for the end of the string.

    Is this logic correct?

    If I put down some of the rest of the code, would you mind having a look for me?
    Yes and yes. Though I would recommend using a constant, like so:
    Code:
    #define SCREEN_WIDTH   16
    ...
    char TopMessage[SCREEN_WIDTH+1];

  5. #20
    Deleted Account
    Join Date
    Nov 2010
    Posts
    16
    Thank you guys so much! Was having a bit of a stress earlier after spending a fruitless few hours down at the electronics labs and wading through forums posts by users a lot more advanced than me!

    OK, I'm jotting down some code right now, which I hope to be able to put up within the next hour or so. Please forgive me if it's a total mess!

    JT

  6. #21
    Deleted Account
    Join Date
    Nov 2010
    Posts
    16
    Hey guys, I'm sorry but I don't think I'm going to be able to put this code up tonight after all. I'm about halfway through and I want to make sure I get everything together before I stick it up in case there are glaringly obvious structural problems with it.

    I hope to get it up by midday tomorrow (for reference, it's 9:30pm over here now).

    Thanks for being patient.

    JT

  7. #22
    Deleted Account
    Join Date
    Nov 2010
    Posts
    16
    Hey guys,

    I've managed to get to this stage (please go easy on me!):

    Code:
    #include <pic.h>						// Standard Pic library
    #include <stdio.h>						// This is included to allow me to use the 'sprintf' function
    #include <string.h>
    
    /*  Pedometer - A pedometer program built using C
    
    This Program Initializes Hitachi 44780 Based LCD in 4 Bit Mode
      and then writes a simple string to it.  The simulator was used
      to time delay values.  
    
    RC3:RC0 - LCD I/O D7:D4 (Pins 14:11)
    RC4     - LCD E Clocking Pin
    RC5     - LCD R/S Pin
    
    */
    __CONFIG(INTIO & WDTDIS & PWRTEN & MCLRDIS & UNPROTECT \
      & UNPROTECT & BORDIS & IESODIS & FCMDIS);
    
    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;
    
    char TopMessage[17];					// Define an array of characters of length 17 called ' TopMessage'
    char BotMessage[17];
    
    #define TILT_SWITCH RA0					// Define 'TILT_SWITCH' as pin RA0 for shorthand later
    #define UP RA1
    #define DOWN RA2
    #define FORWARD RA3
    
    #define E  RC4                  		//  Define the LCD Control Pins
    #define RS RC5
    
    const int Twentyms = 1250;      		//  Declare a Constant for 20 ms Delay
    const int Fivems = 300;
    const int TwoHundredus = 10;
    
    int i, j, k, n;                 		//  Use Global Variables for Debug
    
    
    LCDWrite(int LCDData, int RSValue)		// Define a function called 'LCDWrite' that can be called later to write whatever is fed into it to the LCD screen.
    {
    
        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 LCDWrite
    
    LCDOutput()								// Define a function called LCDOutput that can be called later on
    {
        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);
    
    }										// End LCDOutput
    
    main()
    {
        PORTA = 0;							// Set all pins on PORTA to low
        PORTC = 0;                  		//  Start with Everything Low
        CMCON0 = 7;                 		//  Turn off Comparators
        ANSEL = 0;                  		//  Turn off ADC
        TRISA = 15;							// Pins RA0 - RA3 are inputs
        TRISC = 0;                  		//  All of PORTC are Outputs
    
    //  Initialize LCD by sending a series of instructions as defined in the datasheet for the Hitachi 44870 
    
        j = Twentyms;
        for (i = 0; i < j; i++);    		//  Wait for LCD to Power Up
    
        PORTC = 3;                  		//  Start Initialization Process
        E = 1;  E = 0;              		//  Send Reset Command
        j = Fivems;
        for (i = 0; i < j; i++);
    
        E = 1;  E = 0;              		//  Repeat Reset Command
        j = TwoHundredus;
        for (i = 0; i < j; i++);
    
        E = 1;  E = 0;              		//  Repeat Reset Command Third Time
        j = TwoHundredus;
        for (i = 0; i < j; i++);
    
        PORTC = 2;                  		//  Initialize LCD 4 Bit Mode
        E = 1;  E = 0;
        j = TwoHundredus;
        for (i = 0; i < j; i++);
    
        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
    
    	TopMessage = ("Please input stride");	// Fill the character aray 'TopMessage' with the text shown
    
    	sprintf(BotMessage, "%d cm", stridelength);// Take the value of the integer 'stridelength' and write it into the character array BotMessage in ASCII format
    
    	LCDOutput();						// Call the LCDOutput function
    
    // At this point, the LCD screen should read "Please input stride" on the top line and "70 cm" on the bottom line
    
    if (UP==1)								// If the user presses the "UP" button...
    
    {
    
    	stridelength = stridelength +5;		// Increase 'stridelength' by five
    
    	sprintf(BotMessage, "%d cm", stridelength);// Take the value of the integer 'stridelength' and write it into the character array BotMessage in ASCII format
    
    	LCDOutput();						// Call the LCDOutput function
    
    }
    
    else if (DOWN==1)						// If the user presses the 'DOWN' button...
    
    {
    	stridelength = stridelength -5;		// Decrease 'stridelength' by five
    
    	sprintf(BotMessage, "%d cm", stridelength);// Take the value of the integer 'stridelength' and write it into the character array BotMessage in ASCII format
    
    	LCDOutput();							// Run 'LCDOutput' by reading using the current values of 'TopMessage' and 'BotMessage'
    
    }
    
    
    else if (FORWARD==1)					// If the user presses the forward button...
    
    {
    	TopMessage = ("Please input weight");	// Fill the character array 'TopMessage' with the text shown
    
    	sprintf(BotMessage, "%d kg", bodyweight);	// Take the value of the integer 'weight' and write it into the character array BotMessage in ASCII format
    
    	LCDOutput();							// Run 'LCDOutput' by reading using the current values of 'TopMessage' and 'BotMessage'
    
    }	
    
    if (UP==1)								// If the user presses the 'UP' button...
    
    {
    	bodyweight = bodyweight + 2;				// Increment the integer variable 'weight' by 2.
    
    	sprintf(BotMessage, "%d kg", bodyweight);	// Take the value of the integer 'weight' and write it into the character array BotMessage in ASCII format
    	
    	LCDOutput();
    }
    
    else if (DOWN==1)						// If the user presses the 'DOWN' button...
    
    {
    	bodyweight = bodyweight - 2;				// Decrease the value of the integer 'weight' by 2
    
    	sprintf(BotMessage, "%d kg", bodyweight);	// Take the value of the integer 'weight' and write it into the character array BotMessage in ASCII format
    
    	LCDOutput();
    	
    }
    
    	else if (FORWARD==1)
    	
    {
    
    goto LOCATION;
    
    }
    
    LOCATION:
    
    if (menulevel == 0)
    
    {
    
    	TopMessage = ("Number of Steps");		// The character array named 'TopMessage' is filled with the shown text
    
    	sprintf(BotMessage, "%d", noofsteps);	// Take the value of the variable 'noofsteps' (which is currently o) and write it into the character BotMessage in ASCII format
    
    	LCDOutput();						// Run 'LCDOutput' by reading using the current values of 'TopMessage' and 'BotMessage'
    
    	if (TILT_SWITCH==1)					// If the tilt switch is tilted and the input signal goes high...
    
    {
    	noofsteps++;						// Increment the variable 'noofsteps' by one
    
    	goto LOCATION;						// Go to the label 'LOCATION'
    }
    
    if (FORWARD==1)							// If the user presses the 'FORWARD' button...
    
    {
    	menulevel++;							// Increment the integer 'menulevel' by one
    	
    	goto LOCATION;						// Go to the label 'LOCATION'
    }
    
    }
    
    else if (menulevel == 1)
    
    {										// Start of while loop
    
    
    	TopMessage = ("You have walked");		// Fill the character array named 'TopMessage' with the text shown
    
    	distance = (stridelength*noofsteps)/100;	// Calculate the value of 'distance'
    
    	sprintf(BotMessage, "%d metres", distance);	// Take the value of the variable 'distance' and write it into the character array 'BotMessage' in ASCII format
    
    	LCDOutput();							// Run 'LCDOutput' by reading using the current values of 'TopMessage' and 'BotMessage'
    	
    if (TILT_SWITCH == 1)
    
    {
    	noofsteps++;
    	
    	goto LOCATION;
    
    }
    	if (FORWARD == 1)					// If the user presses the 'FORWARD' button...
    
    {
    
    	menulevel++;						// Increment the integer 'menulevel' by one
    
    	goto LOCATION;						// Go to the label 'LOCATION'
    	
    }
    
    else if (menulevel == 2)
    
    {
    
    	TopMessage = ("You have burned");		// Fill the character array named 'TopMessage' with the text shown
    
    	calories = ((noofsteps*stridelength*bodyweight)/123);	// Calculate the value of 'calories'
    	
    	sprintf(BotMessage, "%d kCals", calories); 	// Take the value of the variable 'calories' and write it into the character array 'BotMessage' in ASCII format
    		
    	LCDOutput();
    	
    if (TILT_SWITCH==1)
    
    {
    
    	noofsteps++;						// Increment the value of the integer 'noofsteps'
    	
    	goto LOCATION;						// Go to the label 'LOCATION'
    
    }
    
    }
    
    else if(menulevel == 3)					// When the user tries to access menu level 3 (which doesn't exist)...
    
    {
    
    	menulevel = 0;						// Reset 'menulevel' to zero i.e. go back to the 'Number of Steps' screen
    	
    	goto LOCATION;						// Go to the label 'LOCATION'
    
    }
    }
    }										// End of program
    I've tried compiling and I'm getting the following errors:

    "Only lvalues may be assigned to or modified" and "Illegal conversion between pointer types pointer to const unsigned char -> pointer to unsigned char".

    I'm guessing this means that I've not done the 'sprintf' bit right.

    There are probably a lot of other errors throughout, and the code could definitely be shortened a lot. I'm guessing some sort of array of messages for TopMessage and BotMessage which is selected from using the menulevel variable.

    If anybody had any advice on how to get this to compile so I've got something that actually works, I'd be incredibly grateful!

  8. #23
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,629
    Line numbers for the errors would be immensely helpful, or if you at least point out the specific lines that are causing those errors.

    I think the "Only lvalue" error means you're trying to assign a value to something that's not directly assignable (using a =). You can't assign strings in C using the = sign. You have to use strcpy. Change all 5 occurances of
    Code:
    TopMessage = ("Please input weight");
    to
    Code:
    strcpy(TopMessage, "Please input weight");
    That might take care of the "Illegal conversion" errors too, but I'm not really sure without having a line number to look at.

    Some other things I noticed:
    1. You should try to avoid global variables. All of you values could be local to main, since they're only used in that function.
    2. You should be explicit and declare LCDOutput and LCDWrite with a return type void.
    3. You don't need a second variable in your delay loops to hold the Fivems value. You could just do for (i = 0; i < Fivems; i++).
    4. You need your input portion in a loop so they can press up/down more than once. Try something like this (and a similar one for weight):
    Code:
    do {
        if (UP == 1) {
            stridelength += 5;
        }
        else if (DOWN == 1) {
            stridelength -= 5;
            if (stridelength < 0) {
                stridelength = 0;  // we don't want a negative stridelength
            }
        }
        sprintf(BotMessage, "%d cm", stridelength);
        LCDOutput();
    } while (FORWARD != 1);
    5. Try to avoid using goto, unless you have very good cause. I think you can get the same results by using another loop:
    Code:
    while (1) {    // yes, this is infinite, but we don't want to quit
        if (FORWARD == 1) {
            menulevel = (menulevel + 1) % 3;  // this forces the menu level to wrap around when you hit 3
        }
        if (TILTSWITCH == 1) {
            numofsteps++;
        }
    
        if (menulevel == 0) {
            // calculate the value and print the message
        }
    }
    Work on getting those changes implemented and then we can refine your printing stuff into a nice little array

  9. #24
    Deleted Account
    Join Date
    Nov 2010
    Posts
    16
    Code:
    #include <pic.h>						// Standard Pic library
    #include <stdio.h>						// This is included to allow me to use the 'sprintf' function
    #include <string.h>
    
    /*  Pedometer - A pedometer program built using C
    
    This Program Initializes Hitachi 44780 Based LCD in 4 Bit Mode
      and then writes a simple string to it.  The simulator was used
      to time delay values.  
    
    RC3:RC0 - LCD I/O D7:D4 (Pins 14:11)
    RC4     - LCD E Clocking Pin
    RC5     - LCD R/S Pin
    
    */
    __CONFIG(INTIO & WDTDIS & PWRTEN & MCLRDIS & UNPROTECT \
      & UNPROTECT & BORDIS & IESODIS & FCMDIS);
    
    char TopMessage[17];					// Define an array of characters of length 17 called ' TopMessage'
    char BotMessage[17];
    
    #define TILT_SWITCH RA0					// Define 'TILT_SWITCH' as pin RA0 for shorthand later
    #define UP RA1
    #define DOWN RA2
    #define FORWARD RA3
    
    #define E  RC4                  		//  Define the LCD Control Pins
    #define RS RC5
    
    const int Twentyms = 1250;      		//  Declare a Constant for 20 ms Delay
    const int Fivems = 300;
    const int TwoHundredus = 10;
    
    int i, j, k, n;                 		//  Use Global Variables for Debug
    
    
    void LCDWrite(int LCDData, int RSValue)		// Define a function called 'LCDWrite' that can be called later to write whatever is fed into it to the LCD screen.
    
    {
    
        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 LCDWrite
    
    void LCDOutput()						// Define a function called LCDOutput that can be called later on
    
    {
        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);
    
    }										// End LCDOutput
    
    main()
    {
        PORTA = 0;							// Set all pins on PORTA to low
        PORTC = 0;                  		//  Start with Everything Low
        CMCON0 = 7;                 		//  Turn off Comparators
        ANSEL = 0;                  		//  Turn off ADC
        TRISA = 15;							// Pins RA0 - RA3 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;
    
    //  Initialize LCD by sending a series of instructions as defined in the datasheet for the Hitachi 44870 
    
        for (i = 0; i < Twentyms; i++);    	//  Wait for LCD to Power Up
    
        PORTC = 3;                  		//  Start Initialization Process
        E = 1;  E = 0;              		//  Send Reset Command
        for (i = 0; i < Fivems; i++);
    
        E = 1;  E = 0;              		//  Repeat Reset Command
        for (i = 0; i < TwoHundredus; i++);
    
        E = 1;  E = 0;              		//  Repeat Reset Command Third Time
        for (i = 0; i < TwoHundredus; i++);
    
        PORTC = 2;                  		//  Initialize LCD 4 Bit Mode
        E = 1;  E = 0;
        for (i = 0; i < TwoHundredus; i++);
    
        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
    
    	strcpy(TopMessage, "Please input stride");	// Fill the character aray 'TopMessage' with the text shown
    	sprintf(BotMessage, "%d cm", stridelength);// Take the value of the integer 'stridelength' and write it into the character array BotMessage in ASCII format
    	LCDOutput();						// Call the LCDOutput function
    
    // At this point, the LCD screen should read "Please input stride" on the top line and "70 cm" on the bottom line
    
    do {									// Do the following until the user presses the 'FORWARD' button
    	if (UP == 1) {						// If the user presses the "UP" button...
    		stridelength +=5;				// Increase 'stridelength' by five
    	}									// fi
    	else if (DOWN==1) {					// If the user presses the 'DOWN' button...
    		stridelength -=5;				// Decrease 'stridelength' by five
    		if (stridelength < 25) {		// If the user tries to adjust 'stridelength' lower than 25...
    			stridelength = 25;			// Set 'stridelength' back to 25
    		}								// fi
    	}									// fi esle
    
    	sprintf(BotMessage, "%d cm", stridelength);// Take the value of the integer 'stridelength' and write it into the character array BotMessage in ASCII format
    	LCDOutput();						// Call the LCDOutput function
    } while (FORWARD != 1);
    
    if (FORWARD == 1)	{						// If the user presses the forward button...
    	strcpy(TopMessage, "Please input weight");	// Fill the character array 'TopMessage' with the text shown
    	sprintf(BotMessage, "%d kg", bodyweight);	// Take the value of the integer 'bodyweight' and write it into the character array BotMessage in ASCII format
    	LCDOutput();							// Run 'LCDOutput' by reading using the current values of 'TopMessage' and 'BotMessage'
    	}	
    
    do {									// Do the following until the user presses the 'FORWARD' button
    	if (UP == 1) {						// If the user presses the 'UP' button...
    		bodyweight += 2;				// Increase 'bodyweight' by two
    	}									// fi
    	else if (DOWN == 1) {				// If the user presses the 'DOWN' button...
    		bodyweight -= 2;				// Decrease the value of 'bodyweight' by two
    		if (bodyweight < 44) {			// If the user tries to decrease 'bodyweight' lower than 44...
    			bodyweight = 44;			// Set 'bodyweight' back to 44
    		}								// fi
    	}									// fi esle
    
    	sprintf(BotMessage, "%d kg", bodyweight);	// Take the value of the integer 'weight' and write it into the character array BotMessage in ASCII format
    	LCDOutput();
    } while (FORWARD != 1);
    
    
    if (FORWARD==1) {						// If the user presses the forward button...
    	menulevel = 0;						// Set the value of 'menulevel' to 0
    	}									// fi
    
    // Once the user has finished inputting their info, they have no more input (except TILTSWITCH) and the following code runs forever
    
    while (1) {								// Loop forever
    	if (FORWARD == 1) {					// If the user presses 'FORWARD'...
    		menulevel = (menulevel + 1) % 3;// Increment the value of 'menulevel' by 1, modulo 3
    	}									// fi
    	if (TITSWITCH == 1) {				// If the 'TILTSWITCH' input is triggered...
    		noofsteps++;						// Increment 'noofsteps'
    	}									// fi
    
    if (menulevel == 0) {					// If the 'menulevel' is zero...
    
    	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
    	LCDOutput();							// Run 'LCDOutput' by reading using the current values of 'TopMessage' and 'BotMessage'
    	}										// fi
    
    else if (menulevel == 1) {						// If the 'menulevel' is one...	
    
    	strcpy(TopMessage, "You have walked");		// Fill the character array named 'TopMessage' with the text shown
    	distance = (stridelength*noofsteps)/100;	// Calculate the value of 'distance'
    	sprintf(BotMessage, "%d metres", distance);	// Take the value of the variable 'distance' and write it into the character array 'BotMessage' in ASCII format
    	LCDOutput();								// Run 'LCDOutput' by reading using the current values of 'TopMessage' and 'BotMessage'
    	}											// fi
    
    else {													// If the 'menulevel' is two...
    
    	strcpy(TopMessage, "You have burned");				// Fill the character array named 'TopMessage' with the text shown
    	calories = ((noofsteps*stridelength*bodyweight)/123);	// Calculate the value of 'calories'
    	sprintf(BotMessage, "%d kCals", calories); 			// Take the value of the variable 'calories' and write it into the character array 'BotMessage' in ASCII format
    	LCDOutput();										// Run 'LCDOutput' by reading using the current values of 'TopMessage' and 'BotMessage'
    	}													// esle
    
    }										// elihw
    }										// End of program
    Okedokee, I've taken on board what you said and tried to make all the changes suggested.

    The above code compiles correctly now, apologies for not giving you the full output from the compiler errors earlier (will do so in future).

    I don't really know what you meant when you said declare those two functions as 'return type void'. I've been reading around and from what I can interpret I think the above is correct? I'm basically saying that neither of those functions can return information into the main body of the code, but that information can be sent to them?

    I do have a concern about what happens when the 'calories' variable is calculated. I can't remember what the actual figure is, but say for example as I have in the code that you need to divide by 123. Can an int hold a decimal number (e.g. 1534.5)? And if not, do I need to declare 'calories' as a different variable type? Will I still be able to use sprintf to put the contents into 'BotMessage'?

    I'm guessing I'd have to define 'calories' as a float or double, change the "%d"s to something else, then I'd still be able to write it in. But say I wanted just the first decimal place to be displayed? e.g. 351.6666666 I just want 351.6. How would I go about doing that? I guess there's some sort of way of working out where the decimal point is, then junking all the LSBs after that point?

    I guess if I just leave 'calories' as an int, it will junk anything after the decimal point and just store the last whole number? Or will it just get confused?

    Lol, I'm getting confused.

    Thanks for your help, I think we're getting somewhere!

  10. #25
    Deleted Account
    Join Date
    Nov 2010
    Posts
    16
    PS Oops, formatting has gone all funny, the indents are playing up. Apologies, JT

  11. #26
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,629
    Good job! Looking much better.

    I don't really know what you meant when you said declare those two functions as 'return type void'
    I meant for you to do exactly what you did. If you don't have a return type for your function, C defaults it to return type int, then you will likely get warnings about "this function isn't returning a value". I forgot to mention though, that declaring it with an empty parameter list is not the same as saying "no parameters". It actually means "any number of parameters of any type". It's best to be explicit, and do "void LCDOutput(void)". Also, to be correct, it should be "int main(void)" too, then you need a return 0 at the bottom of main (even if the code won't make it there).

    I do have a concern about what happens when the 'calories' variable is calculated. I can't remember what the actual figure is, but say for example as I have in the code that you need to divide by 123. Can an int hold a decimal number (e.g. 1534.5)? And if not, do I need to declare 'calories' as a different variable type? Will I still be able to use sprintf to put the contents into 'BotMessage'?
    It will not store the decimal portion of the calculation. I believe it just throws away the decimal portion instead of trying to round it. Even if the variable where you store the result is a float, you will still end up with the integer division value since all your operands are integers. You need to force the compiler to treat one of the operands as a float, then it will type-promote the result to a float. You can solve this by casting any one of the variables in the expression as a float or by appending a decimal place on to the 123, making it 123.0 (the . makes it a float).

    I'm guessing I'd have to define 'calories' as a float or double, change the "%d"s to something else, then I'd still be able to write it in. But say I wanted just the first decimal place to be displayed? e.g. 351.6666666 I just want 351.6. How would I go about doing that? I guess there's some sort of way of working out where the decimal point is, then junking all the LSBs after that point?
    Again, if you are okay with just throwing away the decimal portion instead of rounding the calories value, than you can leave it as an int. Otherwise, make it a float and use %f in your format string to sprintf instead of %d. Here's a link to a C reference site that has a good explanation of how to format printf output: The C Library Reference Guide.

    Last quick note, you can drop the if (FORWARD == 1) statements from after your two input loops. The only way to make it out of those loops is if they hit forward, so it's certain that FORWARD was one, but it might not be anymore, so you could totally skip changing the message on the screen. Good luck!

  12. #27
    Deleted Account
    Join Date
    Nov 2010
    Posts
    16
    Making final amendments now, will post so you can see the fruits of your hard work and patience!

    Thanks a bunch for taking the time to help a total beginner, I never would have gotten this far without help.

    Hope to post the code tonight, will be trying on the PIC circuit first thing Monday morning. Fingers crossed it all works out!

    JT

  13. #28
    Deleted Account
    Join Date
    Nov 2010
    Posts
    16

    "Final" Code

    For any who have been following this thread, and especially to those who have helped me (particularly you, Anduril) please find here the "final" version of the code:

    Code:
    #include <pic.h>			// Standard PIC library
    #include <stdio.h>			// Required to enable 'sprintf' function
    #include <string.h>			// Required to enable 'string' functions
    
    //////////////////////////////////////////////////////
    //												  	//
    //					  PEDOMETER				  		//
    //												  	//
    //	WRITTEN BY:									  	//
    //												  	//
    //					                           				  	//
    //						                         			  	//
    //	J Terry						         			  	//
    //												  	//
    //											  		//
    //	OVERVIEW:								  	  	//
    //											  		//
    //  The code below is used to control the	  		//
    //	control the operation of a pedometer	  		//
    //	circuit.								  		//
    //											  		//
    //											  		//
    //	INPUTS:									  		//
    //											  		//
    //	The circuit has 4 digital inputs:		  		//
    //											  		//
    //	RA1 is a connected to a tilt switch.	  		//
    //	RA2 is defined as the 'up' control.		  		//
    //	RA3 is defined as the 'down' control.	  		//
    //	RA4 is defined as the 'forward' control.  		//
    //											  		//
    //	The circuit also has one analogue input:  		//
    //											  		//
    //	RA0 is connected to the centre of a potential	//
    //	divider between a thermistor and a fixed  		//
    //	resistor.								  		//
    //											  		//
    //											  		//
    //	OUTPUTS:								  		//
    //											  		//
    //	The circuit drives a Hitachi44780 LCD.	  		//
    //											  		//
    //	For reference:							  		//
    //											  		//
    //	RC3:RC0 - LCD I/O D7:D4 (Pins 14:11)			//
    //	RC4     - LCD E Clocking Pin					//
    //	RC5     - LCD R/S Pin							//
    //											  		//
    //											  		//
    //	CONTROL:								  		//
    //											  		//
    //	A PIC16F684 IC is used to monitor these input	//
    //	signals and control the output to the LCD. 		//
    //											  		//
    //////////////////////////////////////////////////////
    
    __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 TILT_SWITCH 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;			//
    
    int i, j, k, n;                 		// Declare global variables
    
    void LCDWrite(int LCDData, int RSValue)
    
    // [ABOVE] 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
    
    void LCDOutput(void)					
    
    // [ABOVE] Define a function called 'LCDOutput' that can be called later to feed data to 'LCDWrite' //
    
    {										// Start of LCDOutput
        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);
    
    }										// End LCDOutput
    
    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
    
    	strcpy(TopMessage, "Input stride");	// Fill the character aray 'TopMessage' with the text shown
    	sprintf(BotMessage, "%d cm", stridelength);	// Take the value of the integer 'stridelength' and write it into the character array 'BotMessage' in ASCII format
    	LCDOutput();						// Call the LCDOutput function
    
    // At this point, the LCD screen should read "Input stride" on the top line and "70 cm" on the bottom line
    
    do {									// Do the following until the user presses the 'FORWARD' button
    	if (UP == 1) {						// If the user presses the "UP" button...
    		stridelength +=5;				// Increase 'stridelength' by five
    	}									// fi
    	else if (DOWN==1) {					// If the user presses the 'DOWN' button...
    		stridelength -=5;				// Decrease 'stridelength' by five
    		if (stridelength < 25) {		// If the user tries to adjust 'stridelength' lower than 25...
    			stridelength = 25;			// Set 'stridelength' back to 25
    		}								// fi
    	}									// fi esle
    
    	sprintf(BotMessage, "%d cm", stridelength);// Take the value of the integer 'stridelength' and write it into the character array 'BotMessage' in ASCII format
    	LCDOutput();						// Call the LCDOutput function
    } while (FORWARD != 1);					// Do the above for as long as 'FORWARD' does not equal one
    
    	strcpy(TopMessage, "Input weight");	// Fill the character array 'TopMessage' with the text shown
    	sprintf(BotMessage, "%d kg", bodyweight);	// Take the value of the integer 'bodyweight' and write it into the character array 'BotMessage' in ASCII format
    	LCDOutput();						// Call the LCDOutput function
    	
    do {									// Do the following until the user presses the 'FORWARD' button
    	if (UP == 1) {						// If the user presses the 'UP' button...
    		bodyweight += 2;				// Increase 'bodyweight' by two
    	}									// fi
    	else if (DOWN == 1) {				// If the user presses the 'DOWN' button...
    		bodyweight -= 2;				// Decrease the value of 'bodyweight' by two
    		if (bodyweight < 44) {			// If the user tries to decrease 'bodyweight' lower than 44...
    			bodyweight = 44;			// Set 'bodyweight' back to 44
    		}								// fi
    	}									// fi esle
    
    	sprintf(BotMessage, "%d kg", bodyweight);	// Take the value of the integer 'bodyweight' and write it into the character array 'BotMessage' in ASCII format
    	LCDOutput();						// Call the LCDOutput function
    } while (FORWARD != 1);					// Do the above for as long as 'FORWARD' does not equal one
    
    	menulevel = 0;						// Set the value of 'menulevel' to 0
    
    while (1) {								// Loop forever
    	if (FORWARD == 1) {					// If the user presses 'FORWARD'...
    		menulevel = (menulevel + 1) % 3;// Increment the value of 'menulevel' by 1, modulus 3
    	}									// fi
    	if (TILT_SWITCH == 1) {				// If the 'TILTSWITCH' input is triggered...
    		noofsteps++;					// Increment 'noofsteps'
    	}									// fi
    
    // From this point onwards...	
    
    if (menulevel == 0) {										// If the 'menulevel' is zero...
    
    	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' and write it into the character array 'BotMessage' in ASCII format
    	LCDOutput();											// Call the LCDOutput function
    	}														// fi
    
    else if (menulevel == 1) {									// If the 'menulevel' is one...	
    
    	strcpy(TopMessage, "You have walked");					// Fill the character array named 'TopMessage' with the text shown
    	distance = (stridelength*noofsteps)/100;				// Calculate the value of 'distance'
    	sprintf(BotMessage, "%d metres", distance);				// Take the value of the variable 'distance' and write it into the character array 'BotMessage' in ASCII format
    	LCDOutput();											// Run 'LCDOutput' by reading using the current values of 'TopMessage' and 'BotMessage'
    	}														// fi
    
    else {														// If the 'menulevel' is two...
    
    	strcpy(TopMessage, "You have burned");					// Fill the character array named 'TopMessage' with the text shown
    	calories = ((noofsteps*stridelength*bodyweight)/123);	// Calculate the value of 'calories'
    	sprintf(BotMessage, "%d kCals", calories); 				// Take the value of the variable 'calories' and write it into the character array 'BotMessage' in ASCII format
    	LCDOutput();											// Run 'LCDOutput' by reading using the current values of 'TopMessage' and 'BotMessage'
    	}														// esle
    
    }										// elihw
    return 0;
    }										// End of program
    It may not be the simplest way of achieving what we're setting out to do, but if it works then it's a job well done in my book!

    Only one more task to do:

    We've got an analogue input going into the PIC at RA0. The plan is to read out the value of this using the ADC when the user enters 'menulevel' 3 (yet to be added) and display their body temperature on the screen. If the temperature is too high, the screen should display a warning message.

    We'll have a go at coding that by ourselves and if we run into any problems (or miraculously manage to figure it out by ourselves!) we'll post it up here in case it's of any other to other beginners trawling the forums for advice.

    Thanks again to everyone, and wish us luck for testing it on Monday!

    JT

    PS Sorry again for the messed up indentation, it looks fine in Notepad, don't know why it looks odd in this box.
    Last edited by JTerry; 12-04-2010 at 05:48 PM.

  14. #29
    Deleted Account
    Join Date
    Nov 2010
    Posts
    16

    Code Shrinking

    Hey guys, we've modified the code (and circuit!) a bit to get everything working.

    The weight input and stridelength are now working as desired, and we've coded up most of the rest of the functionality.

    However, we're getting the following compiler errors:

    Error [1347] ; 0. can't find 0x79 words (0x79 withtotal) for psect "strings" in segment "STRING" (largest unused contiguous range 0x31)
    Error [1347] ; 0. can't find 0x79 words (0x79 withtotal) for psect "text178" in segment "CODE" (largest unused contiguous range 0x31)
    Error [1347] ; 0. can't find 0x62 words (0x62 withtotal) for psect "text183" in segment "CODE" (largest unused contiguous range 0x31)
    Error [1347] ; 0. can't find 0x50 words (0x50 withtotal) for psect "text180" in segment "CODE" (largest unused contiguous range 0x31)
    Error [1347] ; 0. can't find 0x44 words (0x44 withtotal) for psect "text179" in segment "CODE" (largest unused contiguous range 0x31)
    Error [1347] ; 0. can't find 0x1E words (0x1e withtotal) for psect "text182" in segment "CODE" (largest unused contiguous range 0x5)
    Error [1347] ; 0. can't find 0x8 words (0x8 withtotal) for psect "clrtext" in segment "CODE" (largest unused contiguous range 0x5)
    Error [1347] ; 0. can't find 0x7 words (0x7 withtotal) for psect "cinit" in segment "CODE" (largest unused contiguous range 0x5)

    I'm pretty sure that the above errors mean that I'm running out of space on the chip.

    We've tried modifying and streamlining the code a bit and have ended up with:

    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.

    Does anybody have any recommendations as to how these code could be made smaller / simpler? Ideally, we also wanted to output a readout from an ADC into menulevel 3, but so far we can't even fit the above on, let alone the further coding for that.

    Thanks again everyone.

    JT

  15. #30
    Registered User
    Join Date
    May 2009
    Posts
    2,746
    get ride of redundant code;

    Code:
    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;
    	}
    
    }
    Re-write below
    Code:
    while (FORWARD !=1)
    {
    	if (UP==1)
    	{
    		stridelength +=5;
    	}
    	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;
    }

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21