PJ3007 Project  1.1
Motor Controller Board. 4A dual H Bridge
 All Files Functions Variables Macros
init.c File Reference
#include "init.h"

Go to the source code of this file.

Functions

signed char board_initialization (void)
 
signed char system_check (void)
 
signed char initialize_DC_motor (unsigned char modality, int pwm_frequency, int max_current)
 
void power_control_PWM_set_period (int period)
 
void set_duty_cycle_DC0 (int duty_cycle)
 
void set_duty_cycle_DC1 (int duty_cycle)
 
signed char rotate_motor_clockwise (unsigned char motor, int speed)
 
signed char rotate_motor_anticlockwise (unsigned char motor, int speed)
 
signed char stop_motor_rotation (unsigned char motor)
 
signed char initialize_RS485 (unsigned char baud_rate, unsigned char rx_interrupt_enable, unsigned char tx_interrupt_enable)
 
signed char read_data_RS485 (unsigned char *data)
 
signed char write_const_string_RS485 (const char *data)
 
void send_return_RS485 (void)
 
void send_delete_RS485 (void)
 
signed char write_int_ASCII_RS485 (int value, unsigned char number_of_digits)
 
unsigned char get_dip_switch_value (void)
 
int ADC_read_buffer (void)
 
int ADC_read_POT_1 (void)
 
int ADC_read_POT_2 (void)
 
int ADC_read_TEMP (void)
 
int ADC_read_VIN_TEST (void)
 
int ADC_read_motor_current (unsigned char motor)
 
signed char initialize_stepper_motor (unsigned char modality, unsigned char pwm_frequency, int max_current)
 
signed char step_motor_clockwise (int current)
 
signed char step_motor_anticlockwise (int current)
 
void turn_on_FAN (void)
 
void turn_off_FAN (void)
 
unsigned char is_emergency_stop_out_active (void)
 
unsigned char is_emergency_stop_in_active (void)
 
unsigned char set_emergency_stop_out (void)
 
unsigned char set_emergency_stop_in (void)
 
unsigned char reset_emergency_stop_out (void)
 
unsigned char reset_emergency_stop_in (void)
 
unsigned char emergency_stops_check (void)
 
void initialize_TIMER0 (unsigned char interrupt_priority)
 
void activate_signal_interupt (unsigned char signal, unsigned char interrupt_priority)
 
void initialize_interrupt (unsigned char interrupt_status)
 
void set_error_status (unsigned char error)
 
unsigned char get_error_status (void)
 
void set_warning_status (unsigned char warning)
 
unsigned char get_warning_status (void)
 

Variables

unsigned char motor_modality = 0
 
signed char current_state = 0
 
unsigned char number_of_states = 0
 
unsigned char emergency_stop_out = 0
 
unsigned char emergency_stop_in = 0
 
int motor_max_current = 0
 
unsigned char stepper_state [8]
 

Function Documentation

void activate_signal_interupt ( unsigned char  signal,
unsigned char  interrupt_priority 
)

The function activate the interrupt for one of the follwowing signal ENABLE, STEP, DIRECTION, SWITCH

Parameters
interrupt_priorityIt specifies the Interrupt Priority Level
Returns

Definition at line 1098 of file init.c.

int ADC_read_buffer ( void  )

Definition at line 724 of file init.c.

int ADC_read_motor_current ( unsigned char  motor)

The function reads the the current sinked from the selected Motor. The returned value is expressed in mA. This function is called from system_check () to monitor that the curernt is within maximum pick motor current.

Parameters
motorSelect the motor from which the curren shlould be read. Value [MOTOR_1, MOTOR_2].
Returns
Current expressed in mA from the selected motor.
Note
The ADC reading is a blocking function until the ADC conversion is over and the ADC buffer is read out.

Definition at line 807 of file init.c.

int ADC_read_POT_1 ( void  )

The function reads the the voltage from the potenziometer POT_1 connected to AUX_1 connector.

