Page 1 of 1

display large number

Posted: Mon Apr 03, 2006 1:37 pm
by Adan_T
Hi,
I'm using the Atmega32 uC to send characters to the LK204-25 LCD. I can
send individual characters (e.g. 'A' , 'B' , '1' '7', etc); I can also display a large number
such as '11500' by send one number at a time with a function call. (USART_TRANSMIT).
The problem arises when I want to send a large number such as '11500' but
with just
one function call. Does anybody have any suggestions as to how to do this?
Thanks in advance
I'm using the following code to send characters:


#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <stdio.h>

unsigned char txbuffer[];
long int txbuffer_numbers[10];

void USART_Transmit( unsigned char data);
void USART_Transmit_Set_Cursor_Position(void);
void USART_Transmit_Clear_Screen(void);
void USART_Transmit_Number(void);
void delay_ms(unsigned short ms);

int main (void)
{
UCSRA=0x02;
UCSRB=0x08;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x33;

USART_Transmit_Clear_Screen();

USART_Transmit_Number();
}

void USART_Transmit_Clear_Screen(void)
{
int x;

txbuffer[0] = 254;
txbuffer[1] = 88;

for (x=0; x<2; x++)
{
USART_Transmit(txbuffer[x]);
}
}

void USART_Transmit_Set_Cursor_Position(void)
{
int k;

txbuffer[0] = 254;
txbuffer[1] = 71;
txbuffer[2] = 4;
txbuffer[3] = 3;

for (k=0; k<4; k++)
{
USART_Transmit(txbuffer[k]);
}
}

void USART_Transmit( unsigned char data)
{
// Wait for empty buffer
while ( !( UCSRA & (1<<UDRE)) )
;

// Put data into buffer, sends the data
UDR = data;
}

void USART_Transmit_Number(void)
{
USART_Transmit_Set_Cursor_Position();
txbuffer_numbers[0] = '11500';
USART_Transmit(txbuffer_numbers[0]);
}

Posted: Mon Apr 03, 2006 3:43 pm
by Raquel
Hi Adan_T,

Your USART_Transmit function accepts only unsigned char data, by doing USART_Transmit(txbuffer_numbers[0]); which passes a long int to the function, you will be losing your large number and only the last 8 bits will actually be sent.

Also, when you actually do this: txbuffer_numbers[0] = '11500'; you should be getting some kind of a compiler error. Most compilers I've seen takes ' ' as ASCII equivalent of the character, so I am sure you have some kind of a warning (at least) when you do txbuffer_numbers[0] = '11500'.

If you want to send characters 1,1,5,0,0 on the screen, try:
txbuffer[0] = '1';
txbuffer[1] = '1';
txbuffer[2] = '5';
txbuffer[3] = '0';
txbuffer[4] = '0';
then send the array to USART_Transmit

Posted: Mon Apr 03, 2006 4:19 pm
by Adan_T
Hello Raquel,
I can display the number '11500' on the LCD by sending one number at
a time
but my question is:
Would it be possible to send this same number with just one function call?

You're right I get two warnings when compiling the program:
-character constant too long for its type for the followwing line:
txbuffer_numbers[0] = '11500';
and

-array 'txbuffer' assumed to have one element for the followwing line:
unsigned char txbuffer[];
The second warning ('txbuffer' assumed to have one element) causes no problem and the first one I still can't figure out.
Thanks for your help

Posted: Mon Apr 03, 2006 4:50 pm
by Raquel
Hi Adan_T,

unsigned char number[5] = {'1','1','5','0','0'};

void Transmit_String(unsigned char txt[], unsigned char length)
{
unsigned char i;
for (i = 0; i < length; i++)
USART_Transmit(txt);
}

then call Transmit_String(number, 5); somewhere
I am not sure if this is what you are looking for

Posted: Mon Apr 03, 2006 7:08 pm
by Adan_T
Hello Raquel,

Your suggestion was good, but doesn't work for my program.

I'm writing a program that counts from 1 to 15,000 each time incrementing
by one.
Each time the program increments by one it delays for about 15 seconds.
I need
to display all these numbers (1 to 15,000) on the LCD as the program is
counting
The program needs to do the following:
(display 1 - wait 15 seconds - clear screen, display 2 - wait 15 seconds - clear screen,
display 3 - wait 15 seconds - clear screen, ... display 15,000).
As you can
see I need to display a variable. I have everything figured out
except
for the fact of displaying this variable. Is it possible to display this variable
as the
program is counting?

Posted: Tue Apr 04, 2006 10:06 am
by Raquel
Hi Adan_T,

Unfortunately, there isn't a way to display your variable straight out. You will need to do some extra work. Since numbers 0 - 9 can be displayed in the display by sending 30d to 39d, you can easily increment each digit this way, but then you need to know which digit is to be incremented next. Say for you number 1 to 15,000. At the beginning (1-9), it is easy enough to send a variable, say ones_variable and increment it after sending it, but then when it reaches 9, you need to look at another variable, say tens_variable that indicates that you need to add another digit before sending your other variable, this one increments when your ones_variable have again reached 9. You will have to have 5 of these variables.

I hope that you kind of understood what I am trying to say.

Posted: Tue Apr 04, 2006 1:34 pm
by Jon
hi, this will work for regular size numbers:
#include "string.h" //for strlen()
#include "stdio.h" //for sprintf()



void printNumber(int number)
{
byte string[10];

sprintf(string,"%i",number);
printString(string);
}

void printString(char txt[])
{
byte i;

for(i=0;i<strlen(txt);i++)
{
USART_Transmit(txt);
}
}

I can't test it out right now, and it seems kind dirty but for large numbers it'd be similar, you will need to include:
#include "string.h" //for strlen()
#include "stdio.h" //for sprintf()


void printLNumber(int number,int row, int col)
{
byte string[10];

sprintf(string,"%i",number);
printLString(string,col,row);
}

void printLString(char txt[], int row, int col)
{
byte i;
int digit;
USART_Transmit(254); //Initialize the large numbers
USART_Transmit(110);

for(i=0;i<strlen(txt);i++)
{

digit=txt; //set the integer to the ascii value (0 = 48 ASCII)
digit=digit-48; //subtract 48 from the ascii value to get the numeric representation
printLnumber(digit,row,col+i);

}

void printLnumber(int Digit,byte Row,byte Col)
{
USART_Transmit(254);
USART_Transmit(35);
USART_Transmit(Row);
USART_Transmit(Col);
USART_Transmit(Digit);
}

Posted: Wed Apr 05, 2006 7:48 pm
by Adan_T
Hi everyone,

Does anybody know how to get a hold of the <cstdio.h> and <stdlib.h> header files
and add them to the Command Prompt compiler to compile a C program?

Posted: Thu Apr 06, 2006 9:45 am
by Raquel
Hi Adan_T,

What compiler are you using?

Posted: Thu Apr 06, 2006 4:16 pm
by Jon
Sorry about the library mix-up guys, I edited the post so it should work now. Let me know how it works.