This is a complete listing of the RS-EDP software for the mbed module to support the RS-EDP platform.

Dependencies:   mbed

Committer:
DavidGilesHitex
Date:
Fri Nov 19 09:49:16 2010 +0000
Revision:
0:5b7639d1f2c4

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
DavidGilesHitex 0:5b7639d1f2c4 1 /* Motor Drive Master Functions */
DavidGilesHitex 0:5b7639d1f2c4 2 /* **************************** */
DavidGilesHitex 0:5b7639d1f2c4 3
DavidGilesHitex 0:5b7639d1f2c4 4
DavidGilesHitex 0:5b7639d1f2c4 5 /* This module allows the MCU to act as a Master Controller in the system */
DavidGilesHitex 0:5b7639d1f2c4 6 /* It transmitts data down to the other Slave Modules and can also read parameters from the other dsPIC Slave Modules */
DavidGilesHitex 0:5b7639d1f2c4 7
DavidGilesHitex 0:5b7639d1f2c4 8
DavidGilesHitex 0:5b7639d1f2c4 9
DavidGilesHitex 0:5b7639d1f2c4 10
DavidGilesHitex 0:5b7639d1f2c4 11
DavidGilesHitex 0:5b7639d1f2c4 12 /* Includes Here */
DavidGilesHitex 0:5b7639d1f2c4 13 #include "mbed.h" /* mbed header file */
DavidGilesHitex 0:5b7639d1f2c4 14 #include "misra_types.h" /* MISRA Types header file */
DavidGilesHitex 0:5b7639d1f2c4 15 #include "defines.h"
DavidGilesHitex 0:5b7639d1f2c4 16 #include "RSEDP_Slave_Address_Defines.h" /* Slave address of I2C Peripherals */
DavidGilesHitex 0:5b7639d1f2c4 17
DavidGilesHitex 0:5b7639d1f2c4 18 #include "RSEDP_CNTRL_I2C.h" /* Control I2C Driver */
DavidGilesHitex 0:5b7639d1f2c4 19 #include "RS-EDP_AM_MC2_Globals.h" /* Global variables referenced here for MC2 Motor Drive Module */
DavidGilesHitex 0:5b7639d1f2c4 20
DavidGilesHitex 0:5b7639d1f2c4 21
DavidGilesHitex 0:5b7639d1f2c4 22
DavidGilesHitex 0:5b7639d1f2c4 23 /* Function Prototypes */
DavidGilesHitex 0:5b7639d1f2c4 24 /* Write Functions */
DavidGilesHitex 0:5b7639d1f2c4 25 uint8_t I2C0_dsPIC_Reset(uint8_t slave_address);
DavidGilesHitex 0:5b7639d1f2c4 26 uint8_t I2C0_dsPIC_Emergency_Stop(uint8_t slave_address);
DavidGilesHitex 0:5b7639d1f2c4 27 uint8_t I2C0_dsPIC_Normal_Stop(uint8_t slave_address);
DavidGilesHitex 0:5b7639d1f2c4 28 uint8_t I2C0_dsPIC_Ping(uint8_t slave_address);
DavidGilesHitex 0:5b7639d1f2c4 29 uint8_t I2C0_dsPIC_Set_Motor_Speed_Demand_Forward(uint8_t slave_address, uint16_t motor_speed_Demand);
DavidGilesHitex 0:5b7639d1f2c4 30 uint8_t I2C0_dsPIC_Set_Motor_Speed_Demand_Reverse(uint8_t slave_address, uint16_t motor_speed_Demand);
DavidGilesHitex 0:5b7639d1f2c4 31 uint8_t I2C0_dsPIC_Set_Ramp_Up_Speed(uint8_t slave_address, uint16_t ramp_up_speed);
DavidGilesHitex 0:5b7639d1f2c4 32 uint8_t I2C0_dsPIC_Set_Ramp_Down_Speed(uint8_t slave_address, uint16_t ramp_down_speed);
DavidGilesHitex 0:5b7639d1f2c4 33 uint8_t I2C0_dsPIC_Set_Motor_Direction(uint8_t slave_address, uint8_t direction);
DavidGilesHitex 0:5b7639d1f2c4 34 uint8_t I2C0_dsPIC_Start_Motor_Rotation(uint8_t slave_address);
DavidGilesHitex 0:5b7639d1f2c4 35
DavidGilesHitex 0:5b7639d1f2c4 36 uint8_t I2C0_dsPIC_Set_Rotation_Counts(uint8_t slave_address, uint32_t motor_rotation_counts);
DavidGilesHitex 0:5b7639d1f2c4 37 static uint8_t I2C0_dsPIC_Set_Rotation_Counts_High_Word(uint8_t slave_address, uint16_t motor_rotation_counts_high);
DavidGilesHitex 0:5b7639d1f2c4 38 static uint8_t I2C0_dsPIC_Set_Rotation_Counts_Low_Word(uint8_t slave_address, uint16_t motor_rotation_counts_low);
DavidGilesHitex 0:5b7639d1f2c4 39 uint8_t I2C0_dsPIC_Goto_Home(uint8_t slave_address, uint8_t home_direction, uint8_t home_speed);
DavidGilesHitex 0:5b7639d1f2c4 40
DavidGilesHitex 0:5b7639d1f2c4 41
DavidGilesHitex 0:5b7639d1f2c4 42 /* Read Functions */
DavidGilesHitex 0:5b7639d1f2c4 43 uint8_t I2C0_dsPIC_Read_Tacho_Speed_Instantaneous(uint8_t slave_address, uint8_t *rx_array);
DavidGilesHitex 0:5b7639d1f2c4 44 uint8_t I2C0_dsPIC_Read_Tacho_Speed_Average(uint8_t slave_address, uint8_t *rx_array);
DavidGilesHitex 0:5b7639d1f2c4 45 uint8_t I2C0_dsPIC_Read_Motor_Current_Instantaneous(uint8_t slave_address, uint8_t *rx_array);
DavidGilesHitex 0:5b7639d1f2c4 46 uint8_t I2C0_dsPIC_Read_Motor_Current_Average(uint8_t slave_address, uint8_t *rx_array);
DavidGilesHitex 0:5b7639d1f2c4 47 uint8_t I2C0_dsPIC_Read_Vbus_Instantaneous(uint8_t slave_address, uint8_t *rx_array);
DavidGilesHitex 0:5b7639d1f2c4 48 uint8_t I2C0_dsPIC_Read_Vbus_Average(uint8_t slave_address, uint8_t *rx_array);
DavidGilesHitex 0:5b7639d1f2c4 49 uint8_t I2C0_dsPIC_Read_Demand_Pot_Instantaneous(uint8_t slave_address, uint8_t *rx_array);
DavidGilesHitex 0:5b7639d1f2c4 50 uint8_t I2C0_dsPIC_Read_Demand_Pot_Average(uint8_t slave_address, uint8_t *rx_array);
DavidGilesHitex 0:5b7639d1f2c4 51 uint8_t I2C0_dsPIC_Read_Hall_Sensor_Positions(uint8_t slave_address, uint8_t *rx_array);
DavidGilesHitex 0:5b7639d1f2c4 52 uint8_t I2C0_dsPIC_Read_Motor_Status(uint8_t slave_address, uint8_t *rx_array);
DavidGilesHitex 0:5b7639d1f2c4 53 uint8_t I2C0_dsPIC_Read_Maximum_RPM(uint8_t slave_address, uint8_t *rx_array);
DavidGilesHitex 0:5b7639d1f2c4 54
DavidGilesHitex 0:5b7639d1f2c4 55 uint8_t I2C0_dsPIC_Read_Quad_Encoder_Counter(uint8_t slave_address, uint8_t *rx_array);
DavidGilesHitex 0:5b7639d1f2c4 56 static uint8_t I2C0_dsPIC_Read_Quad_Encoder_Counter_High(uint8_t slave_address, uint8_t *rx_array);
DavidGilesHitex 0:5b7639d1f2c4 57 static uint8_t I2C0_dsPIC_Read_Quad_Encoder_Counter_Low(uint8_t slave_address, uint8_t *rx_array);
DavidGilesHitex 0:5b7639d1f2c4 58
DavidGilesHitex 0:5b7639d1f2c4 59
DavidGilesHitex 0:5b7639d1f2c4 60
DavidGilesHitex 0:5b7639d1f2c4 61 /* Reset A Slave Unit */
DavidGilesHitex 0:5b7639d1f2c4 62 uint8_t I2C0_dsPIC_Reset(uint8_t slave_address)
DavidGilesHitex 0:5b7639d1f2c4 63 {
DavidGilesHitex 0:5b7639d1f2c4 64 /* 00 - Reset The Processor */
DavidGilesHitex 0:5b7639d1f2c4 65 /* data bytes to follow will be */
DavidGilesHitex 0:5b7639d1f2c4 66 /* databyte0 - 0x55 */
DavidGilesHitex 0:5b7639d1f2c4 67 /* databyte1 - 0xaa */
DavidGilesHitex 0:5b7639d1f2c4 68 uint8_t Ack_Error = 0u;
DavidGilesHitex 0:5b7639d1f2c4 69 sint8_t Tx_Array[3] = {0x00u, 0x55u, 0xaau};
DavidGilesHitex 0:5b7639d1f2c4 70
DavidGilesHitex 0:5b7639d1f2c4 71 Ack_Error = CNTRL_I2C_Master_Mode_Transmit(slave_address, Tx_Array, 3u);
DavidGilesHitex 0:5b7639d1f2c4 72 return Ack_Error;
DavidGilesHitex 0:5b7639d1f2c4 73 }
DavidGilesHitex 0:5b7639d1f2c4 74
DavidGilesHitex 0:5b7639d1f2c4 75
DavidGilesHitex 0:5b7639d1f2c4 76
DavidGilesHitex 0:5b7639d1f2c4 77 /* Generate an emergency Stop */
DavidGilesHitex 0:5b7639d1f2c4 78 uint8_t I2C0_dsPIC_Emergency_Stop(uint8_t slave_address)
DavidGilesHitex 0:5b7639d1f2c4 79 {
DavidGilesHitex 0:5b7639d1f2c4 80 /* 01 - Emergency Stop */
DavidGilesHitex 0:5b7639d1f2c4 81 /* data bytes to follow will be */
DavidGilesHitex 0:5b7639d1f2c4 82 /* databyte0 - 0x00 */
DavidGilesHitex 0:5b7639d1f2c4 83 /* databyte1 - 0x00 */
DavidGilesHitex 0:5b7639d1f2c4 84 uint8_t Ack_Error = 0u;
DavidGilesHitex 0:5b7639d1f2c4 85 sint8_t Tx_Array[3] = {0x01u, 0x00u, 0x00u};
DavidGilesHitex 0:5b7639d1f2c4 86
DavidGilesHitex 0:5b7639d1f2c4 87 Ack_Error = CNTRL_I2C_Master_Mode_Transmit(slave_address, Tx_Array, 3u);
DavidGilesHitex 0:5b7639d1f2c4 88 return Ack_Error;
DavidGilesHitex 0:5b7639d1f2c4 89 }
DavidGilesHitex 0:5b7639d1f2c4 90
DavidGilesHitex 0:5b7639d1f2c4 91
DavidGilesHitex 0:5b7639d1f2c4 92
DavidGilesHitex 0:5b7639d1f2c4 93 /* Generate a normal stop condition */
DavidGilesHitex 0:5b7639d1f2c4 94 uint8_t I2C0_dsPIC_Normal_Stop(uint8_t slave_address)
DavidGilesHitex 0:5b7639d1f2c4 95 {
DavidGilesHitex 0:5b7639d1f2c4 96 /* 02 - Normal Stop */
DavidGilesHitex 0:5b7639d1f2c4 97 /* data bytes to follow will be */
DavidGilesHitex 0:5b7639d1f2c4 98 /* databyte0 - 0x00 */
DavidGilesHitex 0:5b7639d1f2c4 99 /* databyte1 - 0x00 */
DavidGilesHitex 0:5b7639d1f2c4 100 uint8_t Ack_Error = 0u;
DavidGilesHitex 0:5b7639d1f2c4 101 sint8_t Tx_Array[3] = {0x02u, 0x00u, 0x00u};
DavidGilesHitex 0:5b7639d1f2c4 102
DavidGilesHitex 0:5b7639d1f2c4 103 Ack_Error = CNTRL_I2C_Master_Mode_Transmit(slave_address, Tx_Array, 3u);
DavidGilesHitex 0:5b7639d1f2c4 104 return Ack_Error;
DavidGilesHitex 0:5b7639d1f2c4 105 }
DavidGilesHitex 0:5b7639d1f2c4 106
DavidGilesHitex 0:5b7639d1f2c4 107
DavidGilesHitex 0:5b7639d1f2c4 108 /* Ping the Processor to see if it is attached to the bus */
DavidGilesHitex 0:5b7639d1f2c4 109 uint8_t I2C0_dsPIC_Ping(uint8_t slave_address)
DavidGilesHitex 0:5b7639d1f2c4 110 {
DavidGilesHitex 0:5b7639d1f2c4 111 /* 03 - Ping Stop */
DavidGilesHitex 0:5b7639d1f2c4 112 /* data bytes to follow will be */
DavidGilesHitex 0:5b7639d1f2c4 113 /* databyte0 - don't care */
DavidGilesHitex 0:5b7639d1f2c4 114 /* databyte1 - don't care */
DavidGilesHitex 0:5b7639d1f2c4 115 uint8_t Ack_Error = 0u;
DavidGilesHitex 0:5b7639d1f2c4 116 sint8_t Tx_Array[3] = {0x03u, 0x00u, 0x00u};
DavidGilesHitex 0:5b7639d1f2c4 117
DavidGilesHitex 0:5b7639d1f2c4 118 Ack_Error = CNTRL_I2C_Master_Mode_Transmit(slave_address, Tx_Array, 3u);
DavidGilesHitex 0:5b7639d1f2c4 119 return Ack_Error;
DavidGilesHitex 0:5b7639d1f2c4 120 }
DavidGilesHitex 0:5b7639d1f2c4 121
DavidGilesHitex 0:5b7639d1f2c4 122
DavidGilesHitex 0:5b7639d1f2c4 123
DavidGilesHitex 0:5b7639d1f2c4 124 /* Set the motor speed demand Forward */
DavidGilesHitex 0:5b7639d1f2c4 125 uint8_t I2C0_dsPIC_Set_Motor_Speed_Demand_Forward(uint8_t slave_address, uint16_t motor_speed_demand)
DavidGilesHitex 0:5b7639d1f2c4 126 {
DavidGilesHitex 0:5b7639d1f2c4 127 /* 10 - Set motor Speed Demand Forward */
DavidGilesHitex 0:5b7639d1f2c4 128 /* data bytes to follow will be a 16bit value in the range 0 - 1023 */
DavidGilesHitex 0:5b7639d1f2c4 129 /* databyte0 - highbyte */
DavidGilesHitex 0:5b7639d1f2c4 130 /* databyte1 - lowbyte */
DavidGilesHitex 0:5b7639d1f2c4 131 uint8_t Ack_Error = 0u;
DavidGilesHitex 0:5b7639d1f2c4 132 sint8_t Tx_Array[3] = {10u, 0x00u, 0x00u};
DavidGilesHitex 0:5b7639d1f2c4 133 Tx_Array[1] = (uint8_t)(motor_speed_demand / 256u);
DavidGilesHitex 0:5b7639d1f2c4 134 Tx_Array[2] = (uint8_t)(motor_speed_demand % 256u);
DavidGilesHitex 0:5b7639d1f2c4 135
DavidGilesHitex 0:5b7639d1f2c4 136 Ack_Error = CNTRL_I2C_Master_Mode_Transmit(slave_address, Tx_Array, 3u);
DavidGilesHitex 0:5b7639d1f2c4 137 return Ack_Error;
DavidGilesHitex 0:5b7639d1f2c4 138 }
DavidGilesHitex 0:5b7639d1f2c4 139
DavidGilesHitex 0:5b7639d1f2c4 140
DavidGilesHitex 0:5b7639d1f2c4 141
DavidGilesHitex 0:5b7639d1f2c4 142 /* Set the motor speed demand Reverse */
DavidGilesHitex 0:5b7639d1f2c4 143 uint8_t I2C0_dsPIC_Set_Motor_Speed_Demand_Reverse(uint8_t slave_address, uint16_t motor_speed_demand)
DavidGilesHitex 0:5b7639d1f2c4 144 {
DavidGilesHitex 0:5b7639d1f2c4 145 /* 11 - Set motor Speed Demand Forward */
DavidGilesHitex 0:5b7639d1f2c4 146 /* data bytes to follow will be a 16bit value in the range 0 - 1023 */
DavidGilesHitex 0:5b7639d1f2c4 147 /* databyte0 - highbyte */
DavidGilesHitex 0:5b7639d1f2c4 148 /* databyte1 - lowbyte */
DavidGilesHitex 0:5b7639d1f2c4 149 uint8_t Ack_Error = 0u;
DavidGilesHitex 0:5b7639d1f2c4 150 sint8_t Tx_Array[3] = {11u, 0x00u, 0x00u};
DavidGilesHitex 0:5b7639d1f2c4 151 Tx_Array[1] = (uint8_t)(motor_speed_demand / 256u);
DavidGilesHitex 0:5b7639d1f2c4 152 Tx_Array[2] = (uint8_t)(motor_speed_demand % 256u);
DavidGilesHitex 0:5b7639d1f2c4 153
DavidGilesHitex 0:5b7639d1f2c4 154 Ack_Error = CNTRL_I2C_Master_Mode_Transmit(slave_address, Tx_Array, 3u);
DavidGilesHitex 0:5b7639d1f2c4 155 return Ack_Error;
DavidGilesHitex 0:5b7639d1f2c4 156 }
DavidGilesHitex 0:5b7639d1f2c4 157
DavidGilesHitex 0:5b7639d1f2c4 158
DavidGilesHitex 0:5b7639d1f2c4 159
DavidGilesHitex 0:5b7639d1f2c4 160 /* Set the ramp up speed */
DavidGilesHitex 0:5b7639d1f2c4 161 uint8_t I2C0_dsPIC_Set_Ramp_Up_Speed(uint8_t slave_address, uint16_t ramp_up_speed)
DavidGilesHitex 0:5b7639d1f2c4 162 {
DavidGilesHitex 0:5b7639d1f2c4 163 /* 12 - Set Ramp Up Speed */
DavidGilesHitex 0:5b7639d1f2c4 164 /* data bytes to follow will be a 16bit value in the range 0 - 1023 */
DavidGilesHitex 0:5b7639d1f2c4 165 /* databyte0 - highbyte */
DavidGilesHitex 0:5b7639d1f2c4 166 /* databyte1 - lowbyte */
DavidGilesHitex 0:5b7639d1f2c4 167 uint8_t Ack_Error = 0u;
DavidGilesHitex 0:5b7639d1f2c4 168 sint8_t Tx_Array[3] = {12u, 0x00u, 0x00u};
DavidGilesHitex 0:5b7639d1f2c4 169 Tx_Array[1] = (uint8_t)(ramp_up_speed / 256u);
DavidGilesHitex 0:5b7639d1f2c4 170 Tx_Array[2] = (uint8_t)(ramp_up_speed % 256u);
DavidGilesHitex 0:5b7639d1f2c4 171
DavidGilesHitex 0:5b7639d1f2c4 172 Ack_Error = CNTRL_I2C_Master_Mode_Transmit(slave_address, Tx_Array, 3u);
DavidGilesHitex 0:5b7639d1f2c4 173 return Ack_Error;
DavidGilesHitex 0:5b7639d1f2c4 174 }
DavidGilesHitex 0:5b7639d1f2c4 175
DavidGilesHitex 0:5b7639d1f2c4 176
DavidGilesHitex 0:5b7639d1f2c4 177
DavidGilesHitex 0:5b7639d1f2c4 178 /* Set the ramp down speed */
DavidGilesHitex 0:5b7639d1f2c4 179 uint8_t I2C0_dsPIC_Set_Ramp_Down_Speed(uint8_t slave_address, uint16_t ramp_down_speed)
DavidGilesHitex 0:5b7639d1f2c4 180 {
DavidGilesHitex 0:5b7639d1f2c4 181 /* 13 - Set Ramp Up Speed */
DavidGilesHitex 0:5b7639d1f2c4 182 /* data bytes to follow will be a 16bit value in the range 0 - 1023 */
DavidGilesHitex 0:5b7639d1f2c4 183 /* databyte0 - highbyte */
DavidGilesHitex 0:5b7639d1f2c4 184 /* databyte1 - lowbyte */
DavidGilesHitex 0:5b7639d1f2c4 185 uint8_t Ack_Error = 0u;
DavidGilesHitex 0:5b7639d1f2c4 186 sint8_t Tx_Array[3] = {13u, 0x00u, 0x00u};
DavidGilesHitex 0:5b7639d1f2c4 187 Tx_Array[1] = (uint8_t)(ramp_down_speed / 256u);
DavidGilesHitex 0:5b7639d1f2c4 188 Tx_Array[2] = (uint8_t)(ramp_down_speed % 256u);
DavidGilesHitex 0:5b7639d1f2c4 189
DavidGilesHitex 0:5b7639d1f2c4 190 Ack_Error = CNTRL_I2C_Master_Mode_Transmit(slave_address, Tx_Array, 3u);
DavidGilesHitex 0:5b7639d1f2c4 191 return Ack_Error;
DavidGilesHitex 0:5b7639d1f2c4 192 }
DavidGilesHitex 0:5b7639d1f2c4 193
DavidGilesHitex 0:5b7639d1f2c4 194
DavidGilesHitex 0:5b7639d1f2c4 195
DavidGilesHitex 0:5b7639d1f2c4 196 /* Set the motor Direction */
DavidGilesHitex 0:5b7639d1f2c4 197 uint8_t I2C0_dsPIC_Set_Motor_Direction(uint8_t slave_address, uint8_t direction)
DavidGilesHitex 0:5b7639d1f2c4 198 {
DavidGilesHitex 0:5b7639d1f2c4 199 /* 14 - Set Motor Direction */
DavidGilesHitex 0:5b7639d1f2c4 200 /* data bytes to follow will be */
DavidGilesHitex 0:5b7639d1f2c4 201 /* databyte0 - don't care */
DavidGilesHitex 0:5b7639d1f2c4 202 /* databyte1 - direction */
DavidGilesHitex 0:5b7639d1f2c4 203 /* where direction = 0 - forward */
DavidGilesHitex 0:5b7639d1f2c4 204 /* where direction = 1 - reverse */
DavidGilesHitex 0:5b7639d1f2c4 205 uint8_t Ack_Error = 0u;
DavidGilesHitex 0:5b7639d1f2c4 206 sint8_t Tx_Array[3] = {14u, 0x00u, 0x00u};
DavidGilesHitex 0:5b7639d1f2c4 207
DavidGilesHitex 0:5b7639d1f2c4 208 Tx_Array[2] = direction;
DavidGilesHitex 0:5b7639d1f2c4 209
DavidGilesHitex 0:5b7639d1f2c4 210 Ack_Error = CNTRL_I2C_Master_Mode_Transmit(slave_address, Tx_Array, 3u);
DavidGilesHitex 0:5b7639d1f2c4 211 return Ack_Error;
DavidGilesHitex 0:5b7639d1f2c4 212 }
DavidGilesHitex 0:5b7639d1f2c4 213
DavidGilesHitex 0:5b7639d1f2c4 214
DavidGilesHitex 0:5b7639d1f2c4 215
DavidGilesHitex 0:5b7639d1f2c4 216 /* Motor Start */
DavidGilesHitex 0:5b7639d1f2c4 217 uint8_t I2C0_dsPIC_Start_Motor_Rotation(uint8_t slave_address)
DavidGilesHitex 0:5b7639d1f2c4 218 {
DavidGilesHitex 0:5b7639d1f2c4 219 /* 20 - Start Rotation */
DavidGilesHitex 0:5b7639d1f2c4 220 /* data bytes to follow will be */
DavidGilesHitex 0:5b7639d1f2c4 221 /* databyte0 - 0 */
DavidGilesHitex 0:5b7639d1f2c4 222 /* databyte1 - 0 */
DavidGilesHitex 0:5b7639d1f2c4 223 uint8_t Ack_Error = 0u;
DavidGilesHitex 0:5b7639d1f2c4 224 sint8_t Tx_Array[3] = {20u, 0x00u, 0x00u};
DavidGilesHitex 0:5b7639d1f2c4 225
DavidGilesHitex 0:5b7639d1f2c4 226 Ack_Error = CNTRL_I2C_Master_Mode_Transmit(slave_address, Tx_Array, 3u);
DavidGilesHitex 0:5b7639d1f2c4 227 return Ack_Error;
DavidGilesHitex 0:5b7639d1f2c4 228 }
DavidGilesHitex 0:5b7639d1f2c4 229
DavidGilesHitex 0:5b7639d1f2c4 230
DavidGilesHitex 0:5b7639d1f2c4 231
DavidGilesHitex 0:5b7639d1f2c4 232
DavidGilesHitex 0:5b7639d1f2c4 233 /* Send a 32byte Counts number to the slave peripheral */
DavidGilesHitex 0:5b7639d1f2c4 234 uint8_t I2C0_dsPIC_Set_Rotation_Counts(uint8_t slave_address, uint32_t motor_rotation_counts)
DavidGilesHitex 0:5b7639d1f2c4 235 {
DavidGilesHitex 0:5b7639d1f2c4 236 /* Set Rotation Counts */
DavidGilesHitex 0:5b7639d1f2c4 237 /* data bytes to follow will be a 32bit value - sent as two packets */
DavidGilesHitex 0:5b7639d1f2c4 238
DavidGilesHitex 0:5b7639d1f2c4 239 uint8_t Ack_Error = 0u;
DavidGilesHitex 0:5b7639d1f2c4 240
DavidGilesHitex 0:5b7639d1f2c4 241 Ack_Error = I2C0_dsPIC_Set_Rotation_Counts_High_Word(slave_address, (uint16_t)(motor_rotation_counts >> 16u));
DavidGilesHitex 0:5b7639d1f2c4 242
DavidGilesHitex 0:5b7639d1f2c4 243 if (Ack_Error == 0u)
DavidGilesHitex 0:5b7639d1f2c4 244 {
DavidGilesHitex 0:5b7639d1f2c4 245 Ack_Error = I2C0_dsPIC_Set_Rotation_Counts_Low_Word(slave_address, (uint16_t)(motor_rotation_counts));
DavidGilesHitex 0:5b7639d1f2c4 246 }
DavidGilesHitex 0:5b7639d1f2c4 247
DavidGilesHitex 0:5b7639d1f2c4 248 return Ack_Error;
DavidGilesHitex 0:5b7639d1f2c4 249 }
DavidGilesHitex 0:5b7639d1f2c4 250
DavidGilesHitex 0:5b7639d1f2c4 251 /* Send the first 16 bits count data across the I2C bus to the slave device */
DavidGilesHitex 0:5b7639d1f2c4 252 static uint8_t I2C0_dsPIC_Set_Rotation_Counts_High_Word(uint8_t slave_address, uint16_t motor_rotation_counts_high)
DavidGilesHitex 0:5b7639d1f2c4 253 {
DavidGilesHitex 0:5b7639d1f2c4 254 /* 21 - Set Rotation Counts High word */
DavidGilesHitex 0:5b7639d1f2c4 255 /* data bytes to follow will be a 16bit value- High word of the 32bit variables */
DavidGilesHitex 0:5b7639d1f2c4 256 /* databyte0 - highbyte (msb byte of the 32 bit word) */
DavidGilesHitex 0:5b7639d1f2c4 257 /* databyte1 - lowbyte */
DavidGilesHitex 0:5b7639d1f2c4 258 uint8_t Ack_Error = 0u;
DavidGilesHitex 0:5b7639d1f2c4 259 sint8_t Tx_Array[3] = {21u, 0x00u, 0x00u};
DavidGilesHitex 0:5b7639d1f2c4 260 Tx_Array[1] = (uint8_t)(motor_rotation_counts_high / 256u);
DavidGilesHitex 0:5b7639d1f2c4 261 Tx_Array[2] = (uint8_t)(motor_rotation_counts_high % 256u);
DavidGilesHitex 0:5b7639d1f2c4 262
DavidGilesHitex 0:5b7639d1f2c4 263 Ack_Error = CNTRL_I2C_Master_Mode_Transmit(slave_address, Tx_Array, 3u);
DavidGilesHitex 0:5b7639d1f2c4 264 return Ack_Error;
DavidGilesHitex 0:5b7639d1f2c4 265 }
DavidGilesHitex 0:5b7639d1f2c4 266
DavidGilesHitex 0:5b7639d1f2c4 267 /* Send the lower order 16 bit count data across the I2C network to the slave.*/
DavidGilesHitex 0:5b7639d1f2c4 268 static uint8_t I2C0_dsPIC_Set_Rotation_Counts_Low_Word(uint8_t slave_address, uint16_t motor_rotation_counts_low)
DavidGilesHitex 0:5b7639d1f2c4 269 {
DavidGilesHitex 0:5b7639d1f2c4 270 /* 22 - Set Rotation Counts Low word */
DavidGilesHitex 0:5b7639d1f2c4 271 /* data bytes to follow will be a 16bit value- Low word of the 32bit variables */
DavidGilesHitex 0:5b7639d1f2c4 272 /* databyte0 - highbyte */
DavidGilesHitex 0:5b7639d1f2c4 273 /* databyte1 - lowbyte */
DavidGilesHitex 0:5b7639d1f2c4 274 uint8_t Ack_Error = 0u;
DavidGilesHitex 0:5b7639d1f2c4 275 sint8_t Tx_Array[3] = {22u, 0x00u, 0x00u};
DavidGilesHitex 0:5b7639d1f2c4 276 Tx_Array[1] = (uint8_t)(motor_rotation_counts_low / 256u);
DavidGilesHitex 0:5b7639d1f2c4 277 Tx_Array[2] = (uint8_t)(motor_rotation_counts_low % 256u);
DavidGilesHitex 0:5b7639d1f2c4 278
DavidGilesHitex 0:5b7639d1f2c4 279 Ack_Error = CNTRL_I2C_Master_Mode_Transmit(slave_address, Tx_Array, 3u);
DavidGilesHitex 0:5b7639d1f2c4 280 return Ack_Error;
DavidGilesHitex 0:5b7639d1f2c4 281 }
DavidGilesHitex 0:5b7639d1f2c4 282
DavidGilesHitex 0:5b7639d1f2c4 283
DavidGilesHitex 0:5b7639d1f2c4 284
DavidGilesHitex 0:5b7639d1f2c4 285 /* Goto home position */
DavidGilesHitex 0:5b7639d1f2c4 286 uint8_t I2C0_dsPIC_Goto_Home(uint8_t slave_address, uint8_t home_direction, uint8_t home_speed)
DavidGilesHitex 0:5b7639d1f2c4 287 {
DavidGilesHitex 0:5b7639d1f2c4 288 /* 24 - Goto Home Position */
DavidGilesHitex 0:5b7639d1f2c4 289 /* data bytes to follow will be */
DavidGilesHitex 0:5b7639d1f2c4 290 /* databyte0 - direction */
DavidGilesHitex 0:5b7639d1f2c4 291 /* databyte1 - home_speed (8 bit limited 1-255) */
DavidGilesHitex 0:5b7639d1f2c4 292 uint8_t Ack_Error = 0u;
DavidGilesHitex 0:5b7639d1f2c4 293 sint8_t Tx_Array[3] = {24u, 0u, 0u};
DavidGilesHitex 0:5b7639d1f2c4 294 Tx_Array[1] = home_direction;
DavidGilesHitex 0:5b7639d1f2c4 295 Tx_Array[2] = home_speed;
DavidGilesHitex 0:5b7639d1f2c4 296 Ack_Error = CNTRL_I2C_Master_Mode_Transmit(slave_address, Tx_Array, 3u);
DavidGilesHitex 0:5b7639d1f2c4 297 return Ack_Error;
DavidGilesHitex 0:5b7639d1f2c4 298
DavidGilesHitex 0:5b7639d1f2c4 299 }
DavidGilesHitex 0:5b7639d1f2c4 300
DavidGilesHitex 0:5b7639d1f2c4 301
DavidGilesHitex 0:5b7639d1f2c4 302
DavidGilesHitex 0:5b7639d1f2c4 303
DavidGilesHitex 0:5b7639d1f2c4 304
DavidGilesHitex 0:5b7639d1f2c4 305
DavidGilesHitex 0:5b7639d1f2c4 306 /* Read Functions Below Here */
DavidGilesHitex 0:5b7639d1f2c4 307
DavidGilesHitex 0:5b7639d1f2c4 308 /* Read The Instantaneous RPM figure */
DavidGilesHitex 0:5b7639d1f2c4 309 uint8_t I2C0_dsPIC_Read_Tacho_Speed_Instantaneous(uint8_t slave_address, uint8_t *rx_array)
DavidGilesHitex 0:5b7639d1f2c4 310 {
DavidGilesHitex 0:5b7639d1f2c4 311 /* 50 - Read tacho speed instantaneous RPM */
DavidGilesHitex 0:5b7639d1f2c4 312 /* data bytes to follow will be the instantaneous RPM data */
DavidGilesHitex 0:5b7639d1f2c4 313 /* databyte0 - highbyte */
DavidGilesHitex 0:5b7639d1f2c4 314 /* databyte1 - lowbyte */
DavidGilesHitex 0:5b7639d1f2c4 315 uint8_t Ack_Error = 0u;
DavidGilesHitex 0:5b7639d1f2c4 316 sint8_t Tx_Array[1] = {50u};
DavidGilesHitex 0:5b7639d1f2c4 317 sint8_t Rx_Temp[2] = {0u, 0u};
DavidGilesHitex 0:5b7639d1f2c4 318
DavidGilesHitex 0:5b7639d1f2c4 319 Ack_Error = CNTRL_I2C_Master_Mode_Transmit(slave_address, Tx_Array, 1u); /* Send the command Byte */
DavidGilesHitex 0:5b7639d1f2c4 320
DavidGilesHitex 0:5b7639d1f2c4 321 if (Ack_Error == 0u)
DavidGilesHitex 0:5b7639d1f2c4 322 {
DavidGilesHitex 0:5b7639d1f2c4 323 Ack_Error = CNTRL_I2C_Master_Mode_Receive(slave_address, Rx_Temp, 2u); /* Read the return data */
DavidGilesHitex 0:5b7639d1f2c4 324 rx_array[0] = (uint8_t) Rx_Temp[0];
DavidGilesHitex 0:5b7639d1f2c4 325 rx_array[1] = (uint8_t) Rx_Temp[1];
DavidGilesHitex 0:5b7639d1f2c4 326
DavidGilesHitex 0:5b7639d1f2c4 327 }
DavidGilesHitex 0:5b7639d1f2c4 328 return Ack_Error;
DavidGilesHitex 0:5b7639d1f2c4 329 }
DavidGilesHitex 0:5b7639d1f2c4 330
DavidGilesHitex 0:5b7639d1f2c4 331
DavidGilesHitex 0:5b7639d1f2c4 332
DavidGilesHitex 0:5b7639d1f2c4 333 /* Read The Average RPM figure */
DavidGilesHitex 0:5b7639d1f2c4 334 uint8_t I2C0_dsPIC_Read_Tacho_Speed_Average(uint8_t slave_address, uint8_t *rx_array)
DavidGilesHitex 0:5b7639d1f2c4 335 {
DavidGilesHitex 0:5b7639d1f2c4 336 /* 51 - Read tacho speed average RPM */
DavidGilesHitex 0:5b7639d1f2c4 337 /* data bytes to follow will be the average RPM data */
DavidGilesHitex 0:5b7639d1f2c4 338 /* databyte0 - highbyte */
DavidGilesHitex 0:5b7639d1f2c4 339 /* databyte1 - lowbyte */
DavidGilesHitex 0:5b7639d1f2c4 340 uint8_t Ack_Error = 0u;
DavidGilesHitex 0:5b7639d1f2c4 341 sint8_t Tx_Array[1] = {51u};
DavidGilesHitex 0:5b7639d1f2c4 342 sint8_t Rx_Temp[2] = {0u, 0u};
DavidGilesHitex 0:5b7639d1f2c4 343
DavidGilesHitex 0:5b7639d1f2c4 344 Ack_Error = CNTRL_I2C_Master_Mode_Transmit(slave_address, Tx_Array, 1u); /* Send the command Byte */
DavidGilesHitex 0:5b7639d1f2c4 345
DavidGilesHitex 0:5b7639d1f2c4 346 if (Ack_Error == 0u)
DavidGilesHitex 0:5b7639d1f2c4 347 {
DavidGilesHitex 0:5b7639d1f2c4 348 Ack_Error = CNTRL_I2C_Master_Mode_Receive(slave_address, Rx_Temp, 2u); /* Read the return data */
DavidGilesHitex 0:5b7639d1f2c4 349 rx_array[0] = (uint8_t) Rx_Temp[0];
DavidGilesHitex 0:5b7639d1f2c4 350 rx_array[1] = (uint8_t) Rx_Temp[1];
DavidGilesHitex 0:5b7639d1f2c4 351 }
DavidGilesHitex 0:5b7639d1f2c4 352 return Ack_Error;
DavidGilesHitex 0:5b7639d1f2c4 353 }
DavidGilesHitex 0:5b7639d1f2c4 354
DavidGilesHitex 0:5b7639d1f2c4 355
DavidGilesHitex 0:5b7639d1f2c4 356
DavidGilesHitex 0:5b7639d1f2c4 357
DavidGilesHitex 0:5b7639d1f2c4 358 /* Read The Instantaneous Motor Current */
DavidGilesHitex 0:5b7639d1f2c4 359 uint8_t I2C0_dsPIC_Read_Motor_Current_Instantaneous(uint8_t slave_address, uint8_t *rx_array)
DavidGilesHitex 0:5b7639d1f2c4 360 {
DavidGilesHitex 0:5b7639d1f2c4 361 /* 52 - Read Motor Current Instantaneous in mA */
DavidGilesHitex 0:5b7639d1f2c4 362 /* data bytes to follow will be the instantaneous motor current in mA */
DavidGilesHitex 0:5b7639d1f2c4 363 /* databyte0 - highbyte */
DavidGilesHitex 0:5b7639d1f2c4 364 uint8_t Ack_Error = 0u;
DavidGilesHitex 0:5b7639d1f2c4 365 sint8_t Tx_Array[1] = {52u};
DavidGilesHitex 0:5b7639d1f2c4 366 sint8_t Rx_Temp[2] = {0u, 0u};
DavidGilesHitex 0:5b7639d1f2c4 367
DavidGilesHitex 0:5b7639d1f2c4 368 Ack_Error = CNTRL_I2C_Master_Mode_Transmit(slave_address, Tx_Array, 1u); /* Send the command Byte */
DavidGilesHitex 0:5b7639d1f2c4 369
DavidGilesHitex 0:5b7639d1f2c4 370 if (Ack_Error == 0u)
DavidGilesHitex 0:5b7639d1f2c4 371 {
DavidGilesHitex 0:5b7639d1f2c4 372 Ack_Error = CNTRL_I2C_Master_Mode_Receive(slave_address, Rx_Temp, 2u); /* Read the return data */
DavidGilesHitex 0:5b7639d1f2c4 373 }
DavidGilesHitex 0:5b7639d1f2c4 374 return Ack_Error;
DavidGilesHitex 0:5b7639d1f2c4 375 }
DavidGilesHitex 0:5b7639d1f2c4 376
DavidGilesHitex 0:5b7639d1f2c4 377
DavidGilesHitex 0:5b7639d1f2c4 378
DavidGilesHitex 0:5b7639d1f2c4 379 /* Read The Average Motor Current */
DavidGilesHitex 0:5b7639d1f2c4 380 uint8_t I2C0_dsPIC_Read_Motor_Current_Average(uint8_t slave_address, uint8_t *rx_array)
DavidGilesHitex 0:5b7639d1f2c4 381 {
DavidGilesHitex 0:5b7639d1f2c4 382 /* 53 - Read Motor Current Average */
DavidGilesHitex 0:5b7639d1f2c4 383 /* data bytes to follow will be the computed average motor current in mA */
DavidGilesHitex 0:5b7639d1f2c4 384 /* databyte0 - highbyte */
DavidGilesHitex 0:5b7639d1f2c4 385 /* databyte1 - lowbyte */
DavidGilesHitex 0:5b7639d1f2c4 386 uint8_t Ack_Error = 0u;
DavidGilesHitex 0:5b7639d1f2c4 387 sint8_t Tx_Array[1] = {53u};
DavidGilesHitex 0:5b7639d1f2c4 388 sint8_t Rx_Temp[2] = {0u, 0u};
DavidGilesHitex 0:5b7639d1f2c4 389
DavidGilesHitex 0:5b7639d1f2c4 390 Ack_Error = CNTRL_I2C_Master_Mode_Transmit(slave_address, Tx_Array, 1u); /* Send the command Byte */
DavidGilesHitex 0:5b7639d1f2c4 391
DavidGilesHitex 0:5b7639d1f2c4 392 if (Ack_Error == 0u)
DavidGilesHitex 0:5b7639d1f2c4 393 {
DavidGilesHitex 0:5b7639d1f2c4 394 Ack_Error = CNTRL_I2C_Master_Mode_Receive(slave_address, Rx_Temp, 2u); /* Read the return data */
DavidGilesHitex 0:5b7639d1f2c4 395 rx_array[0] = (uint8_t) Rx_Temp[0];
DavidGilesHitex 0:5b7639d1f2c4 396 rx_array[1] = (uint8_t) Rx_Temp[1];
DavidGilesHitex 0:5b7639d1f2c4 397 }
DavidGilesHitex 0:5b7639d1f2c4 398 return Ack_Error;
DavidGilesHitex 0:5b7639d1f2c4 399
DavidGilesHitex 0:5b7639d1f2c4 400
DavidGilesHitex 0:5b7639d1f2c4 401 }
DavidGilesHitex 0:5b7639d1f2c4 402
DavidGilesHitex 0:5b7639d1f2c4 403
DavidGilesHitex 0:5b7639d1f2c4 404 /* Measure the Vbus Bridge voltage */
DavidGilesHitex 0:5b7639d1f2c4 405 uint8_t I2C0_dsPIC_Read_Vbus_Instantaneous(uint8_t slave_address, uint8_t *rx_array)
DavidGilesHitex 0:5b7639d1f2c4 406 {
DavidGilesHitex 0:5b7639d1f2c4 407 /* 54 - Read Vbus Voltage Instantaneous */
DavidGilesHitex 0:5b7639d1f2c4 408 /* data bytes to follow will be the instantaneous Vbus Power Supply Voltage to the bridge in mV */
DavidGilesHitex 0:5b7639d1f2c4 409 /* databyte0 - highbyte */
DavidGilesHitex 0:5b7639d1f2c4 410 /* databyte1 - lowbyte */
DavidGilesHitex 0:5b7639d1f2c4 411 uint8_t Ack_Error = 0u;
DavidGilesHitex 0:5b7639d1f2c4 412 sint8_t Tx_Array[1] = {54u};
DavidGilesHitex 0:5b7639d1f2c4 413 sint8_t Rx_Temp[2] = {0u, 0u};
DavidGilesHitex 0:5b7639d1f2c4 414
DavidGilesHitex 0:5b7639d1f2c4 415 Ack_Error = CNTRL_I2C_Master_Mode_Transmit(slave_address, Tx_Array, 1u); /* Send the command Byte */
DavidGilesHitex 0:5b7639d1f2c4 416
DavidGilesHitex 0:5b7639d1f2c4 417 if (Ack_Error == 0u)
DavidGilesHitex 0:5b7639d1f2c4 418 {
DavidGilesHitex 0:5b7639d1f2c4 419 Ack_Error = CNTRL_I2C_Master_Mode_Receive(slave_address, Rx_Temp, 2u); /* Read the return data */
DavidGilesHitex 0:5b7639d1f2c4 420 rx_array[0] = (uint8_t) Rx_Temp[0];
DavidGilesHitex 0:5b7639d1f2c4 421 rx_array[1] = (uint8_t) Rx_Temp[1];
DavidGilesHitex 0:5b7639d1f2c4 422 }
DavidGilesHitex 0:5b7639d1f2c4 423 return Ack_Error;
DavidGilesHitex 0:5b7639d1f2c4 424 }
DavidGilesHitex 0:5b7639d1f2c4 425
DavidGilesHitex 0:5b7639d1f2c4 426
DavidGilesHitex 0:5b7639d1f2c4 427
DavidGilesHitex 0:5b7639d1f2c4 428 /* Measure the Vbus Bridge Voltage Average */
DavidGilesHitex 0:5b7639d1f2c4 429 uint8_t I2C0_dsPIC_Read_Vbus_Average(uint8_t slave_address, uint8_t *rx_array)
DavidGilesHitex 0:5b7639d1f2c4 430 {
DavidGilesHitex 0:5b7639d1f2c4 431 /* 55 - Read Vbus Voltage Instantaneous */
DavidGilesHitex 0:5b7639d1f2c4 432 /* data bytes to follow will be the computed average Vbus Power Supply Voltage to the bridge in mV */
DavidGilesHitex 0:5b7639d1f2c4 433 /* databyte0 - highbyte */
DavidGilesHitex 0:5b7639d1f2c4 434 /* databyte1 - lowbyte */
DavidGilesHitex 0:5b7639d1f2c4 435 uint8_t Ack_Error = 0u;
DavidGilesHitex 0:5b7639d1f2c4 436 sint8_t Tx_Array[1] = {55u};
DavidGilesHitex 0:5b7639d1f2c4 437 sint8_t Rx_Temp[2] = {0u, 0u};
DavidGilesHitex 0:5b7639d1f2c4 438
DavidGilesHitex 0:5b7639d1f2c4 439 Ack_Error = CNTRL_I2C_Master_Mode_Transmit(slave_address, Tx_Array, 1u); /* Send the command Byte */
DavidGilesHitex 0:5b7639d1f2c4 440
DavidGilesHitex 0:5b7639d1f2c4 441 if (Ack_Error == 0u)
DavidGilesHitex 0:5b7639d1f2c4 442 {
DavidGilesHitex 0:5b7639d1f2c4 443 Ack_Error = CNTRL_I2C_Master_Mode_Receive(slave_address, Rx_Temp, 2u); /* Read the return data */
DavidGilesHitex 0:5b7639d1f2c4 444 rx_array[0] = (uint8_t) Rx_Temp[0];
DavidGilesHitex 0:5b7639d1f2c4 445 rx_array[1] = (uint8_t) Rx_Temp[1];
DavidGilesHitex 0:5b7639d1f2c4 446 }
DavidGilesHitex 0:5b7639d1f2c4 447 return Ack_Error;
DavidGilesHitex 0:5b7639d1f2c4 448 }
DavidGilesHitex 0:5b7639d1f2c4 449
DavidGilesHitex 0:5b7639d1f2c4 450
DavidGilesHitex 0:5b7639d1f2c4 451 /* Measure The Demand Pot Instantaneous */
DavidGilesHitex 0:5b7639d1f2c4 452 uint8_t I2C0_dsPIC_Read_Demand_Pot_Instantaneous(uint8_t slave_address, uint8_t *rx_array)
DavidGilesHitex 0:5b7639d1f2c4 453 {
DavidGilesHitex 0:5b7639d1f2c4 454 /* 56 - Read Demand Pot instantaneous */
DavidGilesHitex 0:5b7639d1f2c4 455 /* data bytes to follow will be the instanteneous Pot Demand setting in the range 0 - 1023 */
DavidGilesHitex 0:5b7639d1f2c4 456 /* databyte0 - highbyte */
DavidGilesHitex 0:5b7639d1f2c4 457 /* databyte1 - lowbyte */
DavidGilesHitex 0:5b7639d1f2c4 458 uint8_t Ack_Error = 0u;
DavidGilesHitex 0:5b7639d1f2c4 459 sint8_t Tx_Array[1] = {56u};
DavidGilesHitex 0:5b7639d1f2c4 460 sint8_t Rx_Temp[2] = {0u, 0u};
DavidGilesHitex 0:5b7639d1f2c4 461
DavidGilesHitex 0:5b7639d1f2c4 462 Ack_Error = CNTRL_I2C_Master_Mode_Transmit(slave_address, Tx_Array, 1u); /* Send the command Byte */
DavidGilesHitex 0:5b7639d1f2c4 463
DavidGilesHitex 0:5b7639d1f2c4 464 if (Ack_Error == 0u)
DavidGilesHitex 0:5b7639d1f2c4 465 {
DavidGilesHitex 0:5b7639d1f2c4 466 Ack_Error = CNTRL_I2C_Master_Mode_Receive(slave_address, Rx_Temp, 2u); /* Read the return data */
DavidGilesHitex 0:5b7639d1f2c4 467 rx_array[0] = (uint8_t) Rx_Temp[0];
DavidGilesHitex 0:5b7639d1f2c4 468 rx_array[1] = (uint8_t) Rx_Temp[1];
DavidGilesHitex 0:5b7639d1f2c4 469 }
DavidGilesHitex 0:5b7639d1f2c4 470 return Ack_Error;
DavidGilesHitex 0:5b7639d1f2c4 471 }
DavidGilesHitex 0:5b7639d1f2c4 472
DavidGilesHitex 0:5b7639d1f2c4 473
DavidGilesHitex 0:5b7639d1f2c4 474 uint8_t I2C0_dsPIC_Read_Demand_Pot_Average(uint8_t slave_address, uint8_t *rx_array)
DavidGilesHitex 0:5b7639d1f2c4 475 {
DavidGilesHitex 0:5b7639d1f2c4 476 /* 57 - Read Demand Pot average */
DavidGilesHitex 0:5b7639d1f2c4 477 /* data bytes to follow will be the average Pot Demand setting in the range 0 - 1023 */
DavidGilesHitex 0:5b7639d1f2c4 478 /* databyte0 - highbyte */
DavidGilesHitex 0:5b7639d1f2c4 479 /* databyte1 - lowbyte */
DavidGilesHitex 0:5b7639d1f2c4 480 uint8_t Ack_Error = 0u;
DavidGilesHitex 0:5b7639d1f2c4 481 sint8_t Tx_Array[1] = {57u};
DavidGilesHitex 0:5b7639d1f2c4 482 sint8_t Rx_Temp[2] = {0u, 0u};
DavidGilesHitex 0:5b7639d1f2c4 483
DavidGilesHitex 0:5b7639d1f2c4 484 Ack_Error = CNTRL_I2C_Master_Mode_Transmit(slave_address, Tx_Array, 1u); /* Send the command Byte */
DavidGilesHitex 0:5b7639d1f2c4 485
DavidGilesHitex 0:5b7639d1f2c4 486 if (Ack_Error == 0u)
DavidGilesHitex 0:5b7639d1f2c4 487 {
DavidGilesHitex 0:5b7639d1f2c4 488 Ack_Error = CNTRL_I2C_Master_Mode_Receive(slave_address, Rx_Temp, 2u); /* Read the return data */
DavidGilesHitex 0:5b7639d1f2c4 489 rx_array[0] = (uint8_t) Rx_Temp[0];
DavidGilesHitex 0:5b7639d1f2c4 490 rx_array[1] = (uint8_t) Rx_Temp[1];
DavidGilesHitex 0:5b7639d1f2c4 491 }
DavidGilesHitex 0:5b7639d1f2c4 492 return Ack_Error;
DavidGilesHitex 0:5b7639d1f2c4 493 }
DavidGilesHitex 0:5b7639d1f2c4 494
DavidGilesHitex 0:5b7639d1f2c4 495
DavidGilesHitex 0:5b7639d1f2c4 496
DavidGilesHitex 0:5b7639d1f2c4 497 /* Read The Hall Effect Sensors */
DavidGilesHitex 0:5b7639d1f2c4 498 uint8_t I2C0_dsPIC_Read_Hall_Sensor_Positions(uint8_t slave_address, uint8_t *rx_array)
DavidGilesHitex 0:5b7639d1f2c4 499 {
DavidGilesHitex 0:5b7639d1f2c4 500 /* 58 - Read Hall Sensors Positions */
DavidGilesHitex 0:5b7639d1f2c4 501 /* data bytes to follow will be the hall sensor reading */
DavidGilesHitex 0:5b7639d1f2c4 502 /* databyte0 - 0 */
DavidGilesHitex 0:5b7639d1f2c4 503 /* databyte1 - hall effect sensors */
DavidGilesHitex 0:5b7639d1f2c4 504 /* where halleffect reading in bit form is 0b 0000 0ABC, where A is the logic level on the Hall Input A ('1' or '0') etc */
DavidGilesHitex 0:5b7639d1f2c4 505 uint8_t Ack_Error = 0u;
DavidGilesHitex 0:5b7639d1f2c4 506 sint8_t Tx_Array[1] = {58u};
DavidGilesHitex 0:5b7639d1f2c4 507 sint8_t Rx_Temp[2] = {0u, 0u};
DavidGilesHitex 0:5b7639d1f2c4 508
DavidGilesHitex 0:5b7639d1f2c4 509 Ack_Error = CNTRL_I2C_Master_Mode_Transmit(slave_address, Tx_Array, 1u); /* Send the command Byte */
DavidGilesHitex 0:5b7639d1f2c4 510
DavidGilesHitex 0:5b7639d1f2c4 511 if (Ack_Error == 0u)
DavidGilesHitex 0:5b7639d1f2c4 512 {
DavidGilesHitex 0:5b7639d1f2c4 513 Ack_Error = CNTRL_I2C_Master_Mode_Receive(slave_address, Rx_Temp, 2u); /* Read the return data */
DavidGilesHitex 0:5b7639d1f2c4 514 rx_array[0] = (uint8_t) Rx_Temp[0];
DavidGilesHitex 0:5b7639d1f2c4 515 rx_array[1] = (uint8_t) Rx_Temp[1];
DavidGilesHitex 0:5b7639d1f2c4 516 }
DavidGilesHitex 0:5b7639d1f2c4 517 return Ack_Error;
DavidGilesHitex 0:5b7639d1f2c4 518 }
DavidGilesHitex 0:5b7639d1f2c4 519
DavidGilesHitex 0:5b7639d1f2c4 520
DavidGilesHitex 0:5b7639d1f2c4 521
DavidGilesHitex 0:5b7639d1f2c4 522 /* Read The Fault Status Register */
DavidGilesHitex 0:5b7639d1f2c4 523 uint8_t I2C0_dsPIC_Read_Motor_Status(uint8_t slave_address, uint8_t *rx_array)
DavidGilesHitex 0:5b7639d1f2c4 524 {
DavidGilesHitex 0:5b7639d1f2c4 525 /* 59 - Read Motor Status - run/stop/direction/Faults etc */
DavidGilesHitex 0:5b7639d1f2c4 526 /* data bytes to follow will be the Fault Status Register */
DavidGilesHitex 0:5b7639d1f2c4 527 /* databyte0 - Flags Status Register - high */
DavidGilesHitex 0:5b7639d1f2c4 528 /* databyte1 - Flags Status Register - low */
DavidGilesHitex 0:5b7639d1f2c4 529 uint8_t Ack_Error = 0u;
DavidGilesHitex 0:5b7639d1f2c4 530 sint8_t Tx_Array[1] = {59u};
DavidGilesHitex 0:5b7639d1f2c4 531 sint8_t Rx_Temp[2] = {0u, 0u};
DavidGilesHitex 0:5b7639d1f2c4 532
DavidGilesHitex 0:5b7639d1f2c4 533 Ack_Error = CNTRL_I2C_Master_Mode_Transmit(slave_address, Tx_Array, 1u); /* Send the command Byte */
DavidGilesHitex 0:5b7639d1f2c4 534
DavidGilesHitex 0:5b7639d1f2c4 535 if (Ack_Error == 0u)
DavidGilesHitex 0:5b7639d1f2c4 536 {
DavidGilesHitex 0:5b7639d1f2c4 537 Ack_Error = CNTRL_I2C_Master_Mode_Receive(slave_address, Rx_Temp, 2u); /* Read the return data */
DavidGilesHitex 0:5b7639d1f2c4 538 rx_array[0] = (uint8_t) Rx_Temp[0];
DavidGilesHitex 0:5b7639d1f2c4 539 rx_array[1] = (uint8_t) Rx_Temp[1];
DavidGilesHitex 0:5b7639d1f2c4 540 }
DavidGilesHitex 0:5b7639d1f2c4 541 return Ack_Error;
DavidGilesHitex 0:5b7639d1f2c4 542 }
DavidGilesHitex 0:5b7639d1f2c4 543
DavidGilesHitex 0:5b7639d1f2c4 544
DavidGilesHitex 0:5b7639d1f2c4 545
DavidGilesHitex 0:5b7639d1f2c4 546 /* Read the Maximum RPM Information */
DavidGilesHitex 0:5b7639d1f2c4 547 uint8_t I2C0_dsPIC_Read_Maximum_RPM(uint8_t slave_address, uint8_t *rx_array)
DavidGilesHitex 0:5b7639d1f2c4 548 {
DavidGilesHitex 0:5b7639d1f2c4 549 /* 60 - Read The Maxium RPM information */
DavidGilesHitex 0:5b7639d1f2c4 550 /* data bytes to follow will be the Maximum RPM of the motor */
DavidGilesHitex 0:5b7639d1f2c4 551 /* databyte0 - Max RPM - high byte */
DavidGilesHitex 0:5b7639d1f2c4 552 /* databyte1 - Max RPM - low byte */
DavidGilesHitex 0:5b7639d1f2c4 553 uint8_t Ack_Error = 0u;
DavidGilesHitex 0:5b7639d1f2c4 554 sint8_t Tx_Array[1] = {60u};
DavidGilesHitex 0:5b7639d1f2c4 555 sint8_t Rx_Temp[2] = {0u, 0u};
DavidGilesHitex 0:5b7639d1f2c4 556
DavidGilesHitex 0:5b7639d1f2c4 557 Ack_Error = CNTRL_I2C_Master_Mode_Transmit(slave_address, Tx_Array, 1u); /* Send the command Byte */
DavidGilesHitex 0:5b7639d1f2c4 558
DavidGilesHitex 0:5b7639d1f2c4 559 if (Ack_Error == 0u)
DavidGilesHitex 0:5b7639d1f2c4 560 {
DavidGilesHitex 0:5b7639d1f2c4 561 Ack_Error = CNTRL_I2C_Master_Mode_Receive(slave_address, Rx_Temp, 2u); /* Read the return data */
DavidGilesHitex 0:5b7639d1f2c4 562 rx_array[0] = (uint8_t) Rx_Temp[0];
DavidGilesHitex 0:5b7639d1f2c4 563 rx_array[1] = (uint8_t) Rx_Temp[1];
DavidGilesHitex 0:5b7639d1f2c4 564 }
DavidGilesHitex 0:5b7639d1f2c4 565 return Ack_Error;
DavidGilesHitex 0:5b7639d1f2c4 566 }
DavidGilesHitex 0:5b7639d1f2c4 567
DavidGilesHitex 0:5b7639d1f2c4 568
DavidGilesHitex 0:5b7639d1f2c4 569 /* Read The 32bit Quadrature Encoder Counter */
DavidGilesHitex 0:5b7639d1f2c4 570 uint8_t I2C0_dsPIC_Read_Quad_Encoder_Counter(uint8_t slave_address, uint8_t *rx_array)
DavidGilesHitex 0:5b7639d1f2c4 571 {
DavidGilesHitex 0:5b7639d1f2c4 572 uint8_t Ack_Error = 0u;
DavidGilesHitex 0:5b7639d1f2c4 573
DavidGilesHitex 0:5b7639d1f2c4 574 Ack_Error = I2C0_dsPIC_Read_Quad_Encoder_Counter_High(slave_address, rx_array);
DavidGilesHitex 0:5b7639d1f2c4 575 if (Ack_Error == 0u)
DavidGilesHitex 0:5b7639d1f2c4 576 {
DavidGilesHitex 0:5b7639d1f2c4 577 Ack_Error = I2C0_dsPIC_Read_Quad_Encoder_Counter_Low(slave_address, (rx_array + 2u));
DavidGilesHitex 0:5b7639d1f2c4 578 }
DavidGilesHitex 0:5b7639d1f2c4 579 return Ack_Error;
DavidGilesHitex 0:5b7639d1f2c4 580 }
DavidGilesHitex 0:5b7639d1f2c4 581
DavidGilesHitex 0:5b7639d1f2c4 582
DavidGilesHitex 0:5b7639d1f2c4 583 /* Read The High Word of the 32ibt Quadrature Encoder */
DavidGilesHitex 0:5b7639d1f2c4 584 static uint8_t I2C0_dsPIC_Read_Quad_Encoder_Counter_High(uint8_t slave_address, uint8_t *rx_array)
DavidGilesHitex 0:5b7639d1f2c4 585 {
DavidGilesHitex 0:5b7639d1f2c4 586 /* 61 - Read Quadrature Counter (high byte) */
DavidGilesHitex 0:5b7639d1f2c4 587 /* data bytes to follow will be the Quadrature Encoder high bytes */
DavidGilesHitex 0:5b7639d1f2c4 588 /* databyte0 - Quadrature Encoder Counter - High Byte (byte3 MSB) */
DavidGilesHitex 0:5b7639d1f2c4 589 /* databyte1 - Quadrature Encoder Counterr - Low byte (byte2) */
DavidGilesHitex 0:5b7639d1f2c4 590 /* Reading this byte will freeze the low byte, allwoing glitchless reading of the 32 bit counter */
DavidGilesHitex 0:5b7639d1f2c4 591 uint8_t Ack_Error = 0u;
DavidGilesHitex 0:5b7639d1f2c4 592 sint8_t Tx_Array[1u] = {61u};
DavidGilesHitex 0:5b7639d1f2c4 593 sint8_t Rx_Temp[2] = {0u, 0u};
DavidGilesHitex 0:5b7639d1f2c4 594
DavidGilesHitex 0:5b7639d1f2c4 595 Ack_Error = CNTRL_I2C_Master_Mode_Transmit(slave_address, Tx_Array, 1u); /* Send the command Byte */
DavidGilesHitex 0:5b7639d1f2c4 596
DavidGilesHitex 0:5b7639d1f2c4 597 if (Ack_Error == 0u)
DavidGilesHitex 0:5b7639d1f2c4 598 {
DavidGilesHitex 0:5b7639d1f2c4 599 Ack_Error = CNTRL_I2C_Master_Mode_Receive(slave_address, Rx_Temp, 2u); /* Read the return data */
DavidGilesHitex 0:5b7639d1f2c4 600 rx_array[0] = (uint8_t) Rx_Temp[0];
DavidGilesHitex 0:5b7639d1f2c4 601 rx_array[1] = (uint8_t) Rx_Temp[1];
DavidGilesHitex 0:5b7639d1f2c4 602 }
DavidGilesHitex 0:5b7639d1f2c4 603 return Ack_Error;
DavidGilesHitex 0:5b7639d1f2c4 604 }
DavidGilesHitex 0:5b7639d1f2c4 605
DavidGilesHitex 0:5b7639d1f2c4 606
DavidGilesHitex 0:5b7639d1f2c4 607 /* Read The Low Word of the 32bit Quadrature Encoder */
DavidGilesHitex 0:5b7639d1f2c4 608 static uint8_t I2C0_dsPIC_Read_Quad_Encoder_Counter_Low(uint8_t slave_address, uint8_t *rx_array)
DavidGilesHitex 0:5b7639d1f2c4 609 {
DavidGilesHitex 0:5b7639d1f2c4 610 /* 62 - Read Quadrature Counter (low bytes) */
DavidGilesHitex 0:5b7639d1f2c4 611 /* data bytes to follow will be the Quadrature Encoder low bytes */
DavidGilesHitex 0:5b7639d1f2c4 612 /* databyte0 - Quadrature Encoder Counter - High Byte (byte3 MSB) */
DavidGilesHitex 0:5b7639d1f2c4 613 /* databyte1 - Quadrature Encoder Counterr - Low byte (byte2) */
DavidGilesHitex 0:5b7639d1f2c4 614 /* Reading this byte will unlock the 32 bit counter. Read the high byte first. */
DavidGilesHitex 0:5b7639d1f2c4 615 /* Note: It is important to read the high byte first */
DavidGilesHitex 0:5b7639d1f2c4 616 uint8_t Ack_Error = 0u;
DavidGilesHitex 0:5b7639d1f2c4 617 sint8_t Tx_Array[1u] = {62u};
DavidGilesHitex 0:5b7639d1f2c4 618 sint8_t Rx_Temp[2] = {0u, 0u};
DavidGilesHitex 0:5b7639d1f2c4 619
DavidGilesHitex 0:5b7639d1f2c4 620 Ack_Error = CNTRL_I2C_Master_Mode_Transmit(slave_address, Tx_Array, 1u); /* Send the command Byte */
DavidGilesHitex 0:5b7639d1f2c4 621
DavidGilesHitex 0:5b7639d1f2c4 622 if (Ack_Error == 0u)
DavidGilesHitex 0:5b7639d1f2c4 623 {
DavidGilesHitex 0:5b7639d1f2c4 624 Ack_Error = CNTRL_I2C_Master_Mode_Receive(slave_address, Rx_Temp, 2u); /* Read the return data */
DavidGilesHitex 0:5b7639d1f2c4 625 rx_array[0] = (uint8_t) Rx_Temp[0];
DavidGilesHitex 0:5b7639d1f2c4 626 rx_array[1] = (uint8_t) Rx_Temp[1];
DavidGilesHitex 0:5b7639d1f2c4 627 }
DavidGilesHitex 0:5b7639d1f2c4 628 return Ack_Error;
DavidGilesHitex 0:5b7639d1f2c4 629 }
DavidGilesHitex 0:5b7639d1f2c4 630
DavidGilesHitex 0:5b7639d1f2c4 631
DavidGilesHitex 0:5b7639d1f2c4 632