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

problem with gethex function

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



Joined: 05 Jan 2006
Posts: 105

View user's profile Send private message

problem with gethex function
PostPosted: Mon Aug 06, 2007 5:33 pm     Reply with quote

hi
i want to get array of hex from rs232 using gethex function.i get them correctly, now i need to deal with each one of the array independent of other
i cant get hex in location 4(for example)>>>so what to do?
i hope you can understand me
here is my TX code:

Code:


#include <16f877a.h>
#fuses xt,NOWDT,NOLVP
#use delay(clock=4000000)    //one instruction=1us
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7,ERRORS)


int8 value;




void main()   {


again:

    delay_ms(5000);
    printf("01\r\n");
    delay_ms(100);
    printf("02\r\n");
    delay_ms(100);
    printf("3\r\n");
    delay_ms(100);
    printf("4\r\n");
    delay_ms(100);
    printf("*\r\n");
    delay_ms(100);
    printf("01\r\n");
    delay_ms(100);
    printf("&\r\n");
    printf("01\r\n");
    delay_ms(100);
    printf("02\r\n");
    delay_ms(100);
    printf("45\r\n");
    delay_ms(100);
    printf("02\r\n");

goto again;

}//MAIN



and RX code:

Code:


#include <16f877a.h>
#include <stdlib.h>
#fuses xt,NOWDT,NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#include <INPUT.C>

int RX[10] ,value[10];
static int1 flag;
static int8 i,n,f,t,address;



#int_rda
void serial_isr()  {
RX[i++]=gethex();

flag=1;
}

void main()
   {

   enable_interrupts(GLOBAL);
   enable_interrupts(INT_RDA);
   flag=0;
   i=0;
   while(1) {

   printf("%x\r\n",RX[i]);
   delay_ms(500);
   i++;
   if (flag==1)
   {
 //  for(i=0;i<11;i++)
//   {
//   value[i]= RX[i];
//   write_eeprom(address,value);
//   f++;
//   i++;
//   }//for
   flag=0;
   }//if

  /////////empty///////
   for(i=0;i<11;i++)
   {
   RX[i]=0;
   }//for

   }//while
   }//main



as it clear in rx code, in need to save each hex value in a location in eeprom
another point, when i send "*" i didn't receive it as hex!!!!
regards,
hadeel
Ttelmah
Guest







PostPosted: Tue Aug 07, 2007 2:49 am     Reply with quote

Of course you don't.
'Gethex', accepts two hex digits. It does not convert a value to hex for you. As such, it'll accept the characters 0 to 9, and A to F. As such, the first printf, sends the two digits '0' and '1', and this value (1), will be put into the first line of the array. However then things go wrong. 'Gethex', does not understand line feed and carriage return, so will try to treat these as hex digits, and because both are below '9' (in ASCII terms), it'll result in a garbage high value being put in the next line.
There are then loads of other potential problems. Using the same counter, inside the interrupt, and in the main code, will result in values being placed almost at random in the array. If the interrupt occurs inside the counting loop in main, i, could be at any value...
Then, the limit checking for the array is wrong. You allocate arrays of ten values, which are indexed as 0...9, yet allow i to count to 10...
Start by just sending one value. Modify gethex, or wite your own version, to see the carriage return as the end of the value. Add checking for values like '*' and '&' (what do you actually want to do with these?...). Once you have this working, then add the loop to send the multiple values (getting the counter right). Remembr that the write counter in the interrupt, should be separate from the output conter in main.

Best Wishes
hadeelqasaimeh



Joined: 05 Jan 2006
Posts: 105

View user's profile Send private message

PostPosted: Tue Aug 07, 2007 5:03 pm     Reply with quote

thank you Ttelmah, i modify my code (such as ex_intee)and i can get 10 hex ,but when save them in eeprom i get somthing strange
for example i send(010203040506070809)
i gethex each one,and write in eeprom,to make sure i try to read from eeprom and the result was:
aa
01
aa
02
aa
03
aa
04
aa
05
.
.
.
.
09
aa


but why?!!! Sad
Ttelmah
Guest







PostPosted: Wed Aug 08, 2007 2:04 am     Reply with quote

I'd suspect your counter.
Remember in your interrupt, you are counting _hex characters_, while in the EEPROM, you are counting _bytes_. The latter each hold two hex characters. Hence for the second 'gethex', if you are counting the incoming characters, the counter will be '2', while the address required in the eeprom, would be '1'. Hence you are writying the values to alternate locations in the eeprom, and leaving the ones between, containing whatever they had before...

Best Wishes
hadeelqasaimeh



Joined: 05 Jan 2006
Posts: 105

View user's profile Send private message

PostPosted: Wed Aug 08, 2007 5:38 pm     Reply with quote

hi Ttelmah
yes,thats right
so i try to use
Code:

if((i%2)==0)
adress++;


where i is hex counter
it works good up to 18 numbers then it get mixing agian

thank you Ttelmah very much
Ttelmah
Guest







PostPosted: Thu Aug 09, 2007 4:24 am     Reply with quote

Since there are 18 characters in the string you show, and if this is from a terminal package, this will be followed by a CR, or CR/LF, then this is presumably why it then goes wrong at this point...

Best Wishes
hadeelqasaimeh



Joined: 05 Jan 2006
Posts: 105

View user's profile Send private message

PostPosted: Fri Aug 10, 2007 6:01 pm     Reply with quote

hi Ttelmah
i make simulation on two pics
thank you, when i duplicate the counter its work fine,but there is question on my mind:
when i use ds1307(real time driver) may this affect rs232 intterupt (rda) work?i mean is any one of them disable the other???


thank you all
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