Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

A simple C18 ?

Status
Not open for further replies.
OK i didnt look much but if S works then this should:

Code:
LCDStr(char *string)
{
    while(*string != 0)
    {
        LCDChar(*string++);
    }
}

// TEST CALL
char Variable[13] = "Hello AGCB!\0";
LCDStr(Variable);
//or
LCDStr((char *)"Hello AGCB!\0");
 
Last edited:
OK. It works now. I'm sure I had it that way more than once but I may not have had the string written correctly. I have been trying all sorts of things for 2 days, many hours.

Jason, I'll try your code too and report when I have.

I need to experiment with different ways of writing the variable or data.

Thanks Aaron
 
Volatile... I
If you create an array ..ie.. char text[] ={"hello"} There is a good chance the compiler will compile the array as a constant (in code memory). To stop that from happening you can make the array volatile.
 
Volatile... I
If you create an array ..ie.. char text[] ={"hello"} There is a good chance the compiler will compile the array as a constant (in code memory). To stop that from happening you can make the array volatile.

Volatile tells the compiler that the value of that variable can change at any time.. the effect is that the compiler does not optimize that variable.
Sometimes compiler can discard the whole variable if it thinks that the variable is never used.. this can happen if the variable is only used in interrupt routine. Interrupt routines are never called in the actual code, so compiler thinks that the variables used in interrupt routines are useless. Volatile keyword prevents that kind of optimization from happening.
 
Yes MrT! But when you initialize an array with text.. The compiler optimizes the array to a constant!! the only way to override is to make it volatile to stop this from happening... OR!!! as Jason pointed out... Leave it as a constant and cast the text when you use it!!!
 
Yes MrT! But when you initialize an array with text.. The compiler optimizes the array to a constant!! the only way to override is to make it volatile to stop this from happening... OR!!! as Jason pointed out... Leave it as a constant and cast the text when you use it!!!

I agree. Off topic: when did you start using the exclamation mark so much.. it feels really aggressive.. :)
 
Great! Let me digest all this and I'll get back..

My brain is swirling.

Thanks much. Aaron
 
Code:
LCDStr(char *string)
{
    while(*string != 0)
    {
        LCDChar(*string++);
    }
}

// TEST CALL
char Variable[13] = "Hello AGCB!\0";
LCDStr(Variable);
//or
LCDStr((char *)"Hello AGCB!\0");

You guys must think I understand more than I really do.

I assume this is the declaration of the variable that goes before the main function
Code:
char Variable[13] = "Hello AGCB!\0";

But how is that included in the main function call?
Which I assume is this part of code
Code:
LCDStr(char *string)
{
     while(*string != 0)
     {
         LCDChar(*string++);
     }
}

Maybe I really don't understand this!
Please don't mind my ignorance. Aaron
 
Ok! Jason's code
C:
LCDStr(char *string)
{
     while(*string != 0)
     {
         LCDChar(*string++);
     }
}

Is a function you can place it with the other low level LCD routines..
Then when you want to print a text string to the LCD just do this in your main.
C:
LCDStr((char *)text);
 
Try this code:

Code:
/////////////////////////////More LCD ///////////////////////////////////////
                                //Sat AM Feb 22 added Line and Column GOTO
#include <p18F43k20.h>
#pragma config FOSC=INTIO67,BOREN=OFF,PWRT=ON,WDTEN=OFF,PBADEN=OFF,STVREN=OFF,LVP=OFF
//////////// Defines ////////////////////////////////////////////////////////
// The lower 4 bits of this port will be used
#define LCD_DATA        LATD
#define LCD_EN             LATDbits.LATD7
#define LCD_RS             LATDbits.LATD4
#define TRIS_LCD_DATA    TRISD
#define TRIS_LCD_EN     TRISDbits.TRISD7
#define TRIS_LCD_RS     TRISDbits.TRISD4
#define TRIS_LCD_PWR    TRISEbits.TRISE2    //RE2 is for LCD power control through MOSFET
// Commands for Hitachi LCD
#define CLEAR_DISPLAY    0x01
#define FOUR_BIT       0b00101111  /* 4-bit Interface               */
#define EIGHT_BIT      0b00111111  /* 8-bit Interface               */
#define LINE_5X7       0b00110011  /* 5x7 characters, single line   */
#define LINE_5X10      0b00110111  /* 5x10 characters               */
#define LINES_5X7      0b00111011  /* 5x7 characters, multiple line */
#define DISPLAY_ON  0b00001111  /* Display on      */
#define DISPLAY_OFF 0b00001011  /* Display off     */
#define CURSOR_ON   0b00001111  /* Cursor on       */
#define CURSOR_OFF  0b00001101  /* Cursor off      */
#define BLINK_ON    0b00001111  /* Cursor Blink    */
#define BLINK_OFF   0b00001110  /* Cursor No Blink */
#define UNDERLINE_ON    0b00001111
#define UNDERLINE_OFF   0b00001101
#define INCREMENT    0b00000111    /* Entry mode increment */
#define DECREMENT    0b00000101    /* Entry mode decrement */
#define SHIFT_ON    0b00000111    /* Display shift on        */
#define SHIFT_OFF    0b00000110    /* Display shift off    */
#define PulseEnable { LCD_EN = 1; Nop(); LCD_EN = 0; }
                   // Required prototypes.. each function needs to be declared
                // if called BEFORE definition.
void LCDInit(void);
void LCDCmd(unsigned char);  //RS is 0 for commands
void LCDChar(unsigned char);  //RS is 1 for characters
void LCD_GOTO(unsigned char,unsigned char);  //Sets position, line and column
void Delay5ms(void);
void Delay15ms(void);
void Delay100ms(void);  //100 ms delay
void LCDStr(char *string);

