View Feed
group-icon
Coffee Room
Discuss anything here - everything that you wish to discuss with fellow engineers.
12915 Members
Join this group to post and comment.
keigo911
keigo911 • Aug 25, 2008

Need assistance in PIC using C programming!!!

Hello guys, i am newbie here, was doing a project regarding infrared sensor autogate...

what i wish to ask is how to write the command for the situation stated below?

When a IR receiver received a signal from IR transmitter for more than 3 sec, only the DC motor will move CW or CCW? else, the motor will be remained stationary, even there is something pass by between.... the concept is some sort like a level sensor, to sense the container volume, which the IR receiver was triggered when the container full....

I have try the delay command be4, but it does not work.... it just delay the time of the motor to move, even the IR receiver just get the signal.... so what i wish is to make sure that when the object is stick stationary for more than 3 sec only the motor will be triggered...

thanks very much! hope some1 to help out......URGENT PROJECT!!!


to let all more understand about the code, i hereby posted up the program to emphasize the prob i encounter now...



#define IN1 PIN_B0
#define IN2 PIN_B1
#define IN3 PIN_B2
#define IN4 PIN_B3

#define MOTOR_1_CW 1
#define MOTOR_1_CCW 2
#define MOTOR_1_STOP 3
#define MOTOR_2_CW 4
#define MOTOR_2_CCW 5
#define MOTOR_2_STOP 6

#define OFF 1 //IR receiver open circuited
#define ON 0 //IR receiver connected to ground

void motor_motion(unsigned int motion);

void main()
{

// setup_adc_ports(ALL_ANALOG);
// setup_adc(ADC_CLOCK_INTERNAL);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);

SET_TRIS_B(0x00);
SET_TRIS_A(0xFF);

motor_motion(MOTOR_1_STOP);
motor_motion(MOTOR_2_STOP);

while(1){
if(input_state(PIN_A0) == ON
|| input_state(PIN_A1) == ON
|| input_state(PIN_A2) == ON
|| input_state(PIN_A3) == ON
|| input_state(PIN_A5) == ON){

int i;
i = 1;
if(i < delay_ms(1000)){
motor_motion(MOTOR_1_STOP);
}
motor_motion(MOTOR_1_CCW);
}
else motor_motion(MOTOR_1_STOP);


>>> Here is the condition to make my situation proved, i nid some1 help here, my program ady written as, but it only works as stated above<<<
}

}

void motor_motion(unsigned int command){
if(command == MOTOR_1_CW){
output_high(IN1);
output_low(IN2);
}
else if(command == MOTOR_1_CCW){
output_high(IN2);
output_low(IN1);
}
else if(command == MOTOR_1_STOP){
output_low(IN1);
output_low(IN2);
}
}
bayazidahmed
bayazidahmed • Aug 26, 2008
I couldnt understand when do you want to turn it CW and when CCW?

As far as my understanding goes your while part can be modified to give a simple solution as follows

while(1)
{
if(input_state(PIN_A0) == ON
|| input_state(PIN_A1) == ON
|| input_state(PIN_A2) == ON
|| input_state(PIN_A3) == ON
|| input_state(PIN_A5) == ON)

{
delay_ms(3000);


if(input_state(PIN_A0) == ON
|| input_state(PIN_A1) == ON
|| input_state(PIN_A2) == ON
|| input_state(PIN_A3) == ON
|| input_state(PIN_A5) == ON)

{
motor_motion(MOTOR_1_CW); //or CCW if you want
}

}
}
Ahmed, doesnt your solution mean it'll check twice: the second time AFTER a delay of 3 seconds? The condition stated in the problem is that the IR is received for a continuous 3 seconds before the motor will move. If you just delay it, you wouldn't know whether there was an interruption during the delay.

I think better we try adding a timer counter within a while loop, so when the timer reaches 3 seconds, we switch on the motor. Maybe we can take advantage of the TMR function of the PIC? When the conditions are met, run the timer, and check timer again continously. If there is an elapsed time of at least 3 seconds, then run the motor.

Of course, timing is based on the clock frequency you are using 😛 Keigo, can you tell us more about the PIC you are using, and at which clock speed?
keigo911
keigo911 • Aug 27, 2008
ya ash, it is actually exactly what u r saying.... this is the prob i encountered and the code is just able to delay, but not continue....

btw i am using PIC16F877A with the DC clock speed of 20Mhz as usual...

if u have the idea on how to do it, would u mind to edit the code given to me so i can test it on my Proteus ISIS 7 Pro simulator..... I am using CCS C Compiler for the code compilation

thank you!
hope to get your reply soon as it is an urgent project..... 😀

Share this content on your social channels -