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 support@ccsinfo.com

How to rotate a string ?

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



Joined: 16 Sep 2003
Posts: 71
Location: Moers, Germany

View user's profile Send private message

How to rotate a string ?
PostPosted: Mon Nov 24, 2003 8:27 am     Reply with quote

Hello to all,
I would like to monitor the status of a button via RS232. The character for high (button pressed) should be '-' and for low '_'. So that I can see on the terminal for example: ___-_-_-_-____
But I see : nnnnpnpnpnpnpnnnnn

Please have a look to the code:

Code:

#include <18f452.h>
#use delay (clock=20000000)
#use RS232(baud=19200, xmit=PIN_C6, rcv=PIN_C7)

long lStartUp = 3017;

struct {
   short int Button_1; // RC0
   short int Button_2; // RC1
   short int Button_3; // RC2
   short int Button_4; // RC3
   short int unused : 4; // RC4-7
} port_c;
#byte port_c = 0xF82

char sButton_1[31] = "______________________________";
char sLow[2] = "_";
char sHigh[2] = "-";




#int_timer1
timer1_isr() {

   int ni;

   setup_timer_1(T1_DISABLED);
   set_timer1 (lStartUp);
   setup_timer_1(T1_INTERNAL | T1_DIV_BY_8);

   for (ni=0;ni<=29;ni++) {
      sButton_1[ni] = sButton_1[ni+1];
   }

   if (port_c.Button_1 == 0) {
         sButton_1[30] = sHigh;
   }
   else {
         sButton_1[30] = sLow;
   }

}




void main()   {

   set_timer1(lStartUp);
   setup_timer_1(T1_INTERNAL | T1_DIV_BY_8);
   enable_interrupts(INT_TIMER1);
   enable_interrupts(GLOBAL);

   putc(12);

   For (;;) {

      printf("Status of button 1: %s", sButton_1);
                }

}



I think the mistake is in the isr where I rotate the characters.
I heard lots about pointers but can't use them. Embarassed
Is there an easier way for that problem?

Thank you very much for any help

73 Sven
KerryW
Guest







PostPosted: Mon Nov 24, 2003 9:05 am     Reply with quote

When you define this:

char sLow[2] = "_";
char sHigh[2] = "-";

And use this:
buff[10]=sLow;

you are using the address of sLow, not its value.

Change the definition to:

char sLow = '_';
char sHigh = '-';
And use this:
buff[10]=sLow;

or use
char sLow[2] = "_";
char sHigh[2] = "-";

sLow[0] when you want a '_' character and sHigh[0] when you want a '-' character.

The first one (char sLow = '_'; ) will use less memory.
wedilo



Joined: 16 Sep 2003
Posts: 71
Location: Moers, Germany

View user's profile Send private message

PostPosted: Tue Nov 25, 2003 2:56 am     Reply with quote

Hello KerryW,

I can't understand why, but it works. Very Happy
Thank you very much

73 Sven
KerryW
Guest







Understanding pointers and arrays is crucial.
PostPosted: Tue Nov 25, 2003 2:00 pm     Reply with quote

If you don't understand them, these problems will occur again and again.

Here is a thumbnail sketch of how it works.

char a; // location=0, 1 byte
int b; // location=1, 2 bytes
float c; //location 3, 4 bytes.
char *pa; // location 7, 1 byte
int *pb; // location 8, 1 byte
float *pc; // location 9, 1 byte
char x[2]; // location 10, 2 bytes
int y[2]; // location 12, 4 bytes
float z[2]; // location 16, 8 bytes

a=100; // location 0 contains 100
b=101; // location 1 contains 0, location 2 contains 101
y[0]=0x123; // location 12 contains 0x23, location 13 contains 0x01
pa=&a; // ‘&a’ mean ‘address of a’, or 0. Location 7 contains 0
pb=&y[0]; // location 8 contains 12
pb=y; // a synonym for ‘pb=&y[0]’, location 8 still contains 12.
*pb=102; // ‘*pb’ means ‘the location whose address is in location 8’. Location 8 contains 12, so it puts 0 in 12 and 102 in 13, because the compiler knows that pb points to a 2 byte integer.
pb++; // Location 8 was 12, but since it points to a 2 byte number, the compiler adds 2 instead of 1, so location 8 now contains 14.


If this doesn't do it for you, let me know. It's hard to explain in just a few lines.
wedilo



Joined: 16 Sep 2003
Posts: 71
Location: Moers, Germany

View user's profile Send private message

PostPosted: Thu Nov 27, 2003 2:18 am     Reply with quote

Hello KerryW,
I was surprised about so much information. I will work through your message and will going to understand it completely.
It's hard stuff, but your are right: "it will occur ever and ever"

Thank you very much for your response Very Happy

73 Sven
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

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

PostPosted: Thu Nov 27, 2003 9:24 am     Reply with quote

KerryW,

CCS is a little different that most compilers:

int b in CCS is 1 byte
long c is 2 bytes
int32 is 4 bytes

Mark
KerryW
Guest







My compiler is old.
PostPosted: Thu Nov 27, 2003 10:24 am     Reply with quote

I'm using something like 2.7?

I don't even have int32, although I knew that the newer ones had it. I wasn't sure how they had implemented if, if they had made int a 16 bit and long a 32, or what. I'm also not sure in they use high byte/low byte or low byte/high byte format. (I knew at one time, but I never remeber such things. If it becomes important, I just take a peek and see)

So many things to forget, and so little time. Wink
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