Shop OBEX P1 Docs P2 Docs Learn Events
Send counter output to LCD — Parallax Forums

Send counter output to LCD

Hello, I have the following code, I didn’t include the forward declarations, or the functions as they are not in question. I’m trying to send a counter (every 1 sec) to a LCD.
The code I wrote will send “this is line 2” to the LCD. I can’t think up a way to send the counter to the LCD. I’m working in C code. Any thoughts?

for(int c = 0; c <=10000;  c++);					//This is any counter
{
Pause(1);
}								//end counter

          /* I want to send the output of the counter (or any A/D output) to the LCD*/

line2();							//Sends to LCD line 2
char a[25] = " this is line 2 "; 				// Initialize the array
for(int i = 0; i < 20; i++) 					// Count i from 0 to 20
{ 
pause(2);							 //wait 2mS
data();								//tell LCD data follows
set_outputs (0,7, (a[i])); 			                //Send (output) LINE 2 to data bus pins 0-7 (8bit)
pause(TW);							//wait about 1 mS
strobe();							        //latch data
}

Comments

  • Your semicolor on the first line, after the closing parenthesis for the for-loop, is probably a mistake. Your delay only happens once, rather than 10,000 times.
  • yes you're right, it was a typo. Thanks.
  • Also, for arrays that are statically initialized (such as `char a[10]="Hello"`, as opposed to dynamically intialized which would be `char a[10]; a = "Hello";`) you don't need to specify the size. The compiler is smart enough to determine the length of the string literal and allocate the appropriate number of bytes for the array. So, you could re write
    char a[25] = " this is line 2 ";
    

    as
    char a[] = " this is line 2 ";
    

    Also, this looks suspiciously similar to the HD44780 protocol. Have you seen PropWare's HD44780 class?
    // Lots of constants defined here for all the pins and stuff. Omitted for brevity...
    
    int main () {
        // Create and initialize our LCD object
        PropWare::HD44780 lcd;
        lcd.start(FIRST_DATA_PIN, RS, RW, EN, BITMODE, DIMENSIONS);
    
        // Print hello world
        lcd.puts("Hello, world!");
    
        return 0;
    }
    

    If you have enough code space, you can even use printf with that object:
    // Lots of constants defined here for all the pins and stuff. Omitted for brevity...
    
    int main () {
        // Create and initialize our LCD object
        PropWare::HD44780 lcd;
        lcd.start(FIRST_DATA_PIN, RS, RW, EN, BITMODE, DIMENSIONS);
    
        // Create a printer for easy, formatted writing to the LCD
        PropWare::Printer lcdPrinter(&lcd);
    
        // Print to the LCD (exactly 32 characters so that we fill up both lines)
        lcdPrinter.printf("%u %s%d 0x%07X", 123456789, "Hello!", -12345, 0xabcdef);
    
        return 0;
    }
    

  • I have seen PropWare before, but I really don't yet understand it, as I'm new to C, and I have no illusions of working in C++ as of yet.
    I was aware of the compiler working dynamically but I put in [25] for testing and should not included it in this example.

    So... what does PropWare::HD44780 lcd; do? I'm not acquainted with ' :: '

    If I down load HD44780.h should I not #include "HD44780.h " in the program?

    If I do would the statements you supplied above work to send dynamic data (like from a A/D) to the LCD?
    Thank you.
  • I'd be happy to help you get started.

    The "::" is used to separate a "namespace" from a class within that namespace. A "namespace" can simply be thought of as a group of objects. For instance, I wrote a group of objects called PropWare. SRLM wrote a group of objects called libpropeller. Because we both wrote objects for the exact same microcontroller, there is a very high probability that we wrote objects with exactly the same name (for instance, we both have "I2C" objects. A namespace allows you to uniquely identify both versions of the object - PropWare::I2C and libpropeller::I2C.

    If you know you're only using PropWare, and not libpropeller, then you can save yourself some typing by adding an extra line at the top of your file to say "just assume that I want PropWare objects"
    // Lots of constants defined here for all the pins and stuff. Omitted for brevity...
    
    using namespace PropWare;
    
    int main () {
        // Create and initialize our LCD object
        HD44780 lcd;
        lcd.start(FIRST_DATA_PIN, RS, RW, EN, BITMODE, DIMENSIONS);
    
        // Create a printer for easy, formatted writing to the LCD
        Printer lcdPrinter(&lcd);
    
        // Print to the LCD (exactly 32 characters so that we fill up both lines)
        lcdPrinter.printf("%u %s%d 0x%07X", 123456789, "Hello!", -12345, 0xabcdef);
    
        return 0;
    }
    

    Notice the "using namespace PropWare;" at the top. That means you no longer have to type "PropWare::" in front of PropWare objects.

    In previous posts here on the forums, I might have recommended downloading specific files from PropWare's GitHub page and then copying them into your project. As of last night, there is now an easier way to use PropWare in SimpleIDE. Check out the first section of the download page: http://david.zemon.name/PropWare/Download.xhtml
  • gregfoxgregfox Posts: 68
    edited 2016-01-13 20:09
    Thank you for the info, and help, I could certainly use the help. I'll look at http://david.zemon.name/PropWare/Download.xhtml but I see in the above example you're printing "Hello" and -12345, 0xabcdef. Could -12345, or 0xabcdef be a variable from an A/D converter? If so would it be something like:
    int main()
    data = //output from A/D or counter
        // Create and initialize our LCD object
        HD44780 lcd;
        lcd.start(FIRST_DATA_PIN, RS, RW, EN, BITMODE, DIMENSIONS);
    
        // Create a printer for easy, formatted writing to the LCD
        Printer lcdPrinter(&lcd);
    
        // Print to the LCD (exactly 32 characters so that we fill up both lines)
        lcdPrinter.printf("%u %s%d 0x%07X", 123456789, "Hello!", data, 0xabcdef);//<------------------
        return 0;
    
    
  • Absolutely. Print variables or "literals" as they're called and either will work.
Sign In or Register to comment.