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

How to convert a char to an int ?

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



Joined: 30 Sep 2004
Posts: 14

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

How to convert a char to an int ?
PostPosted: Thu May 05, 2005 6:00 am     Reply with quote

I am receiving ASCII characters from the RS232 receive port.

An interrupt occurs to store the received string.

However, to calculate the checksum I need the received ASCII characters to be converted to an int.

The two lines of code....

Code:
EvenChkSum=RxBuffer[r]^EvenChkSum;


and

Code:
OddChkSum=RxBuffer[r]^OddChkSum;


keep producing the value 0x00.

Presumably this is because the array RxBuffer[] is declared as a char.

So How can I convert the char to an int ?

My program so far looks like this.....

Code:

#include <16C74.h>
#device adc=8
#use delay(clock=20000000)
#fuses NOWDT,HS, PUT, NOPROTECT
#use rs232(baud=115200,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8, BRGH1OK)

#define Odd    0
#define Even   1

int i;
int Get_Version[16]={0x1E,0x00,0x0C,0xD1,0x00,0x07,0x00,0x01,0x00,0x03,0x00,0x01,0x60,0x00,0x72,0xD5};
int r=0;
char RxBuffer[56];
int OddChkSum=0;
int EvenChkSum=0;
int1 ChkSumByte=Even;

#int_RDA
RDA_isr()
{
RxBuffer[r++]=getchar();

   if (ChkSumByte==Even)
   {
      EvenChkSum=RxBuffer[r]^EvenChkSum;
      ChkSumByte=Odd;
   }
   else
   {
      OddChkSum=RxBuffer[r]^OddChkSum;
      ChkSumByte=Even;
   }
}

void main()
{

   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   setup_psp(PSP_DISABLED);
   setup_spi(FALSE);
   setup_counters(RTCC_INTERNAL,RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   enable_interrupts(INT_RDA);   // enable RS232 receive data interrupt
   enable_interrupts(GLOBAL);      // enable all global interrupts

   for (i=0;i<=56;i++)
   {
      RxBuffer[i]=0;
   }

   // initialise phones UART by sending 0x55 (ASCII 'U') 128 times
   for (i=0;i<128;i++)
   {
      putc(0x55);
   }
   delay_ms(10);
   for (i=0;i<=15;i++)
   {
      putc(Get_Version[i]);
   }

   do
   {
   }
   while(TRUE);

}
rwyoung



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

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

PostPosted: Thu May 05, 2005 7:23 am     Reply with quote

Code:
EvenChkSum=RxBuffer[r]^EvenChkSum;

becomes
Code:
EvenChkSum=(int)RxBuffer[r]^EvenChkSum;


But in your case, "int" is still 8-bits, same as "char".

Look at the LST file created by your compiler and check for problems there. If necessary you can use an intermediate variable to hold the buffer value if the pointer dereferncing is causing a problem.

By the way, I don't see where you are preventing your index variable "r" from running past the end of your RxBuffer array. You should have some kind of limit test or use a power-of-2 length array and do some modulus arithmetic to get "r" to roll around.
_________________
Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month!
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Sun May 08, 2005 8:48 am     Reply with quote

Code:
#int_RDA
RDA_isr()
{
RxBuffer[r++]=getchar();

   if (ChkSumByte==Even)
   {
      EvenChkSum=RxBuffer[r]^EvenChkSum;
      ChkSumByte=Odd;
   }
   else
   {
      OddChkSum=RxBuffer[r]^OddChkSum;
      ChkSumByte=Even;
   }
}
The 'r++' is causing your problems. A few lines later in the code your are refering to RxBuffer[r] which now points to an undefined value, by chance this was always zero in your tests.

Another bug:
Code:
   for (i=0;i<=56;i++)
   {
      RxBuffer[i]=0;
   }
You are clearing 57 bytes in a 56 byte RAM buffer...... Unpredicatble results will haunt you !
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