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

printf("%wt",varname) doubt

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



Joined: 09 Dec 2003
Posts: 13

View user's profile Send private message

printf("%wt",varname) doubt
PostPosted: Mon Jan 26, 2004 3:33 pm     Reply with quote

I'm using the next variable (reg_canales):

struct canales
{
int Cont[2];
short DI;
int Reg;
long Moves;
long Error;
}reg_canales [4];

I want to send the data through RS-232, but I don't know how to send a variable type long.
I tried that:
printf(%2LX%2LX",aux,reg_canales[canal].Moves,reg_canales[canal].Error);

Could anybody of you help me?
Thanks in advance
rwyoung



Joined: 12 Nov 2003
Posts: 563
Location: Lawrence, KS USA

View user's profile Send private message Send e-mail

PostPosted: Mon Jan 26, 2004 3:45 pm     Reply with quote

Code:
#case
#include <16f877a.h>

#fuses HS, NOLVP, NOWDT, PUT
#use delay (clock=20000000,RESTART_WDT)
#use rs232(baud=19200,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,stream=OUT232)

struct canales
{
int Cont[2];
short DI;
int Reg;
long Moves;
long Error;
}reg_canales [4];

void main(void)
{
   reg_canales[0].Moves = 10l; // test data, would appear as 000A
   reg_canales[0].Error = 20l;  // test data, would appear as 0014

   puts("Starting test...");

   printf("%4LX%4LX",reg_canales[0].Moves,reg_canales[0].Error);
   puts("\r\nDone.");
   while(TRUE) {
   }
}


This works just fine with V1.384 and a PIC16F877A.

The first problem I saw with your code was a missing leading " in your print statement. Then you are only allowing 2 characters for displaying what should be a 4 character wide hexidecimal number.
_________________
Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month!
Bomba



Joined: 09 Dec 2003
Posts: 13

View user's profile Send private message

More info needed
PostPosted: Mon Jan 26, 2004 4:48 pm     Reply with quote

I don't understand, because i want to send through rs-232 only the value in the memory, I�m not interested in the representation.... The only things that i understood that i don�t understand the statement printf Embarassed . Could you explain me or recommend something to read that open my main.
Well I'm using ccsv3.181 and to simulate proteus 6.3sp1, to visualises the output rs-232 I�m using the virtual terminal. For example if the number allocated into memory like long type is 0x0000, using the sentence that you provided me, I see in the virtual terminal 90 90 90 90

Thanks
rwyoung



Joined: 12 Nov 2003
Posts: 563
Location: Lawrence, KS USA

View user's profile Send private message Send e-mail

PostPosted: Tue Jan 27, 2004 8:49 am     Reply with quote

From the CCS manual:

PRINTF( )
FPRINTF PRINTFFPRINTF()

Syntax:
printf (string)

or

printf (cstring, values...)

or

printf (fname, cstring, values...)

fprintf (stream, cstring, values...)


Parameters:
String is a constant string or an array of characters null terminated. Values is a list of variables separated by commas, fname is a function name to be used for outputting (default is putc is none is specified). Stream is a stream identifier (a constant byte)

Returns:
undefined

Function:
Outputs a string of characters to either the standard RS-232 pins (first two forms) or to a specified function. Formatting is in accordance with the string argument. When variables are used this string must be a constant. The % character is used within the string to indicate a variable value is to be formatted and output. Longs in the printf may be 16 or 32 bit. A %% will output a single %. Formatting rules for the % follows.

If fprintf() is used then the specified stream is used where printf() defaults to STDOUT (the last USE RS232).

Format:

The format takes the generic form %wt where w is optional and may be 1-9 to specify how many characters are to be outputted, or 01-09 to indicate leading zeros or 1.1 to 9.9 for floating point. t is the type and may be one of the following:

C Character
S String or character
U Unsigned int
x Hex int (lower case output)
X Hex int (upper case output)
D Signed int
e Float in exp format
f Float
Lx Hex long int (lower case)
LX Hex long int (upper case)
lu unsigned decimal long
ld signed decimal long
% Just a %

Example formats:

Specifier
Value=0x12 Value=0xfe

%03u 018 254
%u 18 254
%2u 18 *
%5 18 254
%d 18 -2
%x 12 Fe
%X 12 FE
%4X 0012 00FE

* Result is undefined - Assume garbage.

Availability:
All Devices

Requires
#use rs232 (unless fname is used)

Examples:
byte x,y,z;

printf("HiThere");

printf("RTCCValue=>%2x\n\r",get_rtcc());

printf("%2u %X %4X\n\r",x,y,z);

printf(LCD_PUTC, "n=%u",n);

Example Files:
ex_admm.c, ex_lcdkb.c

Also See:
atoi(), puts(), putc(), getc() (for a stream example)


You string contained "%2LX" which means print a long integer in hex format using capital letters and only allow 2 characters. Depending on the implementation of printf and the compiler this would either be allowed or flagged with a warning. I think CCS allows it even though a long integer is 16 bits in the CCS compiler and would need 4 characters to properly display.

If you don't want to see the ASCII text representing you number but rather want exactly what is in memory then don't use printf. Use putc or fputc. In the case of a long int, you will need to combine the putc with make8

Code:

int16  my16bitvalue;

my16bitvalue = 1234l;

// send as binary
putc(make8(my16bitvalue,1)); // send msbyte
putc(make8(my16bitvalue,0)); // send lsbyte

// send as text, hex format
printf("my16bitvalue = %4LX", my16bitvalue);

_________________
Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month!
Bomba



Joined: 09 Dec 2003
Posts: 13

View user's profile Send private message

I'm using 16f84a
PostPosted: Tue Jan 27, 2004 9:55 am     Reply with quote

well I prove all that you say, but the data that I see in the virtual terminal still showing wrong.
Maybe is something asociated qith prooteus 6.3sp1 virtual terminal, or with the pic16f84a.

Please see them:

#include <16F84A.h>
#use delay(clock=18432000)
#fuses NOWDT,HS, PUT, NOPROTECT
#use rs232(baud=9600,parity=N,xmit=PIN_A2,rcv=PIN_A3,bits=8,stream=OUT232)

#use fast_io(B)
#zero_ram

struct canales
{
int Cont[2];
short DI;
int Reg;
long Moves;
long Error;
}reg_canales [4];

void Verificar(int canal)
{
int aux=0;

if (reg_canales[canal].Reg==Vuelta)
{
if (reg_canales[canal].DI) aux=252;
aux|=canal;
putc(aux);
putc(make8(reg_canales[canal].Moves,0));
putc(make8(reg_canales[canal].Moves,1));
reg_canales[canal].Reg=0;
reg_canales[canal].Moves=0;
reg_canales[canal].Error=0;
}
}


where canal = 0 to 3

well I'm thinking now that maybe the problem is in virtual terminal.... Sad

thank you for all...
rwyoung



Joined: 12 Nov 2003
Posts: 563
Location: Lawrence, KS USA

View user's profile Send private message Send e-mail

PostPosted: Tue Jan 27, 2004 11:12 am     Reply with quote

I don't have a PIC16F84 to test with so I used one of my PIC16F877A parts and tested this code:

Code:
#include <16F877A.h>
#fuses HS, NOLVP, NOWDT, PUT
#use delay (clock=20000000,RESTART_WDT)
#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7)

#zero_ram

struct canales
{
int Cont[2];
short DI;
int Reg;
long Moves;
long Error;
}reg_canales [4];

void main(void)
{
   int i;

   // load structure with dummy data
   for(i=0;i<4;i++)
   {
      reg_canales[i].Cont[0] = 1;
      reg_canales[i].Cont[1] = 2;
      reg_canales[i].DI = i;
      reg_canales[i].Reg = i;
      reg_canales[i].Moves = (long)(i*10);
      reg_canales[i].Error = (long)(i*20);
   }
   for(i=0;i<4;i++)
   {
      putc(i);
      delay_ms(1);
      putc(make8(reg_canales[i].Moves,0));
      delay_ms(1);
      putc(make8(reg_canales[i].Moves,1));
      delay_ms(1);
   }
   // let PIC sleep
}


I captured the output with the CCS serial port monitor and saw these bytes:

00 00 00 01 0A 00 02 14 00 03 1E 00


This is the correct output. One important note is I have added a few extra delay_ms() calls to the code. I have found that some PC serial port routines do not capture streaming data well and need some dead space between bytes. The CCS serial port monitor is one of those programs. It may be a programming issue on their part, it may be the FIFOs on this PC, it may be the PIC. I can't say for certian but I can say that this code works with a PIC16F877A.

One other recomendation I would make to you is to try to upgrade to a more "modern" chip. The PIC16F628A or PIC16F88 make good replacements for the F84. The F628A is cheaper and has much better built-in peripherials than the F84.
_________________
Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month!
Bomba



Joined: 09 Dec 2003
Posts: 13

View user's profile Send private message

I'm so stupid
PostPosted: Tue Jan 27, 2004 4:12 pm     Reply with quote

Dear rwyoung:
Embarassed Embarassed
Well; I couldn�t believe, the errors was differences between the clock setting in the #use delay CCS, and the clock setting in proteus PIC. I spent 2 days with this....


Thanks you very much for your time....
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