[SOLVED] MikroC 6.4.0 Count and put in LCD???

Status
Not open for further replies.

nagkiller

Full Member level 4
Joined
Jul 9, 2009
Messages
238
Helped
37
Reputation
74
Reaction score
37
Trophy points
1,308
Location
Brazil
Visit site
Activity points
2,706
How to print an integer count in mikroC ???
I've tried to do with sprintf (gives error), with IntToStr, etc ...
I would like to print the value of count 0-255, for example !!!
Thereby:
1-2-3-4-5-6-7-8-9-10-11-...94-95-96-97-98-99-100-101-102.... 252-253-254-255 ...


Code:
sbit LCD_RS at RB4_bit; sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN at RB5_bit; sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4 at RB3_bit; sbit LCD_D4_Direction at TRISB3_bit;
sbit LCD_D5 at RB2_bit; sbit LCD_D5_Direction at TRISB2_bit;
sbit LCD_D6 at RB1_bit; sbit LCD_D6_Direction at TRISB1_bit;
sbit LCD_D7 at RB0_bit; sbit LCD_D7_Direction at TRISB1_bit;

int count=0; char text[3];

void main(){
  OPTION_REG = 0x80;  CMCON = 0xFF;
  TRISA = 0xFF;       TRISB = 0;
  PORTA = 0;          PORTB = 0;
  Lcd_Init(); Lcd_Cmd(_LCD_CLEAR); Lcd_Cmd(_LCD_CURSOR_OFF);
  while(1)
  {
    Lcd_Out( 1, 1, "COUNT" );
    //**************************************
    IntToStr(count, text);
    Lcd_Out( 2, 1, "> ");
    Lcd_Chr_CP( count/10 + '0');
    Lcd_Chr_CP( count%10 + '0');
    //**************************************
    delay_ms(500); count++; Lcd_Cmd(_LCD_CLEAR);
    //**************************************
  }
}

Whats wrong???

I've seen other topics, but not solved!!!
 
Last edited:

Try this:


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
void WriteNum3dec( unsigned char num )
{
   unsigned char dig;
   dig = num / 100 + '0';
   Lcd_Chr_CP( dig );
   dig = (num % 100) / 10 + '0';
   Lcd_Chr_CP( dig );
   dig = (num % 10) + '0';
   Lcd_Chr_CP( dig );
}

 
Andre!!!

Count with 4 digits:

Code:
sbit LCD_RS at RB4_bit; sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN at RB5_bit; sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4 at RB3_bit; sbit LCD_D4_Direction at TRISB3_bit;
sbit LCD_D5 at RB2_bit; sbit LCD_D5_Direction at TRISB2_bit;
sbit LCD_D6 at RB1_bit; sbit LCD_D6_Direction at TRISB1_bit;
sbit LCD_D7 at RB0_bit; sbit LCD_D7_Direction at TRISB1_bit;

int count=0;

void WriteNum3dec( unsigned char num )
{
   unsigned char dig;
   dig = num / 100 + '0';
   Lcd_Chr_CP( dig );
   dig = (num % 100) / 10 + '0';
   Lcd_Chr_CP( dig );
   dig = (num % 10) + '0';
   Lcd_Chr_CP( dig );
}

void main(){
  OPTION_REG = 0x80;  CMCON = 0xFF;
  TRISA = 0xFF;       TRISB = 0;
  PORTA = 0;          PORTB = 0;
  Lcd_Init(); Lcd_Cmd(_LCD_CLEAR); Lcd_Cmd(_LCD_CURSOR_OFF);
  while(1)
  {
    Lcd_Out( 1, 1, "COUNT" );
    //**************************************
    //IntToStr(count, text);
    Lcd_Out( 2, 1, "->" );
    WriteNum3dec( count );
    //**************************************
    delay_ms(100); count++; Lcd_Cmd(_LCD_CLEAR);
    //**************************************
  }
}

How to proceed if it is 5 or 6 digits ???

I forgot the principal!!!

Which form to use correctly IntToStr??? Because it did not work!!!

Thanks!!!
 
Last edited:

Try this


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
sbit LCD_RS at RB4_bit; 
sbit LCD_EN at RB5_bit; 
sbit LCD_D4 at RB3_bit; 
sbit LCD_D5 at RB2_bit; 
sbit LCD_D6 at RB1_bit; 
sbit LCD_D7 at RB0_bit; 
 
sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB3_bit;
sbit LCD_D5_Direction at TRISB2_bit;
sbit LCD_D6_Direction at TRISB1_bit;
sbit LCD_D7_Direction at TRISB1_bit;
 
 
unsigned int count = 0;
char LCDData[17];
 
 
void main(){
  OPTION_REG = 0x80;  CMCON = 0xFF;
  TRISA = 0xFF;       TRISB = 0;
  PORTA = 0;          PORTB = 0;
  Lcd_Init(); 
  Lcd_Cmd(_LCD_CLEAR); 
  Lcd_Cmd(_LCD_CURSOR_OFF);
  Lcd_Out( 1, 1, "COUNT" );
  Lcd_Out( 2, 1, "->" );
 
  while(1)
  {
    
    IntToStr(count, LCDData);
    LCD_Out(2,6,LCDData);
   
    delay_ms(500); 
    count++; 
    
  }
}

 
hello,

yes,you forgot he principal !
to declare a char table for the txt variable.

I have not forgotten!!!

See my first example!!!

My char is declared as text [3] [/ B] ... In the second example, as I have not used it, I exclude!!! Was a lack of my attention!!! Sorry!!!



The program error might be because I stated a small (3) array, some characters appear in ASC table, now doing as below, worked perfectly!!!

Code:
sbit LCD_RS at RB4_bit; sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN at RB5_bit; sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4 at RB3_bit; sbit LCD_D4_Direction at TRISB3_bit;
sbit LCD_D5 at RB2_bit; sbit LCD_D5_Direction at TRISB2_bit;
sbit LCD_D6 at RB1_bit; sbit LCD_D6_Direction at TRISB1_bit;
sbit LCD_D7 at RB0_bit; sbit LCD_D7_Direction at TRISB1_bit;

long int count=0;
char text[7];

void main(){
  OPTION_REG = 0x80;  CMCON = 0xFF;
  TRISA = 0xFF;       TRISB = 0;
  PORTA = 0;          PORTB = 0;
  Lcd_Init(); Lcd_Cmd(_LCD_CLEAR); Lcd_Cmd(_LCD_CURSOR_OFF);
  while(1)
  {
    Lcd_Out( 1, 1, "COUNT" );
    Lcd_Out( 2, 1, "->" );
    //IntToStr(count, text);
    IntToStrWithZeros(count, text);
    LCD_Out(2,3,Ltrim(text));
    delay_ms(100); count++; Lcd_Cmd(_LCD_CLEAR);
  }
}

Thanks to everyone who helped me...
 
Last edited:

Using LTrim is good only if the count is incrementing. If it is decrementing and it decrements from 100 to 99 then the LCD will display 199 because the LCD is not cleared of 100. If you clear the LCD on every count you will get a flickering display.

No need the clear LCD on every loop and also no need to print "COUNT" and '-->" in every loop. Just put them before while(1).
 
This is just one example!!!

The error was only in the amount of characters in an array!!!

Ltrim was used in IntToStr function, just to remove the blank spaces!!!

Valuable tips!!!


Thank you!!!!
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…