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

12-Bit ADC value with RS232

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



Joined: 06 Dec 2015
Posts: 42

View user's profile Send private message

12-Bit ADC value with RS232
PostPosted: Thu Nov 14, 2019 9:01 am     Reply with quote

Hi,

I am trying to send set of 12-bit ADC values via rs232. In previous attempt I was using printf() function however it has some delay issues. Now I'm using make8() function to split the value into 2 bytes. The offset in the make8() function, does it offset from the MSB or LSB? Is it possible if I want to send # when sending the MSB byte i.e #010111010111 using the putc() function? I need a marker to identify the receiving value is MSB @ LSB. This is the code:-

arelone

Code:

#include <30F6014A.h>
#device ADC=12
#include <float.h>
#include <string.h>
#fuses NOWDT, NOPROTECT, PUT64, BORV27
#use delay (clock=58982400, xtal=7372800)
#use rs232(baud=9600,UART1,ERRORS)



void main(void)                     //void=nothing, no return value
{
   setup_adc_ports(ALL_ANALOG);
   setup_adc_ports(sAN0 |sAN1|sAN2|sAN3|sAN4|sAN5|sAN6|sAN7|sAN8|sAN9|sAN10|sAN11|sAN12|sAN13|sAN14|sAN15, VSS_VDD);
   setup_adc(ADC_CLOCK_DIV_64 | ADC_TAD_MUL_8);
   
   int8 channel[]=5,6,7,8,9,10,11,12,13;
   int16 ADC_value[sizeof(channel)];
   int8 value;
   int MSB,LSB;
         
   while(true)
  {
   output_low(PIN_D7);
   for (value=0;value<sizeof(channel);value++)   
   {
      set_adc_channel(channel[value]);
      delay_us(10);               
      ADC_value[value]=read_adc();
     
   }
   output_toggle(PIN_D7);
   for (value=0;value<sizeof(channel);value++)
   {
      MSB=make8(ADC_value[value],1);
      LSB=make8(ADC_value[value],0);
      putc(MSB);
      putc(LSB);
   }
     
  }
   
}
temtronic



Joined: 01 Jul 2010
Posts: 9081
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Nov 14, 2019 9:27 am     Reply with quote

Code:
for (value=0;value<sizeof(channel);value++)
   {
      MSB=make8(ADC_value[value],1);
      LSB=make8(ADC_value[value],0);

//simply add this ?

     printf("MSB--LSB");

//as your marker ?

      putc(MSB);
      putc(LSB);
   }
     
  }
newguy



Joined: 24 Jun 2004
Posts: 1899

View user's profile Send private message

PostPosted: Thu Nov 14, 2019 9:38 am     Reply with quote

A UART cannot send anything other than its configured "word size" which is normally 8 bits. I do believe it's possible to configure the UART to send 9 bits at a time and perhaps 7, but 12 is not possible.

You want to send binary data but you also want to be able to somehow mark the data as being the MSB of sample 3 which is different from the LSB of sample 5. You have two major options:
- Use a "reserved character" to mark the beginning of a transmission. The issue with a reserved character is that if your data also happens to contain the reserved character, it must be replaced with an "escape sequence".
- Start your transmission with a series of characters, and always use the same pattern. This will add quite a bit of overhead and may be a problem if you are sampling at a high rate (and thus also transmitting at a high rate).

Reserved character: as an example, say that your starting character would be the ASCII letter 'S', which is 0x53. Start by sending 0x53, then send your data. However, before you actually transmit a byte, you must first check to see if it happens to be 0x53. If it is, you need to send an "escape sequence" instead, because 0x53 is reserved - it can only be present at the start of a transmission. An example of an escape sequence would be:
- 0x1b 0x1b = one byte of 0x1b
- 0x1b 0x1c = 0x53
arelone



Joined: 06 Dec 2015
Posts: 42

View user's profile Send private message

12-Bit ADC value with RS232
PostPosted: Thu Nov 14, 2019 9:39 am     Reply with quote

Hi,

