CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

PROBLEMS WITH POINTERS

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
RGG



Joined: 17 May 2011
Posts: 1

View user's profile Send private message

PROBLEMS WITH POINTERS
PostPosted: Thu May 19, 2011 4:20 am     Reply with quote

I have a problem with the pointers I sent to the functions. An example
is a simulation of the standard function sprintf (...) The definitive
function will not put in the Array BytesString the ASCCII values of
the variables but another series of data (to shown that it is not
working properly, is modified to translate the ASCII values to
BytesString).

If it is compiled for PIC16F877 or the PIC18F6722 everything works
perfectly, but it does not work if it is compiled for the PIC16F1936.
BytesString remains empty.

Apparently what is happening is that errors are introduced when the
address of BytesString are passed to the functions. I do not know why
since the pointers behave correctly in the other micros.

CCS ver: PCWHD 4.110 & 4.114
MPLAB ver: 8.6 & 8.7
Code:

///////////////////////////// FILE ssprintf.c ///////// /////////////
#include <stdarg.h>
typedef void (*putcf) (void*,char);

void uli2a(unsigned long int num, unsigned int base, int uc,char * bff){
   long int nn=0;
   unsigned long int dd=1;
   while (num/dd >= base)
      dd*=base;
   while (dd!=0) {
      int dgt = num / dd;
      num%=dd;
      dd/=base;
      if (nn || dgt>0|| dd==0) {
         *bff++ = dgt+(dgt<10 ? '0' : (uc ? 'A' : 'a')-10);
         ++nn;
         }
      }
   *bff=0;
}
/////////////////////////////////////////////////////
void li2a (long num, char * bff){
   if (num<0) {
      num=-num;
      *bff++ = '-';
      }
   uli2a(num,10,0,bff);
}
/////////////////////////////////////////////////////
void put2Hex(unsigned long int numero, void* putpv){
//char fcc;
unsigned long int nmm;
unsigned int nm;
      //   fcc = *putp;
   putpv++;
   nmm = numero;
   nm = (nmm >>=8);
   *putpv = nm;
   putpv++;
      //   *fcc = nm;
      //   fcc++;
      //   *putp = fcc;

   nmm = numero;
   nm = (nmm &= 0x00FF);
*putpv = nm;
      //   *fcc = nm;
      //   fcc++;
      //   *putp = fcc;

putpv++;

   delay_us(2);
   delay_us(2);
}
/////////////////////////%X y %u (Unsigned int) /////////////////////////////////////////
void putHex(unsigned int numero, void* putp){
unsigned char* fc;
   fc = *putp;
   *fc = numero;
   fc++;
   *putp = fc;
}

void ui2a(unsigned int num, unsigned int base, int uc,char * bff){
   int nn=0;
   unsigned int dd=1;
   while (num/dd >= base)
      dd*=base;
   while (dd!=0) {
      int dgt = num / dd;
      num%= dd;
      dd/=base;
      if (nn || dgt>0 || dd==0) {
         *bff++ = dgt+(dgt<10 ? '0' : (uc ? 'A' : 'a')-10);
         ++nn;
         }
      }
   *bff=0;
}
//////////////////////////////////////////////////
void i2a (int num, char * bff){
   if (num<0) {
      num=-num;
      *bff++ = '-';
      }
   ui2a(num,10,0,bff);
}

////////////////////////////////////////////////
int a2d(char ch)
   {
   if (ch>='0' && ch<='9')
      return ch-'0';
   else if (ch>='a' && ch<='f')
      return ch-'a'+10;
   else if (ch>='A' && ch<='F')
      return ch-'A'+10;
   else return -1;
}
/////////////////////////////////////////////////////
/////////////////////////////////////////////////////
char a2i(char ch, char** src,int base,int* nump){
   char* pp= *src;
   int32 num=0;
   int digit;
   for(digit=a2d(ch);digit>=0;digit=a2d(ch)){
//   while ((digit=a2d(ch))>=0) {
      if (digit>base) break;
      num=num*base+digit;
      ch=*pp++;
   }
   *src=pp;
   *nump=num;
   return ch;
   }