Parameters
@returnAnalog value from the Potenziometer POT_1 connected to AUX_1. The value is expressed in quantum.
Note
The ADC reading is a blocking function until the ADC conversion is over and the ADC buffer is read out.

Definition at line 745 of file init.c.

int ADC_read_POT_2 ( void  )

The function reads the the voltage from the potenziometer POT_2 connected to AUX_2 connector.

Parameters
@returnAnalog value from the Potenziometer POT_2 connected to AUX_2. The value is expressed in quantum.
Note
The ADC reading is a blocking function until the ADC conversion is over and the ADC buffer is read out.

Definition at line 760 of file init.c.

int ADC_read_TEMP ( void  )

The function reads the the voltage from the temperature sensor AN_TEMP and returns the reading expressed in celsius degree. This function is called from system_check () to monitor that the temperature is less than TEMP_MAX. If the themperature reachs TEMP_WARNING the fun is turned on.

Parameters
@returnAnalog value from the temperature sensor AN_TEMP expressed in celsius degree.
Note
The ADC reading is a blocking function until the ADC conversion is over and the ADC buffer is read out.

Definition at line 775 of file init.c.

int ADC_read_VIN_TEST ( void  )

The function reads the the voltage from the temperature sensor VIN_TEST and returns the reading expressed in V. This function is called from system_check () to monitor that the voltage is within VIN_MIN and VIN_MAX.

Parameters
@returnAnalog value from the temperature sensor VIN_TEST expressed in V.
Note
The ADC reading is a blocking function until the ADC conversion is over and the ADC buffer is read out.

Definition at line 790 of file init.c.

signed char board_initialization ( void  )

The functions initializes the board and hardware. All the PIC pins are properly set either as Input or Output. It should be called one time right after the main () function.

Parameters
@returnerror: Refer to the defined error constants.

Definition at line 94 of file init.c.

unsigned char emergency_stops_check ( void  )

The function checks the emergency stop conditions. If either the emergency stop in or out are activeted any motion activity is halted.

Parameters
@returnStatus: EMG_STOP_NOT_ACTIVATED, ERROR_EMG_STOP_ACTIVE

Definition at line 1045 of file init.c.

unsigned char get_dip_switch_value ( void  )

The function reads the position of the dip switches. Jumper JP1 must be properly set if dip switch 4 must be used.

Parameters
@returndip_switch_value It returns the dip switch value.

Definition at line 701 of file init.c.

unsigned char get_error_status ( void  )

This function returns the error status global variable.

Parameters
@returnError Status

Definition at line 1183 of file init.c.

unsigned char get_warning_status ( void  )

This function returns the warning status global variable.

Parameters
@returnError Status

Definition at line 1198 of file init.c.

signed char initialize_DC_motor ( unsigned char  modality,
int  pwm_frequency,
int  max_current 
)

Definition at line 246 of file init.c.

void initialize_interrupt ( unsigned char  interrupt_status)

The function allows enabling and disabling system interrupt.

Parameters
interrupt_statusIt specifies whether the interrupts need to be activated or not.
Returns

Definition at line 1150 of file init.c.

signed char initialize_RS485 ( unsigned char  baud_rate,
unsigned char  rx_interrupt_enable,
unsigned char  tx_interrupt_enable 
)

The function activates and initializes the UART used for the RS485 communication. It should be called before using the RS485 bus and after having initialized the board.

Parameters
baud_rateIs the communication baud rate. Value [BAUD_RATE_9600, BAUD_RATE_19200, BAUD_RATE_57600, BAUD_RATE_115200 ]
rx_interrupt_enableEnable or deactivate the RX interrupt. Value [RX_INTERRUPT_ON, RX_INTERRUPT_OFF]
tx_interrupt_enableEnable or deactivate the RX interrupt. Calue [TX_INTERRUPT_ON, TX_INTERRUPT_OFF]
Returns
error: Refer to the defined error constants.
Note
The UART settings, beside the baudrate that can be selected are: 8 bit format, 1 bit stop, No parity and No hardware handshaking.

Definition at line 564 of file init.c.

signed char initialize_stepper_motor ( unsigned char  modality,
unsigned char  pwm_frequency,
int  max_current 
)

