c & c++ - printf_ANSI

Contact
PC-compilers
last-news
PIC-microcontroller
AVR-microcontroller
Supports
weakly newsletter
c++
Counter
DS1821Thermostat Programmer
pic-asm-foundations
free links downloads
free downloads
mikroc library
=> keypad library
=> printf_ANSI
PWM software
Home



 

 

/*        PRINTF ANSI Function  mselaf.page.tl 2007     */

      

 

#include<stdarg.h>

 

enum {USE_INT16=3,USE_uSIGN =0,USE_INT8 =1 }settype;

 

////////////////////////////////////////////////////////////////

 

////////////////////////////////////////////////////////////////

 char* itoa(unsigned int n)    // new shape of itoa str

{

   static char str[7];

   signed  Sign =0;

   signed short sSign=0;

  char*ptr = &str[6];

   //check sign type 1 or 2 byte  and match type

  if(settype.f0)   

        if(settype == USE_INT16){  // signed type

                   if((Sign = n)<0)n =-n ;

                   }

  else if((sSign =(signed short) n)<0)

       n =-sSign;  // signed short  use

 

 

 do{

    *(--ptr) = (n%10)+'0';

 }while((n/=10)>0);

  if(Sign<0 || sSign<0)*(--ptr) = '-';

  

 return ptr;  // pointer to str 

}

////////////////////////////////////////////////////////////////

 

char printf(const char* fmt,...)

{

 va_list ap;               //pointer to each unnamed arg

 const char *p; char*sval; // pointers to char

 char hval;                // short int val

 int ival =0;              // int val

 char cval;

// double dval ;           // double val

 char cntr =0;

 va_start(ap,fmt);         //make ap point to 1@ unnamed arg

 for( p=fmt;*p;p++){

    if(*p !='%'){

            Lcd_Chr_CP(*p);

            continue;

            }

    cntr++;

    settype = USE_uSIGN;

    switch(*++p){

         case'd':settype = USE_INT8; // signed short int8

         case'u':                    // unsigned short byte          

                 hval = va_arg(ap,char);

                 Lcd_Out_CP(itoa(hval));

                 break;

         case'i':settype = USE_INT16;  //signed int16                                                                                                                                                                                                                  

         case'U':                      // unsigned word

                 ival = va_arg(ap, int);

                 Lcd_Out_CP(itoa(ival));                            

                 break;                     

         case'c':                        // unsigned char

                 cval = va_arg(ap,char);

                 Lcd_Chr_CP(cval);

                 break;

         case'f':

                 break;//dval  = va_arg(ap,double);                            

               

         case's':

                 for(sval = va_arg(ap,char*);*sval;sval++)

                          LCD_Chr_CP(*sval);

                 break;

         default:

                 Lcd_Chr_CP(*p);

                 break;

         }

    }

  return cntr;

}                                                                                           

//////////////////////////////////////////////////////

 

  char TXT[] = "ELLIPSIS demo"; // Demo

 unsigned int WORD;  // unsigned 0 to 65535

 signed int INT16;   // signed -32768 to 32767

 char BYTE;          // unsigned short 0 to 255

 signed INT8 ;       // signed short -128 to 127

 /////////////////////////////////////////////////////

 /* main prog */

void main()

{

 /* functions prototype */

  char* itoa(unsigned int n); //take Integer return ANSI string

  char printf(const char* fmt,...); //standard ANSI printf

  /////////////////////////////////////////////////////

   trisb =0;

   portb =0;

   WORD = 32769;

   INT16 = WORD;  

   BYTE = 129;

   INT8 = BYTE;

   lcd_init(&portb);

   lcd_cmd(LCD_CURSOR_OFF);

 

   printf("PRINTF mikro%c",'C');

   lcd_cmd(LCD_SECOND_ROW);

   printf("%s",TXT);

  /* wait */

  delay_ms(2000);

  lcd_cmd(LCD_CLEAR);

  /* go on */

  lcd_cmd(LCD_FIRST_ROW);

  printf("%U EQU %i",(int)32769,INT16);

  lcd_cmd(LCD_SECOND_ROW);

  printf("%u EQU %d",BYTE,INT8);

}

  

 

 

Today, there have been 10 visitors (26 hits) on this page!
This website was created for free with Own-Free-Website.com. Would you also like to have your own website?
Sign up for free