Thanks for the advice. However I prefer not to use printf() function as it cause additional delay in the output. I am thinking of adding at least '#' in the MSB byte. Not sure if this can be done using putc() or any other functions (not printf()). I am using CCS C V5.010. Very Happy

arelone
temtronic



Joined: 01 Jul 2010
Posts: 9081
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Nov 14, 2019 9:44 am     Reply with quote

...
putc("#");
putc(MSB);
putc(LSB);
...


also if you press F11 ,the CCS manual should open for you ! I leave it open as I can't remember syntax or proper names of functions, getting old I suppose.

Jay
arelone



Joined: 06 Dec 2015
Posts: 42

View user's profile Send private message

PostPosted: Thu Nov 14, 2019 9:49 am     Reply with quote

Hi Mr.New Guy,

Thanks for the reply. According to your advice I would be appreciate if you can give examples code for me to try. Seriously... I am not a good programmer.
I'm sending the 2 bytes to labview, combine the 2 bytes into a single integer. At least I know the received byte is MSB @ LSB.

Arelone
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Nov 14, 2019 9:57 am     Reply with quote

temtronic wrote:
...
putc("#");
putc(MSB);
putc(LSB);
...

This doesn't compile with a modern compiler. It gives an error:
"A numeric expression must appear here".

It must be written as:
Code:
putc('#');

That's because putc() wants a single character as the parameter, not a string.
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Thu Nov 14, 2019 2:21 pm     Reply with quote

Seriously, the UART will be the limiting factor, not printf.
You are only sending at 9600bps. I'm using printf at over 400000bps and
the delay between characters is only the bit time that is inherent in serial
Remember the UART has a hardware buffer. Putc will be putting characters
ahead of what can actually be sent.
I suspect more to the point, you were using putc, to send ASCII. If so, the
processor is having to perform multiple divisions by ten to get the digits.
That is slow.

The big problem with your MSB/LSB approach, is that if a bit get lost
you can never resynchronise, since the code cannot know whether it is
receiving the LSB or MSB... :(
arelone



Joined: 06 Dec 2015
Posts: 42

View user's profile Send private message

PostPosted: Wed Dec 04, 2019 9:48 am     Reply with quote

Hi,

Thanks for the info. I really overlooked the baud rate. I changed the program to test the baud rate and verified with the hyper terminal. Something I don't understand is when I'm testing with baud rate at 9600,19200,38400 the update rates are very much faster than baud rate 115200 ans 230400. Is it normal? I'm using 58982400 MHZ clock (7.3728 x PLL8) what is the maximum baud rate I can use?

Regards
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Wed Dec 04, 2019 9:51 am     Reply with quote

Of course. You are sending the data much faster.

You'd be limited by what the wiring can handle given capacitance etc., not
by the UART. The internal UART can handle Mbaud rates.
temtronic



Joined: 01 Jul 2010
Posts: 9081
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Wed Dec 04, 2019 10:07 am     Reply with quote

Curious, I downloaded the datasheet, max baudrate is +- 3M6Baud, based on 58MHz clock.
As Mr T says, ACTUAL speed depends on external hardware (wiring, PCB, TTL<>RS232 conversion, etc.)
You will of course have to configure the other device (a PC ?) for the same speed, which may not be possible.

Jay
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Thu Dec 05, 2019 12:37 am     Reply with quote

Problem is (of course), that if the connection is actually 'RS232', this
bus has quite limited speed ability depending on length and wiring.
230400bps, is commonly usable on this but much higher is likely to have
problems. Most transceivers have a speed limit at about 250000bps.
Now if the connection is RS485 higher rates are possible but
most RS485 transceivers also have speed limits.
temtronic



Joined: 01 Jul 2010
Posts: 9081
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Dec 05, 2019 7:07 am     Reply with quote

hmm. RS232. I haven't seen any 'new' PCs with real commports ! I'll assume the OP has a PC with USB and wil be using a TTL<>USB module to connect though perhaps he will tell us what the 'RS232 ' device is.
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