test

Dependencies:   EthernetInterface MODSERIAL mbed-rtos mbed

Fork of UDPEchoServer by Mbed

Committer:
vibe
Date:
Fri Sep 07 10:44:11 2018 +0000
Revision:
8:115dc06a8764
Parent:
7:42bc53611fc7
test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vibe 7:42bc53611fc7 1 #include "mbed.h"
vibe 7:42bc53611fc7 2 #include "IO.h"
vibe 7:42bc53611fc7 3
vibe 7:42bc53611fc7 4 extern I2C I2CIO; // I2C DigIOs onBoard
vibe 7:42bc53611fc7 5 extern int IOBase_IOs; // I2C DigIOs onBoard
vibe 7:42bc53611fc7 6 extern SPI spi; // DA-Wandler mosi, miso, sclk
vibe 7:42bc53611fc7 7
vibe 7:42bc53611fc7 8 extern DigitalOut DAC_CS1; // DA-Wandler CS
vibe 7:42bc53611fc7 9 extern DigitalOut DAC_CS2; // DA-Wandler CS
vibe 7:42bc53611fc7 10 extern DigitalOut DAC_CS3; // DA-Wandler CS
vibe 7:42bc53611fc7 11 extern DigitalOut DAC_CS4; // DA-Wandler CS
vibe 7:42bc53611fc7 12 extern DigitalOut RS485_DE; // RS485 DataOutEnabled
vibe 7:42bc53611fc7 13
vibe 7:42bc53611fc7 14 extern AnalogIn ain1; //
vibe 7:42bc53611fc7 15 extern AnalogIn ain2; //
vibe 7:42bc53611fc7 16 extern AnalogIn ain3; //
vibe 7:42bc53611fc7 17 extern AnalogIn ain4; //
vibe 7:42bc53611fc7 18
vibe 7:42bc53611fc7 19 extern double U_Analog1;
vibe 7:42bc53611fc7 20 extern double U_Analog2;
vibe 7:42bc53611fc7 21 extern double U_Analog3;
vibe 7:42bc53611fc7 22 extern double U_Analog4;
vibe 7:42bc53611fc7 23
vibe 7:42bc53611fc7 24 double Data1Int[100];
vibe 7:42bc53611fc7 25 double Data2Int[100];
vibe 7:42bc53611fc7 26 double Data3Int[100];
vibe 7:42bc53611fc7 27 double Data4Int[100];
vibe 7:42bc53611fc7 28 double Data5Int[100];
vibe 7:42bc53611fc7 29
vibe 7:42bc53611fc7 30
vibe 7:42bc53611fc7 31 //##################################################################################
vibe 7:42bc53611fc7 32 // Init_I2C_IOs
vibe 7:42bc53611fc7 33 //##################################################################################
vibe 7:42bc53611fc7 34 int IO::Init_I2C_IOs()
vibe 7:42bc53611fc7 35 {
vibe 7:42bc53611fc7 36 char cmd[1] = {255};
vibe 7:42bc53611fc7 37 I2CIO.frequency (100000);
vibe 7:42bc53611fc7 38 int addr = 0x01;
vibe 7:42bc53611fc7 39 addr = addr << 1;
vibe 7:42bc53611fc7 40 addr = addr + IOBase_IOs;
vibe 7:42bc53611fc7 41 cmd[0] = 255;
vibe 7:42bc53611fc7 42 I2CIO.write(addr, cmd, 1);
vibe 7:42bc53611fc7 43
vibe 7:42bc53611fc7 44 addr = 0x02;
vibe 7:42bc53611fc7 45 addr = addr << 1;
vibe 7:42bc53611fc7 46 addr = addr + IOBase_IOs;
vibe 7:42bc53611fc7 47 cmd[0] = 255;
vibe 7:42bc53611fc7 48 I2CIO.write(addr, cmd, 1);
vibe 7:42bc53611fc7 49
vibe 7:42bc53611fc7 50 addr = 0x00;
vibe 7:42bc53611fc7 51 addr = addr << 1;
vibe 7:42bc53611fc7 52 addr = addr + IOBase_IOs;
vibe 7:42bc53611fc7 53 cmd[0] = 0;
vibe 7:42bc53611fc7 54 I2CIO.read(addr, cmd, 1);
vibe 7:42bc53611fc7 55
vibe 7:42bc53611fc7 56 int tmp = (int (cmd[0]));
vibe 7:42bc53611fc7 57 return tmp;
vibe 7:42bc53611fc7 58 }
vibe 7:42bc53611fc7 59
vibe 7:42bc53611fc7 60 //##################################################################################
vibe 7:42bc53611fc7 61 // Set_Outputs
vibe 7:42bc53611fc7 62 //##################################################################################
vibe 7:42bc53611fc7 63 int IO::WR_Output(int addr,char sr)
vibe 7:42bc53611fc7 64 {
vibe 7:42bc53611fc7 65 addr = addr << 1;
vibe 7:42bc53611fc7 66 addr = addr + IOBase_IOs;
vibe 7:42bc53611fc7 67 char cmd[1] = {0};
vibe 7:42bc53611fc7 68 sr=~sr;
vibe 7:42bc53611fc7 69 cmd[0] += sr;
vibe 7:42bc53611fc7 70 I2CIO.write(addr, cmd, 1);
vibe 7:42bc53611fc7 71 int tmp = (int (cmd[0]));
vibe 7:42bc53611fc7 72 tmp=~tmp;
vibe 7:42bc53611fc7 73 return tmp;
vibe 7:42bc53611fc7 74 }
vibe 7:42bc53611fc7 75
vibe 7:42bc53611fc7 76 //##################################################################################
vibe 7:42bc53611fc7 77 // RD_Inputs
vibe 7:42bc53611fc7 78 //##################################################################################
vibe 7:42bc53611fc7 79 int IO::RD_Input(int addr)
vibe 7:42bc53611fc7 80 {
vibe 7:42bc53611fc7 81 //int addr = 0x00;
vibe 7:42bc53611fc7 82 addr = addr << 1;
vibe 7:42bc53611fc7 83 addr = addr + 0x01 + IOBase_IOs;
vibe 7:42bc53611fc7 84 char cmd[1] = {0x0};
vibe 7:42bc53611fc7 85 I2CIO.read(addr, cmd, 1);
vibe 7:42bc53611fc7 86 int tmp = (int (cmd[0]));
vibe 7:42bc53611fc7 87 tmp=~tmp;
vibe 7:42bc53611fc7 88 return tmp;
vibe 7:42bc53611fc7 89 }
vibe 7:42bc53611fc7 90
vibe 7:42bc53611fc7 91 //#############################################################################
vibe 7:42bc53611fc7 92 // DA-Wandler A
vibe 7:42bc53611fc7 93 //#############################################################################
vibe 7:42bc53611fc7 94 void IO::WR_DARegA(int v)
vibe 7:42bc53611fc7 95 {
vibe 7:42bc53611fc7 96 v = v & 0x0FFF;
vibe 7:42bc53611fc7 97 int word = v | 0x1000; // x2
vibe 7:42bc53611fc7 98 DAC_CS1 = false;
vibe 7:42bc53611fc7 99 wait_us(100);
vibe 7:42bc53611fc7 100 spi.write(word);
vibe 7:42bc53611fc7 101 wait_us(100);
vibe 7:42bc53611fc7 102 DAC_CS1 = true;
vibe 7:42bc53611fc7 103 }
vibe 7:42bc53611fc7 104
vibe 7:42bc53611fc7 105 //##################################################################################
vibe 7:42bc53611fc7 106 // DA-Wandler B
vibe 7:42bc53611fc7 107 //##################################################################################
vibe 7:42bc53611fc7 108 void IO::WR_DARegB(int v)
vibe 7:42bc53611fc7 109 {
vibe 7:42bc53611fc7 110 v = v & 0x0FFF;
vibe 7:42bc53611fc7 111 int word = v | 0x9000; // x2 A-GS109876543210
vibe 7:42bc53611fc7 112 DAC_CS1 = false;
vibe 7:42bc53611fc7 113 wait_us(100);
vibe 7:42bc53611fc7 114 spi.write(word);
vibe 7:42bc53611fc7 115 wait_us(100);
vibe 7:42bc53611fc7 116 DAC_CS1 = true;
vibe 7:42bc53611fc7 117 }
vibe 7:42bc53611fc7 118
vibe 7:42bc53611fc7 119 //#############################################################################
vibe 7:42bc53611fc7 120 // meanValue
vibe 7:42bc53611fc7 121 //#############################################################################
vibe 7:42bc53611fc7 122 double meanValue(double *samples, int n)
vibe 7:42bc53611fc7 123 {
vibe 7:42bc53611fc7 124 double sum = 0.0;
vibe 7:42bc53611fc7 125 for (int i=0; i<n; i++) { sum += samples[i]; }
vibe 7:42bc53611fc7 126 return sum / (double)n;
vibe 7:42bc53611fc7 127 }
vibe 7:42bc53611fc7 128
vibe 7:42bc53611fc7 129 double maxValue(double *samples, int n)
vibe 7:42bc53611fc7 130 {
vibe 7:42bc53611fc7 131 double max = 0.0;
vibe 7:42bc53611fc7 132 for (int i=0; i<n; i++)
vibe 7:42bc53611fc7 133 {
vibe 7:42bc53611fc7 134 if (samples[i]>max) { max=samples[i]; }
vibe 7:42bc53611fc7 135 }
vibe 7:42bc53611fc7 136 return max;
vibe 7:42bc53611fc7 137 }
vibe 7:42bc53611fc7 138
vibe 7:42bc53611fc7 139 //#############################################################################
vibe 7:42bc53611fc7 140 // LED-Strom, Externer Sollwert, Lichtmengen-Messung (Kalibrierung)
vibe 7:42bc53611fc7 141 //#############################################################################
vibe 7:42bc53611fc7 142 void IO::GetAnalogValues()
vibe 7:42bc53611fc7 143 { int anz=50;
vibe 7:42bc53611fc7 144 for (int x=anz-1;x>0;x--) { Data1Int[x]=Data1Int[x-1]; }
vibe 7:42bc53611fc7 145 Data1Int[0]= ain1.read()* 3.3f;
vibe 7:42bc53611fc7 146 U_Analog1 = meanValue(Data1Int,anz);
vibe 7:42bc53611fc7 147
vibe 7:42bc53611fc7 148 for (int x=anz-1;x>0;x--) { Data2Int[x]=Data2Int[x-1]; }
vibe 7:42bc53611fc7 149 Data2Int[0]= ain2.read()* 3.3f;
vibe 7:42bc53611fc7 150 U_Analog2 = meanValue(Data2Int,anz);
vibe 7:42bc53611fc7 151
vibe 7:42bc53611fc7 152 for (int x=anz-1;x>0;x--) { Data3Int[x]=Data3Int[x-1]; }
vibe 7:42bc53611fc7 153 Data3Int[0]= ain3.read()* 3.3f;
vibe 7:42bc53611fc7 154 U_Analog3 = meanValue(Data3Int,anz);
vibe 7:42bc53611fc7 155
vibe 7:42bc53611fc7 156 for (int x=anz-1;x>0;x--) { Data4Int[x]=Data4Int[x-1]; }
vibe 7:42bc53611fc7 157 Data4Int[0]= ain4.read()* 3.3f;
vibe 7:42bc53611fc7 158 U_Analog4 = meanValue(Data4Int,anz);
vibe 7:42bc53611fc7 159 }
vibe 7:42bc53611fc7 160
vibe 7:42bc53611fc7 161 //#####################################################################################
vibe 7:42bc53611fc7 162 // Save_Parameter
vibe 7:42bc53611fc7 163 //#####################################################################################
vibe 7:42bc53611fc7 164 void IO::Save_Parameter()
vibe 7:42bc53611fc7 165 {
vibe 7:42bc53611fc7 166 FILE *fp = fopen("/local/Config.dat", "w"); // Open the local file system for writing
vibe 7:42bc53611fc7 167 // fprintf(fp,"USB : %1d\r\n",USBon);
vibe 7:42bc53611fc7 168 // fprintf(fp,"RS232: %1d\r\n",RS_on);
vibe 7:42bc53611fc7 169 fclose(fp);
vibe 7:42bc53611fc7 170 }
vibe 7:42bc53611fc7 171
vibe 7:42bc53611fc7 172 //#####################################################################################
vibe 7:42bc53611fc7 173 // Load_Parameter
vibe 7:42bc53611fc7 174 //#####################################################################################
vibe 7:42bc53611fc7 175 void IO::Load_Parameter()
vibe 7:42bc53611fc7 176 {
vibe 7:42bc53611fc7 177 int c;
vibe 7:42bc53611fc7 178 int ti;
vibe 7:42bc53611fc7 179 FILE *fp = fopen("/local/Config.dat", "r"); // Open the local file system for reading
vibe 7:42bc53611fc7 180 if(fp==NULL) { Save_Parameter(); }
vibe 7:42bc53611fc7 181 else
vibe 7:42bc53611fc7 182 {
vibe 7:42bc53611fc7 183 do { c = fgetc (fp); }
vibe 7:42bc53611fc7 184 while (c != ':' && c != EOF);
vibe 7:42bc53611fc7 185 fscanf(fp,"%d",&ti);
vibe 7:42bc53611fc7 186 // USBon=ti;
vibe 7:42bc53611fc7 187 do { c = fgetc (fp); }
vibe 7:42bc53611fc7 188 while (c != ':' && c != EOF);
vibe 7:42bc53611fc7 189 fscanf(fp,"%d",&ti);
vibe 7:42bc53611fc7 190 // RS_on=ti;
vibe 7:42bc53611fc7 191 fclose(fp);
vibe 7:42bc53611fc7 192 }
vibe 7:42bc53611fc7 193 }
vibe 7:42bc53611fc7 194