Index: fact/tools/hvMCUtest/ARDUINO_FTDI/ARDUINO_FTDI.pde
===================================================================
--- fact/tools/hvMCUtest/ARDUINO_FTDI/ARDUINO_FTDI.pde	(revision 12147)
+++ fact/tools/hvMCUtest/ARDUINO_FTDI/ARDUINO_FTDI.pde	(revision 12220)
@@ -28,4 +28,6 @@
 Port C consists of Arduino-Pins A0 - A5 and is used for Data exchange with the PLD
 */
+
+
 //define non transferable variables
 #define MAXBOARDS 13
@@ -40,55 +42,72 @@
 #define FREE2 A4
 
+//Default Values for Ramping
+#define RAMPTIME 15 //in millisec
+#define RAMPSTEP 46 // ~1V/STEP depends on voltage in 12 bit (e.g. 2^12 = 4096, 4069/90V ~ 46/V)  in Software
+///How do i know Vmax and can calculate RAMPSTEP?
+//are set on Defaultvalues but shell editable from pc software
+
+
 //#define MAXVOLTAGE 80
 
-typedef enum {
-  set_message,
-  send_message,
-  get_message,
-  pc_serial,
-  parsing
-} state_contr; //Type to define what microcontroller should do
-
-typedef enum {
-  channel_status,
-  sys_reset,
-  global_set,
-  channel_set
-} function_t; //Type to define which function of PLD shell be build
-
-typedef enum {
-  transmit_PLD_answer,
-  get_request,
-} communication; //Type to define communication with PC
-
-
+// Global variables
+    //Control Pins
 const unsigned char wr  =  2;
 const unsigned char rd  =  3;
 const unsigned char txe =  8;
 const unsigned char rxf =  9;
-//variables to save Data-Bytes in
-volatile unsigned char databus = 0x00; //temp. memory for Reading Databus interrupt
-unsigned char message[3];	//field where the 3 Byte message is saved
-unsigned char messageindex = 0;	//index of field
-unsigned char incommingByte = 0;  //for incomming serial Data
-unsigned int recent_voltages[NUMCHANNELS];  //The actual Voltage of each channel is saved in this variable
-unsigned int set_voltage;                   //The Value to which channel shell be set is saved in the variable
+
+    //variables to save Data-Bytes in
+volatile unsigned char databus = 0x00;      //temp. memory for Reading Databus interrupt
+unsigned char message[3]={0};	                //array where the 3 Byte message is saved
+unsigned char messageindex = 0;	            //index of array
+unsigned char incommingByte = 0;            //for incomming serial Data
+unsigned int recent_voltages[NUMCHANNELS] = {0};  //The actual Voltage of each channel is saved in this variable
+unsigned int set_voltage = 0;                   //The Value to which channel shell be set is saved in the variable
+unsigned int temp_voltage = 0;                  //temp. memory fot Value of Voltage
 boolean stat;                               //PLD Status-bit
-unsigned char wrapcounter;                  //Wrap counter:
-unsigned char channel;                      //channelnumber number
-
-function_t function;
-state_contr state_controler;
-communication pc_com = get_request;
-
-//unsigned char call_function;                //variable to call a funtion
-unsigned char error;                        //Errorcode from PLD
-unsigned char board;                        //board number
-//define Variables that mark edge-detection on RD and WR pin
+unsigned char wrapcounter = 0;                  //Wrap counter:
+unsigned char channel = 0;                      //channelnumber number
+unsigned char error = 0;                        //Errorcode from PLD
+unsigned char board = 0;                        //board number
+unsigned long loopstart = 0;
+unsigned long lastloop = 0;
+unsigned long loopduration = 0;
+    //define Variables that mark edge-detection on RD and WR pin for Interrupt
 volatile boolean RisingEdge_RD = false;
 volatile boolean risingEdge_WR = false;
 
