Changeset 12220 for fact/tools/hvMCUtest/ARDUINO_FTDI
- Timestamp:
- 10/20/11 17:17:43 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
fact/tools/hvMCUtest/ARDUINO_FTDI/ARDUINO_FTDI.pde
r12147 r12220 28 28 Port C consists of Arduino-Pins A0 - A5 and is used for Data exchange with the PLD 29 29 */ 30 31 30 32 //define non transferable variables 31 33 #define MAXBOARDS 13 … … 40 42 #define FREE2 A4 41 43 44 //Default Values for Ramping 45 #define RAMPTIME 15 //in millisec 46 #define RAMPSTEP 46 // ~1V/STEP depends on voltage in 12 bit (e.g. 2^12 = 4096, 4069/90V ~ 46/V) in Software 47 ///How do i know Vmax and can calculate RAMPSTEP? 48 //are set on Defaultvalues but shell editable from pc software 49 50 42 51 //#define MAXVOLTAGE 80 43 52 44 typedef enum { 45 set_message, 46 send_message, 47 get_message, 48 pc_serial, 49 parsing 50 } state_contr; //Type to define what microcontroller should do 51 52 typedef enum { 53 channel_status, 54 sys_reset, 55 global_set, 56 channel_set 57 } function_t; //Type to define which function of PLD shell be build 58 59 typedef enum { 60 transmit_PLD_answer, 61 get_request, 62 } communication; //Type to define communication with PC 63 64 53 // Global variables 54 //Control Pins 65 55 const unsigned char wr = 2; 66 56 const unsigned char rd = 3; 67 57 const unsigned char txe = 8; 68 58 const unsigned char rxf = 9; 69 //variables to save Data-Bytes in 70 volatile unsigned char databus = 0x00; //temp. memory for Reading Databus interrupt 71 unsigned char message[3]; //field where the 3 Byte message is saved 72 unsigned char messageindex = 0; //index of field 73 unsigned char incommingByte = 0; //for incomming serial Data 74 unsigned int recent_voltages[NUMCHANNELS]; //The actual Voltage of each channel is saved in this variable 75 unsigned int set_voltage; //The Value to which channel shell be set is saved in the variable 59 60 //variables to save Data-Bytes in 61 volatile unsigned char databus = 0x00; //temp. memory for Reading Databus interrupt 62 unsigned char message[3]={0}; //array where the 3 Byte message is saved 63 unsigned char messageindex = 0; //index of array 64 unsigned char incommingByte = 0; //for incomming serial Data 65 unsigned int recent_voltages[NUMCHANNELS] = {0}; //The actual Voltage of each channel is saved in this variable 66 unsigned int set_voltage = 0; //The Value to which channel shell be set is saved in the variable 67 unsigned int temp_voltage = 0; //temp. memory fot Value of Voltage 76 68 boolean stat; //PLD Status-bit 77 unsigned char wrapcounter; //Wrap counter: 78 unsigned char channel; //channelnumber number 79 80 function_t function; 81 state_contr state_controler; 82 communication pc_com = get_request; 83 84 //unsigned char call_function; //variable to call a funtion 85 unsigned char error; //Errorcode from PLD 86 unsigned char board; //board number 87 //define Variables that mark edge-detection on RD and WR pin 69 unsigned char wrapcounter = 0; //Wrap counter: 70 unsigned char channel = 0; //channelnumber number 71 unsigned char error = 0; //Errorcode from PLD 72 unsigned char board = 0; //board number 73 unsigned long loopstart = 0; 74 unsigned long lastloop = 0; 75 unsigned long loopduration = 0; 76 //define Variables that mark edge-detection on RD and WR pin for Interrupt 88 77 volatile boolean RisingEdge_RD = false; 89 78 volatile boolean risingEdge_WR = false; 90 79 80 //Type definitions 81 typedef enum { 82 set_message, 83 send_message, 84 get_message, //shell be defaultvalue, to await commands from PC 85 pc_serial, 86 parsing, 87 } state_controler_t; //Type to define what microcontroller should do 88 89 90 typedef enum { 91 channel_status, 92 sys_reset, 93 global_set, 94 channel_set, 95 ramp_channel, 96 ramp_global, 97 idle //defaultvalue 98 } function_t; //Type to define which function of PLD shell be build 99 100 typedef enum { 101 transmit_PLD_answer, 102 get_request, 103 transmit_channel_voltage, 104 } communication_t; //Type to define communication with PC 105 106 function_t function = idle; 107 state_controler_t state_controler = pc_serial; 108 communication_t pc_com = get_request; 109 110 91 111 void setup() { 92 //delay(1000);93 112 //Initiate Controlpin Usage & Stati 94 113 pinMode( wr, INPUT); … … 98 117 digitalWrite(rxf, HIGH); //set initial state of RXF# 99 118 pinMode( rxf, OUTPUT); 119 //Initiation of unused pins 100 120 digitalWrite(FREE1, HIGH); 101 121 pinMode(FREE1, OUTPUT); 102 122 pinMode(FREE2, INPUT); 103 //define initial I/O of pins of Port D to Input 104 123 124 //Set Databus to Input 105 125 MakeDataBusInput(); // Binary: 00000000, since the bus is Bidirectional .. we better set to input in the idle case 106 126 107 // init test_values108 board = 0x09; 109 channel = 0x0f;110 function = channel_set;111 set_voltage = 0xaaa;112 state_controler = pc_serial; //just for development 127 //set test_values 128 /// can be erased when commuinication works 129 //board = 0x09; 130 //channel = 0x0f; 131 //function = channel_set; 132 //set_voltage = 0xaaa; 113 133 digitalWrite(FREE1, LOW); 114 134 … … 123 143 124 144 void loop() { 125 // Communication with the PC, handle PC requests 126 127 128 129 //Here the controller builds the message that will be send to PLD 130 PORTC ^= 1<<PC5; 131 PORTC ^= 1<<PC5; 145 //calculate duration for last loop 146 lastloop = loopstart; 147 loopstart = micros(); 148 loopduration = loopstart - lastloop; 149 // Serial.print("Time: "); 150 // Serial.print(loopduration); 151 // Serial.println(" microseconds for loop"); 152 153 // Communication with the PC, handle PC requests 154 // Communication via USB 155 if ( state_controler == pc_serial){ 156 switch ( pc_com ){ 157 //report PLD response to PC 158 case transmit_PLD_answer: 159 Serial.println("PLD answered: "); 160 for (messageindex = 0; messageindex < 3; messageindex++){ 161 Serial.print(message[messageindex], BIN); 162 Serial.print( "\t" ); 163 } 164 Serial.println(" in BIN "); 165 for (messageindex = 0; messageindex < 3; messageindex++){ 166 Serial.print(message[messageindex], HEX); 167 Serial.print( "\t" ); 168 } 169 messageindex = 0; 170 Serial.println(" in HEX "); 171 Serial.println( "_______________________ " ); 172 // if (function == channel_set){ 173 // state_controler = set_message; 174 // } 175 pc_com = get_request; 176 break; 177 178 // Incoming Commands 179 case get_request: 180 while (Serial.available() < 3){}; 181 if (Serial.available() > 0){ 182 Serial.print("I received: "); 183 messageindex = 0; 184 while (messageindex < 3){ 185 message[messageindex] = Serial.read(); 186 Serial.print( "0x" ); 187 Serial.print(message[messageindex], HEX); 188 messageindex++; 189 Serial.print( " " ); 190 } 191 messageindex = 0; 192 //Serial.print(incommingByte, BYTE); 193 Serial.println(",du Nase!"); 194 } 195 // if (function == ramp_channel){ 196 // state_controler = set_message; 197 // function = channel_set; 198 // } 199 state_controler = send_message; 200 //function = channel_set 201 break; 202 203 // case transmit_channel_voltage: 204 // Serial.println("Voltage of channel is: "); 205 // for (messageindex = 0; messageindex < 3; messageindex++){ 206 // Serial.print(recent_voltages[messageindex], BIN); 207 // Serial.print( "\t" ); 208 // } 209 // break; 210 } 211 } 212 213 //The following part sends the 3-Byte-Command to PLD 214 //Build 3-Byte-Command that will be send to PLD 215 PORTC ^= 1<<PC5; //Marker 216 PORTC ^= 1<<PC5; //Marker 132 217 if ( state_controler == set_message){ 133 218 switch( function ){ … … 135 220 case channel_status: 136 221 channelStaus(board, channel); //calls the channelstatus-function so values in message[] are set correctly 222 TransmitChannelVoltage(RecentChannel(board, channel));///verifizieren 137 223 break; 138 224 … … 142 228 143 229 case global_set: 144 globalSet(set_voltage); //calls the GlobalSet-function so values in message[] are set correctly 230 temp_voltage = RampGlobal(set_voltage); //calls the GlobalSet-function so values in message[] are set correctly //globalSet nicht mehr VOID 231 for (int x = 0; x <= NUMCHANNELS; x++){ ///verifizieren 232 recent_voltages[x] = temp_voltage; 233 } 234 TransmitChannelVoltage(0);///verifizieren 145 235 break; 146 236 147 237 case channel_set: 148 channelSet(board, channel, set_voltage); //calls the channelSet-function so values in message[] are set correctly 238 recent_voltages[RecentChannel(board, channel)] = RampChannel(board, channel, set_voltage); //calls the RampChannel-function so values in message[] are set correctly 239 // and saves the new Voltage in recent Voltage 240 if (recent_voltages[RecentChannel(board, channel)] == set_voltage){ 241 function = idle; 242 } 149 243 break; 150 244 … … 152 246 state_controler = send_message; //just for development 153 247 } 248 //Set 3-Byte-Command on DATABUS 249 //This is the FIFO Read Cycle, PLD reads from FIFO, ARDUINO writes to FIFO 154 250 if ( state_controler == send_message){ 155 //Serial.print ("sending message"); 156 //RisingEdge_RD = false; 157 //This is the FIFO Read Cycle, PLD reads from FIFO, ARDUINO writes to FIFO 158 PORTC ^= 1<<PC5; 159 PORTC ^= 1<<PC5; 160 PORTC ^= 1<<PC5; 161 PORTC ^= 1<<PC5; 251 PORTC ^= 1<<PC5; //Marker 252 PORTC ^= 1<<PC5; //Marker 253 PORTC ^= 1<<PC5; //Marker 254 PORTC ^= 1<<PC5; //Marker 162 255 digitalWrite(txe, HIGH); //Forbid PLD to send Datan, needs 4 microsec 163 256 MakeDataBusOutput(); //set Databus as Output before you tell PLD that you have Data 164 //This is the Statemachine where the 3 bytes will be set at PORTD257 //This is the Statemachine where the 3 bytes will be set on DATA Bus 165 258 switch( messageindex ){ 166 259 case 0: … … 193 286 } 194 287 RisingEdge_RD = false; 195 196 } 197 288 } 289 290 //The following part gets the 3-Byte-Answer from PLD 198 291 //The FIFO Write Cycle 199 292 if ( state_controler == get_message){ … … 201 294 MakeDataBusInput(); //set Port D as Input 202 295 digitalWrite(txe, LOW ); //goes to low to tell PLD it can send Data 203 if (risingEdge_WR){ //write Data from Byte into FiFo when WR goes from high to low and there was an Rising Edge before296 if (risingEdge_WR){ //write Data from Byte into FiFo when WR goes from high to low and there was an Rising Edge before 204 297 switch( messageindex ){ 205 298 case 0: … … 227 320 228 321 // delay(1000); //slow down process for test pourpose 229 230 // Communication with PC via USB231 if ( state_controler == pc_serial){232 233 // Outgoing Commands234 switch ( pc_com ){235 //report PLD response to PC236 case transmit_PLD_answer:237 Serial.println("PLD answered: ");238 for (messageindex = 0; messageindex < 3; messageindex++){239 Serial.print(message[messageindex], BIN);240 Serial.print( "\t" );241 }242 Serial.println(" in BIN ");243 for (messageindex = 0; messageindex < 3; messageindex++){244 Serial.print(message[messageindex], HEX);245 Serial.print( "\t" );246 }247 messageindex = 0;248 Serial.println(" in HEX ");249 Serial.println( "_______________________ " );250 pc_com = get_request;251 break;252 253 // Incoming Commands254 case get_request:255 while (Serial.available() < 3){};256 if (Serial.available() > 0){257 Serial.print("I received: ");258 messageindex = 0;259 while (messageindex < 3){260 message[messageindex] = Serial.read();261 Serial.print( "0x" );262 Serial.print(message[messageindex], HEX);263 messageindex++;264 Serial.print( " " );265 }266 messageindex = 0;267 //Serial.print(incommingByte, BYTE);268 Serial.println(",du Nase!");269 }270 state_controler = send_message;271 break;272 273 }274 275 }276 277 322 } 278 323 … … 341 386 342 387 //Global set 343 voidglobalSet(unsigned int volt){388 unsigned int globalSet(unsigned int volt){ 344 389 message[0] = GLOBALSETBYTE; 345 390 message[1] = BLANKBYTE | (volt >> 8); 346 391 message[2] = BLANKBYTE | volt; 392 return volt; 347 393 } 348 394 349 395 //Channel set to Voltage 350 voidchannelSet(unsigned char brd, unsigned char chan, unsigned int volt){396 unsigned int channelSet(unsigned char brd, unsigned char chan, unsigned int volt){ 351 397 message[0] = CHANNELSETBYTE | (brd << 1); 352 398 message[0] = message[0] | (chan >> 4); … … 354 400 message[1] = message[1] | (volt >> 4); 355 401 message[2] = BLANKBYTE | volt; 402 return volt; 356 403 } 357 404 … … 384 431 385 432 } 433 434 void TransmitChannelVoltage(int chan){ ///verifizieren 435 Serial.println("Voltage of channel is: "); 436 Serial.print(recent_voltages[chan], HEX); 437 Serial.print( "in HEX \t" ); 438 } 439 440 int RecentChannel(unsigned char board, unsigned char channel){ ///verifizieren 441 return ((0x000f&board)<<5)|(0x1f&channel); 442 } 443 444 unsigned int RampChannel(unsigned char brd, unsigned char chan, unsigned int volt){ ///verifizieren 445 if ((volt - recent_voltages[RecentChannel(brd, chan)]) > 0) //ramp up 446 { 447 if (recent_voltages[RecentChannel(brd, chan)] + RAMPSTEP < volt){ 448 volt = channelSet(brd, chan, (recent_voltages[RecentChannel(brd, chan)] + RAMPSTEP)); //ramps channel one Step to voltage and gives back voltagevalue 449 function = ramp_channel; 450 } 451 else{ 452 volt = channelSet(brd, chan, volt); //sets channel to chosen voltage ang gives back voltagevalue 453 function = idle; //goes back to idle status that ramping is not recalled 454 } 455 } 456 else if ((volt - recent_voltages[RecentChannel(brd, chan)]) < 0) //ramp down 457 { 458 if (recent_voltages[RecentChannel(brd, chan)] - RAMPSTEP > volt){ 459 volt = channelSet(brd, chan, (recent_voltages[RecentChannel(brd, chan)] - RAMPSTEP)); //ramps channel one Step to voltage and gives back voltagevalue 460 function = ramp_channel; 461 } 462 else{ 463 volt = channelSet(brd, chan, volt); //sets channel to chosen voltage 464 function = idle; //goes back to idle that ramping is not recalled 465 } 466 } 467 else function = channel_status; //goes back to channel status that to tell status 468 return volt; 469 } 470 471 unsigned int RampGlobal(unsigned int volt){ ///ich gehe zunächst mal davon aus, dass ich nur von Null rauf rampe 472 // for (unsigned int channel = 0; channel <= NUMCHANNELS; channel++){ //check if evvery channel is set to 0 473 // if (recent_voltages != 0x0) return; //otherwise leave function 474 // } 475 if ((volt - recent_voltages[0]) > 0 && recent_voltages[0] + RAMPSTEP < volt){ ///wie bekomme ich die Spannungen der einzelnen Kanäle 476 volt = globalSet(recent_voltages[0] + RAMPSTEP); //ramps all channels one Step to voltage and gives back voltagevalue 477 function = ramp_global; 478 } 479 else if ((volt - recent_voltages[0]) > 0 && recent_voltages[0] + RAMPSTEP > volt){ 480 volt = globalSet(volt); //sets channel to chosen voltage ang gives back voltagevalue 481 function = idle; //goes back to idle status that ramping is not recalled 482 } 483 return volt; 484 } 485 486 386 487 /* 387 488 void CommandToPLD(unsigned int txe, unsigned int rxf ){
Note:
See TracChangeset
for help on using the changeset viewer.