/////////////////////////////////////////////////////
void putchw(void* putp,putcf putf,int nnn, char zz, char* bff){
   char fc=zz? '0' : ' ';
   char ch;
   char* pp=bff;
   while (*pp++ && nnn > 0)
      nnn--;
   while (nnn-- > 0)
      putf(putp,fc);
   while ((ch= *bff++))
      putf(putp,ch);

   }
//////////////////////////////////////////////////////////////////
void tfp_format(void* putp,putcf putf,char *fmt, va_list va){
char bff[12];
char ch;
while ((ch=*(fmt++))) {
      if (ch!='%')
         putf(putp,ch);
      else {
         char lz=0;
         char lng=0;

         int ww=0;
         ch=*(fmt++);
         if (ch=='0') {
            ch=*(fmt++);
            lz=1;
         }
         if (ch>='0' && ch<='9') {
            ch=a2i(ch,&fmt,10,&ww);
         }

         if (ch=='l') {
            ch=*(fmt++);
            lng=1;
         }

         switch (ch) {
            case 0:
               goto abort;
            case 'u' : {

               if (lng)
                  uli2a(va_arg(va, unsigned long int),10,0,bff);
               else
                  ui2a(va_arg(va, unsigned int),10,0,bff);
                  putchw(putp,putf,ww,lz,bff);
               break;
               }
            case 'd' :  {
               if (lng)
                  li2a(va_arg(va, unsigned long int),bff);
               else
                  i2a(va_arg(va, int),bff);
                  putchw(putp,putf,ww,lz,bff);
               break;
               }
            case 'x': case 'X' :

               if (lng)
                  uli2a(va_arg(va, unsigned long int),16,(ch=='X'),bff);
               else
                  ui2a(va_arg(va, unsigned int),16,(ch=='X'),bff);
                  putchw(putp,putf,ww,lz,bff);
               break;
            case 'c' :
               putf(putp,(char)(va_arg(va, int)));
               break;
            case 's' :
               putchw(putp,putf,ww,0,va_arg(va, char*));
               break;
            case '%' :
               putf(putp,ch);
            default:
               break;
            }//switcch
         }//      else ch='%')
   }//while
   abort:;
}

void putcp(void* pp,char cac){
   *(*((char**)pp))++ = cac;
}

void ssprintf(char* ss,char *fmt, ...){
   va_list vaa;
   va_start(vaa,fmt);
   tfp_format(&ss,putcp,fmt,vaa);
   putcp(&ss,0);
   va_end(vaa);
}

////////////////////////   FILE MAIN //////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////
//                                                          ///
//                      PIC "C" Compiler CCS                          ///
/////////////////////////////////////////////////////////////////////////
#define compiler __pch__

//#include <16f877.h>
//#include <16f1936.h>
#include <18f6722.h>

#device *=16
#device ICD = TRUE


//#fuses INTRC_IO,NOWDT,NOPROTECT,NOPUT,NOBROWNOUT,NOLVP,MCLR,PLL_SW

#use delay(clock=16000000)   //OSCCON   = 0b01111111;

#include <stdarg.h>

unsigned int8 VarHex[8];
unsigned int16 Ccad16x;
char TemporalX[25],BytesString[40];

#include    "ssprintf.c"

///*********************************************************************
//  M A I N   P R O G R A M     f o r    T E S T
//*********************************************************************
#ZERO_RAM
void main(void){

    Ccad16x = 0xFFFE;
    strcpy(VarHex,"Hola2");
    strcpy(TemporalX,"%ld");
   
    ssprintff(BytesString,TemporalX,Ccad16x);

   delay_cycles(2);
   delay_cycles(2);
   delay_cycles(2);

} ///END
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu May 19, 2011 12:53 pm     Reply with quote

Cut the program down to 1/10 of that size. Then it would be easier for
us to solve the problem.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group