Thread: universal write function

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    68

    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
    Last edited by ak47; 02-17-2013 at 02:40 AM.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

  3. #3
    Registered User
    Join Date
    Sep 2012
    Posts
    68
    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]);
            }
        }
    }

  4. #4
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. a universal compiler....!!!
    By bablu1988 in forum C Programming
    Replies: 7
    Last Post: 08-13-2009, 08:00 AM
  2. Declare universal
    By sea_4_ever in forum C Programming
    Replies: 4
    Last Post: 08-26-2007, 10:04 PM
  3. Universal Fonts
    By sean in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 08-10-2003, 08:07 PM
  4. Universal Engine
    By Travis Dane in forum Game Programming
    Replies: 6
    Last Post: 02-16-2003, 11:54 AM
  5. One Unique Universal Programming Language ?
    By zahid in forum A Brief History of Cprogramming.com
    Replies: 30
    Last Post: 01-29-2003, 12:36 AM