char SimpleHello[13] = "Hello AGCB!\0";

unsigned char text1[] = {"Hello"};                                                     ?????????????????
//////////////////////////// MAIN funtion ////////////////////////////////////
void main (void)
{
    int index = 0;
    OSCCON = 0xD0;            /* Set internal oscillator at 4 Mhz */
    ADCON0 = 0x00;             /* Disable internal ADC */

    TRISEbits.RE2 = 0;  //turns on power to LCD via MOSFET                                 ????????????????
    TRISD = 0;    //Make all bits on the Port B (LCD) output bits.

    PORTD = 0;    //Reset the LCD bits to 0
    Delay15ms();

    LCDInit();

    LCDStr(SimpleHello);

    while(1);
}

void Delay5ms(void) {
    Delay1KTCYx(10); // Delay of 5ms
    // Cycles = (TimeDelay * Fosc) / 4
    // Cycles = (5ms * 8MHz) / 4
    // Cycles = 10,000
    return;
}
void Delay15ms(void) {
    Delay1KTCYx(30); // Delay of 15ms
    // Cycles = (TimeDelay * Fosc) / 4
    // Cycles = (15ms * 8MHz) / 4
    // Cycles = 30,000
    return;
}
void Delay100ms(void)
{
    Delay1KTCYx(100);  //delay 100 ms
}
void LCDInit(void) {
    PORTEbits.RE2 = 1;      //turn on LCD power
    Delay100ms();         //100 ms delay to power up LCD
 
    // Set port directions
    TRIS_LCD_DATA &= 0xf0;    // Clear lower 4 bits
    TRIS_LCD_EN = 0;
     TRIS_LCD_RS = 0;
    //LCDCmd(FOUR_BIT);            // Nigel's code sends this before the next command?
    LCDCmd(FOUR_BIT & LINES_5X7);
    LCDCmd(INCREMENT & SHIFT_OFF);
    LCDCmd(DISPLAY_ON & BLINK_OFF & UNDERLINE_OFF);
    LCDCmd(CLEAR_DISPLAY);
    Delay15ms();
}
void LCDCmd(unsigned char c) {
    LCD_RS = 0;
    LCD_DATA &= 0xf0;
    LCD_DATA |= (c >> 4);
    PulseEnable;
    LCD_DATA &= 0xf0;
    LCD_DATA |= (c & 0x0f);
    PulseEnable;
    Delay5ms();
}
void LCDChar(unsigned char c) {
    LCD_RS = 1;
    LCD_DATA &= 0xf0;
    LCD_DATA |= (c >> 4);
    PulseEnable;
    LCD_DATA &= 0xf0;
    LCD_DATA |= (c & 0x0f);
    PulseEnable;
    Delay5ms();
}
void LCD_GOTO(unsigned char line,unsigned char column)
{
    unsigned char data = 0x80;                // default to 1
    if(line == 2)data = 0xc0;                // change to 2
    data += column;                            // add in  column
    LCDCmd(data);
    Delay5ms();
}

void LCDStr(char *string)
{
     while(*string != 0)
     {
         LCDChar(*string++);
     }
}

I added it to the lastest code i could find : https://www.electro-tech-online.com/threads/a-simple-c18.140154/#post-1170322
 
That code is full of mistakes..

What is this supposed to mean:
LCDStr(SimpleHello);

Or this:
LCDCmd(DISPLAY_ON & BLINK_OFF & UNDERLINE_OFF);

I think the above should be:
LCDStr("SimpleHello");
and
LCDCmd(DISPLAY_ON | BLINK_OFF | UNDERLINE_OFF);

There are many other mistakes.
 
That code is full of mistakes..

What is this supposed to mean:
LCDStr(SimpleHello);

Or this:
LCDCmd(DISPLAY_ON & BLINK_OFF & UNDERLINE_OFF);

I think the above should be:
LCDStr("SimpleHello");
and
LCDCmd(DISPLAY_ON | BLINK_OFF | UNDERLINE_OFF);

There are many other mistakes.

LOL, Not mistakes by us but by your EYES..

SimpleHello is a variable. If you quote it, its TEXT...

The & portion depends if a bit has to be LOW( & ) and not HIGH ( | ), since the "S" character worked then i assume & is correct.
 
LOL, Not mistakes by us but by your EYES..
SimpleHello is a variable. If you quote it, its TEXT...

Haha.. sorry about that. Stupid name for a variable :)

The & portion depends if a bit has to be LOW( & ) and not HIGH ( | ), since the "S" character worked then i assume & is correct.

That sentence is a mystery.. what?
 
OK for instance... you have a CS line ... most CS lines need to be LOW to be enabled so you would AND ( & ) them...

Code:
#define DISPLAY_ON  0b00001111
#define BLINK_OFF   0b00001110
#define UNDERLINE_OFF   0b00001101

VALUE OF THEM USING & = 0b00001100 aka 0x0C
which is the same as using:

Code:
LCDCmd(DISPLAY_ON & BLINK_OFF & UNDERLINE_OFF);
//is the same as
LCDCmd(0b00001100);
//which is also the same as
LCDCmd(0x0C);

If you use OR the VALUE would always be 0xFF
 
Hmm.. right. Sorry. I need some sleep. Haha.
I just got a job in product development of a company that does safety products and now I'm struggling here with basic things..
 
Status
Not open for further replies.

Latest threads

Back
Top