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

Processing received data over RS232

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



Joined: 13 Sep 2005
Posts: 4

View user's profile Send private message

Processing received data over RS232
PostPosted: Tue Oct 04, 2005 9:08 am     Reply with quote

Hi to everyone, I need to ask for some help.
I'm building a weather datalogger. This portable unit
must be conect to a PC to downloading data (this works ok),
but the PC has to send some information: the actual time and date.

The circuit has a RTC (PCF8563), connected to the PIC (16F876A).
The PIC must receive one string send by the PC (running a custom program
made with LabView), and decode the content's of the string to set the
time and date registers of the RTC.

To be more specific: The PC send: 115423\r
The PIC need to process that, so they finalize with: Hours_reg=11; Minutes_reg=54; Seconds_reg=23;

Well, I no have ANY idea in how to archieve this with CCS compiler.

Since this is the first time that I'm receiving data, I has other questions: -from my reading trought the forum-
In my case, is better use getc or gets?
There's trully any sort of interference between LCD routines and received data interruption?
Why this not work?

Code:

if (data_ready) //data_ready is set in INT_RDA, after succefully received a string who are "return terminated"
data_ready=false;
int i; //i have to put this definition outside of main() -the compiler gives an error-, why?   
for (i=o;i==index;i++) //index has the value of total characters readed
                                  //i know that the equality has anything to do in    the problem
putc(receive_buffer[i]) //receive buffer has the string readed


Thank's a lot to every that could help, ok?
Best Regards
ckielstra



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

View user's profile Send private message

PostPosted: Tue Oct 04, 2005 10:52 am     Reply with quote

Quote:
int i; //i have to put this definition outside of main() -the compiler gives an error-, why?
The older K&R style C-compilers didn't allow to put the declaration for i inside the for-loop declaration. Newer C++ compilers do allow this construction, maybe ANSII compilers allow it too. CCS however is more like the old K&R style compilers. You'll have to learn to live with it.

Quote:
for (i=o;i==index;i++)
Try to use a zero '0' instead of a 'o'.
Also, for safety, it's better to use '<=' instead of '=='.

In general I like getc() better than gets() because gets() will be blocking until it reads a newline character. In case of bad transmission quality it is possible to miss the newline character and you will be overwriting buffers or waiting forever, getc() will give you more control.
diegonet



Joined: 13 Sep 2005
Posts: 4

View user's profile Send private message

some more help, please!
PostPosted: Tue Oct 04, 2005 10:55 pm     Reply with quote

ckielstra, thank you for your fast response.
the "o" in the code is just a error (I wrote this code for the post, not copy&paste).