+//Type definitions
+typedef enum {
+  set_message,
+  send_message,
+  get_message, //shell be defaultvalue, to await commands from PC
+  pc_serial,
+  parsing,
+} state_controler_t; //Type to define what microcontroller should do
+
+
+typedef enum {
+    channel_status,
+    sys_reset,
+    global_set,
+    channel_set,
+    ramp_channel,
+    ramp_global,
+    idle //defaultvalue
+} function_t; //Type to define which function of PLD shell be build
+
+typedef enum {
+  transmit_PLD_answer,
+  get_request,
+  transmit_channel_voltage,
+} communication_t; //Type to define communication with PC
+
+function_t function = idle;
+state_controler_t state_controler = pc_serial;
+communication_t pc_com = get_request;
+
+
 void setup() {
-    //delay(1000);
 //Initiate Controlpin Usage & Stati
 	pinMode( wr, INPUT);
@@ -98,17 +117,18 @@
 	digitalWrite(rxf, HIGH); //set initial state of RXF#
 	pinMode( rxf, OUTPUT);
+//Initiation of unused pins
 	digitalWrite(FREE1, HIGH);
     pinMode(FREE1, OUTPUT);
     pinMode(FREE2, INPUT);
-    //define initial I/O of pins of Port D to Input
-
+
+//Set Databus to Input
 MakeDataBusInput();  // Binary: 00000000, since the bus is Bidirectional .. we better set to input in the idle case
 
-//init test_values
-board = 0x09;
-channel = 0x0f;
-function = channel_set;
-set_voltage = 0xaaa;
-state_controler = pc_serial; //just for development
+//set test_values
+/// can be erased when commuinication works
+//board = 0x09;
+//channel = 0x0f;
+//function = channel_set;
+//set_voltage = 0xaaa;
 digitalWrite(FREE1, LOW);
 
@@ -123,11 +143,76 @@
 
 void loop() {
-// Communication with the PC, handle PC requests
-
-
-
-//Here the controller builds the message that will be send to PLD
-        PORTC ^= 1<<PC5;
-        PORTC ^= 1<<PC5;
+    //calculate duration for last loop
+    lastloop = loopstart;
+    loopstart = micros();
+    loopduration = loopstart - lastloop;
+//    Serial.print("Time: ");
+//    Serial.print(loopduration);
+//    Serial.println(" microseconds for loop");
+
+    // Communication with the PC, handle PC requests
+    // Communication via USB
+    if ( state_controler == pc_serial){
+        switch ( pc_com ){
+            //report PLD response to PC
+            case transmit_PLD_answer:
+                Serial.println("PLD answered: ");
+                for (messageindex = 0; messageindex < 3; messageindex++){
+                Serial.print(message[messageindex], BIN);
+                Serial.print( "\t" );
+                }
+                Serial.println(" in BIN ");
+                for (messageindex = 0; messageindex < 3; messageindex++){
+                Serial.print(message[messageindex], HEX);
+                Serial.print( "\t" );
+                }
+                messageindex = 0;
+                Serial.println(" in HEX ");
+                Serial.println( "_______________________ " );
+//                if (function == channel_set){
+//                    state_controler = set_message;
+//                }
+                pc_com = get_request;
+                break;
+
+            // Incoming Commands
+            case get_request:
+                while (Serial.available() < 3){};
+                if (Serial.available() > 0){
+                    Serial.print("I received: ");
+                    messageindex = 0;
+                    while (messageindex < 3){
+                    message[messageindex] = Serial.read();
+                    Serial.print( "0x" );
+                    Serial.print(message[messageindex], HEX);
+                    messageindex++;
+                    Serial.print( " " );
+                    }
+                    messageindex = 0;
+                    //Serial.print(incommingByte, BYTE);
+                    Serial.println(",du Nase!");
+                    }
+//                if (function == ramp_channel){
+//                    state_controler = set_message;
+//                    function = channel_set;
+//                    }
+                state_controler = send_message;
+                    //function = channel_set
+                break;
+
+//            case transmit_channel_voltage:
+//                Serial.println("Voltage of channel is: ");
+//                for (messageindex = 0; messageindex < 3; messageindex++){
+//                Serial.print(recent_voltages[messageindex], BIN);
+//                Serial.print( "\t" );
+//                }
+//            break;
+        }
+    }
+
+//The following part sends the 3-Byte-Command to PLD
+    //Build 3-Byte-Command that will be send to PLD
+        PORTC ^= 1<<PC5; //Marker
+        PORTC ^= 1<<PC5; //Marker
     if ( state_controler == set_message){
         switch( function ){
@@ -135,4 +220,5 @@
             case  channel_status:
                 channelStaus(board, channel);  //calls the channelstatus-function so values in message[] are set correctly
+                TransmitChannelVoltage(RecentChannel(board, channel));///verifizieren
             break;
 
@@ -142,9 +228,17 @@
 
             case global_set:
-                globalSet(set_voltage);       //calls the GlobalSet-function so values in message[] are set correctly
+                temp_voltage = RampGlobal(set_voltage);       //calls the GlobalSet-function so values in message[] are set correctly //globalSet nicht mehr VOID
+                for (int x = 0; x <= NUMCHANNELS; x++){ ///verifizieren
+                recent_voltages[x] = temp_voltage;
+                }
+                TransmitChannelVoltage(0);///verifizieren
             break;
 
             case channel_set:
-                channelSet(board, channel, set_voltage);  //calls the channelSet-function so values in message[] are set correctly
+                recent_voltages[RecentChannel(board, channel)] = RampChannel(board, channel, set_voltage);  //calls the RampChannel-function so values in message[] are set correctly
+                                                                                                            // and saves the new Voltage in recent Voltage
+                if (recent_voltages[RecentChannel(board, channel)] == set_voltage){
+                  function = idle;
+                }
             break;
 
@@ -152,15 +246,14 @@
     state_controler = send_message; //just for development
     }
+    //Set 3-Byte-Command on DATABUS
+    //This is the FIFO Read Cycle, PLD reads from FIFO, ARDUINO writes to FIFO
     if ( state_controler == send_message){
-        //Serial.print ("sending message");
-            //RisingEdge_RD = false;
-    //This is the FIFO Read Cycle, PLD reads from FIFO, ARDUINO writes to FIFO
-                PORTC ^= 1<<PC5;
-                PORTC ^= 1<<PC5;
-                PORTC ^= 1<<PC5;
-                PORTC ^= 1<<PC5;
+                PORTC ^= 1<<PC5; //Marker
+                PORTC ^= 1<<PC5; //Marker
+                PORTC ^= 1<<PC5; //Marker
+                PORTC ^= 1<<PC5; //Marker
             digitalWrite(txe, HIGH); //Forbid PLD to send Datan, needs 4 microsec
             MakeDataBusOutput();             //set Databus as Output before you tell PLD that you have Data
-             //This is the Statemachine where the 3 bytes will be set at PORTD
+             //This is the Statemachine where the 3 bytes will be set on DATA Bus
                 switch( messageindex ){
                     case 0:
@@ -193,7 +286,7 @@
                 }
                 RisingEdge_RD = false;
-
-    }
-
+    }
+
+//The following part gets the 3-Byte-Answer from PLD
 //The FIFO Write Cycle
     if ( state_controler == get_message){
@@ -201,5 +294,5 @@
     MakeDataBusInput(); //set Port D as Input
  	digitalWrite(txe, LOW );	//goes to low to tell PLD it can send Data
-        if (risingEdge_WR){  //write Data from Byte into FiFo when WR goes from high to low and there was an Rising Edge before
+        if (risingEdge_WR){     //write Data from Byte into FiFo when WR goes from high to low and there was an Rising Edge before
             switch( messageindex ){
                         case 0:
@@ -227,52 +320,4 @@
 
   // delay(1000); //slow down process for test pourpose
-
-// Communication with PC via USB
-    if ( state_controler == pc_serial){
-
-    // Outgoing Commands
-    switch ( pc_com ){
-        //report PLD response to PC
-        case transmit_PLD_answer:
-            Serial.println("PLD answered: ");
-            for (messageindex = 0; messageindex < 3; messageindex++){
-            Serial.print(message[messageindex], BIN);
-            Serial.print( "\t" );
-            }
-            Serial.println(" in BIN ");
-            for (messageindex = 0; messageindex < 3; messageindex++){
-            Serial.print(message[messageindex], HEX);
-            Serial.print( "\t" );
-            }
-            messageindex = 0;
-            Serial.println(" in HEX ");
-            Serial.println( "_______________________ " );
-            pc_com = get_request;
-            break;
-
-    // Incoming Commands
-        case get_request:
-            while (Serial.available() < 3){};
-            if (Serial.available() > 0){
-                Serial.print("I received: ");
-                messageindex = 0;
-                while (messageindex < 3){
-                message[messageindex] = Serial.read();
-                Serial.print( "0x" );
-                Serial.print(message[messageindex], HEX);
-                messageindex++;
-                Serial.print( " " );
-                }
-                messageindex = 0;
-                //Serial.print(incommingByte, BYTE);
-                Serial.println(",du Nase!");
-                }
-                state_controler = send_message;
-            break;
-
-    }
-
-    }
-
 }
 
@@ -341,12 +386,13 @@
 
 //Global set
-void globalSet(unsigned int volt){
+unsigned int globalSet(unsigned int volt){
   message[0] = GLOBALSETBYTE;
   message[1] = BLANKBYTE | (volt >> 8);
   message[2] = BLANKBYTE | volt;
+  return volt;
 }
 
 //Channel set to Voltage
-void channelSet(unsigned char brd, unsigned char chan, unsigned int volt){
+unsigned int channelSet(unsigned char brd, unsigned char chan, unsigned int volt){
   message[0] = CHANNELSETBYTE | (brd << 1);
   message[0] = message[0] | (chan >> 4);
@@ -354,4 +400,5 @@
   message[1] = message[1] | (volt >> 4);
   message[2] = BLANKBYTE | volt;
+  return volt;
 }
 
@@ -384,4 +431,58 @@
 
 }
+
+void TransmitChannelVoltage(int chan){ ///verifizieren
+    Serial.println("Voltage of channel is: ");
+    Serial.print(recent_voltages[chan], HEX);
+    Serial.print( "in HEX \t" );
+}
+
+int RecentChannel(unsigned char board, unsigned char channel){ ///verifizieren
+return ((0x000f&board)<<5)|(0x1f&channel);
+}
+
+unsigned int RampChannel(unsigned char brd, unsigned char chan, unsigned int volt){ ///verifizieren
+    if ((volt - recent_voltages[RecentChannel(brd, chan)]) > 0)         //ramp up
+    {
+        if (recent_voltages[RecentChannel(brd, chan)] + RAMPSTEP < volt){
+        volt = channelSet(brd, chan, (recent_voltages[RecentChannel(brd, chan)] + RAMPSTEP)); //ramps channel one Step to voltage and gives back voltagevalue
+        function = ramp_channel;
+        }
+        else{
+            volt = channelSet(brd, chan, volt);    //sets channel to chosen voltage ang gives back voltagevalue
+            function = idle; //goes back to idle status that ramping is not recalled
+        }
+    }
+    else if ((volt - recent_voltages[RecentChannel(brd, chan)]) < 0)    //ramp down
+    {
+        if (recent_voltages[RecentChannel(brd, chan)] - RAMPSTEP > volt){
+        volt = channelSet(brd, chan, (recent_voltages[RecentChannel(brd, chan)] - RAMPSTEP)); //ramps channel one Step to voltage and gives back voltagevalue
+        function = ramp_channel;
+        }
+        else{
+             volt = channelSet(brd, chan, volt);    //sets channel to chosen voltage
+             function = idle; //goes back to idle that ramping is not recalled
+        }
+    }
+    else function = channel_status; //goes back to channel status that to tell status
+    return volt;
+}
+
+unsigned int RampGlobal(unsigned int volt){ ///ich gehe zunächst mal davon aus, dass ich nur von Null rauf rampe
+//    for (unsigned int channel = 0; channel <= NUMCHANNELS; channel++){ //check if evvery channel is set to 0
+//    if (recent_voltages != 0x0) return;                                //otherwise leave function
+//    }
+    if ((volt - recent_voltages[0]) > 0 && recent_voltages[0] + RAMPSTEP < volt){  ///wie bekomme ich die Spannungen der einzelnen Kanäle
+        volt = globalSet(recent_voltages[0] + RAMPSTEP); //ramps all channels one Step to voltage and gives back voltagevalue
+        function = ramp_global;
+    }
+    else if ((volt - recent_voltages[0]) > 0 && recent_voltages[0] + RAMPSTEP > volt){
+        volt = globalSet(volt);    //sets channel to chosen voltage ang gives back voltagevalue
+        function = idle; //goes back to idle status that ramping is not recalled
+    }
+    return volt;
+}
+
+
 /*
 void CommandToPLD(unsigned int txe, unsigned int rxf ){
