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

help me to add interrupt

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



Joined: 24 May 2011
Posts: 14

View user's profile Send private message

help me to add interrupt
PostPosted: Tue Jun 07, 2011 10:31 am     Reply with quote

Below are my code. I'm using an ir sensor to detect object but the problem is when I run it into my robot. It will detect object after finish reading line by line.

Code:

#include <16F877A.H>
#fuses hs, no protect, nowdt, nolvp
#use delay(clock=20000000)
//#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)
//#use i2c(Master, sda=PIN_C4, scl=PIN_C3, FORCE_HW)
//#define use_portb_lcd TRUE
//#include <lcd.c>



#byte PORTa=5
#byte PORTb=6
#byte PORTC=7
#byte PORTd=8
#byte PORTe=9




   set_tris_d(0b00000000);
   set_tris_a(0b11111111);
   set_tris_c(0b00000000);
   set_tris_b(0b00000000);




/////-----------------------------------------------
/////////------------motor movement-----------------
////////--------------------------------------------
void forward()
{
   output_high(pin_b0);   //motor 1
   output_low(pin_b1);
   output_high(pin_b2);

   output_high(pin_b3);   // motor 3
   output_low(pin_b4);
   output_high(pin_b5);
   delay_ms(500);
}



void reverse()
{
   output_high(pin_b1);   //motor 1
   output_low(pin_b0);
   output_high(pin_b2);

   output_high(pin_b4);   // motor 3
   output_low(pin_b3);
   output_high(pin_b5);
   delay_ms(500);
}

void stop()
{
   output_low(pin_b1);   //motor 1
   output_low(pin_b0);
   output_low(pin_b2);

   output_low(pin_b4);   // motor 3
   output_low(pin_b3);
   output_low(pin_b5);

}

void kanan()
{
   output_high(pin_b0);   //motor 1
   output_low(pin_b1);
   output_high(pin_b2);

   output_low(pin_b4);   // motor 3
   output_low(pin_b3);
   output_low(pin_b5);
   delay_ms(500);
}

void kiri()
{
   output_low(pin_b1);   //motor 1
   output_low(pin_b0);
   output_low(pin_b2);

   output_high(pin_b3);   // motor 3
   output_low(pin_b4);
   output_high(pin_b5);
   delay_ms(500);
}



/////-----------------------------------------------
/////////------------IRPD SENSOR--------------------
////////--------------------------------------------