"Also, for safety, it's better to use '<=' instead of '=='. "
Right, but if a want to use, why donīt work?
I have a remote ide that CCS gives an erro, like "tryng to do an asignation in a statement" o something like this (the error only appears if you compile with a external tool (like MPLAB).

But my really and URGENT problem, is how to convert an string into several numbers, like: 123456\r into 12, 34 and 56.

Any help please!
Thank's
Diego
Foppie



Joined: 16 Sep 2005
Posts: 138
Location: The Netherlands

View user's profile Send private message Send e-mail Visit poster's website MSN Messenger

PostPosted: Wed Oct 05, 2005 12:49 am     Reply with quote

the following code converts a character into an integer:
Code:
char c = '1';
int i = 0;

i = c - '0';


it uses the ASCII value of the character.

If you put it in a for-loop you can evaluate a whole string.

Good luck
Guest








PostPosted: Wed Oct 05, 2005 7:24 am     Reply with quote

Code:

char c = '1';
int i = 0;

i = c - '0';

// for next character.

char c = '2';
int j = 0;
j = c - '0';

// For calculating time

time = (i*10) + j ;  // for twelve
diegonet



Joined: 13 Sep 2005
Posts: 4

View user's profile Send private message

Made some progress
PostPosted: Wed Oct 05, 2005 11:15 pm     Reply with quote

Hi people, i'm posting the resulting code for the latest post's.

Code:

#include "test.h"
#include "lcd.c"
#include "pcf8563.h"

#define CR 0x0d

const char weekday [7] [10] = { "domingo",
         "lunes",
         "martes",
         "miercoles",
         "jueves",
         "viernes",
         "sabado",   };

int segundos, minutos, horas;
int h,m,s;
int dia, mes;
long ano;
int wd;

short time_correct;

short Dato_recibido = 0;
int Indice = 0;
char leido[6] = {0,0,0,0,0,0};


//////////////////////////////////////////////////////////////////////

#INT_RDA
void rs232_isr(void)
{
   char temp; // local data storage

   temp = getc(); // get rx data

      if (temp == CR)
      {
         Dato_recibido = TRUE;
         indice=0;
         return;
      }

      leido[Indice]=temp;
      Indice++;
      if (Indice >= 6) Indice=0;

}

///////////////////////////////////////////////////////////////////////

void main()
{
   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   delay_ms(250);

   lcd_init();

enable_interrupts(INT_RDA);
enable_interrupts(GLOBAL);
output_float(PIN_C4);
output_float(PIN_C3);

segundos= 0;
minutos = 0;
horas = 0;

dia=3;
mes=10;
ano=5;
wd=3;

pcf8563_write_config();
pcf8563_write_time(horas,minutos,segundos,1);
pcf8563_write_date(dia, wd, mes, ano, 1);

while(true)
{

dia = pcf8563_read_day(1);
mes= pcf8563_read_month(1);
ano=pcf8563_read_year(1)+2000;
wd=pcf8563_read_weekday();

segundos = pcf8563_read_seconds(1);
minutos = pcf8563_read_minutes(1);
horas = pcf8563_read_hours(1);

if (time_correct)
{
printf(" h:%U m:%U s:%U \n", horas, minutos, segundos);
printf(" d:%U me:%U a:%Lu wd:%S\n", dia, mes, ano, weekday[wd]);
delay_ms(1000L);
}else {printf("\nDebe ajustar la hora");
delay_ms(1000L);}

if (Dato_recibido)
{
                int digito1;
   int digito2;

   disable_interrupts(GLOBAL);

   Dato_recibido=false;

   digito1= (leido[0] - '0')*10;
   digito2= leido[1] - '0';
   h=digito1+digito2;
   digito1= (leido[2] - '0')*10;
   digito2= leido[3] - '0';
   m=digito1+digito2;
   digito1= (leido[4] - '0')*10;
   digito2= leido[5] - '0';
   s=digito1+digito2;

   pcf8563_write_time(h,m,s,1);

                time_correct=true;
   enable_interrupts(GLOBAL);

}

}

}


Well, this code read an RTC PCF8563 and send the time an date over RS-232. Also it must read an string coming over serial port and adjust the internal time registers of the RTC.

The actual behavior, is a little strange. The following is a capture of Docklight (RS-232 monitoring soft):

Debe ajustar la hora //this message tolds that the time is not set
06/10/2005 02:08:21.29 [TX] - 124532<CR> //I send the string
06/10/2005 02:08:21.62 [RX] -
h:32 m:12 s:45 <LF> //the results are wrongs...but if you look well, there are all the string but in WRONG ORDER!
d:3 me:10 a:2005 wd:miercoles<LF>
h:32 m:12 s:46 <LF>
d:3 me:10 a:2005 wd:miercoles<LF>

06/10/2005 02:08:23.34 [TX] - 124532<CR> //the strange thing, if I send the string againg
06/10/2005 02:08:23.69 [RX] -
h:12 m:45 s:32 <LF> //ALL WORK NOW!!!
d:3 me:10 a:2005 wd:miercoles<LF>
h:12 m:45 s:33 <LF>
d:3 me:10 a:2005 wd:miercoles<LF>

As you can see, the first time that I send the string, it's processed in the wrong order, all the consecutives trigns (may be different ones), are sucefully procesed.

I we will be wait for your gently help!!!
Best Regards,
Diego
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