Changeset 12220 for fact/tools


Ignore:
Timestamp:
10/20/11 17:17:43 (13 years ago)
Author:
neise
Message:
now with ramping
File:
1 edited

Legend:

Unmodified
Added
Removed
  • fact/tools/hvMCUtest/ARDUINO_FTDI/ARDUINO_FTDI.pde

    r12147 r12220  
    2828Port C consists of Arduino-Pins A0 - A5 and is used for Data exchange with the PLD
    2929*/
     30
     31
    3032//define non transferable variables
    3133#define MAXBOARDS 13
     
    4042#define FREE2 A4
    4143
     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
    4251//#define MAXVOLTAGE 80
    4352
    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
    6555const unsigned char wr  =  2;
    6656const unsigned char rd  =  3;
    6757const unsigned char txe =  8;
    6858const 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
     61volatile unsigned char databus = 0x00;      //temp. memory for Reading Databus interrupt
     62unsigned char message[3]={0};                   //array where the 3 Byte message is saved
     63unsigned char messageindex = 0;             //index of array
     64unsigned char incommingByte = 0;            //for incomming serial Data
     65unsigned int recent_voltages[NUMCHANNELS] = {0};  //The actual Voltage of each channel is saved in this variable
     66unsigned int set_voltage = 0;                   //The Value to which channel shell be set is saved in the variable
     67unsigned int temp_voltage = 0;                  //temp. memory fot Value of Voltage
    7668boolean 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
     69unsigned char wrapcounter = 0;                  //Wrap counter:
     70unsigned char channel = 0;                      //channelnumber number
     71unsigned char error = 0;                        //Errorcode from PLD
     72unsigned char board = 0;                        //board number
     73unsigned long loopstart = 0;
     74unsigned long lastloop = 0;
     75unsigned long loopduration = 0;
     76    //define Variables that mark edge-detection on RD and WR pin for Interrupt
    8877volatile boolean RisingEdge_RD = false;
    8978volatile boolean risingEdge_WR = false;
    9079
     80//Type definitions
     81typedef 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
     90typedef 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
     100typedef enum {
     101  transmit_PLD_answer,
     102  get_request,
     103  transmit_channel_voltage,
     104} communication_t; //Type to define communication with PC
     105
     106function_t function = idle;
     107state_controler_t state_controler = pc_serial;
     108communication_t pc_com = get_request;
     109
     110
    91111void setup() {
    92     //delay(1000);
    93112//Initiate Controlpin Usage & Stati
    94113        pinMode( wr, INPUT);
     
    98117        digitalWrite(rxf, HIGH); //set initial state of RXF#
    99118        pinMode( rxf, OUTPUT);
     119//Initiation of unused pins
    100120        digitalWrite(FREE1, HIGH);
    101121    pinMode(FREE1, OUTPUT);
    102122    pinMode(FREE2, INPUT);
    103     //define initial I/O of pins of Port D to Input
    104 
     123
     124//Set Databus to Input
    105125MakeDataBusInput();  // Binary: 00000000, since the bus is Bidirectional .. we better set to input in the idle case
    106126
    107 //init test_values
    108 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;
    113133digitalWrite(FREE1, LOW);
    114134
     
    123143
    124144void 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
    132217    if ( state_controler == set_message){
    133218        switch( function ){
     
    135220            case  channel_status:
    136221                channelStaus(board, channel);  //calls the channelstatus-function so values in message[] are set correctly
     222                TransmitChannelVoltage(RecentChannel(board, channel));///verifizieren
    137223            break;
    138224
     
    142228
    143229            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
    145235            break;
    146236
    147237            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                }
    149243            break;
    150244
     
    152246    state_controler = send_message; //just for development
    153247    }
     248    //Set 3-Byte-Command on DATABUS
     249    //This is the FIFO Read Cycle, PLD reads from FIFO, ARDUINO writes to FIFO
    154250    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
    162255            digitalWrite(txe, HIGH); //Forbid PLD to send Datan, needs 4 microsec
    163256            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 PORTD
     257             //This is the Statemachine where the 3 bytes will be set on DATA Bus
    165258                switch( messageindex ){
    166259                    case 0:
     
    193286                }
    194287                RisingEdge_RD = false;
    195 
    196     }
    197 
     288    }
     289
     290//The following part gets the 3-Byte-Answer from PLD
    198291//The FIFO Write Cycle
    199292    if ( state_controler == get_message){
     
    201294    MakeDataBusInput(); //set Port D as Input
    202295        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 before
     296        if (risingEdge_WR){     //write Data from Byte into FiFo when WR goes from high to low and there was an Rising Edge before
    204297            switch( messageindex ){
    205298                        case 0:
     
    227320
    228321  // delay(1000); //slow down process for test pourpose
    229 
    230 // Communication with PC via USB
    231     if ( state_controler == pc_serial){
    232 
    233     // Outgoing Commands
    234     switch ( pc_com ){
    235         //report PLD response to PC
    236         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 Commands
    254         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 
    277322}
    278323
     
    341386
    342387//Global set
    343 void globalSet(unsigned int volt){
     388unsigned int globalSet(unsigned int volt){
    344389  message[0] = GLOBALSETBYTE;
    345390  message[1] = BLANKBYTE | (volt >> 8);
    346391  message[2] = BLANKBYTE | volt;
     392  return volt;
    347393}
    348394
    349395//Channel set to Voltage
    350 void channelSet(unsigned char brd, unsigned char chan, unsigned int volt){
     396unsigned int channelSet(unsigned char brd, unsigned char chan, unsigned int volt){
    351397  message[0] = CHANNELSETBYTE | (brd << 1);
    352398  message[0] = message[0] | (chan >> 4);
     
    354400  message[1] = message[1] | (volt >> 4);
    355401  message[2] = BLANKBYTE | volt;
     402  return volt;
    356403}
    357404
     
    384431
    385432}
     433
     434void 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
     440int RecentChannel(unsigned char board, unsigned char channel){ ///verifizieren
     441return ((0x000f&board)<<5)|(0x1f&channel);
     442}
     443
     444unsigned 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
     471unsigned 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
    386487/*
    387488void CommandToPLD(unsigned int txe, unsigned int rxf ){
Note: See TracChangeset for help on using the changeset viewer.