The function initializes the stepper motor either in Full step, full step two phases or half step. The funtion should be called once, before using the stepper motor functions. It also sets the PWM at which the motor will be driven and the maximum current that will be monitored. Maximum current is monitored using the check_system () function, that should be called periodicaly.

Parameters
modalityIt sets the motor modality in Full step, full step two phases or half step. Value [FULL_STEP, FULL_STEP_2_PHASES, HALF_STEP)
pwm_frequencyIt sest the PWM frequency. Value [PWM_FREQ_1KHZ - PWM_FREQ_50KHZ].
max_currentIt sets the maximum pick current used by the motor expressed in mA. Value [min. 0, max. 5000]
Returns
error: Refer to the defined error constants.
Note
The PWM frequency is not optimyzed to let the user gets the maximum PWM resolution. Timer Prescaler should be set differently if an optimiziation is wanted or required.

Definition at line 840 of file init.c.

void initialize_TIMER0 ( unsigned char  interrupt_priority)

The function initializes the TIMER0 and activate the interuppt.

Parameters
interrupt_priorityIt specifies the Interrupt Priority Level
Returns

Definition at line 1064 of file init.c.

unsigned char is_emergency_stop_in_active ( void  )

The function checks if the emergency stop IN is activated. This is done monitoring the global flag associated to the emergency stop IN.

Parameters
@returnStatus: Emergency stop status. Value [ACTIVATED, DEACTIVATED]
Note
Status 1: EMG_STOP_IN is Active, 0: EMG_STOP_IN is not Active

Definition at line 989 of file init.c.

unsigned char is_emergency_stop_out_active ( void  )

The function checks if the emergency stop OUT is activated. This is done monitoring the global flag associated to the emergency stop OUT.

Parameters
@returnStatus: Emergency stop status. Value [ACTIVATED, DEACTIVATED]
Note
Status 1: EMG_STOP_OUT is Active, 0: EMG_STOP_OUT is not Active

Definition at line 977 of file init.c.

void power_control_PWM_set_period ( int  period)

The function sets the period of the Power Control PWM module (12 bit resolution).

Parameters
periodIt sets the period of the Power Control PWM module (12 bit resolution). Value [ min. 0 - max. 4095].
Returns
Warning
The actual resolution depends on the Frequency and Prescaler settings.
This function should be consdired Private so it should not be used within the user applications.

Definition at line 304 of file init.c.

signed char read_data_RS485 ( unsigned char *  data)

The function reads the data out of the UART input buffer. The UART Input buffer is filled with the data coming from the RS485 bus. The function is a non blocking function. If no data is within the buffer it returns ERROR_RX_DATA_NOT_READY, otherwise OPERATION_PROPERLY_EXECUTED.

Parameters
*dataPointer to the unsigned char that will contain the received data.
Returns
error: Refer to the defined error constants.

Definition at line 601 of file init.c.

unsigned char reset_emergency_stop_in ( void  )

The function resets the emergency stop IN and pin. The global flag associated to the emergency stop IN is also set to DEACTIVATED.

Parameters
@returnStatus: OPERATION_PROPERLY_EXECUTED
Note
Status 1: EMG_STOP_IN is Active, 0: EMG_STOP_IN is not Active

Definition at line 1033 of file init.c.

unsigned char reset_emergency_stop_out ( void  )

The function resets the emergency stop OUT and pin. The global flag associated to the emergency stop OUT is also set to DEACTIVATED.

Parameters
@returnStatus: OPERATION_PROPERLY_EXECUTED
Note
Status 1: EMG_STOP_OUT is Active, 0: EMG_STOP_OUT is not Active

Definition at line 1022 of file init.c.

signed char rotate_motor_anticlockwise ( unsigned char  motor,
int  speed 
)

Definition at line 411 of file init.c.

signed char rotate_motor_clockwise ( unsigned char  motor,
int  speed 
)

Definition at line 336 of file init.c.

void send_delete_RS485 ( void  )

