Author Topic: Problem in my Code (PIC16F877A, CCP Compiler)  (Read 2537 times)

0 Members and 1 Guest are viewing this topic.

Offline Abdulla M.A.Topic starter

  • Jr. Member
  • **
  • Posts: 30
  • Helpful? 0
Problem in my Code (PIC16F877A, CCP Compiler)
« on: February 26, 2010, 11:49:21 AM »
Hello guys,
I'm new to the forum, so nice to meet you  ;)
I built a wall avoiding robot by using PIC16F877A MCU, I used CCP1 and CCP2 to drive
a gear D.C, and everything worked OK, but I have a problem in using IR sensor, I used
two IR receiver (http://ledz.com/led.datasheet/PL-IRM0101-3.pdf) , it's active low IR
receiver. when there is no signal receiving Vout=5v, and when  it's receiving any signal
Vout=4v, and because this bad output, I decided to use ADC, I connected Vout of these
IR Receivers to RA0 and RA1, and I wrote my program by using CCS-program, but the
robot did not work, until now I could not discover the problem, may be the problem
in my code!, so I need your help guys, and I will be really grateful for any help.
BTW, I'm sure there is no any problem in the program part the responsible about the PWM.
this is my code:

Code: [Select]
#include "16f877a.h"
#define adc=10
#use delay(clock=4000000) // 4MHZ crystal
#fuses xt,nowdt,put,nolvp,nocpd

// define the variables
unsigned int i,j;
int16 ch0_min, ch1_min;
unsigned int16 ch1[10], ch2[10];

// define struct for leds work as indicators
typedef struct
{unsigned int rled:1;
unsigned int yled:1;
unsigned int bled:1;
}leds;

#define PORTE (*(leds*) 0x09)

// routaine for ADC
void ADX()
{setup_adc(adc_clock_div_32);
setup_adc_ports(AN0_AN1_AN2_AN3_AN4); // set PORTA as analog, Vcc and GND as reference voltages

set_adc_channel(0); // select channel zero
delay_ms(50);
ch0_min=read_adc(); // read channel zero


set_adc_channel(1); // select channel one
delay_ms(50);
ch1_min=read_adc(); // read channel one

setup_adc(adc_off);
}

// stop the motors
void stop()
{
setup_ccp1(ccp_pwm);
set_pwm1_duty(500);
setup_timer_2(t2_div_by_1,249,1);

setup_ccp2(ccp_pwm);
set_pwm2_duty(500);
setup_timer_2(t2_div_by_1,249,1);
output_d(0x03);} // make the motors always enable

// move forward routaine
void movf() // move forward
{setup_ccp1(ccp_pwm);
set_pwm1_duty(800);
setup_timer_2(t2_div_by_1,249,1);

setup_ccp2(ccp_pwm);
set_pwm2_duty(800);
setup_timer_2(t2_div_by_1,249,1);
output_d(0x03);}

//move to left
void movl() // move left
{setup_ccp1(ccp_pwm);
set_pwm1_duty(500);
setup_timer_2(t2_div_by_1,249,1);

setup_ccp2(ccp_pwm);
set_pwm2_duty(800);
setup_timer_2(t2_div_by_1,249,1);
output_d(0x03); }

// move to right
void movr() // move right
{setup_ccp1(ccp_pwm);
set_pwm1_duty(800);
setup_timer_2(t2_div_by_1,249,1);

setup_ccp2(ccp_pwm);
set_pwm2_duty(500);
setup_timer_2(t2_div_by_1,249,1);
output_d(0x03);}


// start the main program
void main()
{
set_tris_e(0x00); // set PORTE as output (leds)

while(1)
{
ADX();
ch0_min>>2; // shift to right (two bits)
ch1_min>>2;
output_e(0x00); // turn off all leds
delay_ms(500);

if(ch0_min>(ch1_min+25)) // if ch0 > ch1 by 0.5v turn left
{porte.yled=1; // turn on yellow led
for(i=0;i<=3;i++) // loop for moving left
{movl(); // turn left
delay_ms(500);
}
}

else if(ch1_min>(ch0_min+25))
{porte.bled=1; // turn on the blue led
for(i=0;i<=3;i++)
{movr(); // turn right
delay_ms(500);
}
}

else
{
porte.rled=1; // turn on the red led
for(i=0;i<=3;i++)
{movf(); // move forward
delay_ms(500);
}
}

}
 }
     


Abdulla
"A scientist can discover a new star, but he cannot make one. He would have to ask an engineer to do that."
"For an optimist the glass is half full, for a pessimist it's half empty, and for an engin

Offline waltr

  • Supreme Robot
  • *****
  • Posts: 1,928
  • Helpful? 98
Re: Problem in my Code (PIC16F877A, CCP Compiler)
« Reply #1 on: February 26, 2010, 01:20:08 PM »
I haven't followed all of your code line by line but did see a few things.

It is best the do the set-up of ports, PWM (CCP) and ADC at the beginning of main() only once. I use a function for set-up that is called at the beginning of main().

On using the ADCs, after the ADC channel is selected you only need a few usec delay to let the sampling settle before starting a conversion. And it is better the have a function to set the channel and read the ADC without re-settting up the ADC.

I use a different C compiler so I don't know what the built in CCS functions that handle the PWM (CCP) and ADCs really do. I write my own functions for setting up and using the ADCs and PWM so I know exactly what they are doing at the PIC's register level. I'll assume that the CCS set-up functions are correct.

I see that you turn LEDs on or off which is a good idea for debugging. Do these LEDs change?
Try doing the simplest ADC read and see if the motors come back. Once you eliminate the line of code that causes the problem you can then figure out why.

On the IR sensors:
Did you measure their output voltage? Because according to the data sheet their output is either greater than 4.5V or less than 0.5V ( see the Vol & Voh in the Electro-Optical Characteristics). Also are you trying to detect an IR LED modulated at 38kHz? That's what these detect.

Hope this helps.

Offline Abdulla M.A.Topic starter

  • Jr. Member
  • **
  • Posts: 30
  • Helpful? 0
Re: Problem in my Code (PIC16F877A, CCP Compiler)
« Reply #2 on: February 27, 2010, 12:32:31 AM »
Thank you waltr,
for PWM worked very well because I checked the o/p in my scope!

Quote
On using the ADCs, after the ADC channel is selected you only need a few usec delay to let the sampling settle before starting a conversion. And it is better the have a function to set the channel and read the ADC without re-settting up the ADC.

Ok, I will do that, and I will check what will happen ;)

