-
universal write function
Good afternoon everyone :)
I have written two functions to write data to a LCD screen.One writes a character and the other writes a string.
Code:
void write(unsigned char z)
{
enable = 0;
rs = 1;
rw = 0;
delay(9000);
P10_OUT = z;
delay(9000);
enable = 1;
}
void str_write(char *buffer)
{
int i = 0;
for(i=0; i<=(strlen(buffer)-1); i++)
{
if(i > 19)
break;
delay(100000);
write(buffer[i]);
}
}
Is there any way i can combine them into a single function ,so that i don't have to use 2 separate functions to write a char and string?
Something like:
uni_write('h') ==> should write h.
uni_write(str_Array) ==>should write contents of str_Array.
Thanks in advance :)
-
If you get the strlen() from the calling function, you could make that a parameter to the one universal write function. Pass that value in along with the pointer to the first char, and have it print it out in a for loop. One char or 150 char's, it will work either way.
-
Could you please post an example code Adak.
I have got this so far...
Code:
char buff[] = "\0"; //in main
char c = '\0'; //in main
str_write(stringname,c); //for writing string
str_write(buff,char_name); //for writing character
//my function
void univ_write(char *buffer,ch)
{
int i = 0;
if(ch == '\0')
{
__nop(); //no operation
}
else
{
write(ch);
}
if(buffer == "\0")
{
__nop();
}
else
{
for(i=0; i<=(strlen(buffer)-1); i++)
{
if(i > 19)
break;
delay(100000);
write(buffer[i]);
}
}
}
-
Code:
#include <stdio.h>
#include <string.h>
void univ_write(char *buffer, int length);
int main(void) {
int buffLength=0;
char buff[100] = {"The first time, ever I saw your face"};
//just an example. You should get a string from the user -
//one char or 99 char's.
buffLength=strlen(buff);
univ_write(buff, buffLength);
return 0;
}
void univ_write(char *buffer, int length)
{
int i = 0;
for(i=0; i<length; i++)
{
printf("%c",buffer[i]);
}
printf("\n");
}
This is one way, not the only way.