The function sends a Delete ASCII code via RS485.

Parameters
@return

Definition at line 642 of file init.c.

void send_return_RS485 ( void  )

The function sends a Carriage Return via RS485.

Parameters
@return

Definition at line 629 of file init.c.

void set_duty_cycle_DC0 ( int  duty_cycle)

The function sets the period of the PWM 0 module (10 bit resolution).

Parameters
duty_cycleIt sets the period of the PWM 0 module (10 bit resolution). Value [ min. 0 - max. 1023].
Returns
Warning
The actual resolution depends on the Frequency and Prescaler settings.
This function should be consdired Private so it should not be used within the user applications.

Definition at line 314 of file init.c.

void set_duty_cycle_DC1 ( int  duty_cycle)

The function sets the period of the PWM 1 module (10 bit resolution).

Parameters
duty_cycleIt sets the period of the PWM 1 module (10 bit resolution). Value [ min. 0 - max. 1023].
Returns
Warning
The actual resolution depends on the Frequency and Prescaler settings.
This function should be consdired Private so it should not be used within the user applications.

Definition at line 325 of file init.c.

unsigned char set_emergency_stop_in ( void  )

The function sets the emergency stop IN to activated. The global flag associated to the emergency stop IN is also set to ACTIVATED.

Parameters
@returnStatus: OPERATION_PROPERLY_EXECUTED
Note
Status 1: EMG_STOP_IN is Active, 0: EMG_STOP_IN is not Active

Definition at line 1011 of file init.c.

unsigned char set_emergency_stop_out ( void  )

The function sets the emergency stop OUT to ACTIVATED and the EMG_OUT pin is set to 1. The global flag associated to the emergency stop OUT is also set to ACTIVATED.

Parameters
@returnStatus: OPERATION_PROPERLY_EXECUTED
Note
Status 1: EMG_STOP_OUT is Active, 0: EMG_STOP_OUT is not Active

Definition at line 1000 of file init.c.

void set_error_status ( unsigned char  error)

This function sest the error statud global variable accordingly to the Error flags.

Parameters
errorIt specifies the error to be added to the status
Returns

Definition at line 1176 of file init.c.

void set_warning_status ( unsigned char  warning)

This function sets the warning status global variable accordingly to the Warning flags.

Parameters
warningIt specifies the warning to be added to the status
Returns

Definition at line 1191 of file init.c.

signed char step_motor_anticlockwise ( int  current)

The function rotates the stepper motor anticlockwise at certain torque. The motors should be properly initialized before this function can be used.

Parameters
currentPhase current wanted for that rotation expressed in mA. Value [ min. 1 - max. 4000]
Returns
error: refer to the defined error constants
Warning
Currently the current is not monitored and the value does not acually represents the wanted current but the value written inside the PWM period register. For that reason care should be taken to not excede the maximum PWM resolution.

Definition at line 935 of file init.c.

signed char step_motor_clockwise ( int  current)

The function rotates the stepper motor clockwise at certain torque. The motors should be properly initialized before this function can be used.

Parameters
currentPhase current wanted for that rotation expressed in mA. Value [ min. 1 - max. 4000]
Returns
error: Refer to the defined error constants.
Warning
Currently the current is not monitored and the value does not acually represents the wanted current but the value written inside the PWM period register. For that reason care should be taken to not excede the maximum PWM resolution.

Definition at line 907 of file init.c.

signed char stop_motor_rotation ( unsigned char  motor)

The function initializes the DC motors either as LAP or Signed Magnitude modality. The function should be called just once, before using the motor functions. It also sets the PWM at which the motors will be driven and the maximum current that will be monitored. Maximum current is monitored using the check_system () function, that should be called periodicaly.

