direction commands updated to Up down RPM value

Dependencies:   MAX7219pot MCP23S17 mbed

Fork of POT_V_1_0 by Marine Electricals

Committer:
viewdeep51
Date:
Wed Jul 25 19:57:54 2018 +0000
Revision:
5:f7b7ee0702cc
Parent:
4:710464cb44fc
26/07/18-dir command keys replaced to up down rpm value

Who changed what in which revision?

UserRevisionLine numberNew contents of line
viewdeep51 4:710464cb44fc 1 /*Board configuration
viewdeep51 4:710464cb44fc 2 >>>>>place JP5 to E5V side to use external 5V supply
viewdeep51 4:710464cb44fc 3 >>>>>Remove jumper link SB13,SB14 usb uart debug disabled
viewdeep51 4:710464cb44fc 4 >>>>>Place jumper link SB62,SB63...uart PA2,PA3 enabled by these jumper links
viewdeep51 4:710464cb44fc 5 */
viewdeep51 4:710464cb44fc 6
viewdeep51 0:ba33a62aea4e 7 #include "mbed.h"
viewdeep51 0:ba33a62aea4e 8 #include "pot.h"
viewdeep51 0:ba33a62aea4e 9 #include "max7219.h"
viewdeep51 0:ba33a62aea4e 10 #include "MCP23S17.h"
viewdeep51 0:ba33a62aea4e 11
viewdeep51 0:ba33a62aea4e 12 using namespace std;
viewdeep51 0:ba33a62aea4e 13
viewdeep51 0:ba33a62aea4e 14 //------------------------------------
viewdeep51 0:ba33a62aea4e 15 // Hyperterminal configuration
viewdeep51 0:ba33a62aea4e 16 // 9600 bauds, 8-bit data, no parity
viewdeep51 0:ba33a62aea4e 17 //------------------------------------
viewdeep51 0:ba33a62aea4e 18 Serial UART(SERIAL_TX, SERIAL_RX);
viewdeep51 0:ba33a62aea4e 19
viewdeep51 0:ba33a62aea4e 20 //timer instances creation//
viewdeep51 0:ba33a62aea4e 21 Ticker pot_HB;
viewdeep51 0:ba33a62aea4e 22 Ticker HB_Ticker;
viewdeep51 0:ba33a62aea4e 23 Ticker flash_Ticker;
viewdeep51 0:ba33a62aea4e 24 Ticker R_A_Ticker;
viewdeep51 0:ba33a62aea4e 25 Ticker CMD_Ticker;
viewdeep51 5:f7b7ee0702cc 26 //Ticker DIR_Ticker; //function changed to Up-Down Rpm counts
viewdeep51 3:ae45e29f5d4f 27 Ticker HB_fail;
viewdeep51 3:ae45e29f5d4f 28 Ticker RTC_ticker;
viewdeep51 3:ae45e29f5d4f 29 Ticker led_refresh;
viewdeep51 4:710464cb44fc 30 Timeout timeout;
viewdeep51 0:ba33a62aea4e 31
viewdeep51 0:ba33a62aea4e 32 //SPI instances creation//
viewdeep51 0:ba33a62aea4e 33 SPI _SPI2(SPI2_MOSI, SPI2_MISO, SPI2_SCK); //for displays max7219
viewdeep51 0:ba33a62aea4e 34 SPI _SPI3(SPI3_MOSI, SPI3_MISO, SPI3_SCK); //for keyscan mcp23s17
viewdeep51 0:ba33a62aea4e 35
viewdeep51 0:ba33a62aea4e 36 //class object instances for key I/O expander and segment driver//
viewdeep51 0:ba33a62aea4e 37 MCP23S17 mcp23s17_1(1,_SPI3, SPI3_CS1,SPI3_RST); //class MCP23S17 object instance for mcp23s17 IC-1 key detection
viewdeep51 0:ba33a62aea4e 38 MCP23S17 mcp23s17_2(2,_SPI3, SPI3_CS2,SPI3_RST); //class MCP23S17 object instance for mcp23s17 IC-2 key detection
viewdeep51 0:ba33a62aea4e 39 Max7219 max7219(SPI2_MOSI, SPI2_MISO, SPI2_SCK,SPI2_CS1); //object instance for seven segment drivers
viewdeep51 3:ae45e29f5d4f 40 max7219_configuration_t DEVICE1, DEVICE2;
viewdeep51 0:ba33a62aea4e 41 //interrupts definations
viewdeep51 0:ba33a62aea4e 42 InterruptIn int_A(INT_A); //8commands
viewdeep51 0:ba33a62aea4e 43 InterruptIn int_B(INT_B); //4 3commands+1ack
viewdeep51 0:ba33a62aea4e 44 InterruptIn int_C(INT_C); //4(3 controls(INC+,DEC-,TEST)+1 req) enbaled for all
viewdeep51 0:ba33a62aea4e 45
viewdeep51 0:ba33a62aea4e 46 //chip select definations for max7219 & mcp23s17 as digitalout pins//
viewdeep51 0:ba33a62aea4e 47 DigitalOut cs_disp1(SPI2_CS1);
viewdeep51 0:ba33a62aea4e 48 DigitalOut cs_disp2(SPI2_CS2); //max7219
viewdeep51 0:ba33a62aea4e 49 DigitalOut cs_key1(SPI3_CS1);
viewdeep51 0:ba33a62aea4e 50 DigitalOut cs_key2(SPI3_CS2); //mcp23s17
viewdeep51 0:ba33a62aea4e 51
viewdeep51 4:710464cb44fc 52 DigitalOut Drive_En(TX_EN); //uart driver enable
viewdeep51 0:ba33a62aea4e 53
viewdeep51 0:ba33a62aea4e 54 //discrete outputs definations buzzer and hooter
viewdeep51 0:ba33a62aea4e 55 DigitalOut buzzer(BUZ);
viewdeep51 0:ba33a62aea4e 56 DigitalOut hooter(HTR);
viewdeep51 0:ba33a62aea4e 57
viewdeep51 4:710464cb44fc 58
viewdeep51 3:ae45e29f5d4f 59 int err_ALL,sync_count,Hrs_1,Hrs_2, Mins_1, Mins_2, current_cmd, current_status,current_status_dir;
viewdeep51 3:ae45e29f5d4f 60 int addr, req,ack, send_ADDR, rcvd_ADDR, ach_S,ach_M,test_cnt,req_ADDR,ack_ADDR, err_ID, err_Status;
viewdeep51 3:ae45e29f5d4f 61 int rpm_data[4] = {0,0,0,0}; //digits 0-3
viewdeep51 3:ae45e29f5d4f 62 int last_entry[4] = {0,0,0,0}; //digits 0-3
viewdeep51 0:ba33a62aea4e 63
viewdeep51 4:710464cb44fc 64
viewdeep51 3:ae45e29f5d4f 65 uint8_t spi_error,no_pot_connected,num_bytes,ticker_count,ticker_count_fast,norm_count,set_bit;
viewdeep51 0:ba33a62aea4e 66
viewdeep51 3:ae45e29f5d4f 67
viewdeep51 5:f7b7ee0702cc 68 //int dir, last_dir;
viewdeep51 4:710464cb44fc 69 int checksum_send_byte,checksum_rcvd_byte;
viewdeep51 0:ba33a62aea4e 70
viewdeep51 4:710464cb44fc 71 char pot_Addr,addr_ID;
viewdeep51 0:ba33a62aea4e 72
viewdeep51 3:ae45e29f5d4f 73 float HB_timer,duty_cycle,sync_time;
viewdeep51 3:ae45e29f5d4f 74
viewdeep51 3:ae45e29f5d4f 75 bool device_ERR,pot_Error, norm_op;
viewdeep51 3:ae45e29f5d4f 76 bool toggle,test,toggle_fast,display_flash;
viewdeep51 3:ae45e29f5d4f 77 bool key_CMD,key_CTRL,key_SET;
viewdeep51 3:ae45e29f5d4f 78 bool HB_rcvd, HB_ack_rcvd, REQ_rcvd, BROADCAST_rcvd, ACK_rcvd,time_RCVD;
viewdeep51 0:ba33a62aea4e 79
viewdeep51 3:ae45e29f5d4f 80 bool DIR_cmd,DIR_cmd_ack,DIR_cmd_ack_send;
viewdeep51 3:ae45e29f5d4f 81 bool RPM_cmd,RPM_cmd_ack,RPM_cmd_ack_send;
viewdeep51 3:ae45e29f5d4f 82
viewdeep51 3:ae45e29f5d4f 83 bool HB_send, CMD_send, BROADCAST_send, REQ_send, ACK_send;
viewdeep51 3:ae45e29f5d4f 84
viewdeep51 4:710464cb44fc 85 bool ENTER_ACK_key,DIR_key,REQ_key,ACK_key,disp_clear;
viewdeep51 3:ae45e29f5d4f 86
viewdeep51 0:ba33a62aea4e 87 bool switch_CH, pot_MCR_err, pot_ER_err, ACH_SW_over;
viewdeep51 4:710464cb44fc 88 bool data_fail,disp_test;
viewdeep51 4:710464cb44fc 89
viewdeep51 4:710464cb44fc 90 int device_addr = -1;
viewdeep51 0:ba33a62aea4e 91
viewdeep51 1:e116808d8b00 92 char valid;
viewdeep51 4:710464cb44fc 93 char valid_sentence[8] = {'A', 'A','A','A','A','A','A','A'};
viewdeep51 1:e116808d8b00 94 char rx_buf[RX_BUFFER_SIZE+1];
viewdeep51 1:e116808d8b00 95 char tx_buf[TX_BUFFER_SIZE+1];
viewdeep51 1:e116808d8b00 96 char RTC_buffer[5] = {1,2,3,4,0};
viewdeep51 0:ba33a62aea4e 97
viewdeep51 3:ae45e29f5d4f 98 DigitalOut LED_CMD_ACK(_CMD_ACK); //ack command in slave devices only
viewdeep51 1:e116808d8b00 99 DigitalOut LED_CTRL[2] = {_REQ,_ACK}; //control transfer
viewdeep51 5:f7b7ee0702cc 100 //DigitalOut LED_DIR[2] = {_AHEAD,_ASTERN}; //dir command
viewdeep51 1:e116808d8b00 101 DigitalOut LED_ACH[8] = {_WH,_BRDG,_MCR,_ER,_WP,_WS,_OPS,_ASP}; //active channels
viewdeep51 0:ba33a62aea4e 102
viewdeep51 1:e116808d8b00 103 //DigitalOut myled(PB_13); //for internal test LED1
viewdeep51 0:ba33a62aea4e 104
viewdeep51 1:e116808d8b00 105 PwmOut mypwm(_PWM);
viewdeep51 0:ba33a62aea4e 106
viewdeep51 0:ba33a62aea4e 107 struct _POT
viewdeep51 0:ba33a62aea4e 108 {
viewdeep51 0:ba33a62aea4e 109 int slave_ID;
viewdeep51 3:ae45e29f5d4f 110 int err_cnt;
viewdeep51 3:ae45e29f5d4f 111 bool err_status;
viewdeep51 0:ba33a62aea4e 112 };
viewdeep51 3:ae45e29f5d4f 113 struct _POT* POT=NULL;
viewdeep51 0:ba33a62aea4e 114
viewdeep51 0:ba33a62aea4e 115 enum AddrID
viewdeep51 0:ba33a62aea4e 116 {
viewdeep51 0:ba33a62aea4e 117 pot_Master = 1,
viewdeep51 0:ba33a62aea4e 118 pot_Submaster,
viewdeep51 0:ba33a62aea4e 119 pot_Slave_MCR,
viewdeep51 0:ba33a62aea4e 120 pot_Slave_ER,
viewdeep51 0:ba33a62aea4e 121 pot_Listner_WP,
viewdeep51 0:ba33a62aea4e 122 pot_Listner_WS,
viewdeep51 0:ba33a62aea4e 123 pot_Listner_OPS,
viewdeep51 0:ba33a62aea4e 124 pot_Listner_ASP = 8
viewdeep51 0:ba33a62aea4e 125 };
viewdeep51 0:ba33a62aea4e 126
viewdeep51 1:e116808d8b00 127 enum LED_STATUS
viewdeep51 1:e116808d8b00 128 {
viewdeep51 1:e116808d8b00 129 OFF = 0,
viewdeep51 1:e116808d8b00 130 ON,
viewdeep51 1:e116808d8b00 131 _FLASH = 2
viewdeep51 1:e116808d8b00 132 };
viewdeep51 3:ae45e29f5d4f 133 LED_STATUS led_cmd_ack; //only in slave devices
viewdeep51 1:e116808d8b00 134 LED_STATUS led_ctrl[2];
viewdeep51 5:f7b7ee0702cc 135 //LED_STATUS led_dir[2];
viewdeep51 1:e116808d8b00 136 LED_STATUS led_ach[8];
viewdeep51 0:ba33a62aea4e 137
viewdeep51 0:ba33a62aea4e 138 enum KEY
viewdeep51 0:ba33a62aea4e 139 {
viewdeep51 0:ba33a62aea4e 140 ZERO = 0,
viewdeep51 0:ba33a62aea4e 141 ONE,
viewdeep51 0:ba33a62aea4e 142 TWO,
viewdeep51 0:ba33a62aea4e 143 THREE,
viewdeep51 0:ba33a62aea4e 144 FOUR,
viewdeep51 0:ba33a62aea4e 145 FIVE,
viewdeep51 0:ba33a62aea4e 146 SIX,
viewdeep51 0:ba33a62aea4e 147 SEVEN,
viewdeep51 0:ba33a62aea4e 148 EIGHT,
viewdeep51 0:ba33a62aea4e 149 NINE,
viewdeep51 0:ba33a62aea4e 150 FALSE0 = 10
viewdeep51 0:ba33a62aea4e 151 };
viewdeep51 0:ba33a62aea4e 152 enum KEY num_KEY;
viewdeep51 0:ba33a62aea4e 153
viewdeep51 0:ba33a62aea4e 154 enum CTRL
viewdeep51 0:ba33a62aea4e 155 {
viewdeep51 0:ba33a62aea4e 156 REQ_ = 1,
viewdeep51 0:ba33a62aea4e 157 ACK_ = 2,
viewdeep51 0:ba33a62aea4e 158 FALSE1= 3
viewdeep51 0:ba33a62aea4e 159
viewdeep51 0:ba33a62aea4e 160 };
viewdeep51 0:ba33a62aea4e 161 enum CTRL ctrl_KEY;
viewdeep51 0:ba33a62aea4e 162
viewdeep51 5:f7b7ee0702cc 163 enum UP_DOWN
viewdeep51 5:f7b7ee0702cc 164 {
viewdeep51 5:f7b7ee0702cc 165 UP_key=1,
viewdeep51 5:f7b7ee0702cc 166 DOWN_key,
viewdeep51 5:f7b7ee0702cc 167 FALSE2 = 3
viewdeep51 5:f7b7ee0702cc 168 };
viewdeep51 5:f7b7ee0702cc 169 enum UP_DOWN up_down_KEY;
viewdeep51 5:f7b7ee0702cc 170
viewdeep51 5:f7b7ee0702cc 171 /*
viewdeep51 0:ba33a62aea4e 172 enum direction_KEY
viewdeep51 0:ba33a62aea4e 173 {
viewdeep51 0:ba33a62aea4e 174 AHEAD = 1,
viewdeep51 0:ba33a62aea4e 175 ASTERN,
viewdeep51 0:ba33a62aea4e 176 FALSE2 = 3
viewdeep51 0:ba33a62aea4e 177 };
viewdeep51 0:ba33a62aea4e 178 enum direction_KEY dir_KEY;
viewdeep51 5:f7b7ee0702cc 179 */
viewdeep51 0:ba33a62aea4e 180
viewdeep51 0:ba33a62aea4e 181 enum display_KEY
viewdeep51 0:ba33a62aea4e 182 {
viewdeep51 0:ba33a62aea4e 183 clr_ENTRY = 0, //CE
viewdeep51 4:710464cb44fc 184 clr_ALL, //C, all data turns to zero
viewdeep51 0:ba33a62aea4e 185 ENTER_ACK, //enter in case of master and ACK in case of slaves/repeaters
viewdeep51 0:ba33a62aea4e 186 FALSE3 = 3
viewdeep51 0:ba33a62aea4e 187 };
viewdeep51 0:ba33a62aea4e 188 enum display_KEY disp_KEY;
viewdeep51 0:ba33a62aea4e 189
viewdeep51 0:ba33a62aea4e 190 enum SET
viewdeep51 0:ba33a62aea4e 191 {
viewdeep51 0:ba33a62aea4e 192 INC=0,
viewdeep51 0:ba33a62aea4e 193 DEC,
viewdeep51 0:ba33a62aea4e 194 TEST,
viewdeep51 0:ba33a62aea4e 195 FALSE4 = 4
viewdeep51 0:ba33a62aea4e 196 };
viewdeep51 0:ba33a62aea4e 197 enum SET set_KEY;
viewdeep51 0:ba33a62aea4e 198
viewdeep51 0:ba33a62aea4e 199 enum CHANNEL
viewdeep51 0:ba33a62aea4e 200 {
viewdeep51 0:ba33a62aea4e 201 ch_A=1,
viewdeep51 0:ba33a62aea4e 202 ch_B,
viewdeep51 0:ba33a62aea4e 203 ch_C,
viewdeep51 0:ba33a62aea4e 204 ch_D=4
viewdeep51 0:ba33a62aea4e 205 }; //keyscan channelA,B,C,Dof MCP23s17 1& 2 respectively
viewdeep51 0:ba33a62aea4e 206 enum CHANNEL ch;
viewdeep51 0:ba33a62aea4e 207
viewdeep51 3:ae45e29f5d4f 208
viewdeep51 0:ba33a62aea4e 209 /**************************************************************************************
viewdeep51 3:ae45e29f5d4f 210 EOT RTC display update FUNCTIONS
viewdeep51 0:ba33a62aea4e 211 **************************************************************************************/
viewdeep51 3:ae45e29f5d4f 212 void RTC_display(char* digits)
viewdeep51 0:ba33a62aea4e 213 {
viewdeep51 3:ae45e29f5d4f 214 for(uint8_t i = 0; i <= 3; i++) //to write all the four digits
viewdeep51 0:ba33a62aea4e 215 {
viewdeep51 1:e116808d8b00 216 if(i == 1)
viewdeep51 1:e116808d8b00 217 RTC_buffer[i] = RTC_buffer[i]|0b10000000; // to inc
viewdeep51 1:e116808d8b00 218 else
viewdeep51 1:e116808d8b00 219 __nop();
viewdeep51 1:e116808d8b00 220 max7219.write_digit(1, i+1, *(digits+i)); //device 2, digit 1-4 ,data 0x01
viewdeep51 0:ba33a62aea4e 221 }
viewdeep51 1:e116808d8b00 222 }
viewdeep51 1:e116808d8b00 223
viewdeep51 1:e116808d8b00 224 /**************************************************************************************
viewdeep51 1:e116808d8b00 225 EOT RTC read FUNCTION
viewdeep51 1:e116808d8b00 226 **************************************************************************************/
viewdeep51 1:e116808d8b00 227 void read_RTC()
viewdeep51 1:e116808d8b00 228 {
viewdeep51 1:e116808d8b00 229 time_t seconds = time(NULL);
viewdeep51 1:e116808d8b00 230 strftime(RTC_buffer, 5, "%H%M\n", localtime(&seconds));
viewdeep51 1:e116808d8b00 231 wait_ms(5);
viewdeep51 3:ae45e29f5d4f 232 RTC_display(RTC_buffer);
viewdeep51 1:e116808d8b00 233 wait_ms(1);
viewdeep51 1:e116808d8b00 234 }
viewdeep51 1:e116808d8b00 235
viewdeep51 1:e116808d8b00 236 /**************************************************************************************
viewdeep51 1:e116808d8b00 237 EOT CHECK LED STATUS FUNCTION
viewdeep51 1:e116808d8b00 238 **************************************************************************************/
viewdeep51 1:e116808d8b00 239 void update_led()
viewdeep51 1:e116808d8b00 240 {
viewdeep51 1:e116808d8b00 241 if(++ticker_count == 25) //25x20 = 500ms
viewdeep51 1:e116808d8b00 242 {
viewdeep51 1:e116808d8b00 243 ticker_count = 0;
viewdeep51 1:e116808d8b00 244 toggle = !toggle; //normal cmd flash
viewdeep51 1:e116808d8b00 245 }
viewdeep51 1:e116808d8b00 246 else
viewdeep51 1:e116808d8b00 247 __nop();
viewdeep51 1:e116808d8b00 248
viewdeep51 1:e116808d8b00 249 if(++ticker_count_fast == 10) //20x10 =200
viewdeep51 1:e116808d8b00 250 {
viewdeep51 1:e116808d8b00 251 ticker_count_fast = 0;
viewdeep51 1:e116808d8b00 252 toggle_fast = !toggle_fast; //error flashing
viewdeep51 1:e116808d8b00 253 }
viewdeep51 1:e116808d8b00 254 else
viewdeep51 1:e116808d8b00 255 __nop();
viewdeep51 1:e116808d8b00 256
viewdeep51 1:e116808d8b00 257 if(test)
viewdeep51 1:e116808d8b00 258 {
viewdeep51 3:ae45e29f5d4f 259 LED_CMD_ACK = ON; //in slave only
viewdeep51 1:e116808d8b00 260
viewdeep51 1:e116808d8b00 261 for(uint8_t i=0;i<=1;i++) //control
viewdeep51 1:e116808d8b00 262 {
viewdeep51 1:e116808d8b00 263 LED_CTRL[i] = ON;
viewdeep51 1:e116808d8b00 264 }
viewdeep51 5:f7b7ee0702cc 265
viewdeep51 5:f7b7ee0702cc 266 /*
viewdeep51 5:f7b7ee0702cc 267 for(uint8_t i=0;i<=1;i++) // dir control
viewdeep51 1:e116808d8b00 268 {
viewdeep51 1:e116808d8b00 269 LED_DIR[i] = ON; /////////////////working here
viewdeep51 1:e116808d8b00 270 }
viewdeep51 5:f7b7ee0702cc 271 */
viewdeep51 5:f7b7ee0702cc 272
viewdeep51 1:e116808d8b00 273 for(uint8_t i=0;i<=7;i++) //active channel
viewdeep51 1:e116808d8b00 274 {
viewdeep51 1:e116808d8b00 275 LED_ACH[i] = ON; //duty cycle 10%
viewdeep51 0:ba33a62aea4e 276 }
viewdeep51 4:710464cb44fc 277 max7219.set_display_test(); //all on
viewdeep51 4:710464cb44fc 278 wait_us(1);
viewdeep51 4:710464cb44fc 279 disp_test = true;
viewdeep51 0:ba33a62aea4e 280 }
viewdeep51 1:e116808d8b00 281 else
viewdeep51 1:e116808d8b00 282 {
viewdeep51 4:710464cb44fc 283 if(disp_test)
viewdeep51 4:710464cb44fc 284 {
viewdeep51 4:710464cb44fc 285 disp_test = false;
viewdeep51 4:710464cb44fc 286 max7219.clear_display_test(); //normal operation
viewdeep51 4:710464cb44fc 287 }
viewdeep51 4:710464cb44fc 288 else
viewdeep51 4:710464cb44fc 289 __nop();
viewdeep51 4:710464cb44fc 290
viewdeep51 3:ae45e29f5d4f 291 if(led_cmd_ack == ON) //status update LED_CMD_ACK
viewdeep51 3:ae45e29f5d4f 292 LED_CMD_ACK = ON;
viewdeep51 1:e116808d8b00 293 else
viewdeep51 1:e116808d8b00 294 __nop();
viewdeep51 1:e116808d8b00 295
viewdeep51 3:ae45e29f5d4f 296 if(led_cmd_ack == OFF)
viewdeep51 3:ae45e29f5d4f 297 LED_CMD_ACK = OFF;
viewdeep51 1:e116808d8b00 298 else
viewdeep51 1:e116808d8b00 299 __nop();
viewdeep51 1:e116808d8b00 300
viewdeep51 3:ae45e29f5d4f 301 if(led_cmd_ack == _FLASH)
viewdeep51 1:e116808d8b00 302 {
viewdeep51 1:e116808d8b00 303 if(toggle)
viewdeep51 3:ae45e29f5d4f 304 LED_CMD_ACK= ON;
viewdeep51 1:e116808d8b00 305 else
viewdeep51 3:ae45e29f5d4f 306 LED_CMD_ACK = OFF;
viewdeep51 1:e116808d8b00 307 }
viewdeep51 1:e116808d8b00 308 else
viewdeep51 1:e116808d8b00 309 __nop();
viewdeep51 4:710464cb44fc 310
viewdeep51 3:ae45e29f5d4f 311 for(uint8_t i=0;i<=1;i++) //status update LED_CTRL
viewdeep51 1:e116808d8b00 312 {
viewdeep51 1:e116808d8b00 313 if(led_ctrl[i] == ON)
viewdeep51 1:e116808d8b00 314 LED_CTRL[i] = ON;
viewdeep51 1:e116808d8b00 315 else
viewdeep51 1:e116808d8b00 316 __nop();
viewdeep51 1:e116808d8b00 317
viewdeep51 1:e116808d8b00 318 if(led_ctrl[i] == OFF)
viewdeep51 1:e116808d8b00 319 LED_CTRL[i] = OFF; //duty cycle 10%
viewdeep51 1:e116808d8b00 320 else
viewdeep51 1:e116808d8b00 321 __nop();
viewdeep51 1:e116808d8b00 322
viewdeep51 1:e116808d8b00 323 if(led_ctrl[i] == _FLASH)
viewdeep51 1:e116808d8b00 324 {
viewdeep51 1:e116808d8b00 325 if(toggle)
viewdeep51 1:e116808d8b00 326 LED_CTRL[i]= ON;
viewdeep51 1:e116808d8b00 327 else
viewdeep51 1:e116808d8b00 328 LED_CTRL[i] = OFF;
viewdeep51 1:e116808d8b00 329 }
viewdeep51 1:e116808d8b00 330 else
viewdeep51 1:e116808d8b00 331 __nop();
viewdeep51 1:e116808d8b00 332 }
viewdeep51 5:f7b7ee0702cc 333 /*
viewdeep51 4:710464cb44fc 334 for(uint8_t i=0;i<=1;i++) //status update LED_CTRL
viewdeep51 4:710464cb44fc 335 {
viewdeep51 4:710464cb44fc 336 if(led_dir[i] == ON)
viewdeep51 4:710464cb44fc 337 LED_DIR[i] = ON;
viewdeep51 4:710464cb44fc 338 else
viewdeep51 4:710464cb44fc 339 __nop();
viewdeep51 4:710464cb44fc 340
viewdeep51 4:710464cb44fc 341 if(led_dir[i] == OFF)
viewdeep51 4:710464cb44fc 342 LED_DIR[i] = OFF; //duty cycle 10%
viewdeep51 4:710464cb44fc 343 else
viewdeep51 4:710464cb44fc 344 __nop();
viewdeep51 4:710464cb44fc 345
viewdeep51 4:710464cb44fc 346 if(led_dir[i] == _FLASH)
viewdeep51 4:710464cb44fc 347 {
viewdeep51 4:710464cb44fc 348 if(toggle)
viewdeep51 4:710464cb44fc 349 LED_DIR[i]= ON;
viewdeep51 4:710464cb44fc 350 else
viewdeep51 4:710464cb44fc 351 LED_DIR[i] = OFF;
viewdeep51 4:710464cb44fc 352 }
viewdeep51 4:710464cb44fc 353 else
viewdeep51 4:710464cb44fc 354 __nop();
viewdeep51 4:710464cb44fc 355 }
viewdeep51 5:f7b7ee0702cc 356 */
viewdeep51 4:710464cb44fc 357
viewdeep51 3:ae45e29f5d4f 358 for(uint8_t i=0;i<=7;i++) //status update LED_ACH
viewdeep51 1:e116808d8b00 359 {
viewdeep51 1:e116808d8b00 360 if(led_ach[i] == ON)
viewdeep51 1:e116808d8b00 361 LED_ACH[i] = ON; //duty cycle 10%
viewdeep51 1:e116808d8b00 362 else
viewdeep51 1:e116808d8b00 363 __nop();
viewdeep51 1:e116808d8b00 364
viewdeep51 1:e116808d8b00 365 if(led_ach[i] == OFF)
viewdeep51 1:e116808d8b00 366 LED_ACH[i] = OFF; //duty cycle 10
viewdeep51 1:e116808d8b00 367 else
viewdeep51 1:e116808d8b00 368 __nop();
viewdeep51 1:e116808d8b00 369
viewdeep51 1:e116808d8b00 370 if(led_ach[i] == _FLASH)
viewdeep51 1:e116808d8b00 371 {
viewdeep51 1:e116808d8b00 372 if(toggle_fast)
viewdeep51 1:e116808d8b00 373 LED_ACH[i]= ON;
viewdeep51 1:e116808d8b00 374 else
viewdeep51 1:e116808d8b00 375 LED_ACH[i] = OFF;
viewdeep51 1:e116808d8b00 376 }
viewdeep51 1:e116808d8b00 377 else
viewdeep51 1:e116808d8b00 378 __nop();
viewdeep51 1:e116808d8b00 379 }
viewdeep51 1:e116808d8b00 380 }
viewdeep51 1:e116808d8b00 381 if(display_flash) //if command executed or command received
viewdeep51 1:e116808d8b00 382 {
viewdeep51 1:e116808d8b00 383 if(toggle)
viewdeep51 1:e116808d8b00 384 max7219.enable_device(2); //enable device 1 or normal mode
viewdeep51 1:e116808d8b00 385 else
viewdeep51 1:e116808d8b00 386 max7219.disable_device(2); //shutdown mode
viewdeep51 1:e116808d8b00 387 }
viewdeep51 1:e116808d8b00 388 else if(!device_ERR) //specific device is not in error device
viewdeep51 1:e116808d8b00 389 max7219.enable_device(2); //enable device 1 or normal mode;
viewdeep51 1:e116808d8b00 390 else
viewdeep51 1:e116808d8b00 391 __nop();
viewdeep51 0:ba33a62aea4e 392 }
viewdeep51 0:ba33a62aea4e 393
viewdeep51 0:ba33a62aea4e 394 /**************************************************************************************
viewdeep51 3:ae45e29f5d4f 395 POT State Machines FUNCTIONS
viewdeep51 0:ba33a62aea4e 396 **************************************************************************************/
viewdeep51 4:710464cb44fc 397 void HB_State_Machine() //run at an interval of 10ms,event driven start stop
viewdeep51 0:ba33a62aea4e 398 {
viewdeep51 0:ba33a62aea4e 399 static int hb_state_cnt = 0;
viewdeep51 3:ae45e29f5d4f 400 if(HB_send)
viewdeep51 0:ba33a62aea4e 401 {
viewdeep51 0:ba33a62aea4e 402 HB_send = false;
viewdeep51 0:ba33a62aea4e 403 hb_state_cnt = 0;
viewdeep51 0:ba33a62aea4e 404 }
viewdeep51 0:ba33a62aea4e 405 else
viewdeep51 0:ba33a62aea4e 406 __nop();
viewdeep51 3:ae45e29f5d4f 407
viewdeep51 0:ba33a62aea4e 408 if(HB_ack_rcvd)
viewdeep51 0:ba33a62aea4e 409 {
viewdeep51 0:ba33a62aea4e 410 HB_ack_rcvd = false;
viewdeep51 0:ba33a62aea4e 411 HB_Ticker.detach();
viewdeep51 0:ba33a62aea4e 412 POT[device_addr].err_cnt = 0;
viewdeep51 4:710464cb44fc 413 if(POT[device_addr].err_status) //if that specific device is in error before but revert back to healthy state
viewdeep51 3:ae45e29f5d4f 414 {
viewdeep51 3:ae45e29f5d4f 415 POT[device_addr].err_status = false;
viewdeep51 3:ae45e29f5d4f 416 err_Status = 0;
viewdeep51 3:ae45e29f5d4f 417 err_ID = POT[device_addr].slave_ID;
viewdeep51 3:ae45e29f5d4f 418
viewdeep51 3:ae45e29f5d4f 419 set_bit = (uint8_t)1<<(err_ID-1);
viewdeep51 4:710464cb44fc 420 err_ALL = err_ALL^set_bit; //bitwise XOR
viewdeep51 3:ae45e29f5d4f 421
viewdeep51 3:ae45e29f5d4f 422 BROADCAST_send = true; // to tell all device that error diminished
viewdeep51 4:710464cb44fc 423 switch_CH = true; // to tell specific device abot active channels and rpm data
viewdeep51 3:ae45e29f5d4f 424 if(POT[device_addr].slave_ID == pot_Slave_MCR)
viewdeep51 3:ae45e29f5d4f 425 {
viewdeep51 3:ae45e29f5d4f 426 pot_MCR_err = false;
viewdeep51 3:ae45e29f5d4f 427 if(pot_ER_err)
viewdeep51 3:ae45e29f5d4f 428 {
viewdeep51 3:ae45e29f5d4f 429 ach_S = pot_Slave_MCR;
viewdeep51 3:ae45e29f5d4f 430 switch_CH = true; //automatic takeover if ER is already faulty
viewdeep51 3:ae45e29f5d4f 431 }
viewdeep51 3:ae45e29f5d4f 432 else
viewdeep51 3:ae45e29f5d4f 433 __nop();
viewdeep51 3:ae45e29f5d4f 434 }
viewdeep51 3:ae45e29f5d4f 435 else
viewdeep51 3:ae45e29f5d4f 436 __nop();
viewdeep51 3:ae45e29f5d4f 437
viewdeep51 3:ae45e29f5d4f 438 if(POT[device_addr].slave_ID == pot_Slave_ER)
viewdeep51 3:ae45e29f5d4f 439 {
viewdeep51 3:ae45e29f5d4f 440 pot_ER_err = false;
viewdeep51 3:ae45e29f5d4f 441 if(pot_MCR_err)
viewdeep51 3:ae45e29f5d4f 442 {
viewdeep51 3:ae45e29f5d4f 443 ach_S = pot_Slave_ER;
viewdeep51 3:ae45e29f5d4f 444 switch_CH = true; //automatic takeover if ER is already faulty
viewdeep51 3:ae45e29f5d4f 445 }
viewdeep51 3:ae45e29f5d4f 446 else
viewdeep51 3:ae45e29f5d4f 447 __nop();
viewdeep51 3:ae45e29f5d4f 448 }
viewdeep51 3:ae45e29f5d4f 449 else
viewdeep51 3:ae45e29f5d4f 450 __nop();
viewdeep51 3:ae45e29f5d4f 451 }
viewdeep51 3:ae45e29f5d4f 452 else
viewdeep51 4:710464cb44fc 453 {
viewdeep51 4:710464cb44fc 454 POT[device_addr].err_status = false;
viewdeep51 4:710464cb44fc 455 hb_state_cnt = 0;
viewdeep51 4:710464cb44fc 456 err_Status = 0;
viewdeep51 3:ae45e29f5d4f 457
viewdeep51 4:710464cb44fc 458 if((POT[device_addr].slave_ID == ach_S)&&(led_ach[send_ADDR - 1] != ON))
viewdeep51 4:710464cb44fc 459 led_ach[send_ADDR - 1] = ON;
viewdeep51 4:710464cb44fc 460 else if((POT[device_addr].slave_ID != ach_S)&&(led_ach[send_ADDR - 1] != OFF))
viewdeep51 4:710464cb44fc 461 led_ach[send_ADDR - 1] = OFF;
viewdeep51 4:710464cb44fc 462 else
viewdeep51 4:710464cb44fc 463 __nop();
viewdeep51 4:710464cb44fc 464
viewdeep51 4:710464cb44fc 465 if((addr_ID == ach_M)&&(led_ach[ach_M-1] != ON))
viewdeep51 4:710464cb44fc 466 led_ach[ach_M-1] = ON;
viewdeep51 4:710464cb44fc 467 else if((addr_ID != ach_M)&&(led_ach[ach_M-1] != OFF))
viewdeep51 4:710464cb44fc 468 led_ach[ach_M-1] = OFF;
viewdeep51 4:710464cb44fc 469 else
viewdeep51 4:710464cb44fc 470 __nop();
viewdeep51 4:710464cb44fc 471 }
viewdeep51 0:ba33a62aea4e 472 }
viewdeep51 0:ba33a62aea4e 473 else
viewdeep51 0:ba33a62aea4e 474 {
viewdeep51 4:710464cb44fc 475 if(++hb_state_cnt >= 5) //if HB_response not arrived till 50ms
viewdeep51 0:ba33a62aea4e 476 {
viewdeep51 3:ae45e29f5d4f 477 hb_state_cnt=0;
viewdeep51 3:ae45e29f5d4f 478 HB_Ticker.detach();
viewdeep51 3:ae45e29f5d4f 479
viewdeep51 4:710464cb44fc 480 if((++POT[device_addr].err_cnt >= 5)&&(!POT[device_addr].err_status)) //specific device in error state after 05 hb response missing
viewdeep51 0:ba33a62aea4e 481 {
viewdeep51 4:710464cb44fc 482 POT[device_addr].err_cnt = 0;
viewdeep51 3:ae45e29f5d4f 483 led_ach[send_ADDR - 1] = _FLASH;
viewdeep51 3:ae45e29f5d4f 484 err_ID = POT[device_addr].slave_ID;
viewdeep51 3:ae45e29f5d4f 485
viewdeep51 3:ae45e29f5d4f 486 set_bit = (uint8_t)1<<(err_ID-1);
viewdeep51 3:ae45e29f5d4f 487 err_ALL = err_ALL|set_bit; //bitwise OR
viewdeep51 3:ae45e29f5d4f 488
viewdeep51 3:ae45e29f5d4f 489 POT[device_addr].err_status = true;
viewdeep51 0:ba33a62aea4e 490 err_Status = 1;
viewdeep51 3:ae45e29f5d4f 491 BROADCAST_send = true; //broadcast needed to be sent
viewdeep51 3:ae45e29f5d4f 492 if(POT[device_addr].slave_ID == pot_Slave_MCR) //means MCR fail
viewdeep51 3:ae45e29f5d4f 493 {
viewdeep51 3:ae45e29f5d4f 494 pot_MCR_err = true;
viewdeep51 3:ae45e29f5d4f 495 if(ach_S == pot_Slave_MCR)
viewdeep51 3:ae45e29f5d4f 496 {
viewdeep51 3:ae45e29f5d4f 497 if(!pot_ER_err) //automatic changeover if ER healthy
viewdeep51 3:ae45e29f5d4f 498 {
viewdeep51 3:ae45e29f5d4f 499 ach_S = pot_Slave_ER;
viewdeep51 3:ae45e29f5d4f 500 switch_CH = true; //transfer all slaves the new active slave
viewdeep51 3:ae45e29f5d4f 501 }
viewdeep51 3:ae45e29f5d4f 502 else
viewdeep51 3:ae45e29f5d4f 503 {
viewdeep51 3:ae45e29f5d4f 504 pot_Error = true;
viewdeep51 3:ae45e29f5d4f 505 ach_S = 9;
viewdeep51 3:ae45e29f5d4f 506 switch_CH = true; //both start flashing for ER nd MCR in all slaves
viewdeep51 3:ae45e29f5d4f 507 }
viewdeep51 3:ae45e29f5d4f 508 }
viewdeep51 3:ae45e29f5d4f 509 else
viewdeep51 3:ae45e29f5d4f 510 __nop();
viewdeep51 3:ae45e29f5d4f 511 }
viewdeep51 3:ae45e29f5d4f 512 else
viewdeep51 3:ae45e29f5d4f 513 __nop();
viewdeep51 3:ae45e29f5d4f 514
viewdeep51 3:ae45e29f5d4f 515 if(POT[device_addr].slave_ID == pot_Slave_ER)
viewdeep51 0:ba33a62aea4e 516 {
viewdeep51 0:ba33a62aea4e 517 pot_ER_err = true;
viewdeep51 3:ae45e29f5d4f 518 if(ach_S == pot_Slave_ER)
viewdeep51 3:ae45e29f5d4f 519 {
viewdeep51 3:ae45e29f5d4f 520 if(!pot_MCR_err) //automatic changeover if MCR healthy
viewdeep51 3:ae45e29f5d4f 521 {
viewdeep51 3:ae45e29f5d4f 522 ach_S = pot_Slave_MCR;
viewdeep51 3:ae45e29f5d4f 523 switch_CH = true;
viewdeep51 3:ae45e29f5d4f 524 }
viewdeep51 3:ae45e29f5d4f 525 else
viewdeep51 3:ae45e29f5d4f 526 {
viewdeep51 3:ae45e29f5d4f 527 pot_Error = true;
viewdeep51 3:ae45e29f5d4f 528 ach_S = 9;
viewdeep51 3:ae45e29f5d4f 529 switch_CH = true; //both start flashing for all MCR nd ER
viewdeep51 3:ae45e29f5d4f 530 }
viewdeep51 3:ae45e29f5d4f 531 }
viewdeep51 3:ae45e29f5d4f 532 else
viewdeep51 3:ae45e29f5d4f 533 __nop();
viewdeep51 0:ba33a62aea4e 534 }
viewdeep51 0:ba33a62aea4e 535 else
viewdeep51 3:ae45e29f5d4f 536 __nop();
viewdeep51 0:ba33a62aea4e 537 }
viewdeep51 3:ae45e29f5d4f 538 else
viewdeep51 3:ae45e29f5d4f 539 __nop();
viewdeep51 0:ba33a62aea4e 540 }
viewdeep51 3:ae45e29f5d4f 541 else
viewdeep51 3:ae45e29f5d4f 542 __nop();
viewdeep51 0:ba33a62aea4e 543 }
viewdeep51 0:ba33a62aea4e 544 }
viewdeep51 0:ba33a62aea4e 545
viewdeep51 3:ae45e29f5d4f 546 void CMD_State_Machine() //function run periodcally in all connected devices on bus
viewdeep51 0:ba33a62aea4e 547 {
viewdeep51 3:ae45e29f5d4f 548 static int cmd_state_cnt=0;
viewdeep51 0:ba33a62aea4e 549
viewdeep51 3:ae45e29f5d4f 550 if(RPM_cmd_ack) //PEOTX,CMD received in Master/SUB_Master, non Active Slave & Listner
viewdeep51 0:ba33a62aea4e 551 {
viewdeep51 4:710464cb44fc 552 for(uint8_t i = 0; i <=3; i++)
viewdeep51 4:710464cb44fc 553 {
viewdeep51 4:710464cb44fc 554 last_entry[i] = rpm_data[i]; //data saved to last entery
viewdeep51 4:710464cb44fc 555 }
viewdeep51 4:710464cb44fc 556 for(uint8_t i = 5; i <=8; i++)
viewdeep51 4:710464cb44fc 557 {
viewdeep51 4:710464cb44fc 558 max7219.write_digit(1,i,last_entry[i-5]); //last entery displayed
viewdeep51 4:710464cb44fc 559 }
viewdeep51 4:710464cb44fc 560 if(addr_ID == ach_M)
viewdeep51 4:710464cb44fc 561 {
viewdeep51 4:710464cb44fc 562 mcp23s17_1.gpintena(0xff); //ACK key interrupts disabled
viewdeep51 4:710464cb44fc 563 wait_us(1);
viewdeep51 4:710464cb44fc 564 mcp23s17_1.gpintenb(0xff); //ACK key interrupts disabled
viewdeep51 4:710464cb44fc 565 wait_us(1);
viewdeep51 4:710464cb44fc 566 disp_clear = true;
viewdeep51 4:710464cb44fc 567 }
viewdeep51 4:710464cb44fc 568 else if(addr_ID == ach_S)
viewdeep51 4:710464cb44fc 569 {
viewdeep51 4:710464cb44fc 570 int_B.rise(NULL); //all key functions/interrupt disabled for PORTA,B
viewdeep51 4:710464cb44fc 571 wait_us(1);
viewdeep51 4:710464cb44fc 572 mcp23s17_1.gpintenb(0x00); //interrupts disabled
viewdeep51 4:710464cb44fc 573 wait_us(1);
viewdeep51 4:710464cb44fc 574 }
viewdeep51 4:710464cb44fc 575 else
viewdeep51 4:710464cb44fc 576 __nop();
viewdeep51 4:710464cb44fc 577
viewdeep51 4:710464cb44fc 578 led_cmd_ack = ON;
viewdeep51 3:ae45e29f5d4f 579 current_status = 1; //display steady
viewdeep51 0:ba33a62aea4e 580 cmd_state_cnt = 0;
viewdeep51 3:ae45e29f5d4f 581 RPM_cmd = false;
viewdeep51 0:ba33a62aea4e 582 RPM_cmd_ack = false;
viewdeep51 3:ae45e29f5d4f 583 display_flash = false;
viewdeep51 3:ae45e29f5d4f 584 buzzer = OFF;
viewdeep51 3:ae45e29f5d4f 585 hooter = OFF;
viewdeep51 0:ba33a62aea4e 586 CMD_Ticker.detach(); //cmd state machine ticker stopped in all devices
viewdeep51 0:ba33a62aea4e 587 }
viewdeep51 4:710464cb44fc 588 else if(++cmd_state_cnt >= 12) //Hotter ON after 3sec
viewdeep51 3:ae45e29f5d4f 589 {
viewdeep51 4:710464cb44fc 590 if((hooter == OFF)&&(addr_ID == ach_S)) //if hooter still off
viewdeep51 3:ae45e29f5d4f 591 {
viewdeep51 4:710464cb44fc 592 cmd_state_cnt = 0;
viewdeep51 4:710464cb44fc 593 hooter = ON;
viewdeep51 4:710464cb44fc 594 /* if(++cmd_state_cnt >= 96) //still command ack not recived for 24 sec
viewdeep51 4:710464cb44fc 595 {
viewdeep51 4:710464cb44fc 596 LED_CMD[cmd].OFF(); //command suspended
viewdeep51 4:710464cb44fc 597 buzzer = OFF;
viewdeep51 3:ae45e29f5d4f 598 hooter = OFF;
viewdeep51 4:710464cb44fc 599 int_A.rise(NULL);
viewdeep51 4:710464cb44fc 600 int_A.rise(NULL);
viewdeep51 4:710464cb44fc 601 CMD_ticker.detach(); //timer stoped in all connected devices if ack not received in 3sec
viewdeep51 4:710464cb44fc 602 } */
viewdeep51 0:ba33a62aea4e 603 }
viewdeep51 3:ae45e29f5d4f 604 else
viewdeep51 4:710464cb44fc 605 __nop();
viewdeep51 0:ba33a62aea4e 606 }
viewdeep51 4:710464cb44fc 607 else
viewdeep51 4:710464cb44fc 608 __nop();
viewdeep51 0:ba33a62aea4e 609 }
viewdeep51 0:ba33a62aea4e 610
viewdeep51 0:ba33a62aea4e 611 void R_A_Statemachine()
viewdeep51 0:ba33a62aea4e 612 {
viewdeep51 0:ba33a62aea4e 613 static int state_cnt = 0;
viewdeep51 3:ae45e29f5d4f 614 if(++state_cnt >= 60) //REQ not acknowledged for 30 sec, leds turn off, timer stopped
viewdeep51 0:ba33a62aea4e 615 {
viewdeep51 3:ae45e29f5d4f 616 state_cnt = 0;
viewdeep51 4:710464cb44fc 617 REQ_rcvd = false; //clear any pending request
viewdeep51 0:ba33a62aea4e 618 R_A_Ticker.detach();
viewdeep51 4:710464cb44fc 619 /*
viewdeep51 4:710464cb44fc 620 if(addr_ID != ach_M) // TODO
viewdeep51 3:ae45e29f5d4f 621 {
viewdeep51 3:ae45e29f5d4f 622 int_B.rise(NULL);
viewdeep51 3:ae45e29f5d4f 623 mcp23s17_1.gpintenb(0x00); //portb of mcp1 keys disabled related to ACK
viewdeep51 3:ae45e29f5d4f 624 wait_us(10);
viewdeep51 3:ae45e29f5d4f 625 }
viewdeep51 3:ae45e29f5d4f 626 else
viewdeep51 3:ae45e29f5d4f 627 __nop();
viewdeep51 4:710464cb44fc 628 */
viewdeep51 3:ae45e29f5d4f 629
viewdeep51 3:ae45e29f5d4f 630 led_ctrl[REQ_-1] = OFF;
viewdeep51 3:ae45e29f5d4f 631 led_ctrl[ACK_-1] = OFF;
viewdeep51 0:ba33a62aea4e 632 }
viewdeep51 0:ba33a62aea4e 633 else
viewdeep51 3:ae45e29f5d4f 634 __nop();
viewdeep51 3:ae45e29f5d4f 635
viewdeep51 3:ae45e29f5d4f 636 if(ACK_rcvd) //active channel switchover true only after acknowledge receive
viewdeep51 0:ba33a62aea4e 637 {
viewdeep51 3:ae45e29f5d4f 638 ACK_rcvd = false;
viewdeep51 3:ae45e29f5d4f 639 led_ctrl[REQ_-1] = OFF;
viewdeep51 0:ba33a62aea4e 640 if(addr_ID == pot_Master)
viewdeep51 0:ba33a62aea4e 641 {
viewdeep51 0:ba33a62aea4e 642 if(ack_ADDR == 3 || ack_ADDR == 4)
viewdeep51 0:ba33a62aea4e 643 {
viewdeep51 0:ba33a62aea4e 644 if(ach_S == pot_Slave_MCR)
viewdeep51 0:ba33a62aea4e 645 ach_S = pot_Slave_ER;
viewdeep51 0:ba33a62aea4e 646 else if(ach_S == pot_Slave_ER)
viewdeep51 0:ba33a62aea4e 647 ach_S = pot_Slave_MCR;
viewdeep51 0:ba33a62aea4e 648 }
viewdeep51 3:ae45e29f5d4f 649 else
viewdeep51 3:ae45e29f5d4f 650 __nop();
viewdeep51 3:ae45e29f5d4f 651
viewdeep51 3:ae45e29f5d4f 652 if(ack_ADDR == 2 || ack_ADDR == 1)
viewdeep51 0:ba33a62aea4e 653 {
viewdeep51 0:ba33a62aea4e 654 if(ach_M == pot_Master)
viewdeep51 0:ba33a62aea4e 655 ach_M = pot_Submaster;
viewdeep51 0:ba33a62aea4e 656 else if(ach_M == pot_Submaster)
viewdeep51 0:ba33a62aea4e 657 ach_M = pot_Master;
viewdeep51 0:ba33a62aea4e 658 }
viewdeep51 3:ae45e29f5d4f 659 else
viewdeep51 3:ae45e29f5d4f 660 __nop();
viewdeep51 3:ae45e29f5d4f 661
viewdeep51 0:ba33a62aea4e 662 switch_CH = true;
viewdeep51 0:ba33a62aea4e 663 state_cnt = 0;
viewdeep51 0:ba33a62aea4e 664 R_A_Ticker.detach();
viewdeep51 0:ba33a62aea4e 665 }
viewdeep51 0:ba33a62aea4e 666 else
viewdeep51 0:ba33a62aea4e 667 {
viewdeep51 0:ba33a62aea4e 668 state_cnt = 0;
viewdeep51 0:ba33a62aea4e 669 R_A_Ticker.detach();
viewdeep51 0:ba33a62aea4e 670 }
viewdeep51 0:ba33a62aea4e 671 }
viewdeep51 3:ae45e29f5d4f 672 else
viewdeep51 3:ae45e29f5d4f 673 __nop();
viewdeep51 0:ba33a62aea4e 674 }
viewdeep51 0:ba33a62aea4e 675
viewdeep51 5:f7b7ee0702cc 676 //Direction function changed to RPM Up-Down Counter so Dir_state_machine function not required
viewdeep51 5:f7b7ee0702cc 677 /*
viewdeep51 3:ae45e29f5d4f 678 void DIR_state_machine(void)
viewdeep51 0:ba33a62aea4e 679 {
viewdeep51 0:ba33a62aea4e 680 static int dir_state_cnt = 0;
viewdeep51 0:ba33a62aea4e 681 if(DIR_cmd_ack)
viewdeep51 0:ba33a62aea4e 682 {
viewdeep51 3:ae45e29f5d4f 683 DIR_cmd = false;
viewdeep51 0:ba33a62aea4e 684 DIR_cmd_ack = false;
viewdeep51 3:ae45e29f5d4f 685 current_status_dir = 1; //steady
viewdeep51 3:ae45e29f5d4f 686 led_dir[dir-1] = ON; //led direction acknowledged turn steady
viewdeep51 3:ae45e29f5d4f 687 buzzer = OFF; //buzzer Off
viewdeep51 3:ae45e29f5d4f 688 hooter = OFF;
viewdeep51 0:ba33a62aea4e 689 DIR_Ticker.detach();
viewdeep51 0:ba33a62aea4e 690 }
viewdeep51 0:ba33a62aea4e 691 else
viewdeep51 0:ba33a62aea4e 692 {
viewdeep51 3:ae45e29f5d4f 693 if((++dir_state_cnt >= 12) && (hooter == OFF)) //direction command not ack for 03 sec
viewdeep51 0:ba33a62aea4e 694 {
viewdeep51 3:ae45e29f5d4f 695 hooter = ON;
viewdeep51 0:ba33a62aea4e 696 dir_state_cnt = 0;
viewdeep51 0:ba33a62aea4e 697 }
viewdeep51 0:ba33a62aea4e 698 else
viewdeep51 0:ba33a62aea4e 699 __nop();
viewdeep51 3:ae45e29f5d4f 700 }
viewdeep51 0:ba33a62aea4e 701 }
viewdeep51 5:f7b7ee0702cc 702 */
viewdeep51 5:f7b7ee0702cc 703
viewdeep51 3:ae45e29f5d4f 704 void HB_fail_state_machine()
viewdeep51 0:ba33a62aea4e 705 {
viewdeep51 3:ae45e29f5d4f 706 static uint8_t fail_count = 0;
viewdeep51 3:ae45e29f5d4f 707 if(HB_rcvd)
viewdeep51 3:ae45e29f5d4f 708 {
viewdeep51 3:ae45e29f5d4f 709 HB_rcvd = false;
viewdeep51 3:ae45e29f5d4f 710 device_ERR = false;
viewdeep51 3:ae45e29f5d4f 711 }
viewdeep51 3:ae45e29f5d4f 712 else if(++fail_count >=3) // if max 07 number of connected eots 7x3=21, it takes 150x21=3.150 sec to alarm
viewdeep51 3:ae45e29f5d4f 713 {
viewdeep51 3:ae45e29f5d4f 714 fail_count = 0;
viewdeep51 5:f7b7ee0702cc 715 //led_dir[dir-1] = OFF; //current command if any turned OFF
viewdeep51 4:710464cb44fc 716 led_cmd_ack = OFF;
viewdeep51 3:ae45e29f5d4f 717 display_flash = false;
viewdeep51 3:ae45e29f5d4f 718 device_ERR = true;
viewdeep51 3:ae45e29f5d4f 719 max7219.disable_device(2); //shutdown mode
viewdeep51 3:ae45e29f5d4f 720 wait_ms(2);
viewdeep51 4:710464cb44fc 721 if(addr_ID != ach_M)
viewdeep51 3:ae45e29f5d4f 722 {
viewdeep51 4:710464cb44fc 723 for(uint8_t i = 0; i <=7; i++)
viewdeep51 4:710464cb44fc 724 {
viewdeep51 4:710464cb44fc 725 led_ach[i] = OFF; //all other active chnnels leds or device errors
viewdeep51 4:710464cb44fc 726 }
viewdeep51 4:710464cb44fc 727 led_ach[addr_ID - 1] = _FLASH; //specific device is in error flshes only
viewdeep51 3:ae45e29f5d4f 728 }
viewdeep51 3:ae45e29f5d4f 729 HB_fail.detach();
viewdeep51 3:ae45e29f5d4f 730 }
viewdeep51 0:ba33a62aea4e 731 }
viewdeep51 0:ba33a62aea4e 732
viewdeep51 5:f7b7ee0702cc 733 void NUMERIC_handler(uint8_t key_ID) //function sacnning all the 0-9 and up-down number keys
viewdeep51 0:ba33a62aea4e 734 {
viewdeep51 0:ba33a62aea4e 735 uint8_t num;
viewdeep51 0:ba33a62aea4e 736 num = key_ID;
viewdeep51 0:ba33a62aea4e 737 if(key_ID != FALSE0)
viewdeep51 0:ba33a62aea4e 738 {
viewdeep51 0:ba33a62aea4e 739 if(((ach_M == pot_Master) && (addr_ID == pot_Master)) || ((ach_M == pot_Submaster) && (addr_ID == pot_Submaster))) //for Master
viewdeep51 3:ae45e29f5d4f 740 {
viewdeep51 4:710464cb44fc 741 if(disp_clear) //if last command has been acknowledged
viewdeep51 4:710464cb44fc 742 {
viewdeep51 4:710464cb44fc 743 for (uint8_t j = 0; j < 4; j++)
viewdeep51 4:710464cb44fc 744 {
viewdeep51 4:710464cb44fc 745 rpm_data[j] = 0; //clear rpm_data to enter new values
viewdeep51 4:710464cb44fc 746 }
viewdeep51 4:710464cb44fc 747 disp_clear = false;
viewdeep51 4:710464cb44fc 748 }
viewdeep51 3:ae45e29f5d4f 749 else
viewdeep51 4:710464cb44fc 750 __nop();
viewdeep51 3:ae45e29f5d4f 751
viewdeep51 4:710464cb44fc 752 for (uint8_t j = 0; j < 4; j++)
viewdeep51 4:710464cb44fc 753 {
viewdeep51 4:710464cb44fc 754 rpm_data[j] = rpm_data[j + 1]; //at every key press array shift to left
viewdeep51 4:710464cb44fc 755 }
viewdeep51 4:710464cb44fc 756
viewdeep51 4:710464cb44fc 757 rpm_data[3] = num; //new value be updated at place of digit 4
viewdeep51 4:710464cb44fc 758
viewdeep51 4:710464cb44fc 759 for (uint8_t j = 1; j <= 4; j++)
viewdeep51 4:710464cb44fc 760 {
viewdeep51 4:710464cb44fc 761 max7219.write_digit(2,j,rpm_data[j-1]);
viewdeep51 4:710464cb44fc 762 }
viewdeep51 3:ae45e29f5d4f 763 }
viewdeep51 0:ba33a62aea4e 764 else
viewdeep51 0:ba33a62aea4e 765 __nop();
viewdeep51 0:ba33a62aea4e 766 }
viewdeep51 0:ba33a62aea4e 767 else
viewdeep51 0:ba33a62aea4e 768 __nop();
viewdeep51 0:ba33a62aea4e 769 }
viewdeep51 0:ba33a62aea4e 770
viewdeep51 3:ae45e29f5d4f 771
viewdeep51 0:ba33a62aea4e 772 void CTRL_handler(uint8_t CTRL_ID) //handle control transfer
viewdeep51 0:ba33a62aea4e 773 {
viewdeep51 0:ba33a62aea4e 774 if(CTRL_ID != FALSE1)
viewdeep51 0:ba33a62aea4e 775 {
viewdeep51 0:ba33a62aea4e 776 if((addr_ID == pot_Slave_MCR) || (addr_ID == pot_Slave_ER))
viewdeep51 0:ba33a62aea4e 777 {
viewdeep51 4:710464cb44fc 778 if((CTRL_ID == REQ_)&&(!REQ_rcvd)) //if no request has been received
viewdeep51 0:ba33a62aea4e 779 {
viewdeep51 0:ba33a62aea4e 780 req = REQ_; //req data updated 0 here, same int values as in enum
viewdeep51 3:ae45e29f5d4f 781 REQ_key = true;
viewdeep51 3:ae45e29f5d4f 782 led_ctrl[REQ_-1] = _FLASH; //req led flash starts
viewdeep51 0:ba33a62aea4e 783 R_A_Ticker.attach(&R_A_Statemachine,0.5);
viewdeep51 0:ba33a62aea4e 784 }
viewdeep51 3:ae45e29f5d4f 785 else
viewdeep51 3:ae45e29f5d4f 786 __nop();
viewdeep51 3:ae45e29f5d4f 787
viewdeep51 3:ae45e29f5d4f 788 if((CTRL_ID == ACK_)&& (REQ_rcvd))
viewdeep51 0:ba33a62aea4e 789 {
viewdeep51 3:ae45e29f5d4f 790 REQ_rcvd = false;
viewdeep51 0:ba33a62aea4e 791 ack = ACK_; //ack data updated 2 here
viewdeep51 3:ae45e29f5d4f 792 ACK_key = true;
viewdeep51 3:ae45e29f5d4f 793 int_B.rise(NULL); //interrupt disabled
viewdeep51 3:ae45e29f5d4f 794 wait_us(10);
viewdeep51 3:ae45e29f5d4f 795 mcp23s17_1.gpintenb(0x00); //ACK key interrupts disabled
viewdeep51 3:ae45e29f5d4f 796 wait_us(10);
viewdeep51 3:ae45e29f5d4f 797 led_ctrl[ACK_-1] = OFF; //ack led turns off
viewdeep51 0:ba33a62aea4e 798 R_A_Ticker.detach();
viewdeep51 0:ba33a62aea4e 799 }
viewdeep51 3:ae45e29f5d4f 800 else
viewdeep51 3:ae45e29f5d4f 801 __nop();
viewdeep51 0:ba33a62aea4e 802 }
viewdeep51 0:ba33a62aea4e 803 }
viewdeep51 0:ba33a62aea4e 804 else
viewdeep51 0:ba33a62aea4e 805 __nop(); //then do nothing
viewdeep51 0:ba33a62aea4e 806 }
viewdeep51 0:ba33a62aea4e 807
viewdeep51 5:f7b7ee0702cc 808 void UP_DOWN_handler(uint8_t key_ID)
viewdeep51 5:f7b7ee0702cc 809 {
viewdeep51 5:f7b7ee0702cc 810 if(key_ID != FALSE2)
viewdeep51 5:f7b7ee0702cc 811 {
viewdeep51 5:f7b7ee0702cc 812 if(((ach_M == pot_Master) && (addr_ID == pot_Master)) || ((ach_M == pot_Submaster) && (addr_ID == pot_Submaster))) //for Master
viewdeep51 5:f7b7ee0702cc 813 {
viewdeep51 5:f7b7ee0702cc 814 if(key_ID == UP_key)
viewdeep51 5:f7b7ee0702cc 815 {
viewdeep51 5:f7b7ee0702cc 816 rpm_data[3]++;
viewdeep51 5:f7b7ee0702cc 817 if(rpm_data[3] == 10)
viewdeep51 5:f7b7ee0702cc 818 {
viewdeep51 5:f7b7ee0702cc 819 rpm_data[3] = 0;
viewdeep51 5:f7b7ee0702cc 820 rpm_data[2]++;
viewdeep51 5:f7b7ee0702cc 821 if(rpm_data[2] == 10)
viewdeep51 5:f7b7ee0702cc 822 {
viewdeep51 5:f7b7ee0702cc 823 rpm_data[2] = 0;
viewdeep51 5:f7b7ee0702cc 824 rpm_data[1]++;
viewdeep51 5:f7b7ee0702cc 825 if(rpm_data[1] == 10)
viewdeep51 5:f7b7ee0702cc 826 {
viewdeep51 5:f7b7ee0702cc 827 rpm_data[1] = 0;
viewdeep51 5:f7b7ee0702cc 828 rpm_data[0]++;
viewdeep51 5:f7b7ee0702cc 829 if(rpm_data[0] == 10)
viewdeep51 5:f7b7ee0702cc 830 rpm_data[0] = 9;
viewdeep51 5:f7b7ee0702cc 831 else
viewdeep51 5:f7b7ee0702cc 832 __nop();
viewdeep51 5:f7b7ee0702cc 833 }
viewdeep51 5:f7b7ee0702cc 834 else
viewdeep51 5:f7b7ee0702cc 835 __nop();
viewdeep51 5:f7b7ee0702cc 836 }
viewdeep51 5:f7b7ee0702cc 837 else
viewdeep51 5:f7b7ee0702cc 838 __nop();
viewdeep51 5:f7b7ee0702cc 839 }
viewdeep51 5:f7b7ee0702cc 840 else
viewdeep51 5:f7b7ee0702cc 841 __nop();
viewdeep51 5:f7b7ee0702cc 842 }
viewdeep51 5:f7b7ee0702cc 843 else if(key_ID == DOWN_key)
viewdeep51 5:f7b7ee0702cc 844 {
viewdeep51 5:f7b7ee0702cc 845 rpm_data[3]--;
viewdeep51 5:f7b7ee0702cc 846 if(rpm_data[3] == 0)
viewdeep51 5:f7b7ee0702cc 847 {
viewdeep51 5:f7b7ee0702cc 848 rpm_data[3] = 0;
viewdeep51 5:f7b7ee0702cc 849 rpm_data[2]--;
viewdeep51 5:f7b7ee0702cc 850 if(rpm_data[2] == 0)
viewdeep51 5:f7b7ee0702cc 851 {
viewdeep51 5:f7b7ee0702cc 852 rpm_data[2] = 0;
viewdeep51 5:f7b7ee0702cc 853 rpm_data[1]--;
viewdeep51 5:f7b7ee0702cc 854 if(rpm_data[1] == 0)
viewdeep51 5:f7b7ee0702cc 855 {
viewdeep51 5:f7b7ee0702cc 856 rpm_data[1] = 0;
viewdeep51 5:f7b7ee0702cc 857 rpm_data[0]--;
viewdeep51 5:f7b7ee0702cc 858 if(rpm_data[0] == 0)
viewdeep51 5:f7b7ee0702cc 859 rpm_data[0] = 0;
viewdeep51 5:f7b7ee0702cc 860 else
viewdeep51 5:f7b7ee0702cc 861 __nop();
viewdeep51 5:f7b7ee0702cc 862 }
viewdeep51 5:f7b7ee0702cc 863 else
viewdeep51 5:f7b7ee0702cc 864 __nop();
viewdeep51 5:f7b7ee0702cc 865 }
viewdeep51 5:f7b7ee0702cc 866 else
viewdeep51 5:f7b7ee0702cc 867 __nop();
viewdeep51 5:f7b7ee0702cc 868 }
viewdeep51 5:f7b7ee0702cc 869 else
viewdeep51 5:f7b7ee0702cc 870 __nop();
viewdeep51 5:f7b7ee0702cc 871
viewdeep51 5:f7b7ee0702cc 872 }
viewdeep51 5:f7b7ee0702cc 873 else
viewdeep51 5:f7b7ee0702cc 874 __nop();
viewdeep51 5:f7b7ee0702cc 875
viewdeep51 5:f7b7ee0702cc 876 for (uint8_t j = 1; j <= 4; j++)
viewdeep51 5:f7b7ee0702cc 877 {
viewdeep51 5:f7b7ee0702cc 878 max7219.write_digit(2,j,rpm_data[j-1]); //update data to display
viewdeep51 5:f7b7ee0702cc 879 }
viewdeep51 5:f7b7ee0702cc 880 }
viewdeep51 5:f7b7ee0702cc 881 else
viewdeep51 5:f7b7ee0702cc 882 __nop();
viewdeep51 5:f7b7ee0702cc 883 }
viewdeep51 5:f7b7ee0702cc 884 else
viewdeep51 5:f7b7ee0702cc 885 __nop();
viewdeep51 5:f7b7ee0702cc 886
viewdeep51 5:f7b7ee0702cc 887 }
viewdeep51 5:f7b7ee0702cc 888
viewdeep51 5:f7b7ee0702cc 889 //functon not required as DiR keys change to UP-down RPM
viewdeep51 5:f7b7ee0702cc 890 /*
viewdeep51 0:ba33a62aea4e 891 void DIR_handler(uint8_t DIR_ID) //handle control transfer
viewdeep51 0:ba33a62aea4e 892 {
viewdeep51 0:ba33a62aea4e 893 if(DIR_ID != FALSE2)
viewdeep51 0:ba33a62aea4e 894 {
viewdeep51 0:ba33a62aea4e 895 if(addr_ID == ach_M)
viewdeep51 3:ae45e29f5d4f 896 {
viewdeep51 3:ae45e29f5d4f 897 current_status_dir = 2; //flash
viewdeep51 3:ae45e29f5d4f 898 led_dir[dir-1] = OFF;
viewdeep51 3:ae45e29f5d4f 899 dir = DIR_ID;
viewdeep51 3:ae45e29f5d4f 900 led_dir[dir-1] = _FLASH; //led direction AHEAD start flashing
viewdeep51 3:ae45e29f5d4f 901 buzzer = ON; //buzzer ON
viewdeep51 3:ae45e29f5d4f 902 DIR_cmd = true; // dir cmd needs to be sent
viewdeep51 3:ae45e29f5d4f 903 }
viewdeep51 3:ae45e29f5d4f 904 else
viewdeep51 3:ae45e29f5d4f 905 __nop();
viewdeep51 3:ae45e29f5d4f 906
viewdeep51 3:ae45e29f5d4f 907 if (addr_ID == ach_S)
viewdeep51 3:ae45e29f5d4f 908 {
viewdeep51 3:ae45e29f5d4f 909 if((dir == DIR_ID)&&(DIR_cmd)) //direction command is received
viewdeep51 0:ba33a62aea4e 910 {
viewdeep51 3:ae45e29f5d4f 911 current_status_dir = 1;
viewdeep51 3:ae45e29f5d4f 912 led_dir[dir-1] = OFF;
viewdeep51 3:ae45e29f5d4f 913 dir = DIR_ID;
viewdeep51 3:ae45e29f5d4f 914 led_dir[dir-1] = ON; //led direction AHEAD start flashing
viewdeep51 3:ae45e29f5d4f 915 buzzer = OFF; //buzzer OFF
viewdeep51 3:ae45e29f5d4f 916 DIR_key = true; //dir ack will be sent in response to HB
viewdeep51 0:ba33a62aea4e 917 }
viewdeep51 0:ba33a62aea4e 918 else
viewdeep51 0:ba33a62aea4e 919 __nop();
viewdeep51 0:ba33a62aea4e 920 }
viewdeep51 3:ae45e29f5d4f 921 else
viewdeep51 3:ae45e29f5d4f 922 __nop();
viewdeep51 0:ba33a62aea4e 923 }
viewdeep51 0:ba33a62aea4e 924 else
viewdeep51 3:ae45e29f5d4f 925 __nop(); //then do nothing
viewdeep51 0:ba33a62aea4e 926 }
viewdeep51 5:f7b7ee0702cc 927 */
viewdeep51 0:ba33a62aea4e 928
viewdeep51 0:ba33a62aea4e 929 void DISP_handler(uint8_t DISP_ID) //handle control transfer
viewdeep51 0:ba33a62aea4e 930 {
viewdeep51 0:ba33a62aea4e 931 uint8_t disp_key;
viewdeep51 0:ba33a62aea4e 932 disp_key = DISP_ID;
viewdeep51 0:ba33a62aea4e 933 if(DISP_ID != FALSE3)
viewdeep51 0:ba33a62aea4e 934 {
viewdeep51 3:ae45e29f5d4f 935 switch(disp_key)
viewdeep51 0:ba33a62aea4e 936 {
viewdeep51 3:ae45e29f5d4f 937 case clr_ENTRY: //CE,elete only last/4th digit and shift others digits towards right/4th digit
viewdeep51 3:ae45e29f5d4f 938 if(addr_ID == ach_M)
viewdeep51 3:ae45e29f5d4f 939 {
viewdeep51 4:710464cb44fc 940 for (uint8_t j = 3; j > 0; j--)
viewdeep51 3:ae45e29f5d4f 941 {
viewdeep51 4:710464cb44fc 942 rpm_data[j] = rpm_data[j - 1]; //at every key press array shift to left
viewdeep51 3:ae45e29f5d4f 943 }
viewdeep51 4:710464cb44fc 944 rpm_data[0] = 0;
viewdeep51 4:710464cb44fc 945
viewdeep51 4:710464cb44fc 946 for (uint8_t j = 1; j <= 4; j++)
viewdeep51 0:ba33a62aea4e 947 {
viewdeep51 4:710464cb44fc 948 max7219.write_digit(2,j,rpm_data[j-1]);
viewdeep51 0:ba33a62aea4e 949 }
viewdeep51 3:ae45e29f5d4f 950 }
viewdeep51 3:ae45e29f5d4f 951 else
viewdeep51 3:ae45e29f5d4f 952 {
viewdeep51 3:ae45e29f5d4f 953 __nop();
viewdeep51 3:ae45e29f5d4f 954 }
viewdeep51 3:ae45e29f5d4f 955 break;
viewdeep51 3:ae45e29f5d4f 956
viewdeep51 4:710464cb44fc 957 case clr_ALL:
viewdeep51 3:ae45e29f5d4f 958 if(addr_ID == ach_M)
viewdeep51 3:ae45e29f5d4f 959 { //C delete all digits in DS1 and show last entered value
viewdeep51 4:710464cb44fc 960 for (uint8_t j = 0; j <=3; j++)
viewdeep51 0:ba33a62aea4e 961 {
viewdeep51 4:710464cb44fc 962 rpm_data[j] = 0; //at every key press array shift to left
viewdeep51 4:710464cb44fc 963 }
viewdeep51 4:710464cb44fc 964 for (uint8_t j = 1; j <= 4; j++) // all digits turns to zero
viewdeep51 4:710464cb44fc 965 {
viewdeep51 4:710464cb44fc 966 max7219.write_digit(2,j,rpm_data[j-1]);
viewdeep51 0:ba33a62aea4e 967 }
viewdeep51 3:ae45e29f5d4f 968 }
viewdeep51 3:ae45e29f5d4f 969 else
viewdeep51 3:ae45e29f5d4f 970 {
viewdeep51 3:ae45e29f5d4f 971 __nop();
viewdeep51 3:ae45e29f5d4f 972 }
viewdeep51 3:ae45e29f5d4f 973 break;
viewdeep51 3:ae45e29f5d4f 974
viewdeep51 3:ae45e29f5d4f 975 case ENTER_ACK: //work as Enter in master and CMD ACK in slave
viewdeep51 3:ae45e29f5d4f 976 if((addr_ID == ach_S)&&(RPM_cmd)) //RPM command ack needs to be sent
viewdeep51 4:710464cb44fc 977 {
viewdeep51 4:710464cb44fc 978 RPM_cmd = false;
viewdeep51 3:ae45e29f5d4f 979 display_flash = false;
viewdeep51 3:ae45e29f5d4f 980 buzzer = OFF;
viewdeep51 4:710464cb44fc 981 int_B.rise(NULL);
viewdeep51 4:710464cb44fc 982 wait_us(10);
viewdeep51 4:710464cb44fc 983 mcp23s17_1.gpintenb(0x00); //ACK key interrupts disabled
viewdeep51 4:710464cb44fc 984 wait_us(10);
viewdeep51 4:710464cb44fc 985 ENTER_ACK_key = true; //flashing display1 turned steady //RPM command acknowledged by active slave
viewdeep51 3:ae45e29f5d4f 986 }
viewdeep51 3:ae45e29f5d4f 987 else if (addr_ID == ach_M) //in case of active masters
viewdeep51 5:f7b7ee0702cc 988 {
viewdeep51 5:f7b7ee0702cc 989 int RPM_set = 0; //disp1 start flashing, and data sent over UART for all digits 1-4
viewdeep51 5:f7b7ee0702cc 990 RPM_set = (rpm_data[0]*1000) + (rpm_data[1]*100) + (rpm_data[2]*10) + rpm_data[3];
viewdeep51 5:f7b7ee0702cc 991 if(RPM_set>Max_RPM)
viewdeep51 5:f7b7ee0702cc 992 {
viewdeep51 5:f7b7ee0702cc 993 for (uint8_t j = 0; j <=3; j++)
viewdeep51 5:f7b7ee0702cc 994 {
viewdeep51 5:f7b7ee0702cc 995 rpm_data[j] = 0; //at every key press array shift to left
viewdeep51 5:f7b7ee0702cc 996 }
viewdeep51 5:f7b7ee0702cc 997 for (uint8_t j = 1; j <= 4; j++) // all digits turns to zero
viewdeep51 5:f7b7ee0702cc 998 {
viewdeep51 5:f7b7ee0702cc 999 max7219.write_digit(2,j,rpm_data[j-1]);
viewdeep51 5:f7b7ee0702cc 1000 }
viewdeep51 5:f7b7ee0702cc 1001 }
viewdeep51 5:f7b7ee0702cc 1002 else
viewdeep51 5:f7b7ee0702cc 1003 {
viewdeep51 5:f7b7ee0702cc 1004 display_flash = true; //display starts flashing
viewdeep51 5:f7b7ee0702cc 1005 mcp23s17_1.gpintena(0x00); //ACK key interrupts disabled
viewdeep51 5:f7b7ee0702cc 1006 wait_us(10);
viewdeep51 5:f7b7ee0702cc 1007 mcp23s17_1.gpintenb(0x00); //ACK key interrupts disabled
viewdeep51 5:f7b7ee0702cc 1008 wait_us(10);
viewdeep51 5:f7b7ee0702cc 1009 current_status = 2; //flash
viewdeep51 5:f7b7ee0702cc 1010 buzzer = ON;
viewdeep51 5:f7b7ee0702cc 1011 RPM_cmd = true; //command need to be send
viewdeep51 5:f7b7ee0702cc 1012 }
viewdeep51 3:ae45e29f5d4f 1013 }
viewdeep51 3:ae45e29f5d4f 1014 else
viewdeep51 3:ae45e29f5d4f 1015 {
viewdeep51 3:ae45e29f5d4f 1016 __nop();
viewdeep51 3:ae45e29f5d4f 1017 }
viewdeep51 3:ae45e29f5d4f 1018 break;
viewdeep51 3:ae45e29f5d4f 1019
viewdeep51 3:ae45e29f5d4f 1020 default:
viewdeep51 3:ae45e29f5d4f 1021 __nop();
viewdeep51 3:ae45e29f5d4f 1022 break;
viewdeep51 0:ba33a62aea4e 1023 }
viewdeep51 0:ba33a62aea4e 1024 }
viewdeep51 0:ba33a62aea4e 1025 else
viewdeep51 0:ba33a62aea4e 1026 __nop(); //then do nothing
viewdeep51 0:ba33a62aea4e 1027 }
viewdeep51 0:ba33a62aea4e 1028
viewdeep51 3:ae45e29f5d4f 1029 /******************************************************************
viewdeep51 3:ae45e29f5d4f 1030 function to set brightness of leds and seven segment
viewdeep51 3:ae45e29f5d4f 1031 ******************************************************************/
viewdeep51 0:ba33a62aea4e 1032 void SET_handler(uint8_t Set_ID)
viewdeep51 0:ba33a62aea4e 1033 {
viewdeep51 3:ae45e29f5d4f 1034 if(Set_ID != FALSE2)
viewdeep51 0:ba33a62aea4e 1035 {
viewdeep51 3:ae45e29f5d4f 1036 switch(Set_ID)
viewdeep51 0:ba33a62aea4e 1037 {
viewdeep51 3:ae45e29f5d4f 1038 case INC:
viewdeep51 3:ae45e29f5d4f 1039 duty_cycle = mypwm.read(); //as duty cycle decreases creases brightnes increases
viewdeep51 4:710464cb44fc 1040 if( duty_cycle > 0.02f)
viewdeep51 3:ae45e29f5d4f 1041 {
viewdeep51 3:ae45e29f5d4f 1042 duty_cycle = duty_cycle-0.01f;
viewdeep51 3:ae45e29f5d4f 1043 mypwm.write(duty_cycle);
viewdeep51 3:ae45e29f5d4f 1044 }
viewdeep51 3:ae45e29f5d4f 1045 else
viewdeep51 3:ae45e29f5d4f 1046 {
viewdeep51 4:710464cb44fc 1047 duty_cycle = 0.01f;
viewdeep51 3:ae45e29f5d4f 1048 }
viewdeep51 4:710464cb44fc 1049 if(DEVICE1.intensity < 14)
viewdeep51 3:ae45e29f5d4f 1050 {
viewdeep51 3:ae45e29f5d4f 1051 DEVICE1.intensity ++; //intensity modified
viewdeep51 3:ae45e29f5d4f 1052 DEVICE2.intensity ++;
viewdeep51 3:ae45e29f5d4f 1053 max7219.set_intensity(DEVICE1); //updating brightness value of DEVICE1
viewdeep51 3:ae45e29f5d4f 1054 max7219.set_intensity(DEVICE2); //updating brightness value of DEVICE2
viewdeep51 3:ae45e29f5d4f 1055 }
viewdeep51 3:ae45e29f5d4f 1056 else
viewdeep51 3:ae45e29f5d4f 1057 {
viewdeep51 4:710464cb44fc 1058 DEVICE1.intensity =15;
viewdeep51 4:710464cb44fc 1059 DEVICE2.intensity =15;
viewdeep51 3:ae45e29f5d4f 1060 max7219.set_intensity(DEVICE1); //updating brightness value of DEVICE1
viewdeep51 3:ae45e29f5d4f 1061 max7219.set_intensity(DEVICE2); //updating brightness value of DEVICE2
viewdeep51 3:ae45e29f5d4f 1062 }
viewdeep51 3:ae45e29f5d4f 1063 break;
viewdeep51 3:ae45e29f5d4f 1064
viewdeep51 3:ae45e29f5d4f 1065 case DEC:
viewdeep51 3:ae45e29f5d4f 1066 duty_cycle = mypwm.read();
viewdeep51 4:710464cb44fc 1067 if( duty_cycle < 0.98f) //as duty cycle increases brightnes decreases
viewdeep51 3:ae45e29f5d4f 1068 {
viewdeep51 3:ae45e29f5d4f 1069 duty_cycle = duty_cycle+0.01f;
viewdeep51 3:ae45e29f5d4f 1070 mypwm.write(duty_cycle);
viewdeep51 3:ae45e29f5d4f 1071 }
viewdeep51 3:ae45e29f5d4f 1072 else
viewdeep51 3:ae45e29f5d4f 1073 {
viewdeep51 4:710464cb44fc 1074 duty_cycle = 0.99f;
viewdeep51 3:ae45e29f5d4f 1075 }
viewdeep51 4:710464cb44fc 1076 if(DEVICE1.intensity > 2)
viewdeep51 3:ae45e29f5d4f 1077 {
viewdeep51 3:ae45e29f5d4f 1078 DEVICE1.intensity --; //intensity modified
viewdeep51 3:ae45e29f5d4f 1079 DEVICE2.intensity --;
viewdeep51 3:ae45e29f5d4f 1080 max7219.set_intensity(DEVICE1); //updating brightness value of DEVICE1
viewdeep51 3:ae45e29f5d4f 1081 max7219.set_intensity(DEVICE2); //updating brightness value of DEVICE2
viewdeep51 3:ae45e29f5d4f 1082 }
viewdeep51 3:ae45e29f5d4f 1083 else
viewdeep51 3:ae45e29f5d4f 1084 {
viewdeep51 4:710464cb44fc 1085 DEVICE1.intensity =1;
viewdeep51 4:710464cb44fc 1086 DEVICE2.intensity =1; //minimum brightnes
viewdeep51 3:ae45e29f5d4f 1087 max7219.set_intensity(DEVICE1); //updating brightness value of DEVICE1
viewdeep51 3:ae45e29f5d4f 1088 max7219.set_intensity(DEVICE2); //updating brightness value of DEVICE2
viewdeep51 3:ae45e29f5d4f 1089 }
viewdeep51 3:ae45e29f5d4f 1090 break;
viewdeep51 3:ae45e29f5d4f 1091
viewdeep51 3:ae45e29f5d4f 1092 case TEST:
viewdeep51 3:ae45e29f5d4f 1093 test_cnt++;
viewdeep51 3:ae45e29f5d4f 1094 if(test_cnt <= 1)
viewdeep51 3:ae45e29f5d4f 1095 test = true;
viewdeep51 3:ae45e29f5d4f 1096 else if(test_cnt >= 2)
viewdeep51 3:ae45e29f5d4f 1097 {
viewdeep51 3:ae45e29f5d4f 1098 test = false;
viewdeep51 3:ae45e29f5d4f 1099 test_cnt = 0;
viewdeep51 3:ae45e29f5d4f 1100 }
viewdeep51 3:ae45e29f5d4f 1101 else
viewdeep51 3:ae45e29f5d4f 1102 __nop();
viewdeep51 3:ae45e29f5d4f 1103 break;
viewdeep51 3:ae45e29f5d4f 1104
viewdeep51 3:ae45e29f5d4f 1105 default:
viewdeep51 3:ae45e29f5d4f 1106 __nop();
viewdeep51 3:ae45e29f5d4f 1107 break;
viewdeep51 0:ba33a62aea4e 1108 }
viewdeep51 0:ba33a62aea4e 1109 }
viewdeep51 0:ba33a62aea4e 1110 else
viewdeep51 0:ba33a62aea4e 1111 __nop();
viewdeep51 0:ba33a62aea4e 1112 }
viewdeep51 0:ba33a62aea4e 1113 /**************************************************************************************
viewdeep51 3:ae45e29f5d4f 1114 POT SPI Data Read KEY FUNCTIONS
viewdeep51 0:ba33a62aea4e 1115 **************************************************************************************/
viewdeep51 0:ba33a62aea4e 1116 unsigned char read_KEY_SPI(int CH)
viewdeep51 0:ba33a62aea4e 1117 {
viewdeep51 0:ba33a62aea4e 1118 char _key;
viewdeep51 0:ba33a62aea4e 1119 switch(CH)
viewdeep51 0:ba33a62aea4e 1120 {
viewdeep51 0:ba33a62aea4e 1121 case 1:
viewdeep51 3:ae45e29f5d4f 1122 wait_ms(1);
viewdeep51 3:ae45e29f5d4f 1123 _key = mcp23s17_1.intcapa(); // read(INTCAPA_ADDR);
viewdeep51 3:ae45e29f5d4f 1124 wait_ms(1);
viewdeep51 0:ba33a62aea4e 1125 break;
viewdeep51 0:ba33a62aea4e 1126
viewdeep51 0:ba33a62aea4e 1127 case 2:
viewdeep51 3:ae45e29f5d4f 1128 wait_ms(1);
viewdeep51 0:ba33a62aea4e 1129 _key=mcp23s17_1.intcapb();
viewdeep51 3:ae45e29f5d4f 1130 wait_ms(1);
viewdeep51 0:ba33a62aea4e 1131 break;
viewdeep51 0:ba33a62aea4e 1132
viewdeep51 0:ba33a62aea4e 1133 case 3:
viewdeep51 3:ae45e29f5d4f 1134 wait_ms(1);
viewdeep51 0:ba33a62aea4e 1135 _key=mcp23s17_2.intcapa();
viewdeep51 3:ae45e29f5d4f 1136 wait_ms(1);
viewdeep51 0:ba33a62aea4e 1137 break;
viewdeep51 0:ba33a62aea4e 1138 case 4:
viewdeep51 3:ae45e29f5d4f 1139 wait_ms(1);
viewdeep51 3:ae45e29f5d4f 1140 _key = mcp23s17_2.gpiob(); //address read PORTB of chip2
viewdeep51 3:ae45e29f5d4f 1141 wait_ms(1);
viewdeep51 0:ba33a62aea4e 1142 break;
viewdeep51 0:ba33a62aea4e 1143 default:
viewdeep51 0:ba33a62aea4e 1144 _key = 0;
viewdeep51 0:ba33a62aea4e 1145 break;
viewdeep51 0:ba33a62aea4e 1146 }
viewdeep51 0:ba33a62aea4e 1147 return(_key);
viewdeep51 0:ba33a62aea4e 1148 }
viewdeep51 3:ae45e29f5d4f 1149
viewdeep51 0:ba33a62aea4e 1150 void read_KEY_A() //function check status of PORTA of MCP23S17 (1) chip
viewdeep51 0:ba33a62aea4e 1151 {
viewdeep51 0:ba33a62aea4e 1152 char key_status = 0;
viewdeep51 5:f7b7ee0702cc 1153 num_KEY = FALSE0;
viewdeep51 0:ba33a62aea4e 1154 key_status = read_KEY_SPI(ch_A); //to read portA of mcp23s17(1)
viewdeep51 0:ba33a62aea4e 1155 if(key_status != 0xff) //means some key must have been pressed
viewdeep51 0:ba33a62aea4e 1156 {
viewdeep51 0:ba33a62aea4e 1157 switch(key_status)
viewdeep51 0:ba33a62aea4e 1158 {
viewdeep51 0:ba33a62aea4e 1159 case 0xfe: //0xfe GPA0
viewdeep51 1:e116808d8b00 1160 num_KEY = ZERO;
viewdeep51 0:ba33a62aea4e 1161 break;
viewdeep51 0:ba33a62aea4e 1162 case 0xfd: //0xfd GPA1
viewdeep51 0:ba33a62aea4e 1163 num_KEY = ONE;
viewdeep51 0:ba33a62aea4e 1164 break;
viewdeep51 0:ba33a62aea4e 1165 case 0xfb: //0xfb GPA2
viewdeep51 0:ba33a62aea4e 1166 num_KEY = TWO;
viewdeep51 0:ba33a62aea4e 1167 break;
viewdeep51 0:ba33a62aea4e 1168 case 0xf7: //0xf7 GPA3
viewdeep51 0:ba33a62aea4e 1169 num_KEY = THREE;
viewdeep51 0:ba33a62aea4e 1170 break;
viewdeep51 0:ba33a62aea4e 1171 case 0xef: //0xef GPA4
viewdeep51 0:ba33a62aea4e 1172 num_KEY = FOUR;
viewdeep51 0:ba33a62aea4e 1173 break;
viewdeep51 0:ba33a62aea4e 1174 case 0xdf: //0xdf GPA5
viewdeep51 0:ba33a62aea4e 1175 num_KEY = FIVE;
viewdeep51 0:ba33a62aea4e 1176 break;
viewdeep51 0:ba33a62aea4e 1177 case 0xbf: //0xbf GPA6
viewdeep51 0:ba33a62aea4e 1178 num_KEY = SIX;
viewdeep51 0:ba33a62aea4e 1179 break;
viewdeep51 0:ba33a62aea4e 1180 case 0x7f: //0x7f GPA7
viewdeep51 0:ba33a62aea4e 1181 num_KEY = SEVEN;
viewdeep51 0:ba33a62aea4e 1182 break;
viewdeep51 0:ba33a62aea4e 1183 default:
viewdeep51 0:ba33a62aea4e 1184 num_KEY = FALSE0;
viewdeep51 0:ba33a62aea4e 1185 break;
viewdeep51 0:ba33a62aea4e 1186 }
viewdeep51 0:ba33a62aea4e 1187 NUMERIC_handler(num_KEY);
viewdeep51 0:ba33a62aea4e 1188 }
viewdeep51 0:ba33a62aea4e 1189 }
viewdeep51 0:ba33a62aea4e 1190
viewdeep51 0:ba33a62aea4e 1191 void read_KEY_B()
viewdeep51 0:ba33a62aea4e 1192 {
viewdeep51 0:ba33a62aea4e 1193 char key_status = 0;
viewdeep51 5:f7b7ee0702cc 1194 num_KEY = FALSE0;
viewdeep51 5:f7b7ee0702cc 1195 ctrl_KEY = FALSE1;
viewdeep51 5:f7b7ee0702cc 1196 //dir_KEY = FALSE2; /not required
viewdeep51 5:f7b7ee0702cc 1197 up_down_KEY = FALSE2;
viewdeep51 5:f7b7ee0702cc 1198 disp_KEY = FALSE3;
viewdeep51 5:f7b7ee0702cc 1199
viewdeep51 0:ba33a62aea4e 1200 key_status = read_KEY_SPI(ch_B); //to read portBof mcp23s17(1)
viewdeep51 0:ba33a62aea4e 1201 if(key_status != 0xff)
viewdeep51 0:ba33a62aea4e 1202 {
viewdeep51 0:ba33a62aea4e 1203 switch(key_status)
viewdeep51 0:ba33a62aea4e 1204 {
viewdeep51 0:ba33a62aea4e 1205 case 0xfe: //0xfe GPA0
viewdeep51 0:ba33a62aea4e 1206 num_KEY = EIGHT;
viewdeep51 0:ba33a62aea4e 1207 NUMERIC_handler(num_KEY);
viewdeep51 0:ba33a62aea4e 1208 break;
viewdeep51 0:ba33a62aea4e 1209 case 0xfd: //0xfd GPA1
viewdeep51 0:ba33a62aea4e 1210 num_KEY = NINE;
viewdeep51 0:ba33a62aea4e 1211 NUMERIC_handler(num_KEY);
viewdeep51 0:ba33a62aea4e 1212 break;
viewdeep51 5:f7b7ee0702cc 1213 case 0xfb: //0xfb GPA2
viewdeep51 5:f7b7ee0702cc 1214 up_down_KEY = UP_key; //dir_KEY = AHEAD; /not required now
viewdeep51 5:f7b7ee0702cc 1215 UP_DOWN_handler(up_down_KEY); //DIR_handler(dir_KEY); /not required now
viewdeep51 0:ba33a62aea4e 1216 break;
viewdeep51 5:f7b7ee0702cc 1217 case 0xf7: //0xf7 GPA3
viewdeep51 5:f7b7ee0702cc 1218 up_down_KEY = DOWN_key; //dir_KEY = ASTERN; /not required now
viewdeep51 5:f7b7ee0702cc 1219 UP_DOWN_handler(up_down_KEY); //DIR_handler(dir_KEY); /not required now
viewdeep51 0:ba33a62aea4e 1220 break;
viewdeep51 3:ae45e29f5d4f 1221 case 0xef: //0xef GPA4
viewdeep51 3:ae45e29f5d4f 1222 ctrl_KEY = ACK_;
viewdeep51 0:ba33a62aea4e 1223 CTRL_handler(ctrl_KEY);
viewdeep51 0:ba33a62aea4e 1224 break;
viewdeep51 3:ae45e29f5d4f 1225 case 0xdf: //0xdf GPA5
viewdeep51 0:ba33a62aea4e 1226 disp_KEY = clr_ENTRY; //CE
viewdeep51 0:ba33a62aea4e 1227 DISP_handler(disp_KEY);
viewdeep51 0:ba33a62aea4e 1228 break;
viewdeep51 3:ae45e29f5d4f 1229 case 0xbf: //0xbf GPA6
viewdeep51 4:710464cb44fc 1230 disp_KEY = clr_ALL; //C
viewdeep51 0:ba33a62aea4e 1231 DISP_handler(disp_KEY);
viewdeep51 0:ba33a62aea4e 1232 break;
viewdeep51 3:ae45e29f5d4f 1233 case 0x7f: //0x7f GPA7
viewdeep51 0:ba33a62aea4e 1234 disp_KEY = ENTER_ACK;
viewdeep51 0:ba33a62aea4e 1235 DISP_handler(disp_KEY);
viewdeep51 0:ba33a62aea4e 1236 break;
viewdeep51 0:ba33a62aea4e 1237 default:
viewdeep51 0:ba33a62aea4e 1238 num_KEY = FALSE0;
viewdeep51 0:ba33a62aea4e 1239 ctrl_KEY = FALSE1;
viewdeep51 5:f7b7ee0702cc 1240 //dir_KEY = FALSE2;
viewdeep51 5:f7b7ee0702cc 1241 up_down_KEY = FALSE2;
viewdeep51 0:ba33a62aea4e 1242 disp_KEY = FALSE3;
viewdeep51 0:ba33a62aea4e 1243 break;
viewdeep51 0:ba33a62aea4e 1244 }
viewdeep51 0:ba33a62aea4e 1245 }
viewdeep51 0:ba33a62aea4e 1246 }
viewdeep51 0:ba33a62aea4e 1247 void read_KEY_C()
viewdeep51 0:ba33a62aea4e 1248 {
viewdeep51 0:ba33a62aea4e 1249 char key_status = 0;
viewdeep51 5:f7b7ee0702cc 1250 ctrl_KEY = FALSE1;
viewdeep51 5:f7b7ee0702cc 1251 set_KEY = FALSE4;
viewdeep51 0:ba33a62aea4e 1252 key_status = read_KEY_SPI(ch_C); //to read portA of mcp23s17(2)
viewdeep51 0:ba33a62aea4e 1253 if(key_status != 0xff)
viewdeep51 0:ba33a62aea4e 1254 {
viewdeep51 0:ba33a62aea4e 1255 if(key_status==0xfe)
viewdeep51 0:ba33a62aea4e 1256 {
viewdeep51 0:ba33a62aea4e 1257 ctrl_KEY = REQ_; //control transfer REQ button pressed
viewdeep51 0:ba33a62aea4e 1258 CTRL_handler(ctrl_KEY);
viewdeep51 0:ba33a62aea4e 1259 }
viewdeep51 0:ba33a62aea4e 1260 else
viewdeep51 0:ba33a62aea4e 1261 {
viewdeep51 0:ba33a62aea4e 1262 if(key_status==0xfd)
viewdeep51 0:ba33a62aea4e 1263 set_KEY = INC; //setting key INC
viewdeep51 0:ba33a62aea4e 1264 else if(key_status==0xfb)
viewdeep51 0:ba33a62aea4e 1265 set_KEY = DEC; //setting key DEC
viewdeep51 0:ba33a62aea4e 1266 else if(key_status==0xf7)
viewdeep51 0:ba33a62aea4e 1267 set_KEY = TEST; //setting key TEST
viewdeep51 0:ba33a62aea4e 1268 else
viewdeep51 0:ba33a62aea4e 1269 {
viewdeep51 0:ba33a62aea4e 1270 set_KEY = FALSE4; //setting key TEST
viewdeep51 0:ba33a62aea4e 1271 ctrl_KEY = FALSE1;
viewdeep51 0:ba33a62aea4e 1272 }
viewdeep51 0:ba33a62aea4e 1273
viewdeep51 0:ba33a62aea4e 1274 SET_handler(set_KEY);
viewdeep51 0:ba33a62aea4e 1275 }
viewdeep51 0:ba33a62aea4e 1276 }
viewdeep51 0:ba33a62aea4e 1277 }
viewdeep51 0:ba33a62aea4e 1278
viewdeep51 0:ba33a62aea4e 1279
viewdeep51 0:ba33a62aea4e 1280 /**************************************************************************************
viewdeep51 3:ae45e29f5d4f 1281 POT Serial Data Out FUNCTIONS
viewdeep51 0:ba33a62aea4e 1282 **************************************************************************************/
viewdeep51 4:710464cb44fc 1283 uint8_t get_checksum(const char *sentence)
viewdeep51 0:ba33a62aea4e 1284 {
viewdeep51 0:ba33a62aea4e 1285 const char *n = sentence + 1; // Plus one, skip '$'
viewdeep51 0:ba33a62aea4e 1286 uint8_t chk = 0;
viewdeep51 0:ba33a62aea4e 1287
viewdeep51 0:ba33a62aea4e 1288 /* While current char isn't '*' or sentence ending (newline) */
viewdeep51 4:710464cb44fc 1289 while (('*' != *n) &&(NMEA_END_CHAR_1 != *n))
viewdeep51 0:ba33a62aea4e 1290 {
viewdeep51 0:ba33a62aea4e 1291 if ('\0' == *n || n - sentence > NMEA_MAX_LENGTH)
viewdeep51 0:ba33a62aea4e 1292 {
viewdeep51 0:ba33a62aea4e 1293 /* Sentence too long or short */
viewdeep51 4:710464cb44fc 1294 chk = 0;
viewdeep51 0:ba33a62aea4e 1295 }
viewdeep51 0:ba33a62aea4e 1296 chk ^= (uint8_t) *n;
viewdeep51 0:ba33a62aea4e 1297 n++;
viewdeep51 0:ba33a62aea4e 1298 }
viewdeep51 0:ba33a62aea4e 1299 return chk;
viewdeep51 0:ba33a62aea4e 1300 }
viewdeep51 0:ba33a62aea4e 1301
viewdeep51 0:ba33a62aea4e 1302 void resp_HB(uint8_t seq) //presentees(submaster,slaves,listeners will respond back to the HB along with REQ and CMD ack status
viewdeep51 0:ba33a62aea4e 1303 {
viewdeep51 4:710464cb44fc 1304 switch(seq)
viewdeep51 0:ba33a62aea4e 1305 {
viewdeep51 0:ba33a62aea4e 1306 case 0:
viewdeep51 4:710464cb44fc 1307 sprintf(tx_buf,"$PPOTA,%u,%c*", addr_ID,valid);
viewdeep51 4:710464cb44fc 1308 checksum_send_byte = get_checksum(tx_buf);
viewdeep51 4:710464cb44fc 1309 sprintf(tx_buf,"$PPOTA,%u,%c*%u",addr_ID,valid,checksum_send_byte);
viewdeep51 0:ba33a62aea4e 1310 break;
viewdeep51 0:ba33a62aea4e 1311 case 1:
viewdeep51 4:710464cb44fc 1312 sprintf(tx_buf,"$PPOTR,%u,%u,%c*", addr_ID, REQ_,'A');
viewdeep51 4:710464cb44fc 1313 checksum_send_byte = get_checksum(tx_buf);
viewdeep51 4:710464cb44fc 1314 sprintf(tx_buf,"$PPOTR,%u,%u,%c*%u",addr_ID,REQ_,'A',checksum_send_byte);
viewdeep51 0:ba33a62aea4e 1315 break;
viewdeep51 0:ba33a62aea4e 1316 case 2:
viewdeep51 4:710464cb44fc 1317 sprintf(tx_buf,"$PPOTK,%u,%u,%c*", addr_ID, ACK_,'A');
viewdeep51 4:710464cb44fc 1318 checksum_send_byte = get_checksum(tx_buf);
viewdeep51 4:710464cb44fc 1319 sprintf(tx_buf,"$PPOTK,%u,%u,%c*%u",addr_ID, ACK_,'A',checksum_send_byte);
viewdeep51 0:ba33a62aea4e 1320 break;
viewdeep51 0:ba33a62aea4e 1321 default:
viewdeep51 4:710464cb44fc 1322 __nop(); //sprintf(tx_buf,"$PPOTA,%u,%c", addr_ID, valid);
viewdeep51 0:ba33a62aea4e 1323 break;
viewdeep51 4:710464cb44fc 1324 }
viewdeep51 3:ae45e29f5d4f 1325 Drive_En = 1;
viewdeep51 3:ae45e29f5d4f 1326 UART.printf("%s\r\n",tx_buf);
viewdeep51 3:ae45e29f5d4f 1327 wait_ms(2);
viewdeep51 3:ae45e29f5d4f 1328 Drive_En = 0;
viewdeep51 0:ba33a62aea4e 1329 }
viewdeep51 0:ba33a62aea4e 1330
viewdeep51 3:ae45e29f5d4f 1331 void send_ack_CMD(uint8_t cmd_t)
viewdeep51 0:ba33a62aea4e 1332 {
viewdeep51 5:f7b7ee0702cc 1333 sprintf(tx_buf,"$PPOTX,%u,%c*", addr_ID,'A');
viewdeep51 4:710464cb44fc 1334 checksum_send_byte = get_checksum(tx_buf);
viewdeep51 5:f7b7ee0702cc 1335 sprintf(tx_buf,"$PPOTX,%u,%c*%u",addr_ID,'A',checksum_send_byte);
viewdeep51 3:ae45e29f5d4f 1336 Drive_En = 1;
viewdeep51 3:ae45e29f5d4f 1337 UART.printf("%s\r\n",tx_buf);
viewdeep51 3:ae45e29f5d4f 1338 wait_ms(2);
viewdeep51 3:ae45e29f5d4f 1339 Drive_En = 0;
viewdeep51 0:ba33a62aea4e 1340 }
viewdeep51 0:ba33a62aea4e 1341
viewdeep51 3:ae45e29f5d4f 1342 void send_RPM(uint8_t _adr)
viewdeep51 3:ae45e29f5d4f 1343 {
viewdeep51 4:710464cb44fc 1344 sprintf(tx_buf,"$PPOTC,%u,%u,%u,%u,%u,%c*", _adr, rpm_data[0],rpm_data[1],rpm_data[2],rpm_data[3],'A');
viewdeep51 4:710464cb44fc 1345 checksum_send_byte = get_checksum(tx_buf);
viewdeep51 4:710464cb44fc 1346 sprintf(tx_buf,"$PPOTC,%u,%u,%u,%u,%u,%c*%u",_adr, rpm_data[0],rpm_data[1],rpm_data[2],rpm_data[3],'A',checksum_send_byte);
viewdeep51 3:ae45e29f5d4f 1347 Drive_En = 1;
viewdeep51 3:ae45e29f5d4f 1348 UART.printf("%s\r\n",tx_buf);
viewdeep51 3:ae45e29f5d4f 1349 wait_ms(2);
viewdeep51 3:ae45e29f5d4f 1350 Drive_En = 0;
viewdeep51 0:ba33a62aea4e 1351 }
viewdeep51 0:ba33a62aea4e 1352
viewdeep51 5:f7b7ee0702cc 1353 /*
viewdeep51 0:ba33a62aea4e 1354 void send_DIR(uint8_t _adr) //addr 1-8 of leds,error=1 or 0(no error)
viewdeep51 0:ba33a62aea4e 1355 {
viewdeep51 4:710464cb44fc 1356 sprintf(tx_buf,"$PPOTD,%u,%u,%c*",_adr,dir,'A'); // dir is 1 for ahead, 2 for astern
viewdeep51 4:710464cb44fc 1357 checksum_send_byte = get_checksum(tx_buf);
viewdeep51 4:710464cb44fc 1358 sprintf(tx_buf,"$PPOTD,%u,%u,%c*%u",_adr,dir,'A',checksum_send_byte);
viewdeep51 3:ae45e29f5d4f 1359 Drive_En = 1;
viewdeep51 3:ae45e29f5d4f 1360 UART.printf("%s\r\n",tx_buf);
viewdeep51 3:ae45e29f5d4f 1361 wait_ms(2);
viewdeep51 3:ae45e29f5d4f 1362 Drive_En = 0;
viewdeep51 0:ba33a62aea4e 1363 }
viewdeep51 5:f7b7ee0702cc 1364 */
viewdeep51 0:ba33a62aea4e 1365
viewdeep51 4:710464cb44fc 1366 void send_HB(uint8_t _adr) //master will send the heartbeat to all presentees at periodic intervals
viewdeep51 0:ba33a62aea4e 1367 {
viewdeep51 4:710464cb44fc 1368 sprintf(tx_buf,"$PPOTH,%u,%c*", _adr,'A');
viewdeep51 4:710464cb44fc 1369 checksum_send_byte = get_checksum(tx_buf);
viewdeep51 4:710464cb44fc 1370 sprintf(tx_buf,"$PPOTH,%u,%c*%u",_adr,'A',checksum_send_byte);
viewdeep51 3:ae45e29f5d4f 1371 Drive_En = 1;
viewdeep51 3:ae45e29f5d4f 1372 UART.printf("%s\r\n",tx_buf);
viewdeep51 3:ae45e29f5d4f 1373 wait_ms(2);
viewdeep51 3:ae45e29f5d4f 1374 Drive_En = 0;
viewdeep51 0:ba33a62aea4e 1375 }
viewdeep51 0:ba33a62aea4e 1376
viewdeep51 4:710464cb44fc 1377 void send_BROADCAST() //addr 1-8 of leds,error=1 or 0(no error)
viewdeep51 0:ba33a62aea4e 1378 {
viewdeep51 4:710464cb44fc 1379 sprintf(tx_buf,"$PPOTB,%u,%u,%c*",err_ALL,err_Status,'A');
viewdeep51 4:710464cb44fc 1380 checksum_send_byte = get_checksum(tx_buf);
viewdeep51 4:710464cb44fc 1381 sprintf(tx_buf,"$PPOTB,%u,%u,%c*%u",err_ALL,err_Status,'A',checksum_send_byte);
viewdeep51 3:ae45e29f5d4f 1382 Drive_En = 1;
viewdeep51 3:ae45e29f5d4f 1383 UART.printf("%s\r\n",tx_buf);
viewdeep51 3:ae45e29f5d4f 1384 wait_ms(2);
viewdeep51 3:ae45e29f5d4f 1385 Drive_En = 0;
viewdeep51 0:ba33a62aea4e 1386 BROADCAST_send=false;
viewdeep51 0:ba33a62aea4e 1387 }
viewdeep51 3:ae45e29f5d4f 1388 /** Function will send ac-M, ac-S, rpm-data(04digits), cmd_state)**/
viewdeep51 3:ae45e29f5d4f 1389 void send_ACH()
viewdeep51 3:ae45e29f5d4f 1390 {
viewdeep51 5:f7b7ee0702cc 1391 sprintf(tx_buf,"$PPOTS,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%c*", ach_M, ach_S, rpm_data[0],rpm_data[1],rpm_data[2],rpm_data[3],last_entry[0],last_entry[1],last_entry[2],last_entry[3],current_status,'A');
viewdeep51 4:710464cb44fc 1392 checksum_send_byte = get_checksum(tx_buf);
viewdeep51 5:f7b7ee0702cc 1393 sprintf(tx_buf,"$PPOTS,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%c,*%u", ach_M, ach_S, rpm_data[0],rpm_data[1],rpm_data[2],rpm_data[3],last_entry[0],last_entry[1],last_entry[2],last_entry[3],current_status,'A',checksum_send_byte);
viewdeep51 3:ae45e29f5d4f 1394 Drive_En = 1;
viewdeep51 3:ae45e29f5d4f 1395 UART.printf("%s\r\n",tx_buf);
viewdeep51 3:ae45e29f5d4f 1396 wait_ms(2);
viewdeep51 3:ae45e29f5d4f 1397 Drive_En = 0;
viewdeep51 3:ae45e29f5d4f 1398 }
viewdeep51 0:ba33a62aea4e 1399
viewdeep51 3:ae45e29f5d4f 1400 void ask_TIME()
viewdeep51 0:ba33a62aea4e 1401 {
viewdeep51 3:ae45e29f5d4f 1402 Hrs_1 = Hrs_2 = Mins_1 = Mins_2 = 0;
viewdeep51 4:710464cb44fc 1403 sprintf(tx_buf,"$PPOTQ,%u,%u,%u,%u,%c*", Hrs_1,Hrs_2, Mins_1, Mins_2,'A'); //querry to CPU for time
viewdeep51 4:710464cb44fc 1404 checksum_send_byte = get_checksum(tx_buf);
viewdeep51 4:710464cb44fc 1405 sprintf(tx_buf,"$PPOTQ,%u,%u,%u,%u,%c*%u", Hrs_1,Hrs_2, Mins_1, Mins_2,'A',checksum_send_byte);
viewdeep51 3:ae45e29f5d4f 1406 Drive_En = 1;
viewdeep51 3:ae45e29f5d4f 1407 UART.printf("%s\r\n",tx_buf);
viewdeep51 3:ae45e29f5d4f 1408 wait_ms(2);
viewdeep51 3:ae45e29f5d4f 1409 Drive_En = 0;
viewdeep51 0:ba33a62aea4e 1410 }
viewdeep51 0:ba33a62aea4e 1411
viewdeep51 0:ba33a62aea4e 1412 /**************************************************************************************
viewdeep51 0:ba33a62aea4e 1413 EOT Serial Data In FUNCTIONS
viewdeep51 0:ba33a62aea4e 1414 **************************************************************************************/
viewdeep51 3:ae45e29f5d4f 1415 void rcvd_HB() //all devices check for HB along with req response for all presentees
viewdeep51 0:ba33a62aea4e 1416 {
viewdeep51 4:710464cb44fc 1417 if(rcvd_ADDR == addr_ID) //addr_ID self address received
viewdeep51 3:ae45e29f5d4f 1418 {
viewdeep51 3:ae45e29f5d4f 1419 HB_rcvd = true;
viewdeep51 0:ba33a62aea4e 1420 if(addr_ID > pot_Slave_ER)
viewdeep51 3:ae45e29f5d4f 1421 { //all listners replied with
viewdeep51 0:ba33a62aea4e 1422 resp_HB(0); //normal HB response
viewdeep51 0:ba33a62aea4e 1423 }
viewdeep51 3:ae45e29f5d4f 1424 else //in case of active slave
viewdeep51 0:ba33a62aea4e 1425 {
viewdeep51 4:710464cb44fc 1426 if(ENTER_ACK_key) //RPM ACK key is detected at active slave only
viewdeep51 3:ae45e29f5d4f 1427 {
viewdeep51 4:710464cb44fc 1428 send_ack_CMD(1);
viewdeep51 4:710464cb44fc 1429 RPM_cmd_ack = true;
viewdeep51 3:ae45e29f5d4f 1430 ENTER_ACK_key = false;
viewdeep51 3:ae45e29f5d4f 1431 }
viewdeep51 5:f7b7ee0702cc 1432 /*
viewdeep51 3:ae45e29f5d4f 1433 else if(DIR_key) //dir ack key detected at active slave
viewdeep51 0:ba33a62aea4e 1434 {
viewdeep51 3:ae45e29f5d4f 1435 DIR_key = false;
viewdeep51 3:ae45e29f5d4f 1436 DIR_cmd_ack_send = true;
viewdeep51 3:ae45e29f5d4f 1437 send_ack_CMD(2);
viewdeep51 3:ae45e29f5d4f 1438 }
viewdeep51 5:f7b7ee0702cc 1439 */
viewdeep51 3:ae45e29f5d4f 1440 else if(REQ_key)
viewdeep51 3:ae45e29f5d4f 1441 {
viewdeep51 3:ae45e29f5d4f 1442 REQ_key = false;
viewdeep51 0:ba33a62aea4e 1443 resp_HB(1); //req and ack status data in req=1 and ack=2 if true
viewdeep51 0:ba33a62aea4e 1444 }
viewdeep51 3:ae45e29f5d4f 1445 else if(ACK_key)
viewdeep51 0:ba33a62aea4e 1446 {
viewdeep51 3:ae45e29f5d4f 1447 ACK_key = false;
viewdeep51 0:ba33a62aea4e 1448 resp_HB(2); //req and ack status data in req=1 and ack=2 if true
viewdeep51 0:ba33a62aea4e 1449 }
viewdeep51 0:ba33a62aea4e 1450 else
viewdeep51 0:ba33a62aea4e 1451 resp_HB(0); //req and ack status data in req=1 and ack=2 if true
viewdeep51 0:ba33a62aea4e 1452 }
viewdeep51 3:ae45e29f5d4f 1453 HB_fail.attach(&HB_fail_state_machine,1);
viewdeep51 0:ba33a62aea4e 1454 }
viewdeep51 0:ba33a62aea4e 1455 else
viewdeep51 0:ba33a62aea4e 1456 __nop();
viewdeep51 0:ba33a62aea4e 1457 }
viewdeep51 0:ba33a62aea4e 1458
viewdeep51 0:ba33a62aea4e 1459 void resp_rcvd_HB() //master and active/passive slaves check HB response along with req/ack commands
viewdeep51 4:710464cb44fc 1460 {
viewdeep51 4:710464cb44fc 1461 static uint8_t checksum_flt[8] = {0,0,0,0,0,0,0,0};
viewdeep51 4:710464cb44fc 1462 static bool checksum_err[8] = {false,false,false,false,false,false,false,false};
viewdeep51 3:ae45e29f5d4f 1463 if(addr_ID == pot_Master) //only master will be able to monitor status of all eots connected
viewdeep51 0:ba33a62aea4e 1464 {
viewdeep51 4:710464cb44fc 1465 if(send_ADDR == rcvd_ADDR) //valid broadcast not received
viewdeep51 4:710464cb44fc 1466 {
viewdeep51 4:710464cb44fc 1467 if(valid_sentence[rcvd_ADDR] == 'A')
viewdeep51 4:710464cb44fc 1468 {
viewdeep51 4:710464cb44fc 1469 HB_ack_rcvd = true;
viewdeep51 4:710464cb44fc 1470 checksum_err[rcvd_ADDR] = false;
viewdeep51 4:710464cb44fc 1471 checksum_flt[rcvd_ADDR] = 0;
viewdeep51 4:710464cb44fc 1472 }
viewdeep51 4:710464cb44fc 1473 else if((valid_sentence[rcvd_ADDR] == 'V')&&(++checksum_flt[rcvd_ADDR]<=3))
viewdeep51 4:710464cb44fc 1474 {
viewdeep51 4:710464cb44fc 1475 HB_ack_rcvd = true;
viewdeep51 4:710464cb44fc 1476 BROADCAST_send = true; //again broadcast
viewdeep51 4:710464cb44fc 1477 switch_CH = true; //send active channels again
viewdeep51 4:710464cb44fc 1478 }
viewdeep51 4:710464cb44fc 1479 else if((valid_sentence[rcvd_ADDR] == 'V')&&(checksum_flt[rcvd_ADDR]>3)&&(!checksum_err[rcvd_ADDR]))
viewdeep51 4:710464cb44fc 1480 {
viewdeep51 4:710464cb44fc 1481 HB_ack_rcvd = false;
viewdeep51 4:710464cb44fc 1482 checksum_err[rcvd_ADDR] = true;
viewdeep51 4:710464cb44fc 1483 }
viewdeep51 4:710464cb44fc 1484 else
viewdeep51 4:710464cb44fc 1485 __nop();
viewdeep51 4:710464cb44fc 1486
viewdeep51 4:710464cb44fc 1487 }
viewdeep51 0:ba33a62aea4e 1488 else
viewdeep51 4:710464cb44fc 1489 HB_ack_rcvd = false;
viewdeep51 0:ba33a62aea4e 1490 }
viewdeep51 0:ba33a62aea4e 1491 else
viewdeep51 0:ba33a62aea4e 1492 __nop();
viewdeep51 0:ba33a62aea4e 1493 }
viewdeep51 4:710464cb44fc 1494
viewdeep51 3:ae45e29f5d4f 1495 void rcvd_ACH()
viewdeep51 0:ba33a62aea4e 1496 {
viewdeep51 3:ae45e29f5d4f 1497 led_ach[ach_M-1] = ON;
viewdeep51 3:ae45e29f5d4f 1498 if(current_status == 2)
viewdeep51 3:ae45e29f5d4f 1499 {
viewdeep51 4:710464cb44fc 1500 led_cmd_ack = _FLASH;
viewdeep51 3:ae45e29f5d4f 1501 max7219.enable_device(2);
viewdeep51 3:ae45e29f5d4f 1502 display_flash = true;
viewdeep51 3:ae45e29f5d4f 1503 }
viewdeep51 3:ae45e29f5d4f 1504 else if(current_status == 1)
viewdeep51 3:ae45e29f5d4f 1505 {
viewdeep51 4:710464cb44fc 1506 led_cmd_ack = ON;
viewdeep51 3:ae45e29f5d4f 1507 max7219.enable_device(2);
viewdeep51 3:ae45e29f5d4f 1508 display_flash = false;
viewdeep51 3:ae45e29f5d4f 1509 }
viewdeep51 3:ae45e29f5d4f 1510 else
viewdeep51 3:ae45e29f5d4f 1511 {
viewdeep51 4:710464cb44fc 1512 led_cmd_ack = OFF;
viewdeep51 3:ae45e29f5d4f 1513 max7219.enable_device(2);
viewdeep51 3:ae45e29f5d4f 1514 }
viewdeep51 5:f7b7ee0702cc 1515 /*
viewdeep51 3:ae45e29f5d4f 1516 if(current_status_dir == 2)
viewdeep51 3:ae45e29f5d4f 1517 {
viewdeep51 3:ae45e29f5d4f 1518 led_dir[dir-1] = _FLASH;
viewdeep51 3:ae45e29f5d4f 1519 }
viewdeep51 3:ae45e29f5d4f 1520 else if(current_status_dir == 1)
viewdeep51 3:ae45e29f5d4f 1521 {
viewdeep51 3:ae45e29f5d4f 1522 led_dir[dir-1] = ON;
viewdeep51 3:ae45e29f5d4f 1523 }
viewdeep51 3:ae45e29f5d4f 1524 else
viewdeep51 3:ae45e29f5d4f 1525 {
viewdeep51 3:ae45e29f5d4f 1526 led_dir[dir-1] = OFF;
viewdeep51 3:ae45e29f5d4f 1527 }
viewdeep51 5:f7b7ee0702cc 1528 */
viewdeep51 4:710464cb44fc 1529 for(uint8_t i = 1; i <= 4 ; i++) //current cmd information
viewdeep51 4:710464cb44fc 1530 {
viewdeep51 4:710464cb44fc 1531 max7219.write_digit(2,i,rpm_data[i-1]); //device 2 digits 1-4 //data 0 means left most digit will be blank bcoz no decode mode is used
viewdeep51 4:710464cb44fc 1532 }
viewdeep51 4:710464cb44fc 1533
viewdeep51 4:710464cb44fc 1534 for(uint8_t i = 5; i <=8; i++) //last command executed
viewdeep51 4:710464cb44fc 1535 {
viewdeep51 4:710464cb44fc 1536 max7219.write_digit(1,i,last_entry[i-5]); //last cmd 0000
viewdeep51 4:710464cb44fc 1537 }
viewdeep51 3:ae45e29f5d4f 1538
viewdeep51 3:ae45e29f5d4f 1539 if(ach_S != 9)
viewdeep51 0:ba33a62aea4e 1540 {
viewdeep51 3:ae45e29f5d4f 1541 if(ach_S == pot_Slave_MCR)
viewdeep51 3:ae45e29f5d4f 1542 {
viewdeep51 3:ae45e29f5d4f 1543 led_ach[pot_Slave_MCR -1] = ON;
viewdeep51 3:ae45e29f5d4f 1544 if(led_ach[pot_Slave_ER -1] == ON)
viewdeep51 3:ae45e29f5d4f 1545 led_ach[pot_Slave_ER -1] = OFF; //ER off
viewdeep51 3:ae45e29f5d4f 1546 else
viewdeep51 3:ae45e29f5d4f 1547 __nop();
viewdeep51 3:ae45e29f5d4f 1548 }
viewdeep51 3:ae45e29f5d4f 1549 else if(ach_S == pot_Slave_ER)
viewdeep51 3:ae45e29f5d4f 1550 {
viewdeep51 3:ae45e29f5d4f 1551 led_ach[pot_Slave_ER -1] = ON; //ER ON
viewdeep51 3:ae45e29f5d4f 1552 if(led_ach[pot_Slave_MCR -1] == ON)
viewdeep51 3:ae45e29f5d4f 1553 led_ach[pot_Slave_MCR -1] = OFF; //ER off
viewdeep51 3:ae45e29f5d4f 1554 else
viewdeep51 3:ae45e29f5d4f 1555 __nop();
viewdeep51 3:ae45e29f5d4f 1556 }
viewdeep51 3:ae45e29f5d4f 1557 else
viewdeep51 3:ae45e29f5d4f 1558 __nop();
viewdeep51 3:ae45e29f5d4f 1559
viewdeep51 3:ae45e29f5d4f 1560 if((RPM_cmd || DIR_cmd)&& (addr_ID == ach_S)) // cmd ack not received before its failure control shifted to another slave
viewdeep51 3:ae45e29f5d4f 1561 { //all key functions enabled for PORTB in active slave only
viewdeep51 3:ae45e29f5d4f 1562 int_B.rise(&read_KEY_B);
viewdeep51 3:ae45e29f5d4f 1563 wait_us(10);
viewdeep51 3:ae45e29f5d4f 1564 mcp23s17_1.gpintenb(0xff); //ff
viewdeep51 3:ae45e29f5d4f 1565 wait_us(10);
viewdeep51 3:ae45e29f5d4f 1566 }
viewdeep51 3:ae45e29f5d4f 1567 else
viewdeep51 3:ae45e29f5d4f 1568 __nop();
viewdeep51 3:ae45e29f5d4f 1569 }
viewdeep51 3:ae45e29f5d4f 1570 else
viewdeep51 3:ae45e29f5d4f 1571 {
viewdeep51 3:ae45e29f5d4f 1572 led_ach[pot_Slave_MCR-1] = _FLASH;
viewdeep51 3:ae45e29f5d4f 1573 led_ach[pot_Slave_ER-1] = _FLASH;
viewdeep51 3:ae45e29f5d4f 1574 }
viewdeep51 3:ae45e29f5d4f 1575 }
viewdeep51 3:ae45e29f5d4f 1576
viewdeep51 3:ae45e29f5d4f 1577 void rcvd_rpm_CMD() //all devices check for received command
viewdeep51 3:ae45e29f5d4f 1578 {
viewdeep51 3:ae45e29f5d4f 1579 led_cmd_ack = _FLASH; //ack cmd led start flashing
viewdeep51 3:ae45e29f5d4f 1580 for(uint8_t i = 0; i <= 3 ; i++)
viewdeep51 3:ae45e29f5d4f 1581 {
viewdeep51 3:ae45e29f5d4f 1582 max7219.write_digit(2,i+1,rpm_data[i]); //device 2 digits 1-4 //data 0 means left most digit will be blank bcoz no decode mode is used
viewdeep51 3:ae45e29f5d4f 1583 }
viewdeep51 3:ae45e29f5d4f 1584 wait_us(10);
viewdeep51 3:ae45e29f5d4f 1585 buzzer = ON;
viewdeep51 3:ae45e29f5d4f 1586 display_flash = true; //display start flashing //buzzer on
viewdeep51 3:ae45e29f5d4f 1587
viewdeep51 0:ba33a62aea4e 1588 if(ach_S == addr_ID)
viewdeep51 4:710464cb44fc 1589 {
viewdeep51 4:710464cb44fc 1590 int_B.rise(&read_KEY_B);
viewdeep51 4:710464cb44fc 1591 wait_us(10);
viewdeep51 3:ae45e29f5d4f 1592 mcp23s17_1.gpintenb(0xff); //ff
viewdeep51 3:ae45e29f5d4f 1593 wait_us(10); //portc is always enbaled in all cases and PORTA is enabled only for active master
viewdeep51 0:ba33a62aea4e 1594 }
viewdeep51 0:ba33a62aea4e 1595 else
viewdeep51 0:ba33a62aea4e 1596 {
viewdeep51 0:ba33a62aea4e 1597 __nop();
viewdeep51 0:ba33a62aea4e 1598 }
viewdeep51 3:ae45e29f5d4f 1599 CMD_Ticker.attach(&CMD_State_Machine,0.25); //state machine timer started in all connected devices except master
viewdeep51 0:ba33a62aea4e 1600 }
viewdeep51 5:f7b7ee0702cc 1601 /*
viewdeep51 0:ba33a62aea4e 1602 void rcvd_dir_CMD() //all devices check for received command
viewdeep51 0:ba33a62aea4e 1603 {
viewdeep51 3:ae45e29f5d4f 1604 DIR_cmd = true;
viewdeep51 3:ae45e29f5d4f 1605 led_dir[last_dir-1] = OFF;
viewdeep51 3:ae45e29f5d4f 1606 led_dir[dir-1] = _FLASH; //led flash started
viewdeep51 3:ae45e29f5d4f 1607 buzzer = ON; //buzzer on
viewdeep51 3:ae45e29f5d4f 1608 last_dir = dir; //HB-cmd ack pending with "A"
viewdeep51 0:ba33a62aea4e 1609 if(ach_S == addr_ID)
viewdeep51 0:ba33a62aea4e 1610 {
viewdeep51 3:ae45e29f5d4f 1611 int_B.rise(&read_KEY_B); //enable portb interupts
viewdeep51 3:ae45e29f5d4f 1612 mcp23s17_1.gpintenb(0xff); //ff
viewdeep51 3:ae45e29f5d4f 1613 wait_us(10);
viewdeep51 0:ba33a62aea4e 1614 }
viewdeep51 3:ae45e29f5d4f 1615 DIR_Ticker.attach(&DIR_state_machine,0.25);
viewdeep51 0:ba33a62aea4e 1616 }
viewdeep51 5:f7b7ee0702cc 1617 */
viewdeep51 0:ba33a62aea4e 1618
viewdeep51 0:ba33a62aea4e 1619 void rcvd_BROADCAST()
viewdeep51 0:ba33a62aea4e 1620 {
viewdeep51 3:ae45e29f5d4f 1621 for(uint8_t i = 0 ; i <= 7 ; i++)
viewdeep51 0:ba33a62aea4e 1622 {
viewdeep51 3:ae45e29f5d4f 1623 if(((uint8_t)1<<i) & (uint8_t)err_ALL)
viewdeep51 3:ae45e29f5d4f 1624 {
viewdeep51 3:ae45e29f5d4f 1625 led_ach[i] = _FLASH;
viewdeep51 3:ae45e29f5d4f 1626 }
viewdeep51 3:ae45e29f5d4f 1627 else if(led_ach[i] != ON)
viewdeep51 3:ae45e29f5d4f 1628 {
viewdeep51 3:ae45e29f5d4f 1629 led_ach[i]= OFF;
viewdeep51 3:ae45e29f5d4f 1630 }
viewdeep51 3:ae45e29f5d4f 1631 else
viewdeep51 3:ae45e29f5d4f 1632 __nop();
viewdeep51 4:710464cb44fc 1633 }
viewdeep51 0:ba33a62aea4e 1634 }
viewdeep51 4:710464cb44fc 1635
viewdeep51 3:ae45e29f5d4f 1636 void rcvd_TIME()
viewdeep51 3:ae45e29f5d4f 1637 {
viewdeep51 3:ae45e29f5d4f 1638 uint8_t Hrs,Mins;
viewdeep51 3:ae45e29f5d4f 1639 struct tm t;
viewdeep51 3:ae45e29f5d4f 1640 Hrs = 10*Hrs_1+Hrs_2;
viewdeep51 3:ae45e29f5d4f 1641 Mins = 10*Mins_1+Mins_2;
viewdeep51 3:ae45e29f5d4f 1642 t.tm_year = 2018-1900;
viewdeep51 3:ae45e29f5d4f 1643 t.tm_mon = 5;
viewdeep51 3:ae45e29f5d4f 1644 t.tm_mday = 16;
viewdeep51 3:ae45e29f5d4f 1645 t.tm_hour = Hrs;
viewdeep51 3:ae45e29f5d4f 1646 t.tm_min = Mins;
viewdeep51 3:ae45e29f5d4f 1647 t.tm_sec = 51;
viewdeep51 3:ae45e29f5d4f 1648 set_time((time_t)mktime(&t));
viewdeep51 3:ae45e29f5d4f 1649 wait_ms(1);
viewdeep51 3:ae45e29f5d4f 1650 read_RTC();
viewdeep51 3:ae45e29f5d4f 1651 wait_ms(1);
viewdeep51 3:ae45e29f5d4f 1652 time_RCVD = true;
viewdeep51 3:ae45e29f5d4f 1653 }
viewdeep51 0:ba33a62aea4e 1654
viewdeep51 4:710464cb44fc 1655 void at_timeout()
viewdeep51 4:710464cb44fc 1656 {
viewdeep51 4:710464cb44fc 1657 timeout.detach();
viewdeep51 4:710464cb44fc 1658 data_fail = true;
viewdeep51 4:710464cb44fc 1659 }
viewdeep51 4:710464cb44fc 1660
viewdeep51 0:ba33a62aea4e 1661 void read_SERIAL()
viewdeep51 4:710464cb44fc 1662 {
viewdeep51 3:ae45e29f5d4f 1663 char rcvd_char;
viewdeep51 4:710464cb44fc 1664 char rcvd_valid;
viewdeep51 5:f7b7ee0702cc 1665 //int cmd_t;
viewdeep51 4:710464cb44fc 1666 uint8_t checksum_calculated;
viewdeep51 4:710464cb44fc 1667 timeout.attach(&at_timeout,0.100); //100ms timeout
viewdeep51 4:710464cb44fc 1668 data_fail = false;
viewdeep51 4:710464cb44fc 1669 while(UART.readable() && (num_bytes < RX_BUFFER_SIZE)&&(!data_fail))
viewdeep51 0:ba33a62aea4e 1670 {
viewdeep51 3:ae45e29f5d4f 1671 rcvd_char = UART.getc();
viewdeep51 3:ae45e29f5d4f 1672 if(rcvd_char == '$') //first character
viewdeep51 3:ae45e29f5d4f 1673 num_bytes = 0;
viewdeep51 0:ba33a62aea4e 1674 else
viewdeep51 0:ba33a62aea4e 1675 __nop();
viewdeep51 3:ae45e29f5d4f 1676
viewdeep51 3:ae45e29f5d4f 1677 rx_buf[num_bytes] = rcvd_char;
viewdeep51 3:ae45e29f5d4f 1678 num_bytes++;
viewdeep51 4:710464cb44fc 1679 if(rcvd_char == '\n') //last character received
viewdeep51 0:ba33a62aea4e 1680 {
viewdeep51 4:710464cb44fc 1681 timeout.detach();
viewdeep51 3:ae45e29f5d4f 1682 strtok(rx_buf,",");
viewdeep51 3:ae45e29f5d4f 1683 if(strcmp(rx_buf,"$PPOTH") == 0) //* present submaster,Slaves or lsitners receive HB *//
viewdeep51 3:ae45e29f5d4f 1684 {
viewdeep51 4:710464cb44fc 1685 sscanf(strtok (NULL,","),"%u", &rcvd_ADDR); //address of connected device
viewdeep51 4:710464cb44fc 1686 sscanf(strtok (NULL,"*"),"%c", &rcvd_valid); //sentence validity char
viewdeep51 4:710464cb44fc 1687 sscanf(strtok (NULL,","),"%u", &checksum_rcvd_byte);
viewdeep51 4:710464cb44fc 1688 sprintf(rx_buf,"$PPOTH,%u,%c*",rcvd_ADDR,rcvd_valid);
viewdeep51 4:710464cb44fc 1689 checksum_calculated = get_checksum(rx_buf);
viewdeep51 4:710464cb44fc 1690 if(checksum_calculated == checksum_rcvd_byte)
viewdeep51 4:710464cb44fc 1691 {
viewdeep51 4:710464cb44fc 1692 rcvd_HB(); //valid sentence received
viewdeep51 4:710464cb44fc 1693 }
viewdeep51 4:710464cb44fc 1694 else
viewdeep51 4:710464cb44fc 1695 __nop(); //valid sentence receivedrcvd_HB(); //set flag when Heartbeat received
viewdeep51 3:ae45e29f5d4f 1696 }
viewdeep51 3:ae45e29f5d4f 1697 else if(strcmp(rx_buf,"$PPOTA") == 0) //* Master,submaster and Slaves receive heartbeat resp *//
viewdeep51 3:ae45e29f5d4f 1698 {
viewdeep51 4:710464cb44fc 1699 sscanf(strtok (NULL,","),"%u", &rcvd_ADDR);
viewdeep51 4:710464cb44fc 1700 sscanf(strtok (NULL,"*"),"%c", &rcvd_valid);
viewdeep51 4:710464cb44fc 1701 sscanf(strtok (NULL,","),"%u", &checksum_rcvd_byte);
viewdeep51 4:710464cb44fc 1702 sprintf(rx_buf,"$PPOTA,%u,%c*",rcvd_ADDR,rcvd_valid);
viewdeep51 4:710464cb44fc 1703 checksum_calculated = get_checksum(rx_buf);
viewdeep51 4:710464cb44fc 1704 if(checksum_calculated == checksum_rcvd_byte)
viewdeep51 4:710464cb44fc 1705 {
viewdeep51 4:710464cb44fc 1706 valid_sentence[rcvd_ADDR] = rcvd_valid;
viewdeep51 4:710464cb44fc 1707 resp_rcvd_HB(); //valid sentence received
viewdeep51 4:710464cb44fc 1708 }
viewdeep51 4:710464cb44fc 1709 else
viewdeep51 4:710464cb44fc 1710 __nop();
viewdeep51 3:ae45e29f5d4f 1711 }
viewdeep51 3:ae45e29f5d4f 1712 else if(strcmp(rx_buf,"$PPOTB") == 0) //* submaster,Slaves or listener receive broadcast *//
viewdeep51 3:ae45e29f5d4f 1713 {
viewdeep51 3:ae45e29f5d4f 1714 sscanf(strtok (NULL,","), "%u", &err_ALL); //device in error with addr_ID = err_ID
viewdeep51 4:710464cb44fc 1715 sscanf(strtok (NULL,","), "%u",&err_Status);
viewdeep51 4:710464cb44fc 1716 sscanf(strtok (NULL,"*"),"%c", &rcvd_valid); //sentence validity char
viewdeep51 4:710464cb44fc 1717 sscanf(strtok (NULL,","),"%u", &checksum_rcvd_byte);
viewdeep51 4:710464cb44fc 1718 sprintf(rx_buf,"$PPOTB,%u,%u,%c*",err_ALL,err_Status,rcvd_valid);
viewdeep51 4:710464cb44fc 1719 checksum_calculated = get_checksum(rx_buf);
viewdeep51 4:710464cb44fc 1720 if(checksum_calculated == checksum_rcvd_byte)
viewdeep51 4:710464cb44fc 1721 { //valid sentence received
viewdeep51 4:710464cb44fc 1722 valid = 'A';
viewdeep51 4:710464cb44fc 1723 rcvd_BROADCAST();
viewdeep51 4:710464cb44fc 1724 }
viewdeep51 4:710464cb44fc 1725 else
viewdeep51 4:710464cb44fc 1726 valid = 'V'; //not valid
viewdeep51 3:ae45e29f5d4f 1727 }
viewdeep51 3:ae45e29f5d4f 1728 else if(strcmp(rx_buf,"$PPOTC") == 0) //* submaster,Slaves or listener receive CMD *//
viewdeep51 0:ba33a62aea4e 1729 {
viewdeep51 3:ae45e29f5d4f 1730 sscanf(strtok (NULL,","), "%u", &rcvd_ADDR); //cmd extracted
viewdeep51 3:ae45e29f5d4f 1731 sscanf(strtok (NULL,","), "%u", &rpm_data[0]); //rpm data digit 1 and soon
viewdeep51 3:ae45e29f5d4f 1732 sscanf(strtok (NULL,","), "%u", &rpm_data[1]);
viewdeep51 3:ae45e29f5d4f 1733 sscanf(strtok (NULL,","), "%u", &rpm_data[2]);
viewdeep51 4:710464cb44fc 1734 sscanf(strtok (NULL,","), "%u",&rpm_data[3]);
viewdeep51 4:710464cb44fc 1735 sscanf(strtok (NULL,"*"),"%c", &rcvd_valid); //sentence validity char
viewdeep51 4:710464cb44fc 1736 sscanf(strtok (NULL,","),"%u", &checksum_rcvd_byte);
viewdeep51 4:710464cb44fc 1737 sprintf(rx_buf,"$PPOTC,%u,%u,%u,%u,%u,%c*",rcvd_ADDR,rpm_data[0],rpm_data[1],rpm_data[2],rpm_data[3],rcvd_valid);
viewdeep51 4:710464cb44fc 1738 checksum_calculated = get_checksum(rx_buf);
viewdeep51 4:710464cb44fc 1739 if(checksum_calculated == checksum_rcvd_byte)
viewdeep51 4:710464cb44fc 1740 {
viewdeep51 4:710464cb44fc 1741 valid = 'A';
viewdeep51 4:710464cb44fc 1742 RPM_cmd = true; // in all devices who receive RPM command
viewdeep51 4:710464cb44fc 1743 rcvd_rpm_CMD();
viewdeep51 4:710464cb44fc 1744 }
viewdeep51 4:710464cb44fc 1745 else
viewdeep51 4:710464cb44fc 1746 valid = 'V';
viewdeep51 3:ae45e29f5d4f 1747 }
viewdeep51 5:f7b7ee0702cc 1748 /*
viewdeep51 5:f7b7ee0702cc 1749 else if(strcmp(rx_buf,"$PPOTD") == 0) //present submaster,Slaves or lsitners receive HB
viewdeep51 3:ae45e29f5d4f 1750 {
viewdeep51 3:ae45e29f5d4f 1751 sscanf(strtok (NULL,","), "%u", &rcvd_ADDR); //address of connected device
viewdeep51 4:710464cb44fc 1752 sscanf(strtok (NULL,","), "%u",&dir); // 1 for ahead and 2 for astern
viewdeep51 4:710464cb44fc 1753 sscanf(strtok (NULL,"*"),"%c", &rcvd_valid); //sentence validity char
viewdeep51 4:710464cb44fc 1754 sscanf(strtok (NULL,","),"%u", &checksum_rcvd_byte);
viewdeep51 4:710464cb44fc 1755 sprintf(rx_buf,"$PPOTD,%u,%u,%c*",rcvd_ADDR,dir,rcvd_valid);
viewdeep51 4:710464cb44fc 1756 checksum_calculated = get_checksum(rx_buf);
viewdeep51 4:710464cb44fc 1757 if(checksum_calculated == checksum_rcvd_byte)
viewdeep51 4:710464cb44fc 1758 {
viewdeep51 4:710464cb44fc 1759 valid = 'A';
viewdeep51 4:710464cb44fc 1760 DIR_cmd = true;
viewdeep51 4:710464cb44fc 1761 rcvd_dir_CMD();
viewdeep51 4:710464cb44fc 1762 }
viewdeep51 4:710464cb44fc 1763 else
viewdeep51 4:710464cb44fc 1764 valid = 'V'; //set flag when Heartbeat received
viewdeep51 0:ba33a62aea4e 1765 }
viewdeep51 5:f7b7ee0702cc 1766 */
viewdeep51 3:ae45e29f5d4f 1767 else if(strcmp(rx_buf,"$PPOTX") == 0) //* Master,submaster,Slave or listners receive CMD acknowledge *//
viewdeep51 0:ba33a62aea4e 1768 {
viewdeep51 3:ae45e29f5d4f 1769 sscanf(strtok (NULL,","), "%u", &rcvd_ADDR); //address extracted
viewdeep51 5:f7b7ee0702cc 1770 //sscanf(strtok (NULL,","), "%u",&cmd_t);
viewdeep51 4:710464cb44fc 1771 sscanf(strtok (NULL,"*"),"%c", &rcvd_valid); //sentence validity char
viewdeep51 4:710464cb44fc 1772 sscanf(strtok (NULL,","),"%u", &checksum_rcvd_byte);
viewdeep51 5:f7b7ee0702cc 1773 sprintf(rx_buf,"$PPOTX,%u,%c*",rcvd_ADDR,rcvd_valid);
viewdeep51 4:710464cb44fc 1774 checksum_calculated = get_checksum(rx_buf);
viewdeep51 4:710464cb44fc 1775 if(checksum_calculated == checksum_rcvd_byte)
viewdeep51 4:710464cb44fc 1776 {
viewdeep51 4:710464cb44fc 1777 valid = 'A';
viewdeep51 5:f7b7ee0702cc 1778 RPM_cmd_ack = true;
viewdeep51 4:710464cb44fc 1779 }
viewdeep51 4:710464cb44fc 1780 else
viewdeep51 4:710464cb44fc 1781 valid = 'V'; // not valid sentence received;
viewdeep51 0:ba33a62aea4e 1782 }
viewdeep51 3:ae45e29f5d4f 1783 else if(strcmp(rx_buf,"$PPOTS") == 0) //*active channel status recieved *//
viewdeep51 3:ae45e29f5d4f 1784 {
viewdeep51 3:ae45e29f5d4f 1785 sscanf(strtok (NULL,","), "%u", &ach_M); //cmd extracted
viewdeep51 3:ae45e29f5d4f 1786 sscanf(strtok (NULL,","), "%u", &ach_S);
viewdeep51 3:ae45e29f5d4f 1787 sscanf(strtok (NULL,","), "%u", &rpm_data[0]);
viewdeep51 3:ae45e29f5d4f 1788 sscanf(strtok (NULL,","), "%u", &rpm_data[1]);
viewdeep51 3:ae45e29f5d4f 1789 sscanf(strtok (NULL,","), "%u", &rpm_data[2]);
viewdeep51 3:ae45e29f5d4f 1790 sscanf(strtok (NULL,","), "%u", &rpm_data[3]);
viewdeep51 4:710464cb44fc 1791 sscanf(strtok (NULL,","), "%u", &last_entry[0]);
viewdeep51 4:710464cb44fc 1792 sscanf(strtok (NULL,","), "%u", &last_entry[1]);
viewdeep51 4:710464cb44fc 1793 sscanf(strtok (NULL,","), "%u", &last_entry[2]);
viewdeep51 4:710464cb44fc 1794 sscanf(strtok (NULL,","), "%u", &last_entry[3]);
viewdeep51 5:f7b7ee0702cc 1795 //sscanf(strtok (NULL,","), "%u", &dir);
viewdeep51 3:ae45e29f5d4f 1796 sscanf(strtok (NULL,","), "%u", &current_status);
viewdeep51 5:f7b7ee0702cc 1797 //sscanf(strtok (NULL,","),"%u", &current_status_dir);
viewdeep51 4:710464cb44fc 1798 sscanf(strtok (NULL,"*"),"%c", &rcvd_valid); //sentence validity char
viewdeep51 4:710464cb44fc 1799 sscanf(strtok (NULL,","),"%u", &checksum_rcvd_byte);
viewdeep51 5:f7b7ee0702cc 1800 sprintf(rx_buf,"$PPOTS,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%c*",ach_M,ach_S,rpm_data[0],rpm_data[1],rpm_data[2],rpm_data[3],last_entry[0],last_entry[1],last_entry[2],last_entry[3],current_status,rcvd_valid);
viewdeep51 4:710464cb44fc 1801 checksum_calculated = get_checksum(rx_buf);
viewdeep51 4:710464cb44fc 1802 if(checksum_calculated == checksum_rcvd_byte)
viewdeep51 4:710464cb44fc 1803 {
viewdeep51 4:710464cb44fc 1804 valid = 'A'; //valid sentence received
viewdeep51 4:710464cb44fc 1805 rcvd_ACH();
viewdeep51 4:710464cb44fc 1806 }
viewdeep51 4:710464cb44fc 1807 else
viewdeep51 4:710464cb44fc 1808 valid = 'V';
viewdeep51 3:ae45e29f5d4f 1809 }
viewdeep51 3:ae45e29f5d4f 1810 else if(strcmp(rx_buf,"$PPOTR") == 0) //* control transfer request arrived*//
viewdeep51 3:ae45e29f5d4f 1811 {
viewdeep51 4:710464cb44fc 1812 sscanf(strtok (NULL,","),"%u", &rcvd_ADDR);
viewdeep51 4:710464cb44fc 1813 sscanf(strtok (NULL,","),"%u", &req); //e.g $R req or $K ack
viewdeep51 4:710464cb44fc 1814 sscanf(strtok (NULL,"*"),"%c", &rcvd_valid); //sentence validity char
viewdeep51 4:710464cb44fc 1815 sscanf(strtok (NULL,","),"%u", &checksum_rcvd_byte);
viewdeep51 3:ae45e29f5d4f 1816 if(addr_ID <= pot_Slave_ER)
viewdeep51 3:ae45e29f5d4f 1817 {
viewdeep51 4:710464cb44fc 1818 sprintf(rx_buf,"$PPOTR,%u,%u,%c*",rcvd_ADDR,req,rcvd_valid);
viewdeep51 4:710464cb44fc 1819 checksum_calculated = get_checksum(rx_buf);
viewdeep51 4:710464cb44fc 1820 if(checksum_calculated == checksum_rcvd_byte)
viewdeep51 4:710464cb44fc 1821 {
viewdeep51 4:710464cb44fc 1822 valid = 'A';
viewdeep51 4:710464cb44fc 1823 REQ_rcvd = true;
viewdeep51 4:710464cb44fc 1824 req_ADDR = rcvd_ADDR;
viewdeep51 4:710464cb44fc 1825 if(((rcvd_ADDR == pot_Master) && (addr_ID == pot_Submaster)) || ((rcvd_ADDR == pot_Submaster) && (addr_ID == pot_Master)))
viewdeep51 4:710464cb44fc 1826 {
viewdeep51 4:710464cb44fc 1827 int_B.rise(&read_KEY_B); //to enable ACK key interrupt
viewdeep51 4:710464cb44fc 1828 wait_us(10);
viewdeep51 4:710464cb44fc 1829 mcp23s17_1.gpintenb(0xff); // portb of mcp1 ACK key enabled
viewdeep51 4:710464cb44fc 1830 wait_us(10);
viewdeep51 4:710464cb44fc 1831 led_ctrl[ACK_-1] = _FLASH; //ack led blink
viewdeep51 4:710464cb44fc 1832 }
viewdeep51 4:710464cb44fc 1833 else
viewdeep51 4:710464cb44fc 1834 __nop();
viewdeep51 4:710464cb44fc 1835
viewdeep51 4:710464cb44fc 1836 if(((rcvd_ADDR == pot_Slave_MCR) && (addr_ID == pot_Slave_ER)) || ((rcvd_ADDR == pot_Slave_ER) && (addr_ID == pot_Slave_MCR)))
viewdeep51 4:710464cb44fc 1837 {
viewdeep51 4:710464cb44fc 1838 int_B.rise(&read_KEY_B); //to enable ACK key interrupt
viewdeep51 4:710464cb44fc 1839 wait_us(10);
viewdeep51 4:710464cb44fc 1840 mcp23s17_1.gpintenb(0xff); //portb of mcp1 ACK key enabled
viewdeep51 4:710464cb44fc 1841 wait_us(10);
viewdeep51 4:710464cb44fc 1842 led_ctrl[ACK_-1] = _FLASH; //ack led blink
viewdeep51 4:710464cb44fc 1843 }
viewdeep51 4:710464cb44fc 1844 else
viewdeep51 4:710464cb44fc 1845 __nop();
viewdeep51 4:710464cb44fc 1846
viewdeep51 4:710464cb44fc 1847 R_A_Ticker.attach(&R_A_Statemachine,0.5);
viewdeep51 4:710464cb44fc 1848 }
viewdeep51 3:ae45e29f5d4f 1849 else
viewdeep51 4:710464cb44fc 1850 valid = 'V';
viewdeep51 4:710464cb44fc 1851 }
viewdeep51 4:710464cb44fc 1852 else
viewdeep51 3:ae45e29f5d4f 1853 __nop();
viewdeep51 3:ae45e29f5d4f 1854 }
viewdeep51 3:ae45e29f5d4f 1855 else if(strcmp(rx_buf,"$PPOTK") == 0) //* Master,submaster,Slave or listners REQ acknowledge *//
viewdeep51 3:ae45e29f5d4f 1856 {
viewdeep51 3:ae45e29f5d4f 1857 sscanf(strtok (NULL,","), "%u", &rcvd_ADDR);
viewdeep51 4:710464cb44fc 1858 sscanf(strtok (NULL,","),"%u", &ack); //e.g $R req or $K ack
viewdeep51 4:710464cb44fc 1859 sscanf(strtok (NULL,"*"),"%c", &rcvd_valid);
viewdeep51 4:710464cb44fc 1860 sscanf(strtok (NULL,","),"%u", &checksum_rcvd_byte);
viewdeep51 4:710464cb44fc 1861
viewdeep51 3:ae45e29f5d4f 1862 if(addr_ID <= pot_Slave_ER)
viewdeep51 3:ae45e29f5d4f 1863 {
viewdeep51 4:710464cb44fc 1864 sprintf(rx_buf,"$PPOTK,%u,%u,%c*",rcvd_ADDR,ack,rcvd_valid);
viewdeep51 4:710464cb44fc 1865 checksum_calculated = get_checksum(rx_buf);
viewdeep51 4:710464cb44fc 1866 if(checksum_calculated == checksum_rcvd_byte)
viewdeep51 4:710464cb44fc 1867 {
viewdeep51 4:710464cb44fc 1868 valid = 'A';
viewdeep51 4:710464cb44fc 1869 ack_ADDR = rcvd_ADDR;
viewdeep51 4:710464cb44fc 1870 ACK_rcvd = true;
viewdeep51 4:710464cb44fc 1871 //ACH_SW_over = true;
viewdeep51 4:710464cb44fc 1872
viewdeep51 4:710464cb44fc 1873 if(((rcvd_ADDR == pot_Master) && (addr_ID == pot_Submaster)) || ((rcvd_ADDR == pot_Submaster) && (addr_ID == pot_Master)))
viewdeep51 4:710464cb44fc 1874 {
viewdeep51 4:710464cb44fc 1875 led_ctrl[ACK_-1] = OFF;
viewdeep51 4:710464cb44fc 1876 //R_A_Ticker.detach();
viewdeep51 4:710464cb44fc 1877 }
viewdeep51 4:710464cb44fc 1878 else if(((rcvd_ADDR == pot_Slave_ER) && (addr_ID == pot_Slave_MCR)) || ((rcvd_ADDR == pot_Slave_MCR) && (addr_ID == pot_Slave_ER)))
viewdeep51 4:710464cb44fc 1879 {
viewdeep51 4:710464cb44fc 1880 led_ctrl[ACK_-1] = OFF;
viewdeep51 4:710464cb44fc 1881 //R_A_Ticker.detach();
viewdeep51 4:710464cb44fc 1882 }
viewdeep51 4:710464cb44fc 1883 else
viewdeep51 4:710464cb44fc 1884 __nop();
viewdeep51 4:710464cb44fc 1885 }
viewdeep51 4:710464cb44fc 1886 else
viewdeep51 4:710464cb44fc 1887 valid = 'V';
viewdeep51 4:710464cb44fc 1888 }
viewdeep51 3:ae45e29f5d4f 1889 else
viewdeep51 3:ae45e29f5d4f 1890 __nop();
viewdeep51 3:ae45e29f5d4f 1891 }
viewdeep51 4:710464cb44fc 1892 else if(strcmp(rx_buf,"$PPOTT") == 0) //* submaster,Slaves or listener receive CMD *//
viewdeep51 3:ae45e29f5d4f 1893 {
viewdeep51 3:ae45e29f5d4f 1894 sscanf(strtok (NULL,","), "%u", &Hrs_1); //cmd extracted
viewdeep51 3:ae45e29f5d4f 1895 sscanf(strtok (NULL,","), "%u", &Hrs_2);
viewdeep51 3:ae45e29f5d4f 1896 sscanf(strtok (NULL,","), "%u", &Mins_1);
viewdeep51 4:710464cb44fc 1897 sscanf(strtok (NULL,","),"%u", &Mins_2);
viewdeep51 4:710464cb44fc 1898 sscanf(strtok (NULL,"*"),"%c", &rcvd_valid); //sentence validity char
viewdeep51 4:710464cb44fc 1899 sscanf(strtok (NULL,","),"%u", &checksum_rcvd_byte);
viewdeep51 4:710464cb44fc 1900 sprintf(rx_buf,"$PPOTT,%u,%u,%u,%u,%c*",Hrs_1,Hrs_2,Mins_1,Mins_2,rcvd_valid);
viewdeep51 4:710464cb44fc 1901 checksum_calculated = get_checksum(rx_buf);
viewdeep51 4:710464cb44fc 1902 if(checksum_calculated == checksum_rcvd_byte)
viewdeep51 4:710464cb44fc 1903 {
viewdeep51 4:710464cb44fc 1904 valid = 'A';
viewdeep51 4:710464cb44fc 1905 rcvd_TIME();
viewdeep51 4:710464cb44fc 1906 }
viewdeep51 4:710464cb44fc 1907 else
viewdeep51 4:710464cb44fc 1908 valid = 'V';
viewdeep51 3:ae45e29f5d4f 1909 }
viewdeep51 0:ba33a62aea4e 1910 else
viewdeep51 0:ba33a62aea4e 1911 __nop();
viewdeep51 0:ba33a62aea4e 1912 }
viewdeep51 0:ba33a62aea4e 1913 else
viewdeep51 0:ba33a62aea4e 1914 __nop();
viewdeep51 0:ba33a62aea4e 1915 }
viewdeep51 0:ba33a62aea4e 1916 }
viewdeep51 0:ba33a62aea4e 1917
viewdeep51 3:ae45e29f5d4f 1918 /********END of FUNCTION****************/
viewdeep51 3:ae45e29f5d4f 1919
viewdeep51 0:ba33a62aea4e 1920 /**************************************************************************************
viewdeep51 0:ba33a62aea4e 1921 EOT Handler FUNCTIONS
viewdeep51 0:ba33a62aea4e 1922 **************************************************************************************/
viewdeep51 0:ba33a62aea4e 1923 void HB_handler() //function run periodically within active master only
viewdeep51 0:ba33a62aea4e 1924 {
viewdeep51 3:ae45e29f5d4f 1925 if(++sync_count >= sync_time)
viewdeep51 0:ba33a62aea4e 1926 {
viewdeep51 3:ae45e29f5d4f 1927 time_RCVD = false;
viewdeep51 3:ae45e29f5d4f 1928 norm_op = false;
viewdeep51 3:ae45e29f5d4f 1929 sync_count = 0;
viewdeep51 3:ae45e29f5d4f 1930 ask_TIME(); //master will send querry for GPS time
viewdeep51 3:ae45e29f5d4f 1931 }
viewdeep51 3:ae45e29f5d4f 1932 else if(time_RCVD || norm_op) //as all receive time update normal operation starts
viewdeep51 3:ae45e29f5d4f 1933 {
viewdeep51 3:ae45e29f5d4f 1934 if(ach_M == pot_Master)
viewdeep51 0:ba33a62aea4e 1935 {
viewdeep51 0:ba33a62aea4e 1936 device_addr++;
viewdeep51 4:710464cb44fc 1937 if(device_addr == no_pot_connected) //max no-pot-conneted = 7
viewdeep51 3:ae45e29f5d4f 1938 device_addr = 0;
viewdeep51 0:ba33a62aea4e 1939 }
viewdeep51 0:ba33a62aea4e 1940 else
viewdeep51 3:ae45e29f5d4f 1941 __nop();
viewdeep51 0:ba33a62aea4e 1942 /*else if(ach_M == eot_Submaster)
viewdeep51 3:ae45e29f5d4f 1943 {
viewdeep51 3:ae45e29f5d4f 1944 if(device_addr==2)
viewdeep51 3:ae45e29f5d4f 1945 {
viewdeep51 3:ae45e29f5d4f 1946 device_addr=3;
viewdeep51 3:ae45e29f5d4f 1947 }
viewdeep51 3:ae45e29f5d4f 1948 else
viewdeep51 3:ae45e29f5d4f 1949 {
viewdeep51 3:ae45e29f5d4f 1950 device_addr++;
viewdeep51 3:ae45e29f5d4f 1951 if(device_addr>NO_MAX_EOT)
viewdeep51 3:ae45e29f5d4f 1952 device_addr=1;
viewdeep51 3:ae45e29f5d4f 1953 }
viewdeep51 3:ae45e29f5d4f 1954 }
viewdeep51 3:ae45e29f5d4f 1955 */
viewdeep51 4:710464cb44fc 1956
viewdeep51 3:ae45e29f5d4f 1957 send_ADDR = POT[device_addr].slave_ID;
viewdeep51 4:710464cb44fc 1958
viewdeep51 0:ba33a62aea4e 1959 if(RPM_cmd)
viewdeep51 0:ba33a62aea4e 1960 {
viewdeep51 3:ae45e29f5d4f 1961 send_RPM(send_ADDR);
viewdeep51 3:ae45e29f5d4f 1962 RPM_cmd = false; //cmd to be send with current running address and cmd value
viewdeep51 3:ae45e29f5d4f 1963 wait_ms(2);
viewdeep51 3:ae45e29f5d4f 1964 //HB_Ticker.attach(&HB_State_Machine,0.010);
viewdeep51 3:ae45e29f5d4f 1965 CMD_Ticker.attach(&CMD_State_Machine,0.25); //CMD_state_machine ticker started
viewdeep51 0:ba33a62aea4e 1966 }
viewdeep51 5:f7b7ee0702cc 1967 /*
viewdeep51 0:ba33a62aea4e 1968 else if(DIR_cmd)
viewdeep51 0:ba33a62aea4e 1969 {
viewdeep51 3:ae45e29f5d4f 1970 send_DIR(send_ADDR);
viewdeep51 3:ae45e29f5d4f 1971 DIR_cmd = false; //cmd to be send with current running address and cmd value
viewdeep51 3:ae45e29f5d4f 1972 wait_ms(2);
viewdeep51 3:ae45e29f5d4f 1973 //HB_Ticker.attach(&HB_State_Machine,0.010);
viewdeep51 5:f7b7ee0702cc 1974 //DIR_Ticker.attach(&DIR_state_machine,0.25); //CMD_state_machine ticker started
viewdeep51 0:ba33a62aea4e 1975 }
viewdeep51 5:f7b7ee0702cc 1976 */
viewdeep51 3:ae45e29f5d4f 1977 else if(BROADCAST_send)
viewdeep51 0:ba33a62aea4e 1978 {
viewdeep51 3:ae45e29f5d4f 1979 send_BROADCAST(); //broadcast needed err_ID nd err_Status
viewdeep51 0:ba33a62aea4e 1980 BROADCAST_send = false;
viewdeep51 3:ae45e29f5d4f 1981 wait_ms(2);
viewdeep51 0:ba33a62aea4e 1982 }
viewdeep51 3:ae45e29f5d4f 1983 else if(switch_CH) //active channel switch over happened
viewdeep51 0:ba33a62aea4e 1984 {
viewdeep51 3:ae45e29f5d4f 1985 send_ACH(); //active channel changeover
viewdeep51 0:ba33a62aea4e 1986 switch_CH = false;
viewdeep51 3:ae45e29f5d4f 1987 wait_ms(2);
viewdeep51 0:ba33a62aea4e 1988 }
viewdeep51 3:ae45e29f5d4f 1989
viewdeep51 0:ba33a62aea4e 1990 else
viewdeep51 3:ae45e29f5d4f 1991 {
viewdeep51 3:ae45e29f5d4f 1992 send_HB(send_ADDR); //hb is to be going on
viewdeep51 4:710464cb44fc 1993 HB_Ticker.attach(&HB_State_Machine,0.015); //HB_state_machine timer started
viewdeep51 0:ba33a62aea4e 1994 }
viewdeep51 0:ba33a62aea4e 1995 }
viewdeep51 3:ae45e29f5d4f 1996 else if(++norm_count >= 5)
viewdeep51 3:ae45e29f5d4f 1997 {
viewdeep51 3:ae45e29f5d4f 1998 norm_count = 0;
viewdeep51 3:ae45e29f5d4f 1999 if(!time_RCVD)
viewdeep51 3:ae45e29f5d4f 2000 norm_op = true;
viewdeep51 3:ae45e29f5d4f 2001 else
viewdeep51 3:ae45e29f5d4f 2002 __nop();
viewdeep51 3:ae45e29f5d4f 2003 }
viewdeep51 0:ba33a62aea4e 2004 else
viewdeep51 0:ba33a62aea4e 2005 __nop();
viewdeep51 0:ba33a62aea4e 2006 }
viewdeep51 0:ba33a62aea4e 2007
viewdeep51 0:ba33a62aea4e 2008 /**************************************************************************************
viewdeep51 3:ae45e29f5d4f 2009 POT config and address Read FUNCTIONS
viewdeep51 0:ba33a62aea4e 2010 **************************************************************************************/
viewdeep51 0:ba33a62aea4e 2011 void read_POT_ADDR() //function give present eots and no. of connected eots
viewdeep51 0:ba33a62aea4e 2012 {
viewdeep51 0:ba33a62aea4e 2013 pot_Addr = read_KEY_SPI(ch_D);
viewdeep51 0:ba33a62aea4e 2014 if(pot_Addr==0xff) //if 0xff it means no addressing done
viewdeep51 0:ba33a62aea4e 2015 pot_Error=true;
viewdeep51 0:ba33a62aea4e 2016 else if(pot_Addr!=0xff)
viewdeep51 0:ba33a62aea4e 2017 {
viewdeep51 0:ba33a62aea4e 2018 for( uint8_t i=0;i<=7;i++)
viewdeep51 0:ba33a62aea4e 2019 {
viewdeep51 0:ba33a62aea4e 2020 if(((1 << i) & pot_Addr)== 0) // to check which bit of eot_Addr register is '0'
viewdeep51 0:ba33a62aea4e 2021 {
viewdeep51 0:ba33a62aea4e 2022 if(i==0) // bit 0 means master present and soon
viewdeep51 0:ba33a62aea4e 2023 {
viewdeep51 0:ba33a62aea4e 2024 addr_ID = pot_Master;
viewdeep51 0:ba33a62aea4e 2025 ach_M = pot_Master;
viewdeep51 0:ba33a62aea4e 2026 for(i = 1 ; i <= 7 ; i++)
viewdeep51 0:ba33a62aea4e 2027 {
viewdeep51 0:ba33a62aea4e 2028 if((((uint8_t)1<<i) & pot_Addr) == 0)
viewdeep51 0:ba33a62aea4e 2029 {
viewdeep51 0:ba33a62aea4e 2030 switch(i)
viewdeep51 0:ba33a62aea4e 2031 {
viewdeep51 0:ba33a62aea4e 2032 case 1:
viewdeep51 0:ba33a62aea4e 2033 POT[no_pot_connected].slave_ID = pot_Submaster;
viewdeep51 0:ba33a62aea4e 2034 POT[no_pot_connected].err_cnt=0;
viewdeep51 3:ae45e29f5d4f 2035 POT[no_pot_connected].err_status=false;
viewdeep51 3:ae45e29f5d4f 2036 no_pot_connected++;
viewdeep51 0:ba33a62aea4e 2037 break;
viewdeep51 0:ba33a62aea4e 2038 case 2:
viewdeep51 0:ba33a62aea4e 2039 ach_S = pot_Slave_MCR;
viewdeep51 0:ba33a62aea4e 2040 POT[no_pot_connected].slave_ID = pot_Slave_MCR;
viewdeep51 0:ba33a62aea4e 2041 POT[no_pot_connected].err_cnt=0;
viewdeep51 3:ae45e29f5d4f 2042 POT[no_pot_connected].err_status=false;
viewdeep51 0:ba33a62aea4e 2043 no_pot_connected++;
viewdeep51 0:ba33a62aea4e 2044 break;
viewdeep51 0:ba33a62aea4e 2045 case 3:
viewdeep51 0:ba33a62aea4e 2046 if(ach_S == pot_Slave_MCR) //if MCR ispresent
viewdeep51 0:ba33a62aea4e 2047 __nop();
viewdeep51 0:ba33a62aea4e 2048 else
viewdeep51 0:ba33a62aea4e 2049 { //if MCR is not present
viewdeep51 0:ba33a62aea4e 2050 ach_S = pot_Slave_ER;
viewdeep51 0:ba33a62aea4e 2051 }
viewdeep51 0:ba33a62aea4e 2052 POT[no_pot_connected].slave_ID = pot_Slave_ER;
viewdeep51 0:ba33a62aea4e 2053 POT[no_pot_connected].err_cnt = 0;
viewdeep51 3:ae45e29f5d4f 2054 POT[no_pot_connected].err_status=false;
viewdeep51 0:ba33a62aea4e 2055 no_pot_connected++;
viewdeep51 0:ba33a62aea4e 2056 break;
viewdeep51 0:ba33a62aea4e 2057 case 4:
viewdeep51 0:ba33a62aea4e 2058 POT[no_pot_connected].slave_ID = pot_Listner_WP;
viewdeep51 0:ba33a62aea4e 2059 POT[no_pot_connected].err_cnt = 0;
viewdeep51 3:ae45e29f5d4f 2060 POT[no_pot_connected].err_status=false;
viewdeep51 0:ba33a62aea4e 2061 no_pot_connected++;
viewdeep51 0:ba33a62aea4e 2062 break;
viewdeep51 0:ba33a62aea4e 2063 case 5:
viewdeep51 0:ba33a62aea4e 2064 POT[no_pot_connected].slave_ID = pot_Listner_WS;
viewdeep51 0:ba33a62aea4e 2065 POT[no_pot_connected].err_cnt=0;
viewdeep51 3:ae45e29f5d4f 2066 POT[no_pot_connected].err_status=false;
viewdeep51 0:ba33a62aea4e 2067 no_pot_connected++;
viewdeep51 0:ba33a62aea4e 2068 break;
viewdeep51 0:ba33a62aea4e 2069 case 6:
viewdeep51 0:ba33a62aea4e 2070 POT[no_pot_connected].slave_ID = pot_Listner_OPS;
viewdeep51 0:ba33a62aea4e 2071 POT[no_pot_connected].err_cnt=0;
viewdeep51 3:ae45e29f5d4f 2072 POT[no_pot_connected].err_status=false;
viewdeep51 0:ba33a62aea4e 2073 no_pot_connected++;
viewdeep51 0:ba33a62aea4e 2074 break;
viewdeep51 0:ba33a62aea4e 2075 case 7:
viewdeep51 0:ba33a62aea4e 2076 POT[no_pot_connected].slave_ID = pot_Listner_ASP;
viewdeep51 0:ba33a62aea4e 2077 POT[no_pot_connected].err_cnt=0;
viewdeep51 3:ae45e29f5d4f 2078 POT[no_pot_connected].err_status=false;
viewdeep51 0:ba33a62aea4e 2079 no_pot_connected++;
viewdeep51 0:ba33a62aea4e 2080 break;
viewdeep51 0:ba33a62aea4e 2081 }
viewdeep51 0:ba33a62aea4e 2082 }
viewdeep51 3:ae45e29f5d4f 2083 }
viewdeep51 3:ae45e29f5d4f 2084 if(no_pot_connected <=0)
viewdeep51 3:ae45e29f5d4f 2085 pot_Error = true;
viewdeep51 3:ae45e29f5d4f 2086 else
viewdeep51 3:ae45e29f5d4f 2087 pot_Error = false;
viewdeep51 0:ba33a62aea4e 2088 }
viewdeep51 0:ba33a62aea4e 2089 else
viewdeep51 0:ba33a62aea4e 2090 {
viewdeep51 0:ba33a62aea4e 2091 switch(i)
viewdeep51 0:ba33a62aea4e 2092 {
viewdeep51 0:ba33a62aea4e 2093 case 1:
viewdeep51 0:ba33a62aea4e 2094 {
viewdeep51 0:ba33a62aea4e 2095 addr_ID = pot_Submaster;
viewdeep51 3:ae45e29f5d4f 2096 ach_S = pot_Slave_MCR;
viewdeep51 3:ae45e29f5d4f 2097 led_ach[ach_S-1] = ON;
viewdeep51 0:ba33a62aea4e 2098 break;
viewdeep51 0:ba33a62aea4e 2099 }
viewdeep51 0:ba33a62aea4e 2100 case 2:
viewdeep51 0:ba33a62aea4e 2101 {
viewdeep51 0:ba33a62aea4e 2102 addr_ID = pot_Slave_MCR;
viewdeep51 3:ae45e29f5d4f 2103 ach_S = pot_Slave_MCR;
viewdeep51 3:ae45e29f5d4f 2104 led_ach[ach_S-1] = ON;
viewdeep51 0:ba33a62aea4e 2105 break;
viewdeep51 0:ba33a62aea4e 2106 }
viewdeep51 0:ba33a62aea4e 2107 case 3:
viewdeep51 0:ba33a62aea4e 2108 {
viewdeep51 0:ba33a62aea4e 2109 addr_ID = pot_Slave_ER;
viewdeep51 3:ae45e29f5d4f 2110 ach_S = pot_Slave_MCR;
viewdeep51 3:ae45e29f5d4f 2111 led_ach[ach_S-1] = ON;
viewdeep51 0:ba33a62aea4e 2112 break;
viewdeep51 0:ba33a62aea4e 2113 }
viewdeep51 0:ba33a62aea4e 2114 case 4:
viewdeep51 0:ba33a62aea4e 2115 {
viewdeep51 0:ba33a62aea4e 2116 addr_ID = pot_Listner_WP;
viewdeep51 3:ae45e29f5d4f 2117 ach_S = pot_Slave_MCR;
viewdeep51 3:ae45e29f5d4f 2118 led_ach[ach_S-1] = ON;
viewdeep51 0:ba33a62aea4e 2119 break;
viewdeep51 0:ba33a62aea4e 2120 }
viewdeep51 0:ba33a62aea4e 2121 case 5:
viewdeep51 0:ba33a62aea4e 2122 {
viewdeep51 0:ba33a62aea4e 2123 addr_ID = pot_Listner_WS;
viewdeep51 3:ae45e29f5d4f 2124 ach_S = pot_Slave_MCR;
viewdeep51 3:ae45e29f5d4f 2125 led_ach[ach_S-1] = ON;
viewdeep51 0:ba33a62aea4e 2126 break;
viewdeep51 0:ba33a62aea4e 2127 }
viewdeep51 0:ba33a62aea4e 2128 case 6:
viewdeep51 0:ba33a62aea4e 2129 {
viewdeep51 0:ba33a62aea4e 2130 addr_ID = pot_Listner_OPS;
viewdeep51 3:ae45e29f5d4f 2131 ach_S = pot_Slave_MCR;
viewdeep51 3:ae45e29f5d4f 2132 led_ach[ach_S-1] = ON;
viewdeep51 0:ba33a62aea4e 2133 break;
viewdeep51 0:ba33a62aea4e 2134 }
viewdeep51 0:ba33a62aea4e 2135 case 7:
viewdeep51 0:ba33a62aea4e 2136 {
viewdeep51 0:ba33a62aea4e 2137 addr_ID = pot_Listner_ASP;
viewdeep51 3:ae45e29f5d4f 2138 ach_S = pot_Slave_MCR;
viewdeep51 3:ae45e29f5d4f 2139 led_ach[ach_S-1] = ON;
viewdeep51 0:ba33a62aea4e 2140 break;
viewdeep51 0:ba33a62aea4e 2141 }
viewdeep51 0:ba33a62aea4e 2142 default:
viewdeep51 0:ba33a62aea4e 2143 pot_Error = true;
viewdeep51 0:ba33a62aea4e 2144 }
viewdeep51 0:ba33a62aea4e 2145 }
viewdeep51 0:ba33a62aea4e 2146 }
viewdeep51 0:ba33a62aea4e 2147 }
viewdeep51 0:ba33a62aea4e 2148 }
viewdeep51 0:ba33a62aea4e 2149 }
viewdeep51 0:ba33a62aea4e 2150
viewdeep51 3:ae45e29f5d4f 2151 void interrupt_init()
viewdeep51 3:ae45e29f5d4f 2152 {
viewdeep51 3:ae45e29f5d4f 2153 int_A.mode(PullUp);
viewdeep51 3:ae45e29f5d4f 2154 wait_ms(1);
viewdeep51 3:ae45e29f5d4f 2155 int_B.mode(PullUp);
viewdeep51 3:ae45e29f5d4f 2156 wait_ms(1);
viewdeep51 3:ae45e29f5d4f 2157 int_C.mode(PullUp);
viewdeep51 3:ae45e29f5d4f 2158 wait_ms(1);
viewdeep51 3:ae45e29f5d4f 2159 }
viewdeep51 3:ae45e29f5d4f 2160
viewdeep51 3:ae45e29f5d4f 2161 void Uart_init()
viewdeep51 3:ae45e29f5d4f 2162 {
viewdeep51 0:ba33a62aea4e 2163 UART.baud(9600);
viewdeep51 0:ba33a62aea4e 2164 UART.format(8,SerialBase::None,1);
viewdeep51 3:ae45e29f5d4f 2165 wait_ms(1);
viewdeep51 0:ba33a62aea4e 2166 }
viewdeep51 0:ba33a62aea4e 2167
viewdeep51 3:ae45e29f5d4f 2168 void Led_init()
viewdeep51 3:ae45e29f5d4f 2169 {
viewdeep51 3:ae45e29f5d4f 2170
viewdeep51 3:ae45e29f5d4f 2171 LED_CMD_ACK = ON;
viewdeep51 3:ae45e29f5d4f 2172 wait_ms(1);
viewdeep51 5:f7b7ee0702cc 2173
viewdeep51 3:ae45e29f5d4f 2174 for(uint8_t i=0;i<=1;i++) //ctrl led intialisation
viewdeep51 3:ae45e29f5d4f 2175 {
viewdeep51 3:ae45e29f5d4f 2176 LED_CTRL[i] = ON;
viewdeep51 3:ae45e29f5d4f 2177 wait_ms(1);
viewdeep51 3:ae45e29f5d4f 2178 }
viewdeep51 5:f7b7ee0702cc 2179 /*
viewdeep51 4:710464cb44fc 2180 for(uint8_t i=0;i<=1;i++) //ctrl led intialisation
viewdeep51 4:710464cb44fc 2181 {
viewdeep51 4:710464cb44fc 2182 LED_DIR[i] = ON;
viewdeep51 4:710464cb44fc 2183 wait_ms(1);
viewdeep51 4:710464cb44fc 2184 }
viewdeep51 5:f7b7ee0702cc 2185 */
viewdeep51 3:ae45e29f5d4f 2186 for(uint8_t i=0; i<=7;i++) //ach led initialisation
viewdeep51 3:ae45e29f5d4f 2187 {
viewdeep51 3:ae45e29f5d4f 2188 LED_ACH[i] = ON ;
viewdeep51 3:ae45e29f5d4f 2189 wait_ms(1);
viewdeep51 3:ae45e29f5d4f 2190 }
viewdeep51 3:ae45e29f5d4f 2191
viewdeep51 3:ae45e29f5d4f 2192 wait(1);
viewdeep51 3:ae45e29f5d4f 2193
viewdeep51 4:710464cb44fc 2194 LED_CMD_ACK = OFF;
viewdeep51 3:ae45e29f5d4f 2195 led_cmd_ack = OFF;
viewdeep51 3:ae45e29f5d4f 2196 wait_ms(1);
viewdeep51 3:ae45e29f5d4f 2197
viewdeep51 3:ae45e29f5d4f 2198 for(uint8_t i=0;i<=1;i++) //ctrl led turned off
viewdeep51 3:ae45e29f5d4f 2199 {
viewdeep51 3:ae45e29f5d4f 2200 LED_CTRL[i] = OFF;
viewdeep51 3:ae45e29f5d4f 2201 led_ctrl[i] = OFF;
viewdeep51 3:ae45e29f5d4f 2202 wait_ms(1);
viewdeep51 3:ae45e29f5d4f 2203 }
viewdeep51 5:f7b7ee0702cc 2204 /*
viewdeep51 4:710464cb44fc 2205 for(uint8_t i=0;i<=1;i++) //ctrl led intialisation
viewdeep51 4:710464cb44fc 2206 {
viewdeep51 4:710464cb44fc 2207 LED_DIR[i] = OFF;
viewdeep51 4:710464cb44fc 2208 led_dir[i] = OFF;
viewdeep51 4:710464cb44fc 2209 wait_ms(1);
viewdeep51 4:710464cb44fc 2210 }
viewdeep51 5:f7b7ee0702cc 2211 */
viewdeep51 3:ae45e29f5d4f 2212 for(uint8_t i=0; i<=7;i++) //ach led turned off
viewdeep51 3:ae45e29f5d4f 2213 {
viewdeep51 3:ae45e29f5d4f 2214 LED_ACH[i] = OFF;
viewdeep51 3:ae45e29f5d4f 2215 led_ach[i] = OFF;
viewdeep51 3:ae45e29f5d4f 2216 wait_ms(1);
viewdeep51 3:ae45e29f5d4f 2217 }
viewdeep51 3:ae45e29f5d4f 2218 }
viewdeep51 0:ba33a62aea4e 2219
viewdeep51 3:ae45e29f5d4f 2220 void pwm_init()
viewdeep51 0:ba33a62aea4e 2221 {
viewdeep51 4:710464cb44fc 2222 mypwm.period_ms(10); //initialy 20ms
viewdeep51 3:ae45e29f5d4f 2223 wait_us(1);
viewdeep51 4:710464cb44fc 2224 mypwm.write(.10); //10%duty cycle more bright with less duty cycle
viewdeep51 3:ae45e29f5d4f 2225 wait_us(1);
viewdeep51 0:ba33a62aea4e 2226 }
viewdeep51 0:ba33a62aea4e 2227
viewdeep51 0:ba33a62aea4e 2228 void mcp23s17_init()
viewdeep51 0:ba33a62aea4e 2229 {
viewdeep51 0:ba33a62aea4e 2230 //device1 configuration
viewdeep51 3:ae45e29f5d4f 2231 mcp23s17_1.reset(); //reset on power-up
viewdeep51 3:ae45e29f5d4f 2232 wait_ms(1);
viewdeep51 3:ae45e29f5d4f 2233 mcp23s17_2.reset(); //reset on power-up
viewdeep51 3:ae45e29f5d4f 2234 wait_ms(1);
viewdeep51 3:ae45e29f5d4f 2235 mcp23s17_1.iocon(0x2a);
viewdeep51 3:ae45e29f5d4f 2236 wait_ms(1);
viewdeep51 3:ae45e29f5d4f 2237 mcp23s17_2.iocon(0x2a);
viewdeep51 3:ae45e29f5d4f 2238 wait_ms(1);
viewdeep51 3:ae45e29f5d4f 2239 mcp23s17_1.iodira(0xff); //set 8-bits PORTA as inputs
viewdeep51 3:ae45e29f5d4f 2240 wait_ms(1);
viewdeep51 3:ae45e29f5d4f 2241 mcp23s17_1.iodirb(0xff); //set 8-bits PORTB as inputs
viewdeep51 3:ae45e29f5d4f 2242 wait_ms(1);
viewdeep51 0:ba33a62aea4e 2243 mcp23s17_1.ipola(0x00);
viewdeep51 3:ae45e29f5d4f 2244 wait_ms(1);
viewdeep51 0:ba33a62aea4e 2245 mcp23s17_1.ipolb(0x00);
viewdeep51 3:ae45e29f5d4f 2246 wait_ms(1);
viewdeep51 3:ae45e29f5d4f 2247 mcp23s17_1.gpintena(0x00); //initially disabled
viewdeep51 3:ae45e29f5d4f 2248 wait_ms(1);
viewdeep51 3:ae45e29f5d4f 2249 mcp23s17_1.gpintenb(0x00);
viewdeep51 3:ae45e29f5d4f 2250 wait_ms(1);
viewdeep51 0:ba33a62aea4e 2251 mcp23s17_1.defvala(0xff);
viewdeep51 3:ae45e29f5d4f 2252 wait_ms(1);
viewdeep51 0:ba33a62aea4e 2253 mcp23s17_1.defvalb(0xff);
viewdeep51 3:ae45e29f5d4f 2254 wait_ms(1);
viewdeep51 3:ae45e29f5d4f 2255 mcp23s17_1.intcona(0x00); //bank0,mirror disabled, SEQOP disabled, slew rate disabled,HAEN enabled,open drain enabled,interrupt active high,not allowed
viewdeep51 3:ae45e29f5d4f 2256 wait_ms(1);
viewdeep51 3:ae45e29f5d4f 2257 mcp23s17_1.intconb(0x00);
viewdeep51 3:ae45e29f5d4f 2258 wait_ms(1);
viewdeep51 0:ba33a62aea4e 2259 mcp23s17_1.gppua(0xff);
viewdeep51 3:ae45e29f5d4f 2260 wait_ms(1);
viewdeep51 0:ba33a62aea4e 2261 mcp23s17_1.gppub(0xff);
viewdeep51 3:ae45e29f5d4f 2262 wait_ms(1);
viewdeep51 0:ba33a62aea4e 2263
viewdeep51 0:ba33a62aea4e 2264 //device2 configuration
viewdeep51 3:ae45e29f5d4f 2265
viewdeep51 3:ae45e29f5d4f 2266 mcp23s17_2.iodira(0xff); //set 8-bits PORTA as inputs
viewdeep51 3:ae45e29f5d4f 2267 wait_ms(1);
viewdeep51 3:ae45e29f5d4f 2268 mcp23s17_2.iodirb(0xff); //set 8-bits PORTb as inputs
viewdeep51 3:ae45e29f5d4f 2269 wait_ms(1);
viewdeep51 0:ba33a62aea4e 2270 mcp23s17_2.ipola(0x00);
viewdeep51 3:ae45e29f5d4f 2271 wait_ms(1);
viewdeep51 0:ba33a62aea4e 2272 mcp23s17_2.ipolb(0x00);
viewdeep51 3:ae45e29f5d4f 2273 wait_ms(1);
viewdeep51 3:ae45e29f5d4f 2274 mcp23s17_2.gpintena(0xff); //initially 0xff will be always enabled req/inc/dec/test
viewdeep51 3:ae45e29f5d4f 2275 wait_ms(1);
viewdeep51 3:ae45e29f5d4f 2276 mcp23s17_2.gpintenb(0x00); //interrupts disabled for address PORT
viewdeep51 3:ae45e29f5d4f 2277 wait_ms(1);
viewdeep51 0:ba33a62aea4e 2278 mcp23s17_2.defvala(0xff);
viewdeep51 3:ae45e29f5d4f 2279 wait_ms(1);
viewdeep51 0:ba33a62aea4e 2280 mcp23s17_2.defvalb(0xff);
viewdeep51 3:ae45e29f5d4f 2281 wait_ms(1);
viewdeep51 3:ae45e29f5d4f 2282 mcp23s17_2.intcona(0x00); //bank0,mirror disabled, SEQOP disabled, slew rate disabled,HAEN enabled,open drain enabled,interrupt active high,not allowed
viewdeep51 3:ae45e29f5d4f 2283 wait_ms(1);
viewdeep51 3:ae45e29f5d4f 2284 mcp23s17_2.intconb(0x00);
viewdeep51 3:ae45e29f5d4f 2285 wait_ms(1);
viewdeep51 0:ba33a62aea4e 2286 mcp23s17_2.gppua(0xff);
viewdeep51 3:ae45e29f5d4f 2287 wait_ms(1);
viewdeep51 0:ba33a62aea4e 2288 mcp23s17_2.gppub(0xff);
viewdeep51 3:ae45e29f5d4f 2289 wait_ms(1);
viewdeep51 3:ae45e29f5d4f 2290 mcp23s17_1.intcapa(); //dummy read performed to clear any interrupt on power up
viewdeep51 3:ae45e29f5d4f 2291 wait_ms(1);
viewdeep51 3:ae45e29f5d4f 2292 mcp23s17_1.intcapb();
viewdeep51 3:ae45e29f5d4f 2293 wait_ms(1);
viewdeep51 3:ae45e29f5d4f 2294 mcp23s17_2.intcapa();
viewdeep51 3:ae45e29f5d4f 2295 wait_ms(1);
viewdeep51 3:ae45e29f5d4f 2296
viewdeep51 0:ba33a62aea4e 2297 }
viewdeep51 0:ba33a62aea4e 2298
viewdeep51 3:ae45e29f5d4f 2299 void Spi3_init() //for mcp23s17
viewdeep51 3:ae45e29f5d4f 2300 {
viewdeep51 3:ae45e29f5d4f 2301 _SPI3.format(8,0); //8bit,mode 0
viewdeep51 3:ae45e29f5d4f 2302 _SPI3.frequency(1000000); //1mhz
viewdeep51 3:ae45e29f5d4f 2303 wait_ms(1);
viewdeep51 3:ae45e29f5d4f 2304 }
viewdeep51 3:ae45e29f5d4f 2305
viewdeep51 3:ae45e29f5d4f 2306 void RTC_init()
viewdeep51 0:ba33a62aea4e 2307 {
viewdeep51 3:ae45e29f5d4f 2308 read_RTC();
viewdeep51 3:ae45e29f5d4f 2309 wait_ms(1);
viewdeep51 3:ae45e29f5d4f 2310 RTC_display(RTC_buffer);
viewdeep51 3:ae45e29f5d4f 2311 wait_ms(1);
viewdeep51 3:ae45e29f5d4f 2312 }
viewdeep51 3:ae45e29f5d4f 2313
viewdeep51 3:ae45e29f5d4f 2314 void SetDateTime
viewdeep51 3:ae45e29f5d4f 2315 ( int year = 0
viewdeep51 3:ae45e29f5d4f 2316 ,int mon = 0
viewdeep51 3:ae45e29f5d4f 2317 ,int day = 0
viewdeep51 3:ae45e29f5d4f 2318 ,int hour = 0
viewdeep51 3:ae45e29f5d4f 2319 ,int min = 0
viewdeep51 3:ae45e29f5d4f 2320 ,int sec = 0
viewdeep51 3:ae45e29f5d4f 2321 )
viewdeep51 3:ae45e29f5d4f 2322 {
viewdeep51 3:ae45e29f5d4f 2323 struct tm Clock;
viewdeep51 3:ae45e29f5d4f 2324 Clock.tm_year = year - 1900;
viewdeep51 3:ae45e29f5d4f 2325 Clock.tm_mon = mon;
viewdeep51 3:ae45e29f5d4f 2326 Clock.tm_mday = day;
viewdeep51 3:ae45e29f5d4f 2327 Clock.tm_hour = hour;
viewdeep51 3:ae45e29f5d4f 2328 Clock.tm_min = min;
viewdeep51 3:ae45e29f5d4f 2329 Clock.tm_sec = sec;
viewdeep51 3:ae45e29f5d4f 2330 time_t epoch = mktime(&Clock);
viewdeep51 3:ae45e29f5d4f 2331 set_time(epoch); //time set
viewdeep51 0:ba33a62aea4e 2332 }
viewdeep51 0:ba33a62aea4e 2333
viewdeep51 0:ba33a62aea4e 2334 void max7219_init()
viewdeep51 0:ba33a62aea4e 2335 {
viewdeep51 3:ae45e29f5d4f 2336 max7219.set_num_devices(2); //total 02 no. of devices
viewdeep51 3:ae45e29f5d4f 2337 DEVICE1.device_number = 0x01;
viewdeep51 4:710464cb44fc 2338 DEVICE1.decode_mode = 0xff; //bcd decode mode
viewdeep51 3:ae45e29f5d4f 2339 DEVICE1.intensity = 0x01; //Max7219::MAX7219_INTENSITY_8,
viewdeep51 4:710464cb44fc 2340 DEVICE1.scan_limit = 0x07; // 1-4 digits RTC,5-8 digits last cmd RPM
viewdeep51 3:ae45e29f5d4f 2341 wait_ms(1);
viewdeep51 3:ae45e29f5d4f 2342 DEVICE2.device_number = 0x02; //
viewdeep51 0:ba33a62aea4e 2343 DEVICE2.decode_mode = 0xff; //bcd decode mode
viewdeep51 3:ae45e29f5d4f 2344 DEVICE2.intensity = 0x01; //Max7219::MAX7219_INTENSITY_8, //intensity moderate
viewdeep51 4:710464cb44fc 2345 DEVICE2.scan_limit = 0x03; //1-4 digits current
viewdeep51 3:ae45e29f5d4f 2346 wait_ms(1);
viewdeep51 3:ae45e29f5d4f 2347
viewdeep51 0:ba33a62aea4e 2348 max7219.init_device(DEVICE1); //DEVICE 1 configured as per above settings
viewdeep51 3:ae45e29f5d4f 2349 wait_ms(1);
viewdeep51 3:ae45e29f5d4f 2350 max7219.enable_device(1); //normal operation
viewdeep51 3:ae45e29f5d4f 2351 wait_ms(1);
viewdeep51 0:ba33a62aea4e 2352 max7219.init_device(DEVICE2); //DEVICE 2 configured as per above settings
viewdeep51 3:ae45e29f5d4f 2353 wait_ms(1);
viewdeep51 3:ae45e29f5d4f 2354 max7219.enable_device(2); //normal operation
viewdeep51 3:ae45e29f5d4f 2355 wait_ms(1);
viewdeep51 3:ae45e29f5d4f 2356 max7219.set_display_test(); //all on
viewdeep51 0:ba33a62aea4e 2357 wait(1);
viewdeep51 3:ae45e29f5d4f 2358 max7219.clear_display_test(); //normal operation
viewdeep51 3:ae45e29f5d4f 2359 wait_ms(1);
viewdeep51 4:710464cb44fc 2360 for(uint8_t i=1; i<=4; i++)
viewdeep51 0:ba33a62aea4e 2361 {
viewdeep51 4:710464cb44fc 2362 max7219.write_digit(1, i, 0x00); //device 1 RTC, digit 1-4 ,data 0x01
viewdeep51 4:710464cb44fc 2363 wait_us(100);
viewdeep51 0:ba33a62aea4e 2364 }
viewdeep51 3:ae45e29f5d4f 2365 wait_ms(100);
viewdeep51 4:710464cb44fc 2366
viewdeep51 4:710464cb44fc 2367 for(uint8_t i=4; i<=8; i++)
viewdeep51 3:ae45e29f5d4f 2368 {
viewdeep51 4:710464cb44fc 2369 max7219.write_digit(1, i, 0x00); //device 1 last cmd, digit 4-8 ,data 0x01
viewdeep51 4:710464cb44fc 2370 wait_us(100);
viewdeep51 4:710464cb44fc 2371 }
viewdeep51 4:710464cb44fc 2372 wait_ms(100);
viewdeep51 4:710464cb44fc 2373 for(uint8_t i=1; i<=4;i++)
viewdeep51 4:710464cb44fc 2374 {
viewdeep51 4:710464cb44fc 2375 max7219.write_digit(2, i, 0x00); //device 2 current CMD, digits 1-4 ,data 0x01
viewdeep51 3:ae45e29f5d4f 2376 }
viewdeep51 3:ae45e29f5d4f 2377 wait_ms(100);
viewdeep51 0:ba33a62aea4e 2378 }
viewdeep51 0:ba33a62aea4e 2379
viewdeep51 3:ae45e29f5d4f 2380 void Spi2_init() // for max7219
viewdeep51 3:ae45e29f5d4f 2381 {
viewdeep51 3:ae45e29f5d4f 2382 _SPI2.format(16,0); //16bit,mode 0
viewdeep51 3:ae45e29f5d4f 2383 _SPI2.frequency(1000000); //1mhz
viewdeep51 3:ae45e29f5d4f 2384 wait_ms(1);
viewdeep51 0:ba33a62aea4e 2385 }
viewdeep51 0:ba33a62aea4e 2386
viewdeep51 3:ae45e29f5d4f 2387 void Discrete_init()
viewdeep51 3:ae45e29f5d4f 2388 {
viewdeep51 3:ae45e29f5d4f 2389 buzzer = ON;
viewdeep51 3:ae45e29f5d4f 2390 hooter = ON; //on
viewdeep51 3:ae45e29f5d4f 2391 wait(1); // one second delay
viewdeep51 3:ae45e29f5d4f 2392 buzzer = OFF;
viewdeep51 3:ae45e29f5d4f 2393 hooter = OFF; //off
viewdeep51 3:ae45e29f5d4f 2394 }
viewdeep51 3:ae45e29f5d4f 2395
viewdeep51 3:ae45e29f5d4f 2396 void Parameters_init()
viewdeep51 0:ba33a62aea4e 2397 {
viewdeep51 0:ba33a62aea4e 2398 /*intialization of all bool data types indicating different error flags */
viewdeep51 0:ba33a62aea4e 2399 pot_Error = false;
viewdeep51 3:ae45e29f5d4f 2400 device_ERR = false;
viewdeep51 0:ba33a62aea4e 2401 pot_MCR_err = false;
viewdeep51 0:ba33a62aea4e 2402 pot_ER_err = false;
viewdeep51 3:ae45e29f5d4f 2403 test = false;
viewdeep51 3:ae45e29f5d4f 2404 toggle = false;
viewdeep51 3:ae45e29f5d4f 2405 toggle_fast = false;
viewdeep51 3:ae45e29f5d4f 2406 display_flash = false;
viewdeep51 3:ae45e29f5d4f 2407 norm_op = false;
viewdeep51 0:ba33a62aea4e 2408
viewdeep51 0:ba33a62aea4e 2409 /*intialization of all bool data types indicating flag on key press interruption */
viewdeep51 3:ae45e29f5d4f 2410 key_CMD = false;
viewdeep51 0:ba33a62aea4e 2411 key_CTRL = false;
viewdeep51 0:ba33a62aea4e 2412 key_SET = false;
viewdeep51 3:ae45e29f5d4f 2413 REQ_key = false;
viewdeep51 3:ae45e29f5d4f 2414 ACK_key = false;
viewdeep51 0:ba33a62aea4e 2415 RPM_cmd = false; //if true RPM command is raised
viewdeep51 0:ba33a62aea4e 2416 DIR_cmd = false; //if true DIR command is raised
viewdeep51 3:ae45e29f5d4f 2417 ENTER_ACK_key = false;
viewdeep51 4:710464cb44fc 2418 disp_clear = false;
viewdeep51 0:ba33a62aea4e 2419
viewdeep51 0:ba33a62aea4e 2420 /*intialization of all bool data types indicating flags while serial receive */
viewdeep51 3:ae45e29f5d4f 2421 RPM_cmd_ack = false;
viewdeep51 0:ba33a62aea4e 2422 HB_ack_rcvd = false;
viewdeep51 0:ba33a62aea4e 2423 BROADCAST_rcvd = false;
viewdeep51 0:ba33a62aea4e 2424 HB_rcvd = false;
viewdeep51 0:ba33a62aea4e 2425 REQ_rcvd = false;
viewdeep51 0:ba33a62aea4e 2426 ACK_rcvd = false;
viewdeep51 3:ae45e29f5d4f 2427 time_RCVD = false;
viewdeep51 4:710464cb44fc 2428 data_fail = false;
viewdeep51 0:ba33a62aea4e 2429
viewdeep51 0:ba33a62aea4e 2430 /*intialization of all bool data types indicating flags while serial transmit */
viewdeep51 0:ba33a62aea4e 2431 BROADCAST_send = false;
viewdeep51 3:ae45e29f5d4f 2432 RPM_cmd_ack_send = false;
viewdeep51 3:ae45e29f5d4f 2433 DIR_cmd_ack_send = false;
viewdeep51 0:ba33a62aea4e 2434 REQ_send = false;
viewdeep51 0:ba33a62aea4e 2435 ACK_send = false;
viewdeep51 0:ba33a62aea4e 2436 HB_send = false;
viewdeep51 0:ba33a62aea4e 2437
viewdeep51 0:ba33a62aea4e 2438 /*intialization of all bool data types indicating active channel changeover*/
viewdeep51 0:ba33a62aea4e 2439 test=false;
viewdeep51 4:710464cb44fc 2440 disp_test = false;
viewdeep51 0:ba33a62aea4e 2441 ACH_SW_over = switch_CH = false;
viewdeep51 0:ba33a62aea4e 2442
viewdeep51 3:ae45e29f5d4f 2443 display_flash = false;
viewdeep51 0:ba33a62aea4e 2444
viewdeep51 0:ba33a62aea4e 2445 /*intialization of all int data types*/
viewdeep51 3:ae45e29f5d4f 2446 addr = req = ack = ach_S = ach_M = duty_cycle = sync_count = 0;
viewdeep51 0:ba33a62aea4e 2447 pot_Addr = addr_ID = send_ADDR = rcvd_ADDR = req_ADDR = ack_ADDR = 0;
viewdeep51 3:ae45e29f5d4f 2448 test_cnt = err_ID = err_Status = ticker_count = ticker_count_fast = current_status = current_status_dir= 0;
viewdeep51 4:710464cb44fc 2449 spi_error = set_bit = err_ALL = no_pot_connected = norm_count = Hrs_1 = Hrs_2 = Mins_1 = Mins_2 = 0;
viewdeep51 4:710464cb44fc 2450 checksum_send_byte = checksum_rcvd_byte = 0;
viewdeep51 4:710464cb44fc 2451
viewdeep51 5:f7b7ee0702cc 2452 //dir = last_dir = 0;
viewdeep51 0:ba33a62aea4e 2453 HB_timer =0;
viewdeep51 3:ae45e29f5d4f 2454 sync_time = 0;
viewdeep51 3:ae45e29f5d4f 2455 num_bytes = 0;
viewdeep51 0:ba33a62aea4e 2456
viewdeep51 0:ba33a62aea4e 2457 device_addr = -1;
viewdeep51 4:710464cb44fc 2458 valid = 'A';
viewdeep51 0:ba33a62aea4e 2459
viewdeep51 3:ae45e29f5d4f 2460 POT = new _POT[NO_MAX_POT];
viewdeep51 0:ba33a62aea4e 2461 }
viewdeep51 0:ba33a62aea4e 2462
viewdeep51 0:ba33a62aea4e 2463 void POT_INIT()
viewdeep51 0:ba33a62aea4e 2464 {
viewdeep51 3:ae45e29f5d4f 2465 Parameters_init();
viewdeep51 3:ae45e29f5d4f 2466 wait_ms(1);
viewdeep51 3:ae45e29f5d4f 2467
viewdeep51 3:ae45e29f5d4f 2468 Discrete_init();
viewdeep51 3:ae45e29f5d4f 2469 wait_ms(1);
viewdeep51 3:ae45e29f5d4f 2470
viewdeep51 3:ae45e29f5d4f 2471 Spi2_init();
viewdeep51 3:ae45e29f5d4f 2472 wait_ms(1);
viewdeep51 3:ae45e29f5d4f 2473
viewdeep51 0:ba33a62aea4e 2474 max7219_init();
viewdeep51 3:ae45e29f5d4f 2475 wait_ms(1);
viewdeep51 3:ae45e29f5d4f 2476
viewdeep51 3:ae45e29f5d4f 2477 SetDateTime(2017,11,16,16,45,40); // set default date and time
viewdeep51 3:ae45e29f5d4f 2478 wait_ms(1);
viewdeep51 3:ae45e29f5d4f 2479
viewdeep51 3:ae45e29f5d4f 2480 RTC_init();
viewdeep51 3:ae45e29f5d4f 2481 wait_ms(1);
viewdeep51 3:ae45e29f5d4f 2482
viewdeep51 3:ae45e29f5d4f 2483 Spi3_init();
viewdeep51 3:ae45e29f5d4f 2484 wait_ms(1);
viewdeep51 3:ae45e29f5d4f 2485
viewdeep51 0:ba33a62aea4e 2486 mcp23s17_init();
viewdeep51 3:ae45e29f5d4f 2487 wait_ms(1);
viewdeep51 3:ae45e29f5d4f 2488
viewdeep51 3:ae45e29f5d4f 2489 pwm_init();
viewdeep51 3:ae45e29f5d4f 2490 wait_ms(1);
viewdeep51 3:ae45e29f5d4f 2491
viewdeep51 3:ae45e29f5d4f 2492 Led_init();
viewdeep51 3:ae45e29f5d4f 2493 wait_ms(1);
viewdeep51 3:ae45e29f5d4f 2494
viewdeep51 3:ae45e29f5d4f 2495 Uart_init();
viewdeep51 3:ae45e29f5d4f 2496 wait_ms(1);
viewdeep51 3:ae45e29f5d4f 2497
viewdeep51 3:ae45e29f5d4f 2498 read_POT_ADDR();
viewdeep51 3:ae45e29f5d4f 2499 wait_ms(1);
viewdeep51 3:ae45e29f5d4f 2500
viewdeep51 3:ae45e29f5d4f 2501 interrupt_init();
viewdeep51 3:ae45e29f5d4f 2502 wait_ms(1);
viewdeep51 3:ae45e29f5d4f 2503
viewdeep51 3:ae45e29f5d4f 2504 if((addr_ID == pot_Master)&&(!pot_Error)) //if addres of master eot found, by default master
viewdeep51 4:710464cb44fc 2505 {
viewdeep51 4:710464cb44fc 2506 wait(10); //wait for 10sec,as MCU take time to read time from GPS
viewdeep51 3:ae45e29f5d4f 2507 led_ach[ach_M-1] = ON;
viewdeep51 3:ae45e29f5d4f 2508 int_A.rise(&read_KEY_A); //all key functions enabled for PORTA,B,C
viewdeep51 0:ba33a62aea4e 2509 int_B.rise(&read_KEY_B);
viewdeep51 3:ae45e29f5d4f 2510 int_C.rise(&read_KEY_C);
viewdeep51 3:ae45e29f5d4f 2511 wait_ms(2);
viewdeep51 3:ae45e29f5d4f 2512 mcp23s17_1.gpintena(0xff); //interrupts enabled
viewdeep51 3:ae45e29f5d4f 2513 wait_us(100);
viewdeep51 3:ae45e29f5d4f 2514 mcp23s17_1.gpintenb(0xff); //ff
viewdeep51 3:ae45e29f5d4f 2515 wait_us(100);
viewdeep51 3:ae45e29f5d4f 2516 HB_timer = 1.0/no_pot_connected;
viewdeep51 3:ae45e29f5d4f 2517 sync_time = 3600/HB_timer; // 3600.0/HB_timer; required
viewdeep51 3:ae45e29f5d4f 2518 pot_HB.attach(&HB_handler,HB_timer); //
viewdeep51 3:ae45e29f5d4f 2519 wait_ms(1);
viewdeep51 3:ae45e29f5d4f 2520 UART.attach( &read_SERIAL); //receive interrupt enabled
viewdeep51 3:ae45e29f5d4f 2521 wait_ms(1);
viewdeep51 3:ae45e29f5d4f 2522 ask_TIME(); //send enquiry to Central power unit for time setting
viewdeep51 3:ae45e29f5d4f 2523 wait_ms(2);
viewdeep51 3:ae45e29f5d4f 2524 send_ACH();
viewdeep51 3:ae45e29f5d4f 2525 wait_ms(2);
viewdeep51 0:ba33a62aea4e 2526 }
viewdeep51 3:ae45e29f5d4f 2527 else if(pot_Submaster <= addr_ID <= pot_Listner_ASP)
viewdeep51 3:ae45e29f5d4f 2528 {
viewdeep51 4:710464cb44fc 2529 wait(1);
viewdeep51 4:710464cb44fc 2530 led_ach[0] = ON; //by default master led turns on
viewdeep51 3:ae45e29f5d4f 2531 int_A.rise(NULL); //all key functions/interrupt disabled for PORTA,B
viewdeep51 3:ae45e29f5d4f 2532 int_B.rise(NULL);
viewdeep51 3:ae45e29f5d4f 2533 int_C.rise(&read_KEY_C); //only PORTC needs to be intialized in other all cases,inc,dec,tst,req
viewdeep51 3:ae45e29f5d4f 2534 wait_ms(1);
viewdeep51 3:ae45e29f5d4f 2535 UART.attach( &read_SERIAL); //receive interrupt enabled
viewdeep51 3:ae45e29f5d4f 2536 wait_ms(1);
viewdeep51 3:ae45e29f5d4f 2537 }
viewdeep51 3:ae45e29f5d4f 2538 else if(pot_Error)
viewdeep51 3:ae45e29f5d4f 2539 {
viewdeep51 3:ae45e29f5d4f 2540 int_A.rise(NULL);
viewdeep51 3:ae45e29f5d4f 2541 int_B.rise(NULL);
viewdeep51 3:ae45e29f5d4f 2542 int_C.rise(NULL);
viewdeep51 3:ae45e29f5d4f 2543 pot_Error = true;
viewdeep51 3:ae45e29f5d4f 2544 //return(-1);
viewdeep51 3:ae45e29f5d4f 2545 }
viewdeep51 3:ae45e29f5d4f 2546 else
viewdeep51 3:ae45e29f5d4f 2547 {
viewdeep51 3:ae45e29f5d4f 2548 __nop();
viewdeep51 0:ba33a62aea4e 2549 }
viewdeep51 0:ba33a62aea4e 2550 }
viewdeep51 0:ba33a62aea4e 2551
viewdeep51 0:ba33a62aea4e 2552 int main()
viewdeep51 0:ba33a62aea4e 2553 {
viewdeep51 0:ba33a62aea4e 2554 POT_INIT();
viewdeep51 3:ae45e29f5d4f 2555
viewdeep51 4:710464cb44fc 2556 led_refresh.attach(&update_led , 0.020); //refresh rate 20ms
viewdeep51 3:ae45e29f5d4f 2557 wait_ms(2);
viewdeep51 3:ae45e29f5d4f 2558
viewdeep51 3:ae45e29f5d4f 2559 RTC_ticker.attach(&read_RTC , 59); //after 60 sec
viewdeep51 3:ae45e29f5d4f 2560 wait_ms(2);
viewdeep51 3:ae45e29f5d4f 2561
viewdeep51 0:ba33a62aea4e 2562 while(1)
viewdeep51 0:ba33a62aea4e 2563 {
viewdeep51 0:ba33a62aea4e 2564 }
viewdeep51 0:ba33a62aea4e 2565 }