void main()
{

delay_ms(5000);

do
{

if (input (pin_a0)==1)

{ // not detected obstacle

//------------fasa 1---------------------
//---------------------------------------
//{
      forward();
      stop();
      delay_ms(2000);
      kanan();
      stop();
      delay_ms(2000);

      forward();
      stop();
      delay_ms(2000);
      kanan();
      stop();
      delay_ms(2000);

      forward();
      delay_ms(600);


//------------fasa 2---------------------
//---------------------------------------
      stop();
      delay_ms(2000);
      kanan();
      stop();
      delay_ms(2000);
      forward();
      delay_ms(600);

      stop();
      delay_ms(2000);

      kanan();
      stop();
      delay_ms(2000);
      forward();
      delay_ms(600);

      stop();
      delay_ms(2000);
      kanan();
      stop();
      delay_ms(2000);
      forward();
      delay_ms(600);

      stop();
      delay_ms(2000);
      kanan();
      stop();
      delay_ms(2000);
      forward();
      delay_ms(700);


//------------fasa 3---------------------
//---------------------------------------
      stop();
      delay_ms(2000);
      kanan();
      stop();
      delay_ms(2000);
      forward();
      delay_ms(700);

      stop();
      delay_ms(2000);
      kanan();
      stop();
      delay_ms(2000);
      forward();
      delay_ms(700);

      stop();
      delay_ms(2000);
      kanan();
      stop();
      delay_ms(2000);
      forward();
      delay_ms(700);

      stop();
      delay_ms(2000);
      kanan();
      stop();
      delay_ms(2000);
      forward();
      delay_ms(700);


//------------fasa 4---------------------
//---------------------------------------
      stop();
      delay_ms(2000);
      kanan();
      stop();
      delay_ms(2000);
      forward();
      delay_ms(700);

      stop();
      delay_ms(2000);
      kiri();
      stop();
      delay_ms(2000);
      forward();
      delay_ms(700);

      stop;
      delay_ms(3000);

//}

}


if (input (pin_a0)==0) // detected obstacle
{
   stop();
   reverse();
   stop();
   delay_ms(2000);


   kanan();
   stop();
   delay_ms(2000);
   forward();
   stop();
   delay_ms(2000);


   kiri();
   stop();
   delay_ms(2000);
   forward();
   delay_ms(700);
   stop();
   delay_ms(2000);


   kiri();
   stop();
   delay_ms(2000);
   forward();
   stop();
   delay_ms(2000);


   kanan();
   stop();
   delay_ms(2000);
   forward();


}
}
while(1);
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19360

View user's profile Send private message

PostPosted: Wed Jun 08, 2011 3:08 pm     Reply with quote

An 'interrupt' is not what you need.
You need to rethink your code. Currently it is like a driver, who only looks up at what is going on outside, and turns the steering wheel every few seconds, and then is surprised when he drives into things....
Your 'detection', needs to be continuous. Use loops, with a counter, and short time interval, to replace your long delays, and while you loop, test the returns from the sensors. As _soon_ as these go to a state that needs a different response, change to the new movement.

Best Wishes
Fusillade



Joined: 02 Aug 2007
Posts: 31

View user's profile Send private message

PostPosted: Wed Jun 08, 2011 7:20 pm     Reply with quote

This is one possible idea for your code. Please excuse any errors as I wrote it quickly only as an idea. This is not debugged or complete or the best solution. Just an idea.

Delays were removed from your maneuvers.

Every pass through the routine has about 1ms delay plus code processing time.

Every pass through the routine checks the IR status and stops if something is detected.

The timer is approximately how long you want to perform the maneuver in ms.

When the timer reaches zero, the robot stops and defines the new state.

Only performs one pattern. An array could be created (excluding stops as they are assumed) to contain a pattern array with the index incremented upon successful completion of a positive maneuver.


Code:
#typedef enum _ROBOT_STATE
{
    OFF = 0x00,
    FORWARD = 0x55,
    REVERSE = 0xAA,
    KANAN = 0x0F,
    KIRI = 0xF0,
    STOP = 0xFF
} ROBOT_STATE;

void forward()
{
   output_high(pin_b0);   //motor 1
   output_low(pin_b1);
   output_high(pin_b2);

   output_high(pin_b3);   // motor 3
   output_low(pin_b4);
   output_high(pin_b5);
}

void reverse()
{
   output_high(pin_b1);   //motor 1
   output_low(pin_b0);
   output_high(pin_b2);

   output_high(pin_b4);   // motor 3
   output_low(pin_b3);
   output_high(pin_b5);
}

void kanan()
{
   output_high(pin_b0);   //motor 1
   output_low(pin_b1);
   output_high(pin_b2);

   output_low(pin_b4);   // motor 3
   output_low(pin_b3);
   output_low(pin_b5);
}

void kiri()
{
   output_low(pin_b1);   //motor 1
   output_low(pin_b0);
   output_low(pin_b2);

   output_high(pin_b3);   // motor 3
   output_low(pin_b4);
   output_high(pin_b5);
}

void stop()
{
   output_low(pin_b1);   //motor 1
   output_low(pin_b0);
   output_low(pin_b2);

   output_low(pin_b4);   // motor 3
   output_low(pin_b3);
   output_low(pin_b5);
}

void main(void)
{
    unsigned int8 previous_state = OFF;
    unsigned int8 current_state = FORWARD;
    unsigned int8 next_state = OFF;
    unsigned int16 runtime = 0;
    unsigned int16 newruntime = 500;
   
    delay_ms(5000);
    while(1)
    {
        switch(state)
        {
            case FORWARD:
            {
                if(current_state != previous_state)
                {
                    forward();
                    runtime = newruntime;
                    next_state = STOP;                   
                }
                else
                {
                    runtime--;
                }
                break;
            }
            case REVERSE:
            {
                if(current_state != previous_state)
                {
                    reverse();
                    runtime = newruntime;                   
                    next_state = STOP;                   
                }
                else
                {
                    runtime--;
                }
                break;
            }
            case KANAN:
            {
                if(current_state != previous_state)
                {
                    kanan();
                    runtime = newruntime;                   
                    next_state = STOP;                   
                }
                else
                {
                    runtime--;
                }
                break;
            }
            case KIRI:
            {
                if(current_state != previous_state)
                {
                    kanan();
                    runtime = newruntime;                   
                    next_state = STOP;                   
                }
                else
                {
                    runtime--;
                }
                break;
            }
            case STOP:
            {
                if(current_state != previous_state)
                {
                    stop();
                    if(runtime != 0)
                    {
                        new_state = ~previous_state;
                        newruntime = 500 - runtime;
                    }
                    else
                    {
                        switch(previous_state)
                        {
                            case FORWARD:
                            {
                                new_state = KANAN; // could be pattern array
                                break;
                            }
                            case REVERSE:
                            {
                                new_state = KIRI; // could be pattern array
                                break;
                            }
                            case KANAN:
                            case KIRI:
                            {
                                new_state = FORWARD; // could be pattern array
                                break;
                            }
                        }
                        newruntime = 500;
                    }
                    runtime = 2000;                   
                }
                else
                {
                    runtime--;
                }
                break;
            }
        }
        delay_ms(1);
        if(input(pin_a0) == 0)
            current_state = STOP;
        else if(runtime != 0)
            previous_state = current_state;
        else
            current_state = new_state;
    }
}

_________________
New:

Compiler Version: 5.078
IDE Version: MPLAB X V4.15
Devices: PIC18LF****

Old:

Compiler Version: 4.121
IDE Version: MPLAB IDE V8.63
Devices: PIC18LF****
autova



Joined: 24 May 2011
Posts: 14

View user's profile Send private message

PostPosted: Thu Jun 09, 2011 3:05 am     Reply with quote

Ttlemah : ooo , so u saying that i need to recheck and test one by one what going on with my coding . i need to replace the delay and start using loop ?

Fusillade : will do try this concept . thanks for your help . i appreciate it a lot . this coding not for pic16f877a isn't it ? i just need to change it a little bit and apply the concept for it ?
Fusillade



Joined: 02 Aug 2007
Posts: 31

View user's profile Send private message

PostPosted: Thu Jun 09, 2011 6:03 am     Reply with quote

autova wrote:
Ttlemah : ooo , so u saying that i need to recheck and test one by one what going on with my coding . i need to replace the delay and start using loop ?

Fusillade : will do try this concept . thanks for your help . i appreciate it a lot . this coding not for pic16f877a isn't it ? i just need to change it a little bit and apply the concept for it ?


Ttlemah pointed out that you perform a lot of moves without checking the IR port. Their suggestion was for you to continuously check the status of the IR port while you are moving. If you detect an object, then you need to change your movement. This can be done with a loop.

If you simply replaced your delay_ms(500) calls in direction routines with:

Code:
motion_timer(500);  // replaces delay_ms(500)

void motion_timer(unsigned int16 counter)
{
     do
     {
          delay_ms(1);
          counter--;
     } while((counter != 0) && (input(pin_a0) == 1));
     return;
}


At least the device would stop the instant it detects an object.

BTW, what I have provided will work with the pic16f877a. Just add your header information and port definitions into the code as my coding is no different than your own.
_________________
New:

Compiler Version: 5.078
IDE Version: MPLAB X V4.15
Devices: PIC18LF****

Old:

Compiler Version: 4.121
IDE Version: MPLAB IDE V8.63
Devices: PIC18LF****
autova



Joined: 24 May 2011
Posts: 14

View user's profile Send private message

PostPosted: Thu Jun 09, 2011 8:03 am     Reply with quote

Fusillade

ooo , when i try to compile the program u give it to , there's an error .
for the #typedef enum ... the errors are invalid pre-processor directive . what was the problem ?
Fusillade



Joined: 02 Aug 2007
Posts: 31

View user's profile Send private message

PostPosted: Thu Jun 09, 2011 10:56 am     Reply with quote

autova wrote:
Fusillade

ooo , when i try to compile the program u give it to , there's an error .
for the #typedef enum ... the errors are invalid pre-processor directive . what was the problem ?


Try

#define OFF = 0x00
#define FORWARD = 0x55
#define REVERSE = 0xAA
#define KANAN = 0x0F
#define KIRI = 0xF0
#define STOP = 0xFF

instead of my typedef enumeration.

Edit:

And you will need to rename either the above definitions or the function names so that they do not have the same name.
_________________
New:

Compiler Version: 5.078
IDE Version: MPLAB X V4.15
Devices: PIC18LF****

Old:

Compiler Version: 4.121
IDE Version: MPLAB IDE V8.63
Devices: PIC18LF****
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