Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

working with LCD and Problems with showing data ( MpLab) 18f452

Status
Not open for further replies.

baby_1

Advanced Member level 1
Advanced Member level 1
Joined
Dec 3, 2010
Messages
415
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,298
Activity points
4,277
Hello
here is my program


Code:
#include <p18f452.h> 
 #include <delays.h> 
 #include <stdio.h> 
 
 
 #pragma config OSC=HSPLL 
 #pragma config DEBUG=OFF 
 #pragma config WDT=OFF 
 #pragma config BOR=OFF 
 #pragma config LVP=OFF 
 #pragma config PWRT=ON 
 
 #define LED PORTDbits.RD3 
 #define rs PORTDbits.RD0 
 #define en PORTDbits.RD1 
 #define datas PORTB 
 
 
 
 
 void writedata(unsigned char s){ 
     rs=1; 
     en=0; 
     datas=s; 
     en=1; 
     Delay10TCYx(10); 
     en=0; 
     Delay10TCYx(10); 
 } 
 
 void writecommand(unsigned char s){ 
     rs=0; 
     en=0; 
     datas=s; 
     en=1; 
     Delay10TCYx(10); 
     en=0; 
     Delay10TCYx(10); 
 } 
 
 
 void lcdinit() 
 { 
     Delay1KTCYx(1); 
     writecommand(0x38); 
     Delay1KTCYx(1); 
     writecommand(0x0E); 
     Delay1KTCYx(1); 
 } 
 
 void numtostring(unsigned char *s) 
 { 
 
 } 
 
     unsigned char i; 
 void main(void) 
 { 
     TRISDbits.RD3=0; 
     TRISB=0; 
     TRISDbits.RD0=0; 
     TRISDbits.RD1=0; 
     LED=1; 
     lcdinit(); 
     writecommand(0x01); 
     while(1) 
     { 
         if (i>10) 
         { 
          i=0; 
         writecommand(0x01); 
         writecommand(0x02); 
         } 
         Delay10TCYx(10); 
         writedata(i+48); 
         Delay10KTCYx(50); 
         LED=~LED; 
         i=i+1; 
     } 
 }


problem:
1-when i powered up the MCU it start from 3 or 7(in first increasing but the next increasing it works fine)?why it doen't work properly in the first step?
according ascii table when i add 48 to each number it returns the ascii number ( for example if i=0 the ascii should be 48) but it doesn't show me the 0 it start from 1 !!!!

why?
 

You didn't initialize i to be any particular value, so when the program executes, it can be an unexpected value.
Secondly, in general you don't need to remember details like zero corresponding to 48. You could have just typed
something like writedata(i+'0'); and it would function the same.
Also, you can slightly tidy your code by using a 'for' loop instead of the separate 'if' statement and the
'i=i+1' increment.
 
  • Like
Reactions: baby_1

    baby_1

    Points: 2
    Helpful Answer Positive Rating
Thanks Sky_123
1-In Mplab compiler if i declare a variable and doesn't push any value into it , it can have unexpected value instead of zero?
2-why its is better to use for instead of if statment?
3-i checked again it start from 1 instead of 0 why?
4-how can i make a function that get a decimal or hex number and convert each number to assci? i wrote this code but it doesn't work

for example:

numtostring(123);

Code:
void numtostring(unsigned char *s)
{
    char i;
    i=0;
    while(s[i]!=0)
    {
    writedata(s[i]);
    }
}
 

Hi,

Some compilers may initialize variables to zero, but if they do, it is not something standard. Other compilers will
do nothing. It is up to the programmer to initialize variables. The microcontroller will have random garbage in RAM
locations upon power-up, so the programmer must always set RAM contents (i.e. variables) to the correct
initial value if it matters to the program.
It is tidier to use a for loop than a separate 'if' and an increment instruction at the end. The for loop defines it all
on the first line. Try your method a few times, nested, and you'll soon come to the same conclusion.
It may start from 1 until you initialize the code. Please re-post the code with your modifications, if it is still occurring.
Your 'numtostring' function shows you have a misunderstanding of what you're trying to do. If you want to convert
_from_ a number, why is your parameter that you are passing defined as 'unsigned char *s'?. Surely it should be
(say) just 'unsigned char', or 'int', or 'float' or something?
If you want to convert from a number to a decimal string, you need to check to see how many hundreds there are in the
number (by division) and print a character to represent the hundreds column, then see how many tens are remaining
in the number, print in the tens column and so on...
There is a ready-made function called sprintf() that does all that, if you search the forum you'll find info on it. However
it entails your C library supporting it, and I'm not familiar with your compiler.
 
  • Like
Reactions: baby_1

    baby_1

    Points: 2
    Helpful Answer Positive Rating
Thanks SKY_123

here is my new program

Code:
#include <p18f452.h>
#include <delays.h>
#include <stdio.h>


#pragma config OSC=HSPLL
#pragma config DEBUG=OFF
#pragma config WDT=OFF
#pragma config BOR=OFF
#pragma config LVP=OFF
#pragma config PWRT=ON

#define LED PORTDbits.RD3
#define rs PORTDbits.RD0
#define en PORTDbits.RD1
#define datas LATB




void writedata(unsigned char s){
    rs=1;
    en=0;
    datas=s;
    en=1;
    Delay10TCYx(10);
    en=0;
    Delay10TCYx(10);
}

void writecommand(unsigned char s){
    rs=0;
    en=0;
    datas=s;
    en=1;
    Delay10TCYx(10);
    en=0;
    Delay10TCYx(10);
}


void lcdinit()
{
    Delay1KTCYx(1);
    writecommand(0x38);
    Delay1KTCYx(1);
    writecommand(0x0E);
    Delay1KTCYx(1);
}

void numtostring(unsigned char *s)
{
    char i;
    i=0;
    while(s[i]!=0)
    {
    writedata(s[i]+'0');
    }
}

    unsigned char i;
        unsigned char i1;
void main(void)
{
    TRISDbits.RD3=0;
    TRISB=0;
    TRISDbits.RD0=0;
    TRISDbits.RD1=0;
    LED=1;
    lcdinit();
    i=0;
    writecommand(0x01);
    while(1)
    {
        if (i>10)
        {
         i=0;
        writecommand(0x01);
        writecommand(0x02);
        }
        Delay10TCYx(10);
        writedata(i+'0');
        Delay10KTCYx(80);
        LED=~LED;
        i=i+1;
    }
}

i read more about sprintf but all references tell me that it use more RAM and increase my program volume so i decide to create a function to convert decimal numbers to string.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top