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

Urgently in need of help!

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



Joined: 08 Jan 2006
Posts: 26

View user's profile Send private message

Urgently in need of help!
PostPosted: Sun Feb 12, 2006 8:47 pm     Reply with quote

I'm having trouble with the ADC.. I've read all the previous posts about it and tried their solutions but I can't get it to work..

Heres my code:

Code:

#include <18F452.h>
#device ADC=8;
#fuses XT,NOWDT,NOPROTECT,NOLVP
#use delay(clock=4000000)

#define INTS_PER_SECOND 76     // (20000000/(4*256*256))


int ir_value1=0;

/* #int_rtcc                          // This function is called every time
void clock_isr() {                 // the RTCC (timer0) overflows (255->0).
                                   // For this program this is apx 76 times
                                  // per second.
      ir_value1=Read_ADC();
      delay_us(20);


} */


void init_adc(void);
void init_pwm(void);
void init(void);
void main(void);
void forward(void);
void speed(int x1,x2);

void init_adc(void)
{
  setup_adc(ADC_CLOCK_INTERNAL );
  setup_adc_ports(ALL_ANALOG);
  set_adc_channel(5); //maybe use this when you read it because, im using 2 channels also need a delay_us(20) to change channels
  delay_us(20);
}

void init_pwm(void)
{
  setup_ccp1(CCP_PWM);
  setup_ccp2(CCP_PWM);
  setup_timer_2(T2_DIV_BY_1,255,1);
  set_pwm1_duty(0);
  set_pwm1_duty(0);
  delay_ms(1000);
}


void init(void)
{
  init_pwm();
  enable_interrupts(INT_RTCC);
  enable_interrupts(GLOBAL);
  init_adc();
  set_tris_a(0b11111111);
  set_tris_c(0b00000000);
  set_tris_b(0b00000000);
 
}

void forward(void)
{
  output_high(PIN_C6);
  output_high(PIN_C5);
}

void speed(int x1, x2)
{
  set_pwm1_duty(x1); // need to compensate for straight movement
  set_pwm2_duty(x2);
}

void main(void)
{
  init();
 
  speed(200,200);
  while (true)
    {

      forward();
      output_low(PIN_B7);
      if (ir_value1=Read_ADC() > 4) {
        delay_ms(1000);
        output_high(PIN_B7);
        delay_ms(1000);
      }
    }
   
}


I'm trying to read in the input of a sharp 38khz sensor which outputs 0 to 5 volts, but that is aside from the problem. The problem I'm having is that when i start the device, the pin and b7 blinks.. even with no value on the channel (ie the sensor is disconnected). I've tried it with it connected with 0 volts across the sensor, but the same thing happens. I've measured the impedance to be about 6 kohms, which exceeds that of the 2.5 in the documents, but I've tried disconnecting the sensor but it still fails to be zero. What could be wrong and how can I fix it?

Thanks
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Feb 12, 2006 9:02 pm     Reply with quote

Look at your code. Do you see anything wrong with the line in bold:
Quote:
while (true)
{

forward();
output_low(PIN_B7);
if (ir_value1=Read_ADC() > 4) {
delay_ms(1000);
output_high(PIN_B7);
delay_ms(1000);
}
}
[terminate]



Joined: 08 Jan 2006
Posts: 26

View user's profile Send private message

PostPosted: Sun Feb 12, 2006 9:19 pm     Reply with quote

hmmm yeah theres an error there,

i've changed it to this:

Code:
while (true)
    {

      forward();
      output_low(PIN_B7);
      ir_value1=read_adc();
      if (ir_value1 >= 4) {
        delay_ms(1000);
        output_high(PIN_B7);
        delay_ms(1000);

but it still doesn't work... even with the sensor disconnected i get the flashing of the LED
[terminate]



Joined: 08 Jan 2006
Posts: 26

View user's profile Send private message

PostPosted: Sun Feb 12, 2006 10:14 pm     Reply with quote

please dont tell me this is because i'm trying to compare a 8 bit number against a number 4 which is almost true even when its close to 0

and so 4 volts would be like 200 instead of just 4
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sun Feb 12, 2006 10:42 pm     Reply with quote

You're comparing "ir_value1" to 4. So any value from 4 through 255
will cause the LED to blink. The A/D input may reach a value of 4
just due to noise. It's no surprise that it blinks.

If the connector is disconnected then you have a floating input.
It can float to any value. Since a value from 4 to 255 will cause
blinking, again it's no surprise that it blinks.

Also, I notice you're using B7. Pin B7 is used by the CCS debugger
and the Microchip ICD2 debugger. If you're using one of those units,
then please change your LED pin to another pin (Besides B6 and B7).
[terminate]



Joined: 08 Jan 2006
Posts: 26

View user's profile Send private message

PostPosted: Sun Feb 12, 2006 11:04 pm     Reply with quote

thank you so much PCM programmer!

i wish you could be my mentor!

what i initially thought was that ir_value1 was being compared to 4 volts... stupid me... i wasnt thinking 4 in binary
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