direction commands updated to Up down RPM value

Dependencies:   MAX7219pot MCP23S17 mbed

Fork of POT_V_1_0 by Marine Electricals

Committer:
viewdeep51
Date:
Sat Jan 13 11:49:18 2018 +0000
Revision:
3:ae45e29f5d4f
Parent:
1:e116808d8b00
Child:
4:710464cb44fc
13/01/2018

Who changed what in which revision?

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