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

ADC, rs232

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








ADC, rs232
PostPosted: Sun Mar 29, 2009 7:40 am     Reply with quote

Hi all, need help about ADC and rs232 transmit.

Briefing of my project,
I have a signal that looks a like a full-wave rectifier with a period of 1000Hz. For continuous monitoring I have used 8 bit ADC conversion and program as below:
Code:

void main()
{
   int adcValue;

   int16 i;
   float result;
   
   setup_adc_ports(AN0);
   setup_adc(ADC_CLOCK_DIV_32);
   set_adc_channel(0);
   delay_us(20);
   
   while(1){
      for(i=0;i<1000;i++){
         adcValue = read_adc();
         result = (float)(adcValue)*5/255;
         printf("%.2g",result);
         delay_us(40);
        }
    }
}

When I check through hyper-terminal there is no problem with my communication through rs232 with baud rate 115200bps. But when I plot the data through Excel the waveform doesn't re-construct as the input waveform as I seen it in oscilloscope.

fc=1/1ms = 1000Hz
fs=1/40us = 25kHz

1ms/40us = 25 samples per period of 1kHz
Ttelmah
Guest







PostPosted: Sun Mar 29, 2009 9:53 am     Reply with quote

You are not going to get anywhere near 25 samples/cycle.

The ADC conversion itself, takes time. You don't show your clockrate, but the use of an /32 divider, suggests it is up at perhaps 40MHz. You don't say what chip, but the conversion takes typically something like 11 cycles of the ADC clock. So, 352 cycles ofthe master clock. If 40MHz, about 9uSec. Then you are performing floating point arithmetic, taking perhaps another 35uSec (this will depend on how the compiler optimises what you show - I'm assuming the 'best case', that it optimises this into val*0.0196 - if instead it choses to use val/51.0, then the time will increase to perhaps 145uSec...). Then, the output will vary in size, but will typically be about 5 characters. The chip's buffer can hold only two, so the output will have to wait for three characters to be sent. At 115200bps, another 260uSec. It also takes time to perform the floating point divisions here, slowing it yet further. So you are running at a loop time, of perhaps something over 400uSec, and only a cople of samples per cycle...

You need to do a lot of optimising.
Buffer the serial transmit.
Don't delay in the loop. Use a timer, and perform the conversion if it has completed.
Reduce the complexity of the output calculations. Consider sending an unconverted hex value, or at the least, use fixed precision arithmetic, rather than FP.
Remember it takes typically ten bits of data to send one byte. If you want to send 25000 samples per second, you need to be pushing the comms up to about 500kbps, even if you only send two for each sample.....

Best Wishes
takumi



Joined: 07 Mar 2009
Posts: 14

View user's profile Send private message

PostPosted: Sun Mar 29, 2009 6:59 pm     Reply with quote

Not sure why it set to a guess when I sending this problem...
Btw forget to tell that i'm using
PIC16f877A, 20MHz,
Btw about you said using a timer but how? I really didn't know how to?
Is it ok if I put change
Code:

void main()
{
   int adcValue;
   int data[80];
   int i;

   //int16 i;
   //float result;
   
   setup_adc_ports(AN0);
   setup_adc(ADC_CLOCK_DIV_32);
   set_adc_channel(0);
   delay_us(20);
   
   while(1){
      for(i=0;i<80;i++){
         adcValue = read_adc();
         data[i]=adcValue;
  //       result = (float)(adcValue)*5/255;
    //     printf("%.2g",result);
         delay_us(40);
        }
for(i=0;i<80;i++){
delay_us(250); //for sending 2hex value over rs232 example 'FE'
printf("%X",data[i]);
}

    }
}

Is this ok?
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