sbit P2_0 = 0xA0;
sbit relay = P2_0;
sprintf(buf1, "%d", counted);
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 sfr P4 = 0xD1; void test (void) { if (P4 & 0x01) { // if bit 0 set P4 |= 0x80; // set bit 7 } else { P4 &= ~0x02; // reset bit 1 } }
2) My first project is a simple counter with few delays which shift led's, turn on/off relay and print counted number to lcd.
Somewhere in my code I have to make a string (unsigned char[]) from my (counted ) number so I include <stdio.h> at top of program and use
... like I would do in C programming at PC.Code:sprintf(buf1, "%d", counted);
But while all program (hex) is long about 1,5 kb this single line takes additional 3 kb of program!
Using <stdio.h> seems to me very comfortable for use but price (in bytes) seem's very high.
Is this normal?
And if is:
How to convert counted number to char array to be suitable to show on display without too much spended bytes?
sbit rs=P3^5;
rs = 1;
or
rs = 0;
void main()
{
unsigned char x,y,d1,d2,d3;
y = 0xFD; // y is your value, here I've take 0xFD (253 in Decimal)
x = y / 10; // after execution x will have answer and y will have remainder (x=25 & y=3)
d1 = y % 10; // after execution y will have no change and d1 will have remainder (y=3 & d1=3)
// means you've got d1;your LSD (Least Significant Digit)
d2 = x % 10; // after execution x will have no change and d2 will have remainder (x=25 & d2=5)
// means you've got d2;your middle Digit
d3 = x / 10; // after execution d3 will have answer and x will have remainder (d3=2 & x=5)
// means you've got d3;your MSD (Most Significant Digit)
// now we need to display all three digits 'd3=2' , 'd2=5' & 'd1=3'.
// for that you need to add 30 in these values
d1+=30;
d2+=30;
d3+=30;
// Now you have 'd3=32' , 'd2=35' & 'd1=33'
// And these new values are the Ascii Code of your actual numbers (Remember : by adding 30 you get ASCII)
// Now just send these variables to LCD and display them
// Hope this helps you
}
unsigned char buf0[16] = {0};
unsigned char x,y,d1,d2,d3;
y = myint;
x = y / 10;
d1 = y % 10;
d2 = x % 10;
d3 = x / 10;
d1+=0x30;
d2+=0x30;
d3+=0x30;
buf0[0]=d3;
buf0[1]=d2;
buf0[2]=d1;
lcd_pos(0); //first row, first character
p = 0;
while(buf0[p] != '\0')
{
lcd_wchr(buf0[p]);
p++;
}
void command(unsigned char value) // Function to give command to LCD
{
P2 = value;
RS = 0;
RW = 0;
E = 1;
delay(1); // delay of 1ms
E = 0;
}
void display() // Function to Display
{
d+=30;
P2 = d; // sending data to P^2 (LCD)
RS = 1;
RW = 0;
E = 1;
delay(1);
E = 0;
}
void main() // HERE IS THE MAIN FUNCTION
{
unsigned char x,y,d; // 3 variables are enough, NO NEED OF ANY ARRAY
y = myint;
x = y / 10;
d = y % 10;
command(0x83); // command function to set cursor at 3rd place of the LCD for LSD
display();
d = x % 10;
command(0x82); // command function to set cursor at 2nd place of the LCD for Middle digit
display();
d = x / 10;
command(0x81); // command function to set cursor at 1st place of the LCD for MSD
display();
}
// In short try to use MINIMUM variables and no. of Lines.
// Main concept is just use 1 variable and place it wherever you want on LCD. NOTHING ELSE
Later in program I want to simplify this bit by giving him understandable name, like:
sbit relay = P2_0;
#define relay P2_0
Nothing that could be done with type casts. itoa() would be a correct way, but isn't supported by the Keil C51 library. Did you notice the Keil library help files listing the available functions? In fact sprintf() is the only general number to ASCII formatting method provided by KeilI try to cast and also with itoa, but this seem's not work in keil C.
Can you recommend some "better C" to start with when we talk about 8051 family, mostly 89s52?
sprintf() with integer support doesn't need 3 kB according to my observations, it's only about 1 kB. If you are also using float variables in your code, the printf() float support is added, whether you are actually printing float variables or not.Just ask here is it normal to spend 3 kb of hex file to get conversion from number to string.
There's a number of other commercial C51 compilers as well as free ones. Keil can be considered as the industry standard and is known to generate rather effective code.Can you recommend some "better C" to start with when we talk about 8051 family, mostly 89s52?
buf0[3]=(myint%10)|0x30;
buf0[2]=(myint/10)%10|0x30;
buf0[1]=(myint/100)%10|0x30;
buf0[0]=(myint/1000)%10|0x30;
radc = read_adc(0);
buf0[3]=(radc%10)|0x30;
buf0[2]=(radc/10)%10|0x30;
buf0[1]=(radc/100)%10|0x30;
fradc = radc/255.0*5.0;
sprintf(buf1, "%2.3f", fradc);
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?