Thread: interrupt function

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    2

    interrupt function

    hi, my name is wendy and i have just started learning C programming last 2 weeks. (all i know is the hello world program)
    I am in a group project right now and all my teammates has lots of background in programming already so they assigned me the simplest portion of the program to write.

    However, I am having lots of troubles in writing the interrupt function.. I don't even know if I am starting it right.

    Could someone just give me a tip on how to start ?

    Interrupt Subroutine:
    - Counts pins(Data)
    - Retry if nothing
    - Check if it is a command
    - if it is reasonable sets pin(Data) highto signal a correct command to the Primary controller, else cancel
    - calls Send(cmd), sends command number
    - resets pin(Data)
    - returns


    We are going to define this location #DEFINE P#(P0.1-P0.5)DATA0 through DATA4. This way anytime you want to read from data you can justwrite that. The commands are going to be sent as a 5 digit binary number. Whenthe value is received it will be saved as a decimal number in ‘int cmd’. I’vedecided to switch the data bus from one wire to 5. This sill increaseefficiency and accuracy. We will probably use all of the commands. But if wedon’t you have to check if the value received is a command. If it is, set DATA0high, this signals the Primary that it sent a proper command so if cancontinue. It then calls the Send() function with the parameter cmd. (implementlater). It then resets DATA0 to low and returns .

    http://xf3.xanga.com/9748251048d0815...w113406047.jpg

    I am NOT asking for people to write code for me; but it would be very much appreciated if you can give me a hint on how to start..

    this is what i have put together so far. i have no idea on what to do next

    Code:
    #DEFINE P0.5 data0
    #DEFINE P0.4 data1
    #DEFINE P0.3 data2
    #DEFINE P0.2 data3
    #DEFINE P0.1 data4
    
    #DEFINE HIGH 1
    #DEFINE LOW 0
    
    void micro2(void)
    {
    statiic unsigned char datacnt0; //event count at datapin
    int cmd; //command identifer
    char data; //command # sent by primary microcontroller   
    
    
    data[0] == data0;
    data[1] == data1;
    data[2] == data2;
    data[3] == data3;
    data[4] == data4;     
    
        for (i=0; i<4; i++)
        {
            cmd = data[i];
        }
    
    while(1)    //continues to check for cmd;
    
    
    
    }

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Code:
    while(1)
    In an ISR?!?

    Be minimalist. Get/put what you need and get out. Set global flags for the main/background code.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    4
    Quote Originally Posted by Dave_Sinkula View Post
    Code:
    while(1)
    In an ISR?!?

    Be minimalist. Get/put what you need and get out. Set global flags for the main/background code.
    Thank you very much.

    I am having trouble on using the old a/c so i registered another one.

    Code:
    int cmd; //command identifer
    char data; //command # sent by primary microcontroller   
    
    data[0] == data0;
    data[1] == data1;
    data[2] == data2;
    data[3] == data3;
    data[4] == data4;     
    
        for (i=0; i<4; i++)
        {
            cmd = data[i];
        }
    is this portion's syntax correct ? i do not have the code for the handler so i couldn't compile to test >__<

    i have also done some research on the internet and some interrupt example is initialized like this:

    Code:
    void Squad (void) interrupt 1 using 1
    I just wonder what the difference is to have "interrupt 1 using 1" tailed at the end ?

    thank you in advance

  4. #4
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by 2328012 View Post
    Code:
    data[0] == data0;
    data[1] == data1;
    data[2] == data2;
    data[3] == data3;
    data[4] == data4;
    This likely does not do what you think it ought. A comparison is ==, an assignment is =.

    And that for loop kinda looks like a no-op.

    Quote Originally Posted by 2328012 View Post
    i have also done some research on the internet and some interrupt example is initialized like this:
    There is nothing standard, so you have to consult your compiler's documentation.

    Perhaps share that with us too.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > so they assigned me the simplest portion of the program to write.
    > I am having lots of troubles in writing the interrupt function
    Wow, the other parts must be really hard then if the easy bit is to write an ISR.

    You also seem to be writing code for some kind of micro controller, rather than the usual desktop PC/Mac running any popular operating system. One thing which would be useful to know is some information on the type of micro controller and which compiler you're using (I notice some non-standard syntax in your initial attempt).

    For the moment, concentrate on the steps which you need to take after the interrupt has happened.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  6. #6
    Registered User
    Join Date
    Oct 2007
    Posts
    4
    Quote Originally Posted by Salem View Post
    > so they assigned me the simplest portion of the program to write.
    > I am having lots of troubles in writing the interrupt function
    Wow, the other parts must be really hard then if the easy bit is to write an ISR.

    You also seem to be writing code for some kind of micro controller, rather than the usual desktop PC/Mac running any popular operating system. One thing which would be useful to know is some information on the type of micro controller and which compiler you're using (I notice some non-standard syntax in your initial attempt).

    For the moment, concentrate on the steps which you need to take after the interrupt has happened.
    yes it is actually a linkage program between two microcontrollers

    The two controller and connected with 5 binary bidirectional databus and 2 irq lines.

    command is equal to the binary of the 5 databus
    ie. data:00011 -> cmd: 3

    it's a ping check for the databus, at anytime that the data bits is not equal to zero(high), it will call the send(cmd) function and send the cmd number and reset the data to 0 (low)

    if not, it will continue to check

    i wonder if i can use the while() function to check cmd ? so at anytime it will break if cmd is nonzero and call the send function... i found the while() function by googling but i am not sure if this is how i am supposed to use it..

    Thank you very much everyone.. i am actually a material + mechanics students so i am really limited in programming... i couldn't understand all those programming books that i rent >___< (well.. except all those different version of helloworld..)

  7. #7
    Registered User
    Join Date
    Oct 2007
    Posts
    4
    it took me a long while.. but i have revise my code:

    does it look right ? it doesn't look like an interrupt to me

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    //#define P0.5 data0
    //#define P0.4 data1
    //#define P0.3 data2
    //#define P0.2 data3 
    //#define P0.1 data4
    
    //for testing purposes, call interrupt function as 'main' function
    int main(void)
    {
    
    int cmd; //command identifer
    int cmd_num; //command number
    int data[4]; //command # sent by primary microcontroller   
    
    int i;
        
        //for testing purpose, ask user to input databits manually
        printf("data0: ");
        scanf("&#37;i",&data[0]);     
        printf("data1: ");
        scanf("%i",&data[1]);    
        printf("data2: ");
        scanf("%i",&data[2]);    
        printf("data3: ");
        scanf("%i",&data[3]);    
        printf("data4: ");
        scanf("%i",&data[4]);    
        
        //print the value in the 5 databus
        printf("data =");
        for(i=0; i<5; i++)
        {
            printf( "%d",abs(data[4-i]));
        }
        
        cmd_num = data[0]*2 + data[1]*4 + data[2]*8 + data[3]*16 + data[4]*32;
        printf("\ncmd number= %i",cmd_num);
    
        sleep();
        
        while(1) //check at all time if there's a command call
        {
                 if (cmd_num !=0)
                    //send(cmd); //return cum_number
                    for(i=0; i<5; i++)
                    {
                    data[i]=0; //reset databus for
                    }
        }
    
    return (0);
    }

  8. #8
    Registered User
    Join Date
    Oct 2007
    Posts
    4
    this is the function that calls the interrupt
    Code:
    /*  
        intout test code.
        -	Checks if the cmd = STATUS
        -	If its STATUS, call send(), update profile, return
        -	Start count variable
        -	Send interrupt to primary
        -	Send command value 
        -	Increment count value
        -	Sleep for pin(Data) for 0.1s
        -	If pin(Data) = low and count is greater then 0 send error                               
        -	If pin(Data) = low and count is 0 retry
        -	If pin(Data) is high 
        -	Update profile array with cmd
        -	Return
    */
    
    
    #include <8051.h>
    #include <windows.h>
    
    #DEFINE P0.5 data0
    #DEFINE P0.4 data1
    #DEFINE P0.3 data2
    #DEFINE P0.2 data3
    #DEFINE P0.1 data4
    #DEFINE P1.3 INT
    
    
    int intout(int cmd)
    {
      
      //  initialize vars
      int temp = cmd;
      int i = 0;
      
      //  set data pins to binary of cmd
      while ( i < 2)
      {
      //  send interrupt
      INT = 1;
      
      //  set data pins
      if ( temp != 0)
      {
        data0 = temp&#37;2;
        temp = temp/2;
        if ( temp != 0)
        {
          data1 = temp%2;
          temp = temp/2;
          if ( temp != 0)
          {
            data2 = temp%2;
            temp = temp/2;
            if ( temp != 0)
            {
              data3 = temp%2;
              temp = temp/2;
              if ( temp != 0)
              {
                data4 = temp%2;
                temp = temp/2;
              }
            }
          }
        }
      }
      
      //   reset interrupt, Sleep for response
      INT = 0;
      Sleep(100);
      if ( INT == 1 )
        break;
      i++;
      }
      
      //  reset pins for future use
      data0 = 0;
      data1 = 0;
      data2 = 0;
      data3 = 0;
      data4 = 0;
      
      //  check for error
      if ( i > 1 )
      {
        return 1;
    
      }
      
      /*  
          retrieve status
          assume Primary will return status after it has completed the action and set data4 to 1,
          otherwise it will return 0
      */
      
      i = 0;
      while ( 1 ) 
      {
      if (data4 == 1)
        break;
      i++;
      if (i == 500)
        return 1;
      }
      
      //   set status array
      status[0] = data0;
      status[1] = data1;
      status[2] = data2;
      status[4] = data3;
      
      //   completed successfully 
      return 0;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  2. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Including lib in a lib
    By bibiteinfo in forum C++ Programming
    Replies: 0
    Last Post: 02-07-2006, 02:28 PM
  5. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM