user_routines.c (2007)

This page explains in depth the code in the user_routines.c file included with the 2007 FRC default code.

Like most other pages…inclusions:

#include <stdio.h>
 
#include "ifi_aliases.h"
#include "ifi_default.h"
#include "ifi_utilities.h"
#include "user_routines.h"
#include "user_Serialdrv.h"

stdio.h is used to that printf() will work. It is a header that is owned by the compiler. ifi_aliases.h makes accessing the PWMs, analog inputs, and digital I/Os more straightforward. ifi_default.h contains the prototypes for functions used to communicate with the master loop and the operator interface. ifi_utilities.h contains many useful functions. user_routines.h is used to prototype all of the functions found in this file. And finally, user_Serialdrv.h is used so that you have the option to drive one of the onboard RS-232 ports as a serial port.

extern unsigned char aBreakerWasTripped;

This is used to alert your program that one of the circuit breaker was broken. Directly under this line is the best place for you to define any variables that you will be using.

void Limit_Switch_Max(unsigned char switch_state, unsigned char *input_value)
{
  if (switch_state == CLOSED)
  { 
    if(*input_value > 127)
      *input_value = 127;
  }
}

The Limit_Switch_Max() function is used to keep a PWM from exceeding 127. If the input value is over 127, it resets the PWM to 127.

void Limit_Switch_Min(unsigned char switch_state, unsigned char *input_value)
{
  if (switch_state == CLOSED)
  { 
    if(*input_value < 127)
      *input_value = 127;
  }
}

The Limit_Switch_Min() function does just the opposite. If the input value is less than 127, then the PWM is reset to 127.

unsigned char Limit_Mix (int intermediate_value)
{
  static int limited_value;
 
  if (intermediate_value < 2000)
  {
    limited_value = 2000;
  }
  else if (intermediate_value > 2254)
  {
    limited_value = 2254;
  }
  else
  {
    limited_value = intermediate_value;
  }
  return (unsigned char) (limited_value - 2000);
}

The Limit_Mix() function is mostly unnecessary, but it is used in the default code so it is best left alone. Its main purpose is to ensure that a PWM value does not exceed 255 or go below 0.

The next bit is the user initialization code. This is where you are going to set initial values and settings for all of the on board I/O.

void User_Initialization (void)
{

Opens the User_Initialization() function for definition.

Set_Number_of_Analog_Channels(SIXTEEN_ANALOG);    /* DO NOT CHANGE! */
 
/* FIRST: Set up the I/O pins you want to use as digital INPUTS. */
  digital_io_01 = digital_io_02 = digital_io_03 = digital_io_04 = INPUT;
  digital_io_05 = digital_io_06 = digital_io_07 = digital_io_08 = INPUT;
  digital_io_09 = digital_io_10 = digital_io_11 = digital_io_12 = INPUT;
  digital_io_13 = digital_io_14 = digital_io_15 = digital_io_16 = INPUT;
  digital_io_18 = INPUT;  /* Used for pneumatic pressure switch. */
    /* 
     Note: digital_io_01 = digital_io_02 = ... digital_io_04 = INPUT; 
           is the same as the following:
 
           digital_io_01 = INPUT;
           digital_io_02 = INPUT;
           ...
           digital_io_04 = INPUT;
    */

This is where you can set all of the digital I/Os as inputs. A digital channel can only have two initial values: INPUT or OUTPUT. This is read in by the initialization code and it handles the channel accordingly.

/* SECOND: Set up the I/O pins you want to use as digital OUTPUTS. */
  digital_io_17 = OUTPUT;    /* Example - Not used in Default Code. */
 
/* THIRD: Initialize the values on the digital outputs. */
  rc_dig_out17 = 0;

Here you can set any digital I/Os as outputs. When you set a channel as an output, it needs to have an initial value. The value for an output can either be 1 or 0 (true or false).

/* FOURTH: Set your initial PWM values.  Neutral is 127. */
  pwm01 = pwm02 = pwm03 = pwm04 = pwm05 = pwm06 = pwm07 = pwm08 = 127;
  pwm09 = pwm10 = pwm11 = pwm12 = pwm13 = pwm14 = pwm15 = pwm16 = 127;

Next up are the PWMs. Neutral is 127, full reverse is 0, and full forward is 255.

/* FIFTH: Set your PWM output types for PWM OUTPUTS 13-16.
  /*   Choose from these parameters for PWM 13-16 respectively:               */
  /*     IFI_PWM  - Standard IFI PWM output generated with Generate_Pwms(...) */
  /*     USER_CCP - User can use PWM pin as digital I/O or CCP pin.           */
  Setup_PWM_Output_Type(IFI_PWM,IFI_PWM,IFI_PWM,IFI_PWM);

This allows you to use the 4 on board CCP PWM generators. They are not needed for the default code to work. Check the using ccp guide to understand how these work.

Putdata(&txdata);             /* DO NOT CHANGE! */
 
  Serial_Driver_Initialize();
 
  printf("IFI 2006 User Processor Initialized ...\r");  /* Optional - Print initialization message. */
 
  User_Proc_Is_Ready();         /* DO NOT CHANGE! - last line of User_Initialization */
}

The rest is best left alone. Putdata() sends the compiled transmission block to the OI. Serial_Driver_Initialize() can be removed if you do not use the serial port for driving an LCD or something similar. The print command will send the line of text to the console when you enable it. and User_Proc_Is_Ready() tells the master loop that the user initialization block has completed.

Below is the full file for your reference.

Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 License