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

adc question

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



Joined: 05 Feb 2016
Posts: 33

View user's profile Send private message

adc question
PostPosted: Mon Apr 18, 2016 12:46 pm     Reply with quote

Hello,

I have a question. You see my codes are so classical adc codes. If I do size of the character array 3 or greater than 3, there is not problem. But, i have 16 bit adc conversion value. For better speed i don't want to use 12 bit or greater character array. In theory, " unsigned char datas[2]" should be enough for me. But it isn't working, If I use "unsigned char datas[4]" it is working . I watch my simulation by using Proteus and Labview(or matlab) via elitima software.

I hope, I could tell my question.

Thanks..

Code:

#include <18F4550.h>
#device ADC=10


#fuses HSPLL,NODEBUG,NOCPD,NOPROTECT,NOPUT
#FUSES PLL5 //PLL BOLME ORANI 5
#FUSES CPUDIV2 //MCU CLOCK DİVİSİON
#FUSES NOWDT                    //No Watch Dog Timer
#FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOXINST                  //Extended set extension and Indexed Addressing mode disabled (Legacy mode)

#use delay(crystal=20MHz)
#use delay(clock=48MHz)

#define LED PIN_B0
#define DELAY 1000
#use rs232(baud=115200,xmit=pin_c6,rcv=pin_c7,parity=N,stop=1)


-------------------------------------------------

#include <main.h>
unsigned long int adcdeger;
[b]unsigned char veri[2];[/b]

void main()
{
    setup_psp(psp_disabled);
    setup_spi(spi_ss_disabled);
    setup_timer_1(t1_disabled);
    setup_timer_2(t2_disabled,0,1);
    setup_timer_3(t3_disabled);
    setup_ccp1(ccp_off);
    setup_ccp2(ccp_off);
    setup_adc(adc_clock_internal);
    setup_adc_ports(an0);
    set_adc_channel(0);
    delay_ms(1);
   
   //Example blinking LED program
   while(true)
   {
      adcdeger=read_adc();
      while(!adc_done());
      sprintf(veri,"%ld",adcdeger);
      puts(veri);
      delay_ms(3);
     
      //these codes provide us 5 Hz sampling rate.
      //delay_us(20);
      output_b(0x01);
      output_b(0x00);
     
   }

}
jeremiah



Joined: 20 Jul 2010
Posts: 1349

View user's profile Send private message

PostPosted: Mon Apr 18, 2016 1:53 pm     Reply with quote

[2] is not enough. A 2 digit number string has at the minimum 3 characters:

1st digit, 2nd digit, null character

"02" is the same as {'0','2','\0'}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Apr 18, 2016 1:55 pm     Reply with quote

Jeremiah posted his reply while I was typing this. I'll post it anyway.

You have the compiler configured to return a 10-bit ADC result (with the
#device ADC=10 statement). So the result could be 0x3FF, which is 1023 in decimal.

Then you are converting the ADC result into an ASCII string with
the following line:
Quote:
sprintf(veri,"%ld",adcdeger);

Let's suppose the result is 1023. The output of sprintf will be:
'1' '0' '2' '3' 0x00
That's four ASCII bytes for the ADC result, plus a string terminator byte
of 0x00, which must be at the end of every string.

So you must have an array large enough to hold all these bytes.
This requires an array that is at least 5 bytes in size. Example:
Code:
int8 veri[5];
biomed12



Joined: 05 Feb 2016
Posts: 33

View user's profile Send private message

PostPosted: Mon Apr 18, 2016 2:36 pm     Reply with quote

PCM programmer wrote:
Jeremiah posted his reply while I was typing this. I'll post it anyway.

You have the compiler configured to return a 10-bit ADC result (with the
#device ADC=10 statement). So the result could be 0x3FF, which is 1023 in decimal.

Then you are converting the ADC result into an ASCII string with
the following line:
Quote:
sprintf(veri,"%ld",adcdeger);

Let's suppose the result is 1023. The output of sprintf will be:
'1' '0' '2' '3' 0x00
That's four ASCII bytes for the ADC result, plus a string terminator byte
of 0x00, which must be at the end of every string.

So you must have an array large enough to hold all these bytes.
This requires an array that is at least 5 bytes in size. Example:
Code:
int8 veri[5];


Your answer is really perfect, thanks very much.

Could you tell me any other suitable way to send datas ?
temtronic



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

View user's profile Send private message

PostPosted: Mon Apr 18, 2016 3:02 pm     Reply with quote

Sending ASCII data allows you to see the data in an visually understandable format but it does take 4 to send. If you simple send the unsigned integer 16 result as two bytes, transmission is twice as fast !

Either way the receiving computer program has to know the format!

There is no 'right' way, so the choice is up to you.


Jay
biomed12



Joined: 05 Feb 2016
Posts: 33

View user's profile Send private message

PostPosted: Mon Apr 18, 2016 3:38 pm     Reply with quote

temtronic wrote:
Sending ASCII data allows you to see the data in an visually understandable format but it does take 4 to send. If you simple send the unsigned integer 16 result as two bytes, transmission is twice as fast !

Either way the receiving computer program has to know the format!

There is no 'right' way, so the choice is up to you.


Jay


Alright, what is that way? I don't know it. Please share with me.
temtronic



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

View user's profile Send private message

PostPosted: Mon Apr 18, 2016 7:08 pm     Reply with quote

quick answer....

Have a look in the manual for 'make8(var,offset)'

consider you have

unsigned int16 adc_result=0;
char hi_adc=0;
char lo_adc=0;

...
do ADC and store result in adc_result...
....
hi_adc=make8(adc_result,1);
lo_adc+make8(adc_result,0);

..
now send to PC hi_adc then lo_adc

those 2 bytes contain the 10 or 12 bit ASC conversion result.

It's up to you to do the proper coding ( variable names, how to send etc.) Also be sure the PC program can accept the two bytes and convert back into 16 bits and do whatever conversion required to show onto the PC screen or program control.

Jay
biomed12



Joined: 05 Feb 2016
Posts: 33

View user's profile Send private message

PostPosted: Tue Apr 19, 2016 11:23 am     Reply with quote

temtronic wrote:
quick answer....

Have a look in the manual for 'make8(var,offset)'

consider you have

unsigned int16 adc_result=0;
char hi_adc=0;
char lo_adc=0;

...
do ADC and store result in adc_result...
....
hi_adc=make8(adc_result,1);
lo_adc+make8(adc_result,0);

..
now send to PC hi_adc then lo_adc

those 2 bytes contain the 10 or 12 bit ASC conversion result.

It's up to you to do the proper coding ( variable names, how to send etc.) Also be sure the PC program can accept the two bytes and convert back into 16 bits and do whatever conversion required to show onto the PC screen or program control.

Jay


Jay, I have tested your advice and my system is working more stable and fast right now. Thanks.
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