Parameters
modalityIt sets the motor modality eiter as LAP or Signed Magnitude. Value [LAP, SIGNED_MAGNITUDE]
pwm_frequencyIt sets the PWM frequency. Value [PWM_FREQ_1KHZ - PWM_FREQ_50KHZ].
max_currentit sets the maximum pick current used by the motor expressed in mA. Value [min. 1, max. 5000]
Returns
error: Refer to the defined error constants.
Note
The PWM frequency is not optimyzed to let the user gets the maximum PWM resolution. Timer Prescaler should be set differently if an optimiziation is wanted or required. The functions rotates the DC motor clockwise. The motors should be properly initialized before this function can be used.
Parameters
motorIndicates the motor that should be rotated clockwise. Value [MOTOR_1, MOTOR_2, MOTOR_ALL]
speedMotor speed. Value [ min. 1- max. 30000]
Returns
error: Refer to the defined error constants.
Warning
Currently the speed is not monitored and the value does not acually represents the rpm (root per minute)but the value written inside the PWM period register. For that reason care should be taken to not exceed the maximum PWM resolution. the function rotates the DC motor anticlockwise. The motors should be properly initialized before this function can be used.
Parameters
motorIndicates the motor that should be rotated anticlockwise. Value [MOTOR_1, MOTOR_2, MOTOR_ALL]
speedMotor speed. Value [ min. 1- max. 30000]
Returns
error: Refer to the defined error constants.
Warning
Currently the speed is not monitored and the value does not acually represents the rpm (root per minute)but the value written inside the PWM period register. For that reason care should be taken to not exceed the maximum PWM resolution. The function stops the motor rotation, either DC motor or stepper motor. The motors should be properly initialized before this function can be used. It could be used for an emergency stop, beside other actions.
Parameters
motorIndicates the motor that should be halted. Value [MOTOR_1, MOTOR_2, MOTOR_ALL]
Returns
error: Refer to the defined error constants.
Note
If a Stepper motor is used the input parameter is ignored [MOTOR_1, MOTOR_2, MOTOR_ALL], so any choise would be fine.. For code consistency MOTOR_1 is reccomended to be used.

Definition at line 485 of file init.c.

signed char system_check ( void  )

The function checks that the system is working within the maximum values. Temperature, Current and supply voltage are checked out.

Parameters
@returnStatus: system status
Note
If an anomaly is detected the set_emergency_stop_out is called and the system is halted. ERROR_LED is turned on.
If themperature gets hiher than TEMP_MAX the motors are halted. If Themperature gets higher than TEMP_WARNING, the fun is turned ON. The fun is turned OFF, only if the Themperature gets reduced to TEMP_WARNING - TEMP_HYSTERESIS.
Warning
This function should be called periodically. If not periodically called the system is not monitored.

Definition at line 189 of file init.c.

void turn_off_FAN ( void  )

The function turns the Fun OFF. If system_check is regularly called, the fun is turned off if the themperature gets lower than TEMP_WARNING - TEMP_HYSTERESIS.

Parameters
@return

Definition at line 970 of file init.c.

void turn_on_FAN ( void  )

The function turns the Fun ON. If system_check is regularly called, the fun is turned on if the themperature gets higher than TEMP_WARNING.

Parameters
@return

Definition at line 963 of file init.c.

signed char write_const_string_RS485 ( const char *  data)

The function writes a constant string stored to the rom area, to the UART. The function is a blocking function until the data is sent out. RS485 is automaticly changed to READ_WRITE mode and than back to READ_ONLY once the data is sent out.

Parameters
*dataPointer to a constant string (eg. write_const_string_RS485 ("Hello World")).
Returns
error: Refer to the defined error constants.

Definition at line 614 of file init.c.

signed char write_int_ASCII_RS485 ( int  value,
unsigned char  number_of_digits 
)

Definition at line 656 of file init.c.

Variable Documentation

signed char current_state = 0

Definition at line 48 of file init.c.

unsigned char emergency_stop_in = 0

Definition at line 59 of file init.c.

unsigned char emergency_stop_out = 0

Definition at line 55 of file init.c.

int motor_max_current = 0

Definition at line 70 of file init.c.

unsigned char motor_modality = 0

Definition at line 44 of file init.c.

unsigned char number_of_states = 0

Definition at line 51 of file init.c.

unsigned char stepper_state[8]

Definition at line 78 of file init.c.