Quote
I use a different C compiler so I don't know what the built in CCS functions that handle the PWM (CCP) and ADCs really do. I write my own functions for setting up and using the ADCs and PWM so I know exactly what they are doing at the PIC's register level. I'll assume that the CCS set-up functions are correct.

I'm also know what's happening in PIC's registers because I worked on asm. for a long time  ;)

Quote
I see that you turn LEDs on or off which is a good idea for debugging. Do these LEDs change?
Try doing the simplest ADC read and see if the motors come back. Once you eliminate the line of code that causes the problem you can then figure out why.

thanks, the LED's sometime work sometime not, and not in correct form!

but I think all the problem in ADC, I knew the problem in ADC from the LED's,

Quote
On the IR sensors:
Did you measure their output voltage? Because according to the data sheet their output is either greater than 4.5V or less than 0.5V ( see the Vol & Voh in the Electro-Optical Characteristics). Also are you trying to detect an IR LED modulated at 38kHz? That's what these detect.

Yes, I did . I saw it!! , I used the remote control of TV. just for checking right now, then I will build the cct.

Abdulla
"A scientist can discover a new star, but he cannot make one. He would have to ask an engineer to do that."
"For an optimist the glass is half full, for a pessimist it's half empty, and for an engin

 


Get Your Ad Here

data_list