a
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 //reference 00002 //https://docs.google.com/document/d/197dgqlNFhn72JrBeSYGtR4Y7DJQRUKC61oHVD9dvLNY/edit 00003 #include "mbed.h" 00004 SPI ADC(p11,p12,p13); 00005 DigitalOut cs[2] = {p14, p26}; 00006 Serial pc(USBTX, USBRX, 9600); 00007 #define START_BIT 0x04 00008 #define MODE_SINGLE 0x02 // Single-ended mode 00009 #define MODE_DIFF 0x00 // Differential mode 00010 uint16_t MCP3208_get(int channel, int adc_num); 00011 uint16_t HK_data[40]; 00012 int rcmd = 0,cmdflag = 0, hk_num = 0; 00013 uint16_t d; 00014 void commandget(); 00015 void initialize(); 00016 // for temp 00017 float R3 = 110; 00018 float R4 = 1000; 00019 float R5 = 68000; 00020 float Pt = 100; 00021 float R_1 = 3; 00022 float R_2 = 2; 00023 float I = 0.001; 00024 float Vreft = 2.5; 00025 float ce = 0.003851; 00026 float Rth, temp, volt; 00027 float Rtha, tempa, volta; 00028 float Rthb, tempb, voltb; 00029 float Rthc, tempc, voltc; 00030 float Rthd, tempd, voltd; 00031 float Rthe, tempe, volte; 00032 float Rthf, tempf, voltf; 00033 float Rthg, tempg, voltg; 00034 float Rse=0.05; 00035 float Gain=50; 00036 00037 float gal; 00038 00039 int main(void) 00040 { 00041 int bit = 8; 00042 float Vref = 5; 00043 ADC.format(bit,0); 00044 ADC.frequency(2000000); //VDD=5Vの場合は2MHz. 00045 pc.printf("\r\n"); 00046 pc.printf("Hello, eps. This is MCP3208 test code.\r\n"); 00047 pc.printf("if you wanna start, press *a*. \r\n"); 00048 // for temp calucuration 00049 float gain = -R5*I/R4; 00050 float off = Vreft+I*R3; 00051 00052 00053 while(1){ 00054 commandget(); 00055 if(cmdflag == 1){ 00056 if(rcmd == 'a'){ 00057 for(int adc_num = 0; adc_num < 2; adc_num++){ 00058 for(int i = 0; i < 8; i++){ 00059 HK_data[hk_num] = MCP3208_get(i, adc_num); 00060 pc.printf("i = %d, ADC_num: %d, return = %d\n\r",i, adc_num, HK_data[hk_num]); 00061 hk_num++; 00062 } 00063 } 00064 //3.3V 00065 //pc.printf("ADC_num: 0, CH[0]: Volt = %.5f[V]\n\r", Vref * (float)HK_data[0] / 4096); 00066 //Temperature 00067 volt = Vref*(float)HK_data[0] / 4096*(R_1 + R_2)/R_1; 00068 Rth = (volt-off)/gain+R3; 00069 temp = (Rth-Pt)/(ce*Pt); 00070 00071 volta = Vref*(float)HK_data[1] / 4096*(R_1 + R_2)/R_1; 00072 Rtha = (volta-off)/gain+R3; 00073 tempa = (Rtha-Pt)/(ce*Pt); 00074 00075 voltb = Vref*(float)HK_data[2] / 4096*(R_1 + R_2)/R_1; 00076 Rthb = (voltb-off)/gain+R3; 00077 tempb = (Rthb-Pt)/(ce*Pt); 00078 00079 00080 voltc = Vref*(float)HK_data[3] / 4096*(R_1 + R_2)/R_1; 00081 Rthc = (voltc-off)/gain+R3; 00082 tempc = (Rthc-Pt)/(ce*Pt); 00083 00084 voltd = Vref*(float)HK_data[4] / 4096*(R_1 + R_2)/R_1; 00085 Rthd = (voltd-off)/gain+R3; 00086 tempd = (Rthd-Pt)/(ce*Pt); 00087 00088 volte = Vref*(float)HK_data[5] / 4096*(R_1 + R_2)/R_1; 00089 Rthe = (volte-off)/gain+R3; 00090 tempe = (Rthe-Pt)/(ce*Pt); 00091 00092 voltf = Vref*(float)HK_data[6] / 4096*(R_1 + R_2)/R_1; 00093 Rthf = (voltf-off)/gain+R3; 00094 tempf = (Rthf-Pt)/(ce*Pt); 00095 00096 voltg = Vref*(float)HK_data[7] / 4096*(R_1 + R_2)/R_1; 00097 Rthg = (voltg-off)/gain+R3; 00098 tempg = (Rthg-Pt)/(ce*Pt); 00099 00100 00101 pc.printf("ADC_num: 0, CH[0]: Temp = %.5f[degree]\n\r", temp); 00102 pc.printf("ADC_num: 0, CH[1]: Temp = %.5f[degree]\n\r", tempa); 00103 pc.printf("ADC_num: 0, CH[2]: Temp = %.5f[degree]\n\r", tempb); 00104 pc.printf("ADC_num: 0, CH[3]: Temp = %.5f[degree]\n\r", tempc); 00105 pc.printf("ADC_num: 0, CH[4]: Temp = %.5f[degree]\n\r", tempd); 00106 pc.printf("ADC_num: 0, CH[5]: Temp = %.5f[degree]\n\r", tempe); 00107 pc.printf("ADC_num: 0, CH[6]: Temp = %.5f[degree]\n\r", tempf); 00108 pc.printf("ADC_num: 0, CH[7]: Temp = %.5f[degree]\n\r", tempg); 00109 00110 //Galvanometer 00111 //gal = Vref * (float)HK_data[2] / 4096/Gain/Rse; 00112 //pc.printf("ADC_num: 0, CH[2]: Galvanometer = %.5f[A]\n\r", gal); 00113 //pc.printf("ADC_num: 1, CH[0]: Volt = %.5f[V]\n\r", Vref * (float)HK_data[3] / 4096); 00114 //pc.printf("ADC_num: 1, CH[1]: Volt = %.5f[V]\n\r", Vref * (float)HK_data[4] / 4096); 00115 //pc.printf("ADC_num: 1, CH[2]: Volt = %.5f[V]\n\r", Vref * (float)HK_data[5] / 4096); 00116 } 00117 } 00118 for(int j=0; j<hk_num; j++){ 00119 pc.printf("HK[%d]: %d\n\r", j, HK_data[j]); 00120 } 00121 initialize(); 00122 } 00123 00124 } 00125 00126 00127 uint16_t MCP3208_get(int channel, int adc_num){ 00128 cs[adc_num] = 0; 00129 pc.printf("Channel: %d, ", channel); 00130 int command_high = START_BIT | MODE_SINGLE | ((channel & 0x04) >> 2); 00131 int command_low = (channel & 0x03) << 6; 00132 ADC.write(command_high); 00133 int high_byte = ADC.write(command_low) & 0x0F; 00134 int low_byte = ADC.write(0x00); 00135 int conv_result = (high_byte << 8) | low_byte; 00136 cs[adc_num] = 1; 00137 return conv_result; 00138 } 00139 00140 00141 void commandget() 00142 { 00143 rcmd = pc.getc(); 00144 cmdflag = 1; 00145 pc.printf("get command: %d,%d\r\n",rcmd,cmdflag); 00146 } 00147 void initialize() 00148 { 00149 rcmd = 0; 00150 cmdflag = 0; 00151 hk_num = 0; 00152 }
Generated on Tue Nov 1 2022 05:05:40 by
1.7.2