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 Apr 28 10:22:22 2018 +0000
Revision:
4:710464cb44fc
Parent:
3:ae45e29f5d4f
Child:
5:f7b7ee0702cc
POT final code dated 28/04/18

Who changed what in which revision?

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