unsigned char in C program

Status
Not open for further replies.

Parth86

Member
Hello
I am trying to write c program to display A-Z alphabets on display using unsigned char data type not string .
I understand what is unsigned char but I Don't understand how to use in real program. thats why I have taken this example. using char data type we can represent An ASCII character, a decimal number, hexadecimal. actually we are storing byte value in memory .

unsigned char a; // Can hold values from 0 to 255
a= 'V'; // Ascii character.
This code is not working

C:
#include<reg51.h>
#define display_port P1     //Data pins connected to port 2 on microcontroller
sbit rs = P2^0;  //RS pin connected to pin 2 of port 3
sbit rw = P2^1;  // RW pin connected to pin 3 of port 3
sbit e =  P2^2;  //E pin connected to pin 4 of port 3

void msdelay(unsigned int time)  // Function for creating delay in milliseconds.
{
    unsigned i,j ;
    for(i=0;i<time;i++)  
    for(j=0;j<1275;j++);
}
void lcd_cmd(unsigned char command)  //Function to send command instruction to LCD
{
    display_port = command;
    rs= 0;
    rw=0;
    e=1;
    msdelay(1);
    e=0;
}

void lcd_data(unsigned char disp_data)  //Function to send display data to LCD
{
    display_port = disp_data;
    rs= 1;
    rw=0;
    e=1;
    msdelay(1);
    e=0;
}

 void lcd_init()    //Function to prepare the LCD  and get it ready
{
    lcd_cmd(0x38);  // for using 2 lines and 5X7 matrix of LCD
    msdelay(10);
    lcd_cmd(0x0F);  // turn display ON, cursor blinking
    msdelay(10);
    lcd_cmd(0x01);  //clear screen
    msdelay(10);
    lcd_cmd(0x81);  // bring cursor to position 1 of line 1
    msdelay(10);
}
void main()
{
    unsigned char a;  
  
    lcd_init();
  
    {
        for(a = 'A'; a <= 'Z'; ++a)
            msdelay(50);
    }
}
 
This code is not working

Explain!!

Two things stick out... If you are wanting to see A ~ Z on the LCD screen, then you need to call Lcd_data()
Also 'Z' will not be printed as ++a increments a first, then assesses a

a++
on the other hand assesses a then increments...
 
Explain!!

Two things stick out... If you are wanting to see A ~ Z on the LCD screen, then you need to call Lcd_data()
Also 'Z' will not be printed as ++a increments a first, then assesses a

a++
on the other hand assesses a then increments...
I was refereeing this code. when I compile this code on PC . I see output A-Z

C:
#include <stdio.h>
int main()
{
  char c;
    for(c = 'A'; c <= 'Z'; ++c)

       printf("%c ", c);
    return 0;
}
do you mean like this
C:
void main()
{
    unsigned char a;  
  
    lcd_init();
  
    {

    display_port = disp_data;
        for(a = 'A'; a <= 'Z'; ++a)
            msdelay(50);
    }
}
 
I mean.. What's not working.... The code posted should work fine ( apart from the data -> screen part )

for(a = 'A'; a <= 'Z'; a++)
{
msdelay(50);
lcd_data(a);
}​
 
I mean.. What's not working.... The code posted should work fine ( apart from the data -> screen part )
My code is not working . may be I have mistaken somewhere . I have tested other code on proteus that's work fine. there is no problem in circuit design. something is wrong in code
C:
#include<reg51.h>
#define display_port P1     //Data pins connected to port 2 on microcontroller
sbit rs = P2^0;  //RS pin connected to pin 2 of port 3
sbit rw = P2^1;  // RW pin connected to pin 3 of port 3
sbit e =  P2^2;  //E pin connected to pin 4 of port 3

void msdelay(unsigned int time)  // Function for creating delay in milliseconds.
{
    unsigned i,j ;
    for(i=0;i<time;i++) 
    for(j=0;j<1275;j++);
}
void lcd_cmd(unsigned char command)  //Function to send command instruction to LCD
{
    display_port = command;
    rs= 0;
    rw=0;
    e=1;
    msdelay(1);
    e=0;
}

void lcd_data(unsigned char disp_data)  //Function to send display data to LCD
{
    display_port = disp_data;
    rs= 1;
    rw=0;
    e=1;
    msdelay(1);
    e=0;
}

 void lcd_init()    //Function to prepare the LCD  and get it ready
{
    lcd_cmd(0x38);  // for using 2 lines and 5X7 matrix of LCD
    msdelay(10);
    lcd_cmd(0x0F);  // turn display ON, cursor blinking
    msdelay(10);
    lcd_cmd(0x01);  //clear screen
    msdelay(10);
    lcd_cmd(0x81);  // bring cursor to position 1 of line 1
    msdelay(10);
}
void main()
{
    unsigned char a;
   for(a = 'A'; a <= 'Z'; a++)
{
msdelay(50);
lcd_data(a);
}
}
 
My code is not working
Let me re-iterate... What isn't working?? "The code isn't working" doesn't help me..

a) Is it compiling?
b) Is it running incorrectly?
c) Are the variables A ~ Z not storing?

Okay!! I have simulated it!!! Try this.. You didn't call lcd_init()....
C:
void main()
 {
 unsigned char a;
 lcd_init();
 
 for(a = 'A'; a <= 'Z'; a++)
  {
  msdelay(50);
  lcd_data(a);
  }
 }
 
thank you very much for your effort. now I am getting A to O alphabets. I am using jhd16x2 LCD.
I see message ABCDEFHIIJKLMO for few times why I am not seeing A to Z and why it doesn't show message repeatedly
 
Because you are writing to line one only...26 characters do not fit on one 16 character line...

C:
void main()
 {
 unsigned char a;
 lcd_init();
 
 for(a = 'A'; a <= 'O'; a++)
  {
  msdelay(50);
  lcd_data(a);
  }
 lcd_cmd(0xc0);
 for(a = 'P'; a <= 'Z'; a++)
  {
  msdelay(50);
  lcd_data(a);
  }
 }
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…