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

Read Pulse w ADC

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



Joined: 15 Jun 2007
Posts: 6

View user's profile Send private message

Read Pulse w ADC
PostPosted: Thu Jun 28, 2007 4:56 pm     Reply with quote

Hello!
Can someone help me? Here is what I am doing. I have 4 NPN transistors (base) connected to PORTB 0-3 (a basic emmiter follower) . I am pulsing each transistor in a state machine and reading back the (load) voltage with the built in ADC and graphing the data on a GUI program.

Here is the problem! I am not getting back ALL the pulses I send out. (Missing data) I should see all the pulses at the load voltage level. I'm thinking the ADC is not fast enough or its doing its routine before the ADC can read it

Does anyone have a better way to do this? If someone can figure this out can you post example code. I have posted the working code below

Thanks In Advance!

*******************************************************************************/
#include <16F873.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#device adc=10
#use delay(clock=20000000)
#use rs232(baud=38400,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)

/******************** Local Variable Declarations *****************************/
int Selected=0;
enum Port {B0=PIN_B0,B1=PIN_B1,B2=PIN_B2,B3=PIN_B3};

/******************** Initialize Ports *****************************************/
void Initialize(void)
{
setup_adc_ports(ALL_ANALOG);
setup_adc(ADC_CLOCK_INTERNAL);
}

/******************** Send Out A Pulse *****************************************/
void Pulse(int port,int delay)
{
output_low(port);
delay_ms(delay);
output_high(port);
delay_ms(delay);
}

/******************** Read Analog To Digital Converter *************************/
void ADC(int channel)
{
long Raw_Value;
set_adc_channel(channel);
delay_ms(10);
Raw_Value = read_adc();
printf("CH%d,%Lu \r",channel, Raw_Value);
}

/******************** Pulse and Read Voltage From Each Load *************************/
void AnalyseLoad(void)
{
switch(Selected)
{
case 0:
ADC(0); //Read Supply Voltage
Selected=1;
break;

case 1:
Pulse(B0,1); //Pulse Load 1
ADC(1); // Read Voltage
Selected=2;
break;

case 2:
Pulse(B1,1); //Pulse Load 2
ADC(2); //Read Voltage
Selected=3;
break;

case 3:
Pulse(B2,1); //Pulse Load 3
ADC(3); //Read Voltage
Selected=4;
break;

case 4:
Pulse(B3,1);} //Pulse Load 4
ADC(4); //Read Voltage
Selected=0;
break;
}
}

/******************** Main Loop ************************************************/
void main()
{
Initialize();
while (TRUE)
{
AnalyseLoad();
}
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Jun 28, 2007 5:17 pm     Reply with quote

Quote:
I'm thinking the ADC is not fast enough or its doing its routine before the ADC can read it


I didn't try to understand your program, but you're concerned about
the ADC conversion speed, and you have a 10 ms delay after setting
the channel. This really only needs to be 20 us (not ms) or less.
You can change it to: delay_us(20);
Quote:
void ADC(int channel)
{
long Raw_Value;
set_adc_channel(channel);
delay_ms(10); // Really only need a 20 us delay
Raw_Value = read_adc();
printf("CH%d,%Lu \r",channel, Raw_Value);
}

Also, your printf statement will take about 1/4 ms per character.
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