CROSS Technical Documentation User Manual and Technical Doc.
INFN Milano Bicocca
Loading...
Searching...
No Matches
Istruzioni.c
Go to the documentation of this file.
1
2
3#include "tutti_gli_header.h"
4
5/// \file
6
7/*!
8
9\page Istruzioni
10\brief Instruction set is at \ref Instruction_set
11
12*/
13
14int valore_target_per_offset[]={0,0}; //Target offset to adjust
15
16// INIZIO CROSS
17
18/*! \brief This is the pointer array to the instructions set: the byte #istruzione_codice of the CAN message
19is the address used for the array
20
21The input parameter is the global byte #istruzione_codice of the CAN messsage
22*\return The returned parameters is the global 8-byte array to be sent with the CAN.
23*/
25
26
27
28/*! \brief The pointer to the instruction functions is constructed here.
29
30*\snippetlineno Istruzioni.c fun_Setting_of_the_pointer_for_command_parser
31*/
32//! <!-- [fun_Setting_of_the_pointer_for_command_parser] -->
34//! \brief base instrucions
35 ptr_istruzioni[instr_NO_operation] =instr_NO_operation_function; //!< Nop instruction it replais the fw version
38 //! \brief Detector instructions
43 //! \brief Preamplifier instructions
50 //! \brief PGA managing
53 ptr_istruzioni[instr_PGA_DAC_GAIN_CROSS]=instr_PGA_DAC_GAIN_CROSS_function;
54 //! \brief Errors
57 //! \brief Common use
61// ptr_istruzioni[instr_Vbias_set]=instruction_Detector_bias;
62 //! \brief Experts use
69
70
71
72//! \brief ADC instructions
73// ptr_istruzioni[instr_ADC_LETTURA] =instruction_lettura_ADC; //!<
74//! \brief preamplifier instrucions
75// ptr_istruzioni[instr_pre_PGA_gain]= instruction_Detector_PGA_Rele;
76// ptr_istruzioni[instr_PGA_GAIN]=instruction_for_PGA_GAIN_set;
77// ptr_istruzioni[instr_preamplifier_temperature_meas]=instruction_Misura_della_temperatura;
78// ptr_istruzioni[instr_Power_supply_ON_OFF]=instruction_Power_supply_control;
79// ptr_istruzioni[instr_OFFSET_ADJ]=instruction_Offset_compensation;
80
81// ptr_istruzioni[instr_preamplifier_memory]=instruction_preamplifier_memory;
82// ptr_istruzioni[instr_measure_pre_bias_coefficients]=instruction_measure_pre_bias_coefficients;
83// ptr_istruzioni[instr_Some_parameters_to_set] =instruction_Some_parameters_to_set;
84
85// ptr_istruzioni[instr_ricalibrazione_scheda] =instruction_ricalibrazione_scheda;
86}
87//! <!-- [fun_Setting_of_the_pointer_for_command_parser] -->
88
89//In the following there are the instruction functions: every function is an instruction
90
91 /*! \brief The NOP sends backs the fw version.
92
93There is no input parameter for this function.
94*\return No Parameters, the actual result is sent to the CAN bus output throught \ref tx_data.
95
96*\snippetlineno Istruzioni.c fun_instr_NO_operation_function
97*/
98//! <!-- [fun_instr_NO_operation_function] -->
100
101 *(unsigned int *) &tx_data[byte_istr_dati_0] =FW_version; // FW_version is copied in the first 4 bytes of tx_data
102 __NOP();
103}
104//! <!-- [fun_instr_NO_operation_function] -->
105
106 /*****************************************************************************
107 Controllo modalit\'a Power down
108 ******************************************************************************/
109/*! \brief &mu;-controller power down ON/OFF
110
111*\return No Parameters, the actual result is sent to the CAN bus output throught \ref tx_data.
112
113*\snippetlineno Istruzioni.c fun_instr_Shut_Down_ON_OFF_function
114*/
115//! <!-- [fun_instr_Shut_Down_ON_OFF_function] -->
117 //If vettore_istruzioni[istruzione_byte_4_e_scelta_canale]>1 the state is returned only.
118 uint8_t dati_da_scrivere[]={0,0,0,0};
120 //Power down attivo
121 flag_power_down = true;
124 dati_da_scrivere[0]= POWER_DOWN_MAX_ATTESA & 0xFF;
125 EPROM_scrittura_M24C32_64( 0, I2C_postmainboard, Canale_EPROM_postmainboard_down, Memory_postmainboard_micro_PD_wait_time_in_sec << 2 , dati_da_scrivere);
126 EPROM_scrittura_M24C32_64( 1, I2C_postmainboard, Canale_EPROM_postmainboard_up, Memory_postmainboard_micro_PD_wait_time_in_sec << 2 , dati_da_scrivere);
129 //Power down non attivo, micro sempre acceso
130 flag_power_down = false;
131 }
132
133 dati_da_scrivere[0]= flag_power_down;
134 EPROM_scrittura_M24C32_64( 0, I2C_postmainboard, Canale_EPROM_postmainboard_down, Memory_postmainboard_micro_powdown_active_1_alwaysON_0 << 2 , dati_da_scrivere);
135 EPROM_scrittura_M24C32_64( 1, I2C_postmainboard, Canale_EPROM_postmainboard_up, Memory_postmainboard_micro_powdown_active_1_alwaysON_0 << 2 , dati_da_scrivere);
137// flag_power_down = false; //<-------------------- Cancellare questa riga
138
139 if( vettore_istruzioni[5]==0 ){
140 //ADC in standby quando non usati, res dummy attive, power non cambia
141 ADC_sleep_off = 0;
142 ADC_sleep_ON_OFF( 0 , 0);
143 ADC_sleep_ON_OFF( 1 , 0);
144 }else if (vettore_istruzioni[5]==2 ){
145 //ADC sempre accesi
146 ADC_sleep_off = 2;
147 ADC_sleep_ON_OFF( 0 , 2);
148 ADC_sleep_ON_OFF( 1 , 2);
149 } else if (vettore_istruzioni[5]==1 ){
150 //ADC in standby quando non usati, res dummy non attive, power si riduce
151 ADC_sleep_off = 1;
152 ADC_sleep_ON_OFF( 0 , 1);
153 ADC_sleep_ON_OFF( 1 , 1);
154 }
155 dati_da_scrivere[0]=ADC_sleep_off;
156 EPROM_scrittura_M24C32_64( 0, I2C_postmainboard, Canale_EPROM_postmainboard_down, Memory_postmainboard_ADCs_always_ON_0_powerdown_1 << 2 , dati_da_scrivere);
157 EPROM_scrittura_M24C32_64( 1, I2C_postmainboard, Canale_EPROM_postmainboard_up, Memory_postmainboard_ADCs_always_ON_0_powerdown_1 << 2 , dati_da_scrivere);
158 if (ADC_sleep_off ==0) {
159 tx_data[5]= 1; //spenti con dummy resistor
160 }
161 else if (ADC_sleep_off==1){
162 tx_data[5]= 11; //spenti senza dummy resistor
163 }else if (ADC_sleep_off>=2){
164 tx_data[5]= 2; //sempre accesi
165 }
166
167 EPROM_lettura_M24C32_64(0, I2C_postmainboard, Canale_EPROM_postmainboard_down, Memory_postmainboard_micro_PD_wait_time_in_sec << 2,dati_da_scrivere);
168 tx_data[0]= dati_da_scrivere[0];
169}
170//! <!-- [fun_instr_Shut_Down_ON_OFF_function] -->
171
172/*! \brief readback of some parameters
173
174*\return No Parameters, the actual result is sent to the CAN bus output throught \ref tx_data.
175
176*\snippetlineno Istruzioni.c fun_instr_readback_variables_function
177*/
178//! <!-- [fun_instr_readback_variables_function] -->
180uint8_t canale, offset_board=0;
181int32_t variabile_in_mV;
183 offset_board=6;
184 }
185
188 //Detectoe biasing setting, a short
189 for( canale=0; canale<6;canale++){
191 variabile_in_mV = Detector_bias_target[canale + offset_board] / 1000 ;
192 tx_data[byte_istr_dati_0]= variabile_in_mV & 0xFF ;
193 tx_data[byte_istr_dati_1]= (variabile_in_mV >>8) & 0xFF;
194 break;
195 }
196 }
197 break;
199 //Offset voltage value, a short
200 for( canale=0; canale<6;canale++){
202 variabile_in_mV = preamplifier_Output_offset_target[canale + offset_board] / 1000;
203 tx_data[byte_istr_dati_0]= variabile_in_mV & 0xFF ;
204 tx_data[byte_istr_dati_1]= (variabile_in_mV >>8) & 0xFF;
205 break;
206 }
207 }
208 break;
210 //Pga Gain, an uint8
211 for( canale=0; canale<6;canale++){
213 tx_data[byte_istr_dati_0]= PGA_settled_gain[canale + offset_board] ;
214 break;
215 }
216 }
217 break;
218 }
219
220
221 }
222//! <!-- [fun_instr_readback_variables_function] -->
223
224
225/*! \brief readback of some node voltages from external ADC
226
227*\return No Parameters, the actual result is sent to the CAN bus output throught \ref tx_data.
228
229*\snippetlineno Istruzioni.c fun_instr_readback_node_voltages_function
230*/
231//! <!-- [fun_instr_readback_node_voltages_function] -->
233
234 ADC_external_measurement = ( *(int32_t *)&vettore_istruzioni[0] ); //The input value is in microV
235 ADC_external_measured_node = vettore_istruzioni[istruzione_byte_3];
236
237}
238//! <!-- [fun_instr_readback_node_voltages_function] -->
239
240
241/*! \brief preamplifier temperature read
242
243*\return No Parameters, the actual result is sent to the CAN bus output throught \ref tx_data.
244
245*\snippetlineno Istruzioni.c fun_instr_readback_variables_function
246*/
247//! <!-- [fun_instr_readback_variables_function] -->
250 uint8_t scheda_su_scheda_giu = (canale >> 7) & 1;
251 uint32_t temperatura_in_microK_;
252 ERROR_codifica_errore(0,0,0,0); //Reset_ch0 errori
253 for( canale_=0;canale_<6;canale_++){
254 if ( (canale >> canale_) & 1){
255 termometro_lettura( scheda_su_scheda_giu, canale_ , &temperatura_in_microK_ );
256 tx_data[0]= temperatura_in_microK_ & 0xFF;
257 tx_data[1]= (temperatura_in_microK_ >> 8) & 0xFF;
258 tx_data[2]= (temperatura_in_microK_ >> 16) & 0xFF;
259 tx_data[3]= (temperatura_in_microK_ >> 24) & 0xFF;
260 break;
261 }
262 }
264}
265//! <!-- [fun_instr_readback_variables_function] -->
266
267
268/*! \brief preamplifier memory read/write
269
270*\return No Parameters, the actual result is sent to the CAN bus output throught \ref tx_data.
271
272*\snippetlineno Istruzioni.c fun_instr_preamplifier_memory_function
273*/
274//! <!-- [fun_instr_preamplifier_memory_function] -->
277 uint8_t scheda_su_scheda_giu = (canale & board_up) >0 ;// >> 7 ;
278 short indirizzo_memoria= (vettore_istruzioni[4] & 0xFF) + ( ( vettore_istruzioni[5]<<8) & 0xFF00) ;
279 canale_ = canale & 0x3f; //Primi 6 bit sono il canale da considerare
280 ERROR_codifica_errore(0,0,0,0); //Reset_ch0 errori
281 for (canale_=0; canale_<6;canale_++){
282 if( (canale >> canale_) &1){
283 break;
284 }
285 }
286 if ( canale & write_trimmer){
287 EPROM_scrittura_M24C32_64( scheda_su_scheda_giu, I2C_mainboard, canale_, indirizzo_memoria , (uint8_t *) rx_data);
288// EPROM_scrittura_M24C08_16(scheda_su_scheda_giu,canale_,indirizzo_memoria,(uint8_t *) rx_data);
289 }else{
290 EPROM_lettura_M24C32_64( scheda_su_scheda_giu, I2C_mainboard, canale_, indirizzo_memoria , tx_data);
291// EPROM_lettura_M24C08_16(scheda_su_scheda_giu, canale_, indirizzo_memoria,tx_data);
292 }
294}
295//! <!-- [fun_instr_preamplifier_memory_function] -->
296
297/*! \brief preamplifier gain set large or small
298
299*\return No Parameters, the actual result is sent to the CAN bus output throught \ref tx_data.
300
301*\snippetlineno Istruzioni.c fun_instr_preamplifier_gain_low_high_set_function
302*/
303//! <!-- [fun_instr_preamplifier_gain_low_high_set_function] -->
306 uint8_t gain_small_large= vettore_istruzioni[istruzione_byte_3];
307 uint8_t scheda_su_scheda_giu= (canale >> 7) & 1;
308 ERROR_codifica_errore(0,0,0,0); //Reset_ch0 errori
309 if (gain_small_large){
310 gain_small_large=preamplifier_Gain_alto;
311 }else{
312 gain_small_large=preamplifier_Gain_basso;
313 }
314 for(canale_=0; canale_<6;canale_++){
315 if( (canale >> canale_) &1){
316 preamplifier_set_reset_pre_gain (scheda_su_scheda_giu, canale_, gain_small_large );
317 }
318 }
320}
321//! <!-- [fun_instr_preamplifier_gain_low_high_set_function] -->
322
323/*! \brief PGA gain set
324
325*\return No Parameters, the actual result is sent to the CAN bus output throught \ref tx_data.
326
327*\snippetlineno Istruzioni.c fun_instr_PGA_gain_set_function
328*/
329//! <!-- [fun_instr_PGA_gain_set_function] -->
331 #define PGA_mantieni_loffset 1
332
333 uint8_t canale_, preamplifier_canali_da_regolare= (vettore_istruzioni[6] & 0x3F),iii,istruzione_old[8];
334 uint8_t PGA_gain_to_set= vettore_istruzioni[byte_istr_dati_0];
335 uint8_t scheda_su_scheda_giu= ((vettore_istruzioni[6] >> 7) & 1)*6;
336 uint8_t offset_maintained= vettore_istruzioni[istruzione_byte_3];
337 uint8_t preamplifier_externalADC_1_onboardADC_0=0;
338
339 if (vettore_istruzioni[5] & PGA_mantieni_loffset){
340 //ATTENZIONE: se offset_maintained allora bisogna:
341 for (iii =0; iii < 7;iii++){//conserviamo l'sitruzione
342 istruzione_old[iii] = vettore_istruzioni[iii];
343 }
344 // -leggere l'offset prima di cambiare: questo sar\'a il target
345 for ( canale_ =0; canale_<6; canale_++){
346 preamplifier_Output_offset_target[scheda_su_scheda_giu + canale_] = ADC_misura_differenziale_con_media_generico(scheda_su_scheda_giu,preamplifier_externalADC_1_onboardADC_0,canale_,1);
347 }
348 //Impostiamo ilo futuro valore del guadagno nel vettore, poi il guadagno verr\'a messo ad 1 nella funzione di regolazione per evitare saturazioni
349 for ( canale_ =0; canale_<6; canale_++){
350 if( (preamplifier_canali_da_regolare >> canale_) & 1){
351 PGA_settled_gain[canale_ + scheda_su_scheda_giu]=PGA_gain_to_set;
352 }
353 }
354 preamplifier_attesa_tra_le_misure = vettore_istruzioni[3];
355 if( preamplifier_attesa_tra_le_misure==0) preamplifier_attesa_tra_le_misure=500;
356 preamplifier_canali_da_regolare_old = preamplifier_canali_da_regolare ; //serve per la funzione sotto
357 // -determinare i nuovi parametri di offset
358
360 vettore_istruzioni[2]=istruzione_old[2];vettore_istruzioni[3]=0;
361 if(vettore_istruzioni[2] ==0) vettore_istruzioni[2]=istruzione_old[2];
363 vettore_istruzioni[4]=istruzione_old[3];
365 vettore_istruzioni[5]=istruzione_old[4];
366 if(vettore_istruzioni[5]==0) vettore_istruzioni[5] =50;
367 vettore_istruzioni[6]=istruzione_old[6];
369 instr_output_offset_to_be_set_function(); //Lanciamo la funzione di aggiustamento offset, che alla fine imposter\'a i guadagni corretti finali
370 for (iii =0; iii < 7;iii++){//Ritorniamo all'istruzione originaria
371 vettore_istruzioni[iii] = istruzione_old[iii];
372 }
373
374 }else{
375 ERROR_codifica_errore(0,0,0,0); //Reset_ch0 errori
376 //Cambiamo il guadagno al nuovo valore, caso in cui non ci preoccupiamo dell'offset
377 for(canale_=0; canale_<6;canale_++){
378 if( ( preamplifier_canali_da_regolare >> canale_) &1 ){
379 // -cambiare il guadagno
380 PGA_GAIN_CROSS( scheda_su_scheda_giu, canale_, PGA_gain_to_set ) ;
381 }
382 }
383}
384
386}
387//! <!-- [fun_instr_PGA_gain_set_function] -->
388
389/*! \brief PGA input stage gain set
390
391*\return No Parameters, the actual result is sent to the CAN bus output throught \ref tx_data.
392
393*\snippetlineno Istruzioni.c fun_instr_PGA_set_reset_pre_PGA_gain_function
394*/
395//! <!-- [fun_instr_PGA_set_reset_pre_PGA_gain_function] -->
398 uint8_t scheda_su_scheda_giu= (canale >> 7) & 1;
399 uint8_t PGA_gain_da_impostare= vettore_istruzioni[istruzione_byte_2];
400 uint8_t offset_maintained= vettore_istruzioni[istruzione_byte_3];
401 //ATTENZIONE: se offset_maintained allora bisogna:
402 // -leggere l'offset prima di cambiare
403 // -determinare i nuovi parametri di offset
404 // -cambiare il guadagno
405 ERROR_codifica_errore(0,0,0,0); //Reset_ch0 errori
406 uint8_t set_2_reset_1_idle_0=1;
407 if (PGA_gain_da_impostare){
408 set_2_reset_1_idle_0=2;
409 }
410 for(canale_=0; canale_<6;canale_++){
411 if ((canale>> canale_) & 1) {
412 PGA_set_reset_pre_PGA_gain ( scheda_su_scheda_giu, canale_, set_2_reset_1_idle_0 );
413 }
414 }
416}
417//! <!-- [fun_instr_PGA_set_reset_pre_PGA_gain_function] -->
418
419
420void instr_PGA_DAC_GAIN_CROSS_function(void){
422 uint8_t scheda_su_scheda_giu= (canale >> 7) & 1;
423 uint8_t PGA_gain_da_impostare= vettore_istruzioni[istruzione_byte_2];
424 uint8_t PGA_offset_maintained= vettore_istruzioni[istruzione_byte_3];
425 //ATTENZIONE: se PGA_offset_maintained allora bisogna:
426 // -leggere l'offset prima di cambiare
427 // -determinare i nuovi parametri di offset
428 // -cambiare il guadagno
429 ERROR_codifica_errore(0,0,0,0); //Reset_ch0 errori
430 for(canale_=0; canale_<6;canale_++){
431 if ((canale>> canale_) & 1) {
432 PGA_DAC_GAIN_CROSS( scheda_su_scheda_giu, canale_, PGA_gain_da_impostare, PGA_set_pre_pga_low_gain);
433 }
434 }
436}
437
438
439/*! \brief Routing of available node voltage to outout by means of the analog mux
440
441*\return No Parameters, the actual result is sent to the CAN bus output throught \ref tx_data.
442
443*\snippetlineno Istruzioni.c fun_instr_connect_disconnect_mux_node_function
444*/
445//! <!-- [fun_instr_connect_disconnect_mux_node_function] -->
447
448 ERROR_codifica_errore(0,0,0,0); //Reset_ch0 errori
450
451if (vettore_istruzioni[6]==0){
452 tx_data[5]=0;
453 }
455}
456//! <!-- [fun_instr_connect_disconnect_mux_node_function] -->
457
458
459//FINE CROSS
460
461
462 /*! \brief ADC instrunction implementation with node selection
463
464This instruction needs to know which node to read. The node to read is
465at byte #istruzione_byte_4_e_scelta_canale of the CAN message.
466*\return No Parameters, the actual result is sent to the CAN bus output throught \ref tx_data.
467
468
469*\snippetlineno Istruzioni.c fun_istruzione_lettura_ADC
470*/
471//! <!-- [fun_istruzione_lettura_ADC] -->
473 int misura;
475 //! <!-- [cod_parte_finale_ADC] -->
476
477// misura = lettura_ADC( sottoistruzione);
478
479 *(unsigned int *) &tx_data[byte_istr_dati_0] = correzione_misura_ADC(coeffcienti_misura_ADC_nodi[sottoistruzione], misura);
480 //! <!-- [cod_parte_finale_ADC] -->
481 }
482 //! <!-- [fun_istruzione_lettura_ADC] -->
483
484
485 /*! \brief This is a sub-function of the ADC instrunction. It is useful as it cane be used in other
486 part of the program such as detector bias setting and offset adjustement.
487
488*\param[in] sottoistruzione is the selctring node according to the list at \ref ADC_Mux_page
489*\return No Parameters, the actual result is sent to the CAN bus output throught \ref tx_data.
490
491
492*\snippetlineno Istruzioni.c fun_lettura_ADC
493*/
494//! <!-- [fun_lettura_ADC] -->
495// int lettura_ADC( char sottoistruzione){
496// char ii;
497//// ATTIVA_CHIP_SELECTOR_MULTIPLEXER( (sottoistruzione >> 4) & 0x7); //the 4 MSb select the mux
498//// Line_select_in_the_mux( sottoistruzione & 0xF); //The 4 LSb select te mux line
499// Aspetta_tanti_ms(wait_for_ADC_setting); //Wait for nodes stabilization
500// risultato=0;
501// for( ii=0; ii <(1 << medie_ADC); ii++){
502// risultato += ADC_lettura_24bit(0,0,2); //Measurement is taken
503// }
504// risultato=risultato>>medie_ADC;
505
506//// Stampa_risultato_sul_CAN(risultato); //The result is composed in the CAN message, 4 MSB.
507//// ATTIVA_CHIP_SELECTOR_MULTIPLEXER( 0); //Disable all mux's
508// return risultato;
509// }
510//! <!-- [fun_lettura_ADC] -->
511
512
513 /*! \brief We manage the relais which take care of the deetector performances. The relay for the pre PGA
514gain x1 - x10 is managed here, too.
515
516This instruction needs to know the gain to set and the channel to apply the setting. These informztion is
517at byte #istruzione_byte_4_e_scelta_canale of the CAN message.
518*\return No Parameters, the actual result is sent to the CAN bus output throught \ref tx_data.
519
520
521*\snippetlineno Istruzioni.c fun_instruction_Detector_PGA_Rele
522*/
523//! <!-- [fun_instruction_Detector_PGA_Rele] -->
525 uint8_t canale_;
526 for( canale_=0; canale_<6; canale_++){
527 PGA_set_reset_pre_PGA_gain ( scheda_su_scheda_giu, canale_, PGA_set_pre_pga_low_gain ) ;
528 }
529 }
530
531 void Detector_PGA_Rele( char discriminazione){
532 char chan=0, action=0;
533 porta_pin_def PGA_pre_gain;
534 if (discriminazione>=0x10) chan=1;
535 action= discriminazione & 0x0F;
536 if (action>=8){
537 //Pga pre gain set to 1 V/V or to 10 V/V
538 if (action ==8){
539 //pga pre gain =1
540 if (chan){
541 PGA_pre_gain=PGA_pre_gain_1_ch1;
542 }else{
543 PGA_pre_gain=PGA_pre_gain_1_ch0;
544 }
545 }else{
546 //pga pre gain =10
547 if (chan){
548 PGA_pre_gain=PGA_pre_gain_10_ch1;
549 }else{
550 PGA_pre_gain=PGA_pre_gain_10_ch0;
551 }
552 }
553 GPIO_PinWrite(PGA_pre_gain.porta_num, PGA_pre_gain.pin_num, 1); // Viene attivato il PGA_rele_cho_1
555 GPIO_PinWrite(PGA_pre_gain.porta_num, PGA_pre_gain.pin_num, 0); // Viene disattivato il PGA_rele_cho_1
557 }else {
558 // Working with detector relays
559 Funzione_per_attivare( chan, (1 << action));
560 }
561 }
562//! <!-- [fun_instruction_Detector_PGA_Rele] -->
563
564
565
566/*! \brief Some usefull parematers can be set here.
567
568The list of parameters is the enum ***DA AGGIUNGERE***
569This instruction needs to know which parameter to set
570at byte #istruzione_byte_4_e_scelta_canale and the parameter value
571at bytes #byte_istr_dati_0 and #byte_istr_dati_1 of the CAN message.
572*\return No Parameters, the actual result is sent to the CAN bus output throught \ref tx_data.
573
574*\snippetlineno Istruzioni.c fun_instruction_Some_parameters_to_set
575*/
576//! <!-- [fun_instruction_Some_parameters_to_set] -->
578 //The second byte from the CAN says which paremetr to set
579 //The last byt from the CAN says which value to set
580 char ii, canale;
581enum R_W_Parameters {
582 read_write_value_for_wait_for_ADC_setting , //!< NOP, in future release the fw version will be replayed
583 read_write_medie_ADC,
584 read_PGA_Gain_code,
585 read_PGA_Gain_V_su_V,
586 read_write_guadagno_minimo_PGA,
587 read_write_massimo_guadagno_impostabile,
588 read_write_Trimmer_offset_preampli,
589 read_write_Trimmer_thermal_preampli,
590 read_write_Trimmer_detector_bias_positive,
591 read_write_Trimmer_detector_bias_negative,
592 read_write_Trimmer_drift_in_micro_su_C,
593 read_write_Trimmer_common_mode
594} ;
595 //Da aggiungere:
596
598 for(ii =0;ii<2;ii++){
599 if( (sottoistruzione >> ii) && 1) canale=ii;
600 }
601
603 {
604 case read_write_value_for_wait_for_ADC_setting:
605 {
608 //MAximu time is 10 sec: time from the mux and ADC setting and the measurement
609 //Default is 100 ms
611 }
613 tx_data[1]= (wait_for_ADC_setting >>8 ) & 0xFF;
614 return;
615 }
616 case read_write_medie_ADC:
617 {
620 //Default is 2: take care the number of averages is (1 << medie_ADC), or 2^medie_ADC
621 }
622 tx_data[0]= medie_ADC &0xFF;
623 return;
624 }
625 case read_PGA_Gain_code: //Read only
626 {
627 tx_data[0]= PGA_Gain_code[canale] & 0xFF;
628 tx_data[1]= (PGA_Gain_code[canale] >>8 ) & 0xFF;
629 return;
630 }
631 case read_PGA_Gain_V_su_V:
632 {
633 tx_data[0]= PGA_Gain_V_su_V[canale] & 0xFF;
634 return;
635 }
636 case read_write_guadagno_minimo_PGA:
637 {
640 //Default is 4:
641 }
642 tx_data[0]= guadagno_minimo_PGA &0xFF;
643 return;
644 }
645 case read_write_massimo_guadagno_impostabile:
646 {
649 //Default is 100:
650 }
652 return;
653 }
654 case read_write_Trimmer_offset_preampli:
655 {
657 Trimmer_offset_preampli[canale].coarse_offset= vettore_istruzioni[byte_istr_dati_1];
658 Trimmer_offset_preampli[canale].fine_offset = vettore_istruzioni[byte_istr_dati_0];
659 Imposta_canale_del_trimmer_ennesimo_al_valore_I2CA (canale, preampli_corse_offset_trimmer, Trimmer_offset_preampli[canale].coarse_offset);
660 Imposta_canale_del_trimmer_ennesimo_al_valore_I2CA (canale, preampli_fine_offset_trimmer, Trimmer_offset_preampli[canale].fine_offset);
661 }
662 tx_data[0]= Trimmer_offset_preampli[canale].fine_offset & 0xFF;
663 tx_data[1]= Trimmer_offset_preampli[canale].coarse_offset & 0xFF;
664 return;
665 }
666 case read_write_Trimmer_thermal_preampli:
667 {
669 Trimmer_offset_preampli[canale].coarse_thermal= vettore_istruzioni[byte_istr_dati_1];
670 Trimmer_offset_preampli[canale].JFET_Offset = vettore_istruzioni[byte_istr_dati_0];
671// Trimmer_offset_preampli[canale].fine_thermal = vettore_istruzioni[byte_istr_dati_0];
672 Imposta_canale_del_trimmer_ennesimo_al_valore_I2CA (canale, preampli_coarse_thermal_trimmer , Trimmer_offset_preampli[canale].coarse_thermal);
673 Imposta_canale_del_trimmer_ennesimo_al_valore_I2CA (canale, preampli_JFET_Offset_trimmer , Trimmer_offset_preampli[canale].JFET_Offset);
674// Imposta_canale_del_trimmer_ennesimo_al_valore_I2CA (canale, preampli_fine_thermal_trimmer, Trimmer_offset_preampli[canale].fine_thermal);
675 }
676// tx_data[0]= Trimmer_offset_preampli[canale].fine_thermal & 0xFF;
677 tx_data[1]= Trimmer_offset_preampli[canale].coarse_thermal & 0xFF;
678 tx_data[0]= Trimmer_offset_preampli[canale].JFET_Offset & 0xFF;
679 return;
680 }
681 case read_write_Trimmer_detector_bias_positive:
682 {
684 detector_Trimmer_bias[canale].fine_pos= vettore_istruzioni[byte_istr_dati_0];
685 detector_Trimmer_bias[canale].coarse_pos = vettore_istruzioni[byte_istr_dati_1];
686 Imposta_canale_del_trimmer_ennesimo_al_valore_I2CB (canale, trimmer_bias_coarse_pos, detector_Trimmer_bias[canale].coarse_pos);
687 Imposta_canale_del_trimmer_ennesimo_al_valore_I2CB (canale, trimmer_bias_fine_pos, detector_Trimmer_bias[canale].fine_pos);
688 }
689 tx_data[0]= detector_Trimmer_bias[canale].fine_pos & 0xFF;
690 tx_data[1]= detector_Trimmer_bias[canale].coarse_pos & 0xFF;
691 return;
692 }
693 case read_write_Trimmer_detector_bias_negative:
694 {
696 detector_Trimmer_bias[canale].fine_neg= vettore_istruzioni[byte_istr_dati_0];
697 detector_Trimmer_bias[canale].coarse_neg = vettore_istruzioni[byte_istr_dati_1];
698 Imposta_canale_del_trimmer_ennesimo_al_valore_I2CB (canale, trimmer_bias_coarse_neg, detector_Trimmer_bias[canale].coarse_neg);
699 Imposta_canale_del_trimmer_ennesimo_al_valore_I2CB (canale, trimmer_bias_fine_neg, detector_Trimmer_bias[canale].fine_neg);
700 }
701 tx_data[0]= detector_Trimmer_bias[canale].fine_neg & 0xFF;
702 tx_data[1]= detector_Trimmer_bias[canale].coarse_neg & 0xFF;
703 return;
704 }
705 case read_write_Trimmer_drift_in_micro_su_C:
706 {
708 reference_thermal_drift_actual= vettore_istruzioni[byte_istr_dati_0];
709 }
710 case read_write_Trimmer_common_mode:
711 {//Lettura/scrittura del trimmer per aggiustare il CMRR
713 Trimmer_CMRR_JFET_COLD.CMRR[canale]=vettore_istruzioni[byte_istr_dati_0];
714 Imposta_trimmer_common_mode_I2CB ( Trimmer_numero_offset + canale, Trimmer_CMRR_JFET_COLD.CMRR[canale]);
715 }
716 tx_data[0]= Trimmer_CMRR_JFET_COLD.CMRR[canale] & 0xFF;
717 tx_data[1]= 0;
718 return;
719 }
720 }
721 }
722}
723//! <!-- [fun_instruction_Some_parameters_to_set] -->
724
725
726/*! \brief The temperature from both preamplifier is read
727
728The temperature ios read for both the preamplifiers and written
729in the first 4 bytes, the first 2 for ch0 and the second pair for ch1
730*\return No Parameters, the actual result is sent to the CAN bus output throught \ref tx_data.
731
732*\snippetlineno Istruzioni.c fun_instruction_Misura_della_temperatura
733*/
734//! <!-- [fun_instruction_Misura_della_temperatura] -->
736 //TERMOMETRO del canale 0
737 int misura_temperatura_locale[2];
738 int32_t risultato;
739 Misura_della_temperatura(misura_temperatura_locale);
740// uint8_t ii, addr[2] ={ 0x4A, 0x49}; // 100 1010 //L'INDIRIZZO????????
741// uint8_t dati_scrittura[2], dati_scrittura_aa[]={4,0x60},dati_scrittura_bb[]={0};
742// int misura_temperatura_locale[2];
743// int32_t risultato;
744// for(ii=0;ii<2;ii++){
745// I2CAdrv ->MasterTransmit (addr[ii], dati_scrittura_aa, 2, false); //Massima risoluzione 14 bit
746// while (I2CAdrv->GetStatus().busy);
747//
748// I2CAdrv ->MasterTransmit (addr[ii], dati_scrittura_bb, 1, false); //Leggiamo T
749// while (I2CAdrv->GetStatus().busy);
750//
751// I2CAdrv->MasterReceive (addr[ii], dati_scrittura, 2, false);
752// while (I2CAdrv->GetStatus().busy);
753
754// misura_temperatura_locale[ii] =( (dati_scrittura[0]<<8) + (dati_scrittura[1]) )>>2; //14 bit di risoluzione
755// misura_temperatura_locale[ii] = ((misura_temperatura_locale[ii]*1000)) >> 5 ;//moltiplichiamo per 0.03135
756// // ovvero dividiamo per 32//we have 0.25 or 1/4 C/bit
757// //The temperature is im mK
758// }
759
760
761 risultato= (misura_temperatura_locale[0] & 0xFFFF) + ((misura_temperatura_locale[1] & 0xFFFF)<<16);
762// misura_temperatura=misura_temperatura-1500;
763 *(unsigned int *) &tx_data[byte_istr_dati_0] = risultato;
764// Stampa_risultato_sul_CAN(misura_temperatura);
765return;
766}
767//! <!-- [fun_instruction_Misura_della_temperatura] -->
768
769void Misura_della_temperatura(int *misura_temperatura_locale){ //Misura in mK
770 uint8_t ii, addr[2] ={ 0x4A, 0x49}; // 100 1010 //L'INDIRIZZO????????
771 uint8_t dati_scrittura[2], dati_scrittura_aa[]={4,0x60},dati_scrittura_bb[]={0};
772// int misura_temperatura_locale[2];
773// int32_t risultato;
774 for(ii=0;ii<2;ii++){
775 I2CAdrv ->MasterTransmit (addr[ii], dati_scrittura_aa, 2, false); //Massima risoluzione 14 bit
776 while (I2CAdrv->GetStatus().busy);
777
778 I2CAdrv ->MasterTransmit (addr[ii], dati_scrittura_bb, 1, false); //Leggiamo T
779 while (I2CAdrv->GetStatus().busy);
780
781 I2CAdrv->MasterReceive (addr[ii], dati_scrittura, 2, false);
782 while (I2CAdrv->GetStatus().busy);
783
784 misura_temperatura_locale[ii] =( (dati_scrittura[0]<<8) + (dati_scrittura[1]) )>>2; //14 bit di risoluzione
785 misura_temperatura_locale[ii] = ((misura_temperatura_locale[ii]*1000)) >> 5 ;//moltiplichiamo per 0.03135
786 // ovvero dividiamo per 32//we have 0.25 or 1/4 C/bit
787 //The temperature is im mK
788 }
789
790}
791
792 /*****************************************************************************
793 Controllo alimentazione
794 ******************************************************************************/
795//void instruction_Power_supply_control(void){
796// if( vettore_istruzioni[istruzione_byte_4_e_scelta_canale] ){
797// //Accensione
798// Accensione_delle_alimentazioni_analogiche();
799// }else{
800// //Spegnimento
801// Spegnimento_delle_alimentazioni_analogiche();
802// }
803// tx_data[istruzione_byte_4_e_scelta_canale]= vettore_istruzioni[istruzione_byte_4_e_scelta_canale];
804//}
805
806//void Spegnimento_delle_alimentazioni_analogiche(void)
807//{
808// Sleep_ADC(); // Viene spenta l'ADC
809//
810// //Vengono spente Vref pos e Vref neg
811// GPIO_SetDir(1, Shut_Vrefp_Vrefn, GPIO_DIR_OUTPUT);
812//
813// //Vengono spente Vcc e Vee
814// GPIO_SetDir(1, Shut_Vcc_Vee, GPIO_DIR_OUTPUT);
815//
816// //Vengono abbassati altri due pin per non lasciare accese le alimentazioni
817// GPIO_PinWrite(2, DIG_5, 0);
818// GPIO_PinWrite(2, DIG_3_3, 0);
819
820// //Vengono abbassati tutti i PIN
821// GPIO_PinWrite(0, Chip_Sel_Adc, 0); // Viene abbassato il Chip_Sel_Adc
822// GPIO_PinWrite(0, Chip_Sel_rele_0, 0); // Viene abbassato il Chip Sel rele del canale 0
823// GPIO_PinWrite(1, Set_ch0, 0); // Impostando il set a 0 vengono abbassati tutti gli interruttori
824// GPIO_PinWrite(1, Reset_ch0 ,0); // Viene impostato il Reset a 0
825//// GPIO_PinWrite(0, Chip_Sel_rele_1, 0); // Viene abbassato il Chip Sel rele del canale 1
826// GPIO_PinWrite(1, Set_ch1, 0); // Impostando il set a 0 vengono abbassati tutti gli interruttori
827// GPIO_PinWrite(1, Reset_ch1 ,0); // Viene impostato il Reset a 0
828// GPIO_PinWrite(0, PGA_Chip_Sel_DAC_ch0, 0); // Viene abbassato il Chip Sel del DAC del canale 0
829// GPIO_PinWrite(0, PGA_Chip_Sel_DAC_ch1, 0); // Viene abbassato il Chip Sel del DAC del canale 1
830// GPIO_PinWrite(1, PGA_rele_ch0_1, 0); // Viene abbassato il Chip Sel rele del canale 1
831// GPIO_PinWrite(1, PGA_rele_ch0_2, 0); // Viene abbassato il Chip Sel rele del canale 0
832// GPIO_PinWrite(1, PGA_rele_ch1_1, 0); // Viene abbassato il Chip Sel rele del canale 1
833// GPIO_PinWrite(1, PGA_rele_ch1_2, 0); // Viene abbassato il Chip Sel rele del canale 0
834//// GPIO_PinWrite(1, Chip_Sel_Multiplexer_4, 0); // Viene abbassato il Chip Sel rele del canale 0
835//// GPIO_PinWrite(1, Chip_Sel_Multiplexer_3, 0); // Viene abbassato il Chip Sel rele del canale 0
836// GPIO_PinWrite(1, Chip_Sel_Multiplexer_2, 0); // Viene abbassato il Chip Sel rele del canale 0
837// GPIO_PinWrite(1, Chip_Sel_Multiplexer_1, 0); // Viene abbassato il Chip Sel rele del canale 0
838// GPIO_PinWrite(1, Sel_Par_Interruttori_3, 0); // Viene abbassato il Chip Sel rele del canale 0
839// GPIO_PinWrite(1, Sel_Par_Interruttori_2, 0); // Viene abbassato il Chip Sel rele del canale 0
840// GPIO_PinWrite(1, Sel_Par_Interruttori_1, 0); // Viene abbassato il Chip Sel rele del canale 0
841
842// SPIdrv->Uninitialize(); // Viene disinizializzata l'SPI
843// I2CAdrv->Uninitialize(); // Viene disinizializzata l'I2CA
844// I2CBdrv->Uninitialize(); // Viene disinizializzata l'I2CB
845// I2C_termometro_drv->Uninitialize(); // Viene disinizializzata l'I2C_termometro
846//return;
847//}
848
849//void Accensione_delle_alimentazioni_analogiche(void)
850//{
851// ADC_Wakeup();
852//
853// //Vengono accese Vref pos e Vref neg
854// GPIO_SetDir(1, Shut_Vrefp_Vrefn, GPIO_DIR_INPUT);
855//
856// //Vengono accese Vcc e Vee
857// GPIO_SetDir(1, Shut_Vcc_Vee, GPIO_DIR_INPUT);
858
859// //Vengono alzati gli altri due pin per non lasciare spente le alimentazioni
860// GPIO_PinWrite(2, DIG_5, 1);
861// GPIO_PinWrite(2, DIG_3_3, 1);
862//
863// //Vengono reimpostati tutti i PIN
864// GPIO_PinWrite(0, Chip_Sel_Adc, 1); // Viene abbassato il Chip_Sel_Adc
865// GPIO_PinWrite(0, Chip_Sel_rele_0, 1); // Viene abbassato il Chip Sel rele del canale 0
866// GPIO_PinWrite(1, Set_ch0, 1); // Impostando il set a 0 vengono abbassati tutti gli interruttori
867// GPIO_PinWrite(1, Reset_ch0 ,1); // Viene impostato il Reset a 0
868//// GPIO_PinWrite(0, Chip_Sel_rele_1, 1); // Viene abbassato il Chip Sel rele del canale 1
869// GPIO_PinWrite(1, Set_ch1, 1); // Impostando il set a 0 vengono abbassati tutti gli interruttori
870// GPIO_PinWrite(1, Reset_ch1 ,1); // Viene impostato il Reset a 0
871// GPIO_PinWrite(0, PGA_Chip_Sel_DAC_ch0, 1); // Viene abbassato il Chip Sel del DAC del canale 0
872// GPIO_PinWrite(0, PGA_Chip_Sel_DAC_ch1, 1); // Viene abbassato il Chip Sel del DAC del canale 1
873// GPIO_PinWrite(1, PGA_rele_ch0_1, 0); // Viene abbassato il Chip Sel rele del canale 1
874// GPIO_PinWrite(1, PGA_rele_ch0_2, 0); // Viene abbassato il Chip Sel rele del canale 0
875// GPIO_PinWrite(1, PGA_rele_ch1_1, 0); // Viene abbassato il Chip Sel rele del canale 1
876// GPIO_PinWrite(1, PGA_rele_ch1_2, 0); // Viene abbassato il Chip Sel rele del canale 0
877//// GPIO_PinWrite(1, Chip_Sel_Multiplexer_4, 0); // Viene abbassato il Chip Sel rele del canale 0
878//// GPIO_PinWrite(1, Chip_Sel_Multiplexer_3, 0); // Viene abbassato il Chip Sel rele del canale 0
879// GPIO_PinWrite(1, Chip_Sel_Multiplexer_2, 0); // Viene abbassato il Chip Sel rele del canale 0
880// GPIO_PinWrite(1, Chip_Sel_Multiplexer_1, 0); // Viene abbassato il Chip Sel rele del canale 0
881// GPIO_PinWrite(1, Sel_Par_Interruttori_3, 0); // Viene abbassato il Chip Sel rele del canale 0
882// GPIO_PinWrite(1, Sel_Par_Interruttori_2, 0); // Viene abbassato il Chip Sel rele del canale 0
883// GPIO_PinWrite(1, Sel_Par_Interruttori_1, 0); // Viene abbassato il Chip Sel rele del canale 0
884
885
886// SPI_Inizialize(); // Vengono riinizializzate le Periferiche
887// I2C_1_Initialize();
888// I2C_0_Initialize();
889//// I2C_termometro_Initialize(); //Questa non serve???????????????????????????????????
890//return;
891//}
892
893
894
895/*! \brief Output offset voltage compensation
896
897*\return No Parameters, the actual result is sent to the CAN bus output throught \ref tx_data.
898
899*\snippetlineno Istruzioni.c fun_instruction_Offset_compensation
900*/
901//! <!-- [fun_instruction_Offset_compensation] -->
903 #define offset_PGA_1_or_pre_0_out 7 /* This is at byte istruzione_byte_1 */
904 #define offset_different_between_ch 6 /* This is at byte istruzione_byte_1 */
905 #define offset_drift_adjusted_on_offset 5 /* This is at byte istruzione_byte_1 */
906 #define attivazione_CMRR 2 /*if 1 the CMRR is inactive at half scale, if 0 the CMRR is loaded from EPROM and set in the trimmer*/
907 #define JFET_offset_used_0_not_used_1 1 /* If 1 the JFET offset from EPROM is not used as default*/
908 #define JFET_offset_measured_and_stored_in_EPROM 0 /* If 1 the JFET offset is measured and store in EPROM*/
909
910
911
912 //Definizioni delle varibaili di stato
913 #define stato_esci_dalla_misura (int)(-1 )
914 #define stato_compensazione_termica_su_zero_offset 0
915 #define stato_compensazione_in_temperatura_sull_0ffset_secondo_passo 1
916 #define stato_compensazione_in_temperatura_sull_0ffset 2
917 #define stato_senza_compensazione_in_temperatura 3
918 #define stato_misurare_parametro_per_EPROM_terzo_passo 4
919 #define stato_misurare_parametro_per_EPROM_secondo_passo 5
920 #define stato_misurare_parametro_per_EPROM 6
921//struct trimmer_type /*{
922// unsigned char fine_thermal;
923// unsigned char fine_offset;
924// unsigned char coarse_thermal;
925// unsigned char coarse_offset;
926//}*/ Trimmer_offset_preampli[2];
927// int16_t Valori_per_i_trimmer[2]; // Qui verranno inseriti i valori per i trimmer
928 char canale;
929 signed char stato_della_misura;
930 unsigned char dati_letti[4],buffer_vettore_istruzioni_istruzione_byte_0=vettore_istruzioni[istruzione_byte_0];
931 unsigned short errore, errore_temperatura, indirizzo_da_usare;
932 char gain_da_moltiplicare;
933 long int offset, discrepanza[2];
934 int tolleranza=vettore_istruzioni[istruzione_byte_3]*1000; //in microV
935 int attesa, misura_finale[2];
936// char nodo_di_misura_1[]={ADC_ADC_PGA_neg_in_ch0, ADC_ADC_PGA_neg_in_ch1};
937// char nodo_di_misura_2[]={ADC_ADC_PGA_pos_in_ch0, ADC_ADC_PGA_pos_in_ch1};
938 char ii, nodo_di_misura_1[]={ADC_ADC_PGA_neg_out_ch0, ADC_ADC_PGA_neg_out_ch1};
939 char nodo_di_misura_2[]={ADC_ADC_PGA_pos_out_ch0, ADC_ADC_PGA_pos_out_ch1};
940 char chi_aggiustiamo = vettore_istruzioni[istruzione_byte_4_e_scelta_canale] & 0x3;
941// bool errore_canale[]={false,false}, errore_totale=false, saturazione_canale[]={false,false};
942 bool saturazione_canale[]={false,false};
943 int temperatura[2];
944 int coarse_trimmer_step[] = {coarse_Trimmer_offset_step_actual[0] * guadagno_minimo_PGA, coarse_Trimmer_offset_step_actual[1] * guadagno_minimo_PGA} ; //********************************************* coarse_Trimmer_offset_step_actual
945 int fine_trimmer_step[] ={ fine_Trimmer_offset_step_actual[0] * guadagno_minimo_PGA,fine_Trimmer_offset_step_actual[1] * guadagno_minimo_PGA} ; //*********************************************
946// char nodo_di_misura[]={ADC_ADC_PGA_pos_out_ch0, ADC_ADC_PGA_pos_out_ch1};
947
948 //Impostiamo il trimmer del CMRR
949 if( (vettore_istruzioni[istruzione_byte_1] >> attivazione_CMRR ) & 1 ){
950 attiva_il_CMRR( chi_aggiustiamo /*1 per canale 0, 2 per canale 1, 3 per entrambi i canali*/,0);
951 }else{
952 attiva_il_CMRR( chi_aggiustiamo /*1 per canale 0, 2 per canale 1, 3 per entrambi i canali*/,1);
953 }
954
955 if ( (tolleranza << 1) < (fine_trimmer_step[0]+fine_trimmer_step[1])) tolleranza = (fine_trimmer_step[0]+fine_trimmer_step[1]) >> 1;
956// int valore_target_per_ch[canale] =1000*(signed short)( vettore_istruzioni[byte_istr_dati_0] & 0xFF + ( (signed char)(0xFF & vettore_istruzioni[byte_istr_dati_1])*(1<<8) ));
957
958 if( (vettore_istruzioni[istruzione_byte_1] >> offset_PGA_1_or_pre_0_out) &1){
959 //Offset at Pre output
960 nodo_di_misura_1[0]=ADC_ADC_PGA_neg_in_ch0;
961 nodo_di_misura_1[1]= ADC_ADC_PGA_neg_in_ch1;
962 nodo_di_misura_2[0]=ADC_ADC_PGA_pos_in_ch0;
963 nodo_di_misura_2[1]=ADC_ADC_PGA_pos_in_ch1;
964 coarse_trimmer_step[0] = coarse_Trimmer_offset_step_actual[0] ;
965 fine_trimmer_step[1] = coarse_Trimmer_offset_step_actual[1] ;
966 gain_da_moltiplicare= 1;
967 }else{
968 gain_da_moltiplicare= guadagno_minimo_PGA;
969 }
970
971if( (vettore_istruzioni[istruzione_byte_1] >> offset_drift_adjusted_on_offset) & 1){
972 stato_della_misura=stato_compensazione_in_temperatura_sull_0ffset; //Si fanno 2 giri perch\'e si vuole aggiustare la deriva con l'offset
973 }else{
974 stato_della_misura=stato_compensazione_termica_su_zero_offset; //Si fa un solo giro perch\'e si aggiusta la deriva ad offset zero.
975}
976if ( buffer_vettore_istruzioni_istruzione_byte_0 == 0xff) stato_della_misura=stato_senza_compensazione_in_temperatura;
977
978if( (vettore_istruzioni[istruzione_byte_1] >> JFET_offset_measured_and_stored_in_EPROM) & 1) stato_della_misura=stato_misurare_parametro_per_EPROM;
979
980for( canale=0;canale<2;canale++){
981 if( (chi_aggiustiamo >> canale) & 1){
982 if( (vettore_istruzioni[istruzione_byte_1] >> JFET_offset_used_0_not_used_1) & 1) {//Non si usa la EPROM per l'offset del JFET, quindi trimmer a met\'a scala
983 Trimmer_offset_preampli[canale].JFET_Offset=0x80;
984 }else{//Usiamo la EPROM
985// lettura_EPROM(canale, Memory_address_JFET_trimmer_offset , dati_letti);
986 Trimmer_offset_preampli[canale].JFET_Offset=dati_letti[0];
987 Trimmer_offset_preampli[canale].coarse_offset=dati_letti[1];
988 Imposta_canale_del_trimmer_ennesimo_al_valore_I2CA (canale, preampli_corse_offset_trimmer, Trimmer_offset_preampli[canale].coarse_offset);
989 }
990 Imposta_canale_del_trimmer_ennesimo_al_valore_I2CA (canale, preampli_JFET_Offset_trimmer, Trimmer_offset_preampli[canale].JFET_Offset);
991 }
992}
993
994 while (stato_della_misura >=0){
995//***********************************************************************************************************
996 //Vediamo che temperatura eventualmente usare e che range adottare
997 //Lo facciamo per entrambi i canali anche se poi magari agiremo solo su uno
998 if ( buffer_vettore_istruzioni_istruzione_byte_0 <0xff){
999 //Impostaiamo il trimmer per la deriva alla temperatura data o la misuriamo
1000 if ( buffer_vettore_istruzioni_istruzione_byte_0==0){
1001 Misura_della_temperatura(temperatura);
1002 for (ii=0;ii<2;ii++){
1003 temperatura[ii]= temperatura[ii]*3;
1004 temperatura[ii]= temperatura[ii]/1000; //Normalizziamo a quello memorizzato
1005 }}else{
1006 temperatura[0]=buffer_vettore_istruzioni_istruzione_byte_0*3;
1007 temperatura[1]=temperatura[0];
1008 }
1009 //Cerchiamo la temperatura pi\'u prossima per l'impostazione del trimer
1010 for (canale=0; canale<2;canale++){
1011 errore=0xff;
1012 for (ii=0; ii< numero_temperature_impostazione_deriva[canale]; ii++){
1013 errore_temperatura= abs( valori_temperature_impostazione_deriva[canale][ii] - temperatura[canale]);
1014 if (errore_temperatura < errore){
1015 errore=errore_temperatura;
1016 temperatura_di_lavoro[canale]= ii;
1017 }
1018 }
1019 }
1020 }
1021 //Imposttiamo il trimmer alla deriva a media temperatura
1022 for (canale=0; canale<2;canale++){
1023
1024 if( stato_della_misura ==0){// Consideriamo l'offset a zero
1025 if (canale) stato_della_misura=stato_esci_dalla_misura;
1026 if( (chi_aggiustiamo >> canale) & 1){
1027 offset_di_lavoro[canale]= (numero_offset_per_impostazione_deriva[canale] +1 ) >>1 ; //Prendo l'offset a met\'a scala, approssimando all'intero maggiore
1028// indirizzo_da_usare= offset_di_lavoro[canale] * numero_temperature_impostazione_deriva[canale] + temperatura_di_lavoro[canale]+ \
1029// Memory_address_lookup_table_start ;
1030// lettura_EPROM(canale, indirizzo_da_usare , dati_letti);
1031 Trimmer_offset_preampli[canale].coarse_thermal = dati_letti[ indirizzo_da_usare & 3];
1032 }
1033 } // fine stato_della_misura=0
1034
1035 if( stato_della_misura ==stato_compensazione_in_temperatura_sull_0ffset_secondo_passo){ //Aggiustiamo con la temperatura accordata anche sulloffset. Veniamo dallo stato 2
1036 if (canale) stato_della_misura=stato_esci_dalla_misura;
1037 if( (chi_aggiustiamo >> canale) & 1){
1038 errore=0xff;
1039// lettura_EPROM( canale, Memory_address_JFET_trimmer_offset , dati_letti);
1040 for (ii=0; ii< numero_offset_per_impostazione_deriva[canale]; ii++){
1041 errore_temperatura= abs( valori_offset_per_impostazione_deriva[canale][ii] - (signed char)(Trimmer_offset_preampli[canale].coarse_offset - dati_letti[1] ) ) ;
1042 if (errore_temperatura < errore){
1043 errore=errore_temperatura;
1044 offset_di_lavoro[canale]= ii;
1045 }
1046 }
1047// indirizzo_da_usare= offset_di_lavoro[canale] * numero_temperature_impostazione_deriva[canale] + temperatura_di_lavoro[canale]+ \
1048// Memory_address_lookup_table_start ;
1049// lettura_EPROM(canale, indirizzo_da_usare , dati_letti);
1050 Trimmer_offset_preampli[canale].coarse_thermal = dati_letti[ indirizzo_da_usare & 3];
1051 }
1052 } //fine stato_della_misura =stato_compensazione_in_temperatura_sull_0ffset_secondo_passo
1053
1054 if( stato_della_misura ==stato_compensazione_in_temperatura_sull_0ffset){//Vogliamo aggiustare la temperatura sull'offset: primo passaggio , troviamo l'offset
1055 if (canale) stato_della_misura=stato_compensazione_in_temperatura_sull_0ffset_secondo_passo;
1056 if( (chi_aggiustiamo >> canale) & 1){
1057 offset_di_lavoro[canale]= (numero_offset_per_impostazione_deriva[canale] +1 ) >>1 ; //Prendo l'offset a met\'a scala, approssimando all'intero maggiore
1058 //Se stato_della_misura =2 devo valutare quale offset abbiamo , prima di impostare la temperatura. Per cui metto il trimmer a met\'a scala
1059 Trimmer_offset_preampli[canale].coarse_thermal = 0x80;
1060 }
1061 } //fine stato_della_misura =stato_compensazione_in_temperatura_sull_0ffset
1062
1063 if( stato_della_misura ==stato_senza_compensazione_in_temperatura){//Aggiustiamo senza compensazione in temperatura
1064 if (canale) stato_della_misura=stato_esci_dalla_misura;
1065 if( (chi_aggiustiamo >> canale) & 1){
1066 Trimmer_offset_preampli[canale].coarse_thermal = 0x80;
1067 }
1068 } //fine stato_della_misura =stato_senza_compensazione_in_temperatura
1069
1070
1071 if( stato_della_misura ==stato_misurare_parametro_per_EPROM_secondo_passo){//Secondo passo per offset in EPROM: metto il dato eprom in trimmer e l'offset in eprom
1072 if (canale) stato_della_misura=stato_misurare_parametro_per_EPROM_terzo_passo; //Lo stato 4 \'ein fondo perch\'e serve solo per memorizzare l'offset nella eprom
1073 if( (chi_aggiustiamo >> canale) & 1){
1074 Trimmer_offset_preampli[canale].JFET_Offset=0x80 +\
1075 (( (128 - Trimmer_offset_preampli[canale].coarse_offset) * JFET_offset_conversione) >> 7) ;
1076 Trimmer_offset_preampli[canale].coarse_offset=0x80;
1077 Imposta_canale_del_trimmer_ennesimo_al_valore_I2CA (canale, preampli_JFET_Offset_trimmer, Trimmer_offset_preampli[canale].JFET_Offset);
1078 }
1079 } //fine stato_della_misura =stato_misurare_parametro_per_EPROM_secondo_passo
1080
1081 if( stato_della_misura ==stato_misurare_parametro_per_EPROM){//Primo passo per offset in EPROM: misuro a offset 0 con ERPOM a met\'a scala
1082 if (canale) stato_della_misura=stato_misurare_parametro_per_EPROM_secondo_passo;
1083 if( (chi_aggiustiamo >> canale) & 1){
1084 Trimmer_offset_preampli[canale].coarse_thermal = 0x80;
1085 Trimmer_offset_preampli[canale].JFET_Offset=0x80;
1086 Imposta_canale_del_trimmer_ennesimo_al_valore_I2CA (canale, preampli_JFET_Offset_trimmer, Trimmer_offset_preampli[canale].JFET_Offset);
1089 }
1090 } //fine stato_della_misura =stato_misurare_parametro_per_EPROM
1091
1092// if ( buffer_vettore_istruzioni_istruzione_byte_0 <0xff){
1093// if ( (stato_della_misura ==0) || (stato_della_misura==2) ){
1094// //Se stato della misura =0 vogliamo stare a met\'a con l'offset;
1095// //se stato della misura =2 misuriamo a met\'a scala per la temperatura
1096// offset_di_lavoro[canale]= (numero_offset_per_impostazione_deriva[canale] +1 ) >>1 ; //Prendo l'offset a met\'a scala, approssimando all'intero maggiore
1097//
1098// if(stato_della_misura==2){
1099// //Se stato_della_misura =2 devo valutare quale offset abbiamo , prima di impostare la temperatura. Per cui metto il trimmer a met\'a scala
1100// Trimmer_offset_preampli[canale].coarse_thermal = 0x80;
1101// }else{
1102// //Se stato_della_misura \'e zero estraggo dalla memoria il valore da mettere nel trimmer della temperatura
1103//// indirizzo_da_usare= offset_di_lavoro[canale] * numero_temperature_impostazione_deriva[canale] + temperatura_di_lavoro[canale]+ \
1104//// Memory_address_lookup_table_start ;
1105//// lettura_EPROM(canale, indirizzo_da_usare , dati_letti);
1106//// Trimmer_offset_preampli[canale].coarse_thermal = dati_letti[ indirizzo_da_usare & 3];
1107// }
1108//
1109// if (canale) stato_della_misura--;
1110// }else{//Troviamo la polarizzazione pi\'u vicina
1111//
1112// errore=0xff;
1113// for (ii=0; ii< numero_offset_per_impostazione_deriva[canale]; ii++){
1114// errore_temperatura= abs( valori_offset_per_impostazione_deriva[canale][ii] - Trimmer_offset_preampli[canale].coarse_offset) ;
1115// if (errore_temperatura < errore){
1116// errore=errore_temperatura;
1117// offset_di_lavoro[canale]= ii;
1118// }
1119// }
1120// indirizzo_da_usare= offset_di_lavoro[canale] * numero_temperature_impostazione_deriva[canale] + temperatura_di_lavoro[canale]+ \
1121// Memory_address_lookup_table_start ;
1122// lettura_EPROM(canale, indirizzo_da_usare , dati_letti);
1123// Trimmer_offset_preampli[canale].coarse_thermal = dati_letti[ indirizzo_da_usare & 3];
1124// if (canale) stato_della_misura = -1;
1125// }
1126//// indirizzo_da_usare= offset_di_lavoro[canale] * numero_temperature_impostazione_deriva[canale] + temperatura_di_lavoro[canale]+ \
1127//// Memory_address_lookup_table_start ;
1128//// lettura_EPROM(canale, indirizzo_da_usare , dati_letti);
1129//// Trimmer_offset_preampli[canale].coarse_thermal = dati_letti[ indirizzo_da_usare & 3];
1130// }else{
1131// Trimmer_offset_preampli[canale].coarse_thermal=0x80;
1132// if (canale) stato_della_misura = -1;
1133// }
1134 if( (chi_aggiustiamo >> canale) & 1){
1135 Imposta_canale_del_trimmer_ennesimo_al_valore_I2CA (canale, preampli_coarse_thermal_trimmer, Trimmer_offset_preampli[canale].coarse_thermal );
1136// Trimmer_offset_preampli[canale].fine_thermal=0x80;
1137// Imposta_canale_del_trimmer_ennesimo_al_valore_I2CA (canale, preampli_fine_thermal_trimmer, Trimmer_offset_preampli[canale].fine_thermal);
1138 //Ora compensiamo l'offset per il contributo impostato dal trimmer
1139 Trimmer_offset_preampli[canale].coarse_offset = ((Trimmer_offset_preampli[canale].coarse_offset + Trimmer_offset_preampli[canale].coarse_thermal - 128)*coarse_Trimmer_drift_offset_ratio_by_2e7) >> 7 ;
1140 if( Trimmer_offset_preampli[canale].coarse_offset > 255) Trimmer_offset_preampli[canale].coarse_offset=255;
1141 if( Trimmer_offset_preampli[canale].coarse_offset <0) Trimmer_offset_preampli[canale].coarse_offset=0;
1142 Imposta_canale_del_trimmer_ennesimo_al_valore_I2CA (canale, preampli_corse_offset_trimmer, Trimmer_offset_preampli[canale].coarse_offset);
1143 }
1144 }
1145
1146
1147//***********************************************************************************************************
1148
1149 if( ((vettore_istruzioni[istruzione_byte_1] >> offset_different_between_ch) &1)==0 ) {
1150 //Offset is get from the first 2 bytes of the isntruction, otherwise is already given elsewhere
1153 valore_target_per_offset[0] = 1000*( *(signed short *) &vettore_istruzioni[byte_istr_dati_0]);
1154 valore_target_per_offset[1]=valore_target_per_offset[0];
1155 }
1156 int valore_target_per_ch[]={(valore_target_per_offset[0] *(int)gain_da_moltiplicare )/ (int)PGA_Gain_V_su_V[0], (valore_target_per_offset[1] *(int)gain_da_moltiplicare)/ (int)PGA_Gain_V_su_V[1]};
1158 if (attesa == 0) attesa=1000;
1159 if( attesa > 10000) attesa =10000;
1160
1161 //Set the PGA Gain to 1 and retain the actual
1162 char PGA_Gain_V_su_V_old[]={PGA_Gain_V_su_V[0], PGA_Gain_V_su_V[1]}; //*********************************************
1163 PGA_GAIN_set(3, 1);
1164
1165 Aspetta_tanti_ms(attesa);
1166
1167 for( canale=0;canale<2;canale++){
1168 if( (chi_aggiustiamo >> canale) & 1){
1169// offset=lettura_ADC( nodo_di_misura_1[canale]) ; //Il <<1 \'e per renderlo differenziale
1170 risultato=correzione_misura_ADC(coeffcienti_misura_ADC_nodi[nodo_di_misura_1[canale]], offset);
1171 if ( (risultato >= saturazione_positiva) || (risultato <= saturazione_negativa) ){
1172 //Dobbiamo iniziare con SAR
1173 saturazione_canale[canale]=true;
1174 }
1175 }
1176 }
1177
1178aggiustamento_SAR(saturazione_canale, attesa, nodo_di_misura_1); // Effettivo solo se saturazione_canale \'e true
1179
1180// Aspetta_tanti_ms(attesa);
1181//LEGGO L'USCITA Neg
1182 for( canale=0;canale<2;canale++){
1183 if( (chi_aggiustiamo >> canale) & 1){
1184// offset=lettura_ADC( nodo_di_misura_1[canale]) - lettura_ADC( nodo_di_misura_2[canale]); //Il <<1 \'e per renderlo differenziale
1185 offset=correzione_misura_ADC(coeffcienti_misura_ADC_nodi[nodo_di_misura_1[canale]], offset);
1186 discrepanza[canale] = offset - valore_target_per_ch[canale] ;
1187
1188//HO L'OFFSET
1189
1190 Trova_valori_trimmer_per_offset_preamplificatore(discrepanza[canale], &Trimmer_offset_preampli[canale], coarse_trimmer_step[canale], fine_trimmer_step[canale]);
1191
1192 Imposta_canale_del_trimmer_ennesimo_al_valore_I2CA (canale, preampli_corse_offset_trimmer, Trimmer_offset_preampli[canale].coarse_offset);
1193 Imposta_canale_del_trimmer_ennesimo_al_valore_I2CA (canale, preampli_fine_offset_trimmer, Trimmer_offset_preampli[canale].fine_offset);
1194 }
1195 }
1196 Aspetta_tanti_ms(attesa);
1197
1198
1199// offset_Adjust_core( chi_aggiustiamo, valore_target_per_ch, nodo_di_misura_1, nodo_di_misura_2, \
1200// tolleranza, coarse_trimmer_step, fine_trimmer_step, attesa, misura_finale);
1201
1202// Abbassa_chip_selector_multiplexer();
1203
1204 PGA_Gain_V_su_V[0]=PGA_Gain_V_su_V_old[0]; //*********************************************
1205 PGA_Gain_V_su_V[1]=PGA_Gain_V_su_V_old[1];
1206 PGA_GAIN_set(1, PGA_Gain_V_su_V[0]);
1207 PGA_GAIN_set(2, PGA_Gain_V_su_V[1]);
1208
1209 for( canale=0;canale<2;canale++){
1210 if( (chi_aggiustiamo >> canale) & 1){
1211// int offset=lettura_ADC( nodo_di_misura_1[canale]) - lettura_ADC( nodo_di_misura_2[canale]); //Il <<1 \'e per renderlo differenziale
1212 offset=correzione_misura_ADC(coeffcienti_misura_ADC_nodi[nodo_di_misura_1[canale]], offset) * (int)guadagno_minimo_PGA / (int)PGA_Gain_V_su_V[canale] ;
1213 int correzione = ( misura_finale[canale] - offset ) / (( (fine_trimmer_step[0]+fine_trimmer_step[1])>>1) ) ;
1214 Trimmer_offset_preampli[canale].fine_offset= Trimmer_offset_preampli[canale].fine_offset - correzione;
1215 if (Trimmer_offset_preampli[canale].fine_offset >255) Trimmer_offset_preampli[canale].fine_offset=255;
1216 if( Trimmer_offset_preampli[canale].fine_offset <0 ) Trimmer_offset_preampli[canale].fine_offset=0;
1217 Imposta_canale_del_trimmer_ennesimo_al_valore_I2CA (canale, preampli_fine_offset_trimmer, Trimmer_offset_preampli[canale].fine_offset);
1218 }
1219 }
1220
1221
1222 Aspetta_tanti_ms(attesa);
1223
1224for(canale=0;canale<2;canale++){
1225// offset=lettura_ADC( nodo_di_misura_1[canale]) - lettura_ADC( nodo_di_misura_2[canale]); //Il <<1 \'e per renderlo differenziale
1226 misura_finale[canale]=correzione_misura_ADC(coeffcienti_misura_ADC_nodi[nodo_di_misura_1[canale]], offset);
1227}
1228
1229misura_finale[0] =(misura_finale[0] / 1000);
1230tx_data[byte_istr_dati_0] = (unsigned int)(misura_finale[0]) & 0xFF ;
1231tx_data[byte_istr_dati_1] = (( (unsigned int)(misura_finale[0]) ) >> 8 ) & 0xFF ;
1232misura_finale[1] =(misura_finale[1] / 1000);
1233tx_data[byte_istr_dati_2] = (unsigned int)(misura_finale[1]) & 0xFF ;
1234tx_data[byte_istr_dati_3] = (( (unsigned int)(misura_finale[1]) ) >> 8 ) & 0xFF ;
1235 //Set the PGA Gain to 1 and retain the actual
1236 //PGA gain is restablished
1237 if( stato_della_misura ==stato_misurare_parametro_per_EPROM_terzo_passo){////Terzo e ultimo passo per offset in EPROM: metto offset in eprom in trimmer e l'offset in eprom
1238 stato_della_misura=stato_esci_dalla_misura;
1239 for(canale=0;canale<2;canale++){
1240 if( (chi_aggiustiamo >> canale) & 1){
1241// int dato_da_scrivere= Trimmer_offset_preampli[canale].JFET_Offset + \
1242// (Trimmer_offset_preampli[canale].coarse_offset <<8);
1243// scrivi_mem_EPROM( Memory_address_JFET_trimmer_offset , dato_da_scrivere,canale);
1244 }
1245 }
1246 } //fine stato_della_misura =4
1247}
1248}
1249//! <!-- [fun_instruction_Offset_compensation] -->
1250
1251/*! \brief When the preamplifier output is saturated we start with the standard SAR
1252
1253
1254*\param[in] saturazione_canale is boolean ans says which channel is saturated and should be worked
1255*\param[in] attesa is the waiting time between trimmer settings
1256*\param[in] nodo_di_misura_1 is the vector of the nodes to be measured with the ADC
1257*\return No Parameters.
1258
1259*\snippetlineno Istruzioni.c fun_aggiustamento_SAR
1260*/
1261//! <!-- [fun_aggiustamento_SAR] -->
1262void aggiustamento_SAR(bool *saturazione_canale, int attesa, char *nodo_di_misura_1){
1263 char canale;
1264 for( canale=0;canale<2;canale++){
1265 if( saturazione_canale[canale]){
1266 Trimmer_offset_preampli[canale].fine_offset=0x80;
1267 Imposta_canale_del_trimmer_ennesimo_al_valore_I2CA (canale, preampli_fine_offset_trimmer, 0x80);
1268 Trimmer_offset_preampli[canale].coarse_offset =0;
1269 }
1270 }
1271 signed char posizione_bit_di_test;
1272
1273 for( posizione_bit_di_test =7; posizione_bit_di_test>=0;posizione_bit_di_test--){
1274 for( canale=0;canale<2;canale++){
1275 if( saturazione_canale[canale]){
1276 Trimmer_offset_preampli[canale].coarse_offset |=(1 << posizione_bit_di_test) & 0xFF;
1277 Imposta_canale_del_trimmer_ennesimo_al_valore_I2CA (canale, preampli_corse_offset_trimmer, Trimmer_offset_preampli[canale].coarse_offset);
1278 }
1279 }
1280 if ( saturazione_canale[0] | saturazione_canale[1]){
1281 Aspetta_tanti_ms(attesa);
1282 }
1283 for( canale=0;canale<2;canale++){
1284 if( saturazione_canale[canale]){
1285// risultato=lettura_ADC( nodo_di_misura_1[canale]) ; //Il <<1 \'e per renderlo differenziale
1286 risultato=correzione_misura_ADC(coeffcienti_misura_ADC_nodi[nodo_di_misura_1[canale]], risultato);
1287 if ( (risultato >= saturazione_positiva) ){
1288 //Ancora troppo poco
1289 saturazione_canale[canale]=true;
1290 }else{
1291 //Vediamo se non sia troppo
1292 if( (risultato <= saturazione_negativa) ){
1293 //abbiamo esagerato
1294 Trimmer_offset_preampli[canale].coarse_offset &= ~(1 << posizione_bit_di_test);
1295 saturazione_canale[canale]=true;
1296 }else{
1297 //siamo ok
1298 saturazione_canale[canale]=false;
1299 }
1300 }
1301 }
1302 }
1303}
1304}
1305//! <!-- [fun_aggiustamento_SAR] -->
1306
1307
1308///*! \brief This is the core of the adjusting offset. It tryes to get the target voltage knowing the trimmer steps
1309
1310
1311//*\param[in] chi_aggiustiamo which channels to adjust
1312//*\param[in] valore_target_per_ch the final target voltage in &mu;V
1313//*\param[in] nodo_di_misura_1 is the vector of the positive output nodes to be measured with the ADC
1314//*\param[in] nodo_di_misura_2 is the vector of the negative output nodes to be measured with the ADC
1315//*\param[in] tolleranza is the admitted error in &mu;V
1316//*\param[in] coarse_trimmer_step is the coarse trimmer step voltage in &mu;V
1317//*\param[in] fine_trimmer_step is the fine trimmer step voltage in &mu;V
1318//*\param[in] attesa is the waiting time between trimmer settings in ms
1319//*\param[out] misura_finale is the vector with the final measurements
1320//*\return No Parameters.
1321
1322//*\snippetlineno Istruzioni.c fun_offset_Adjust_core */
1323//! <!-- [fun_offset_Adjust_core -->
1324//void offset_Adjust_core(char chi_aggiustiamo, int *valore_target_per_ch, char *nodo_di_misura_1, char *nodo_di_misura_2, \
1325// int tolleranza, int *coarse_trimmer_step, int *fine_trimmer_step, unsigned int attesa, int *misura_finale){
1326// char canale;
1327// int offset, discrepanza[2];
1328// signed short nn;
1329// bool errore_canale[]={false,false}, errore_totale=false;
1330//
1331// for( canale=0;canale<2;canale++){
1332// if( (chi_aggiustiamo >> canale) & 1){
1333//// offset=lettura_ADC( nodo_di_misura_1[canale]) - lettura_ADC( nodo_di_misura_2[canale]); //Il <<1 \'e per renderlo differenziale
1334// risultato=correzione_misura_ADC(coeffcienti_misura_ADC_nodi[nodo_di_misura_1[canale]], offset);
1335// misura_finale[canale]= risultato;
1336// discrepanza[canale] = risultato - valore_target_per_ch[canale] ;
1337// errore_canale[canale]= discrepanza[canale] <= -tolleranza || discrepanza[canale] >= tolleranza;
1338// errore_totale |=errore_canale[canale];
1339// }
1340// }
1341
1342
1343// char massimo_numero_misure=0;
1344// while( errore_totale && (massimo_numero_misure <10) ) // Se la |discrepanza[canale]| < tot, viene corretta la tensione con i trimmer
1345// {
1346// massimo_numero_misure++;
1347// for( canale=0;canale<2;canale++){
1348// if ( errore_canale[canale]){
1349// if (abs( discrepanza[canale] ) > coarse_trimmer_step[canale])
1350// {
1351// nn = discrepanza[canale] / coarse_trimmer_step[canale]; // Passo trimmer grezzo, ogni passo sono 73 mV
1352// Trimmer_offset_preampli[canale].coarse_offset= Trimmer_offset_preampli[canale].coarse_offset+nn;
1353// if ( Trimmer_offset_preampli[canale].coarse_offset >0xff) Trimmer_offset_preampli[canale].coarse_offset=0xFF;
1354// if ( Trimmer_offset_preampli[canale].coarse_offset <0) Trimmer_offset_preampli[canale].coarse_offset=0;
1355// }
1356// else
1357// {
1358// nn = discrepanza[canale] / fine_trimmer_step[canale]; // Passo trimmer fine, ogni passo sono 0.1 mV
1359// Trimmer_offset_preampli[canale].fine_offset= Trimmer_offset_preampli[canale].fine_offset+nn;
1360// if (Trimmer_offset_preampli[canale].fine_offset >0xff){
1361// Trimmer_offset_preampli[canale].fine_offset=0x80;
1362// Trimmer_offset_preampli[canale].coarse_offset ++;
1363// }
1364// if (Trimmer_offset_preampli[canale].fine_offset <0){
1365// Trimmer_offset_preampli[canale].fine_offset=0x80;
1366// Trimmer_offset_preampli[canale].coarse_offset --;
1367// }
1368// }
1369//
1370// Imposta_canale_del_trimmer_ennesimo_al_valore_I2CA (canale, preampli_fine_offset_trimmer, Trimmer_offset_preampli[canale].fine_offset);
1371//
1372// Imposta_canale_del_trimmer_ennesimo_al_valore_I2CA (canale, preampli_corse_offset_trimmer, Trimmer_offset_preampli[canale].coarse_offset);
1373// }
1374// }
1375//
1376// Aspetta_tanti_ms(attesa);
1377//
1378// errore_totale=false;
1379// for( canale=0;canale<2;canale++){
1380// if ( errore_canale[canale]){
1381//// offset=lettura_ADC( nodo_di_misura_1[canale]) - lettura_ADC( nodo_di_misura_2[canale]); //Il <<1 \'e per renderlo differenziale
1382// risultato=correzione_misura_ADC(coeffcienti_misura_ADC_nodi[nodo_di_misura_1[canale]], offset);
1383// misura_finale[canale]= risultato;
1384// discrepanza[canale] = risultato - valore_target_per_ch[canale] ; // Si itera finch\'e |discrepanza| < tot
1385// errore_canale[canale]= discrepanza[canale] <= -tolleranza || discrepanza[canale] >= tolleranza;
1386// errore_totale |=errore_canale[canale];
1387// }
1388// }
1389// }
1390//
1391//}
1392//! <!-- [fun_offset_Adjust_core -->
1393
1394void attiva_il_CMRR( char chi_aggiustiamo /*1 per canale 0, 2 per canale 1, 3 per entrambi i canali*/, char attivare_si_no){
1395 unsigned char canale, dati_letti[4];
1396 for( canale=0;canale<2;canale++){
1397 if( (chi_aggiustiamo >> canale) & 1){
1398 if( attivare_si_no) {
1399// lettura_EPROM(canale, Memory_address_CMRR_compensation , dati_letti);
1400 }else{
1401 dati_letti[0]=128;
1402 }
1403 Trimmer_CMRR_JFET_COLD.CMRR[canale]=dati_letti[0];
1404 Imposta_trimmer_common_mode_I2CB ( Trimmer_numero_offset + canale, Trimmer_CMRR_JFET_COLD.CMRR[canale]);
1405 }
1406}
1407}
1408
int32_t ADC_misura_differenziale_con_media_generico(uint8_t scheda_su_scheda_giu_, uint8_t preamplifier_externalADC_1_onboardADC_0, uint8_t nodo_da_misurare, uint8_t differenziale_1_single_0)
This function sets the original gain when a few LSb have to be determined. The umber of bits is bit_t...
Definition: Adc.c:899
void ADC_sleep_ON_OFF(uint8_t up_down, uint8_t ADC_ON_1_OFF_0_NOP_GT1)
ADC is set to sleep or awake.
Definition: Adc.c:350
void instr_ADC_LETTURA_function(void)
All the posible nodes voltages can be read with this instruction. Either the internal or an external ...
Definition: Adc.c:761
unsigned char medie_ADC
The number of averages done is 1 << medie_ADC, or 2^medie_ADC.
Definition: Adc.c:985
volatile uint8_t ADC_sleep_off
if 0 ADC in sleep when not working, ADC>1 ADC never sleep
Definition: Adc.c:205
@ instr_Shut_Down_ON_OFF
µ-controller can be maintained always ON
@ instr_NO_operation
NOP, the fw version is replayed.
@ instr_offset_trimmer_calibration
fine and coarse trimmer calibration for ofset compensation
@ instr_preamplifier_memory
Write /read the preamplifier EPROM.
@ instr_PGA_DAC_GAIN_CROSS
Set the gain of the second stage of the PGA.
@ instr_PGA_gain_set
Set the PGA gain value.
@ instr_detector_Vbias_we_want_to_set
The voltage we intend to set to the detector. This command does not apply the voltage.
@ instr_readback_node_voltages
Nodes voltage read from external ADC.
@ instr_ERROR_inspection
quering about the presence of any error
@ instr_LED_ON_OFF
LEDs can be switched ON/OFF for test and diagnostic.
@ instr_output_offset_to_be_set
Adjust the ouput voltage that was previously set at instr_ouput_offset_we_want_to_set.
@ instr_Vbias_to_be_set
The voltage set with instr_detector_Vbias_we_want_to_set is set to the detector.
@ instr_ADC_LETTURA
ADC reading, the following byte from the CAN set the reading node.
@ instr_preamplifier_scrittura_lettura_trimmer_offset
Write /read to /from the trimmers for the preamplifier offset adjustment.
@ instr_preamplifier_temperature_meas
Measure the preamplifier temperature.
@ instr_EPROM_read_write
read from preamplifier or mainboard or post frontend board EPROM
@ instr_connect_disconnect_mux_node
select/deselect a node from the analog mux
@ instr_preamplifier_gain_low_high_set
Set /reset of the large gain for the preamplifier.
@ instr_inizializza_tutto_da_zero
µ-controller is reset. The perifelal are all initialized.
@ instr_ERROR_echo_on_off
if enabled, echo of the error via CAN as soon as the error is verified
@ instr_ouput_offset_we_want_to_set
Set the output offset to set channel per channel.
@ instr_detector_scrittura_lettura_trimmer_bias
Write /read to /from the trimmers for the detectors biases.
@ instr_Relays_driver_set_reset_channel
Set / reset of the relais that manage the detectors.
@ instr_readback_variables
Variables Readback
@ instr_DetBias_trimmer_calibration
fine and coarse trimmer calibration for detector bias compensation
@ instr_periferal_reset
I2C, SPI and CAN can be reset.
@ instr_PGA_set_reset_pre_PGA_gain
Set /reset of the first stage of PGA.
@ The_total_number_of_instrucions
This is the total numebr of instructions, not an instruction.
@ instr_offset_voltage_variable
detector set voltage vector
@ instr_PGA_gain_variable
PGA gain vector.
@ instr_detector_bias_variable
detector set voltage vector
uint8_t tx_data[8]
Transmission data vector.
Definition: Can.c:321
volatile uint8_t rx_data[8]
Received data vector.
Definition: Can.c:318
void PGA_GAIN_CROSS(uint8_t scheda_su_scheda_giu, uint8_t canale, uint8_t PGA_gain_to_set)
Here the 2 digital signals from the trimmer are exploited to set the gain of the input stage of the P...
Definition: DAC_PGA.c:109
uint8_t massimo_guadagno_impostabile
Definition: DAC_PGA.c:89
uint8_t guadagno_minimo_PGA
Definition: DAC_PGA.c:88
uint8_t PGA_settled_gain[12]
PGA set gain.
Definition: DAC_PGA.c:95
void PGA_set_reset_pre_PGA_gain(uint8_t scheda_su_scheda_giu, uint8_t canale, uint8_t set_2_reset_1_idle_0)
Here the 2 digital signals from the trimmer are exploited to set the gain of the input stage of the P...
Definition: DAC_PGA.c:143
void PGA_GAIN_set(char canale, unsigned int guadagno_da_impostare)
It sets the new gain irrespective of the offset.
Definition: DAC_PGA.c:443
void PGA_DAC_GAIN_CROSS(uint8_t scheda_su_scheda_giu, uint8_t canale, uint8_t guadagno_da_impostare, uint8_t PGA_si_NO)
Definition: DAC_PGA.c:193
#define PGA_set_pre_pga_low_gain
Set the amplifier gain in front of the PGA to 1 V/V, it is left shifted once in the trimmer.
Definition: DAC_PGA.h:8
void instr_detector_scrittura_lettura_trimmer_bias_function(void)
This is an instruction via CAN bus to read/write the detector trimmers. It calls the detector_scrittu...
int32_t Detector_bias_target[12]
Detector bias target values.
void instr_detector_Vbias_we_want_to_set_function(void)
void instr_Vbias_to_be_set_function(void)
The detector bias will be set at the value set with the function instr_detector_Vbias_we_want_to_set_...
void instr_Relays_driver_set_reset_channel_function(void)
Function to set the relais for detector bias.
#define wait_time_for_rele_setting
[ref_spi_clock_for_relais]
void Error_imposta_la_istruzione(void)
Function to be located at the end of every instruction to mark the error, if any.
void instr_ERROR_inspection_function(void)
The state of the errors can be verified/reset.
void ERROR_codifica_errore(uint8_t scheda_su_scheda_giu, unsigned char error_addres, unsigned char code_to_shift, uint8_t reset_count_se_0)
If an error is found its flag is codified here.
void instr_ERROR_echo_on_off_function(void)
Immediate echos of a verified error activated/disattivated.
void termometro_lettura(uint8_t scheda_su_scheda_giu, uint8_t canale, uint32_t *temperatura_in_microK)
Read the termometer on preamplifier.
void instr_readback_variables_function(void)
readback of some parameters
Definition: Istruzioni.c:179
void instruction_Misura_della_temperatura(void)
The temperature from both preamplifier is read.
Definition: Istruzioni.c:735
void instruction_lettura_ADC(void)
ADC instrunction implementation with node selection.
Definition: Istruzioni.c:472
void instr_NO_operation_function(void)
The NOP sends backs the fw version.
Definition: Istruzioni.c:99
void instr_Shut_Down_ON_OFF_function(void)
μ-controller power down ON/OFF
Definition: Istruzioni.c:116
void Setting_of_the_pointer_for_command_parser(void)
The pointer to the instruction functions is constructed here.
Definition: Istruzioni.c:33
void instr_readback_node_voltages_function(void)
readback of some node voltages from external ADC
Definition: Istruzioni.c:232
void instr_connect_disconnect_mux_node_function(void)
Routing of available node voltage to outout by means of the analog mux.
Definition: Istruzioni.c:446
void instr_PGA_gain_set_function(void)
PGA gain set.
Definition: Istruzioni.c:330
void instruction_Offset_compensation(void)
Output offset voltage compensation.
Definition: Istruzioni.c:902
void instr_preamplifier_gain_low_high_set_function(void)
preamplifier gain set large or small
Definition: Istruzioni.c:304
void instr_preamplifier_memory_function(void)
Definition: Istruzioni.c:275
void instr_preamplifier_temperature_meas_function(void)
Definition: Istruzioni.c:248
void(* ptr_istruzioni[The_total_number_of_instrucions])(void)
This is the pointer array to the instructions set: the byte istruzione_codice of the CAN message is t...
Definition: Istruzioni.c:24
void aggiustamento_SAR(bool *saturazione_canale, int attesa, char *nodo_di_misura_1)
When the preamplifier output is saturated we start with the standard SAR.
Definition: Istruzioni.c:1262
void instruction_Detector_PGA_Rele(void)
This is a sub-function of the ADC instrunction. It is useful as it cane be used in other part of the ...
Definition: Istruzioni.c:524
void instr_PGA_set_reset_pre_PGA_gain_function(void)
PGA input stage gain set.
Definition: Istruzioni.c:396
void instruction_Some_parameters_to_set(void)
Some usefull parematers can be set here.
Definition: Istruzioni.c:577
#define istruzione_byte_1
Definition: Istruzioni.h:21
#define istruzione_byte_3
Definition: Istruzioni.h:23
#define istruzione_byte_2
Definition: Istruzioni.h:22
#define byte_istr_dati_1
Definition: Istruzioni.h:26
#define byte_istr_dati_2
Definition: Istruzioni.h:27
#define byte_istr_dati_0
Definition: Istruzioni.h:25
#define byte_istr_dati_3
Definition: Istruzioni.h:28
#define istruzione_byte_4_e_scelta_canale
Definition: Istruzioni.h:24
#define istruzione_byte_0
Definition: Istruzioni.h:20
void instr_LED_ON_OFF_function(void)
LEDs can be set ON/OFF.
Definition: Led.c:144
void EPROM_lettura_M24C32_64(uint8_t scheda_su_scheda_giu_, uint8_t mainboard_postmainboard, uint8_t canale, short indirizzo_memoria, uint8_t *dati_letti)
Read from preamplifier and on-board flashes.
void EPROM_scrittura_M24C32_64(uint8_t scheda_su_scheda_giu, uint8_t mainboard_postmainboard, uint8_t canale, short indirizzo_memoria, uint8_t *dati_da_scrivere)
Wriring the EEPROM.
void instr_EPROM_read_write_function(void)
CAN insrtuction to Read from preamplifier, on-board and post frontend board EPROM.
@ Memory_postmainboard_ADCs_always_ON_0_powerdown_1
ADCs powerdown ON if 0, always ON if >1.
@ Memory_postmainboard_micro_PD_wait_time_in_sec
wait time before microcontroller power down since last operation
@ Memory_postmainboard_micro_powdown_active_1_alwaysON_0
microntroller aoutopower OFF if true, always ON if false
void Analog_mux_line_to_select_deselect(uint8_t scheda_su_scheda_giu, uint8_t line_to_select, uint8_t select_1_deselect_0)
Selection of the line to mesure with the analog MUX of the mainboard, driven by the I2C->parallel mux...
volatile uint32_t POWER_DOWN_MAX_ATTESA
upper limit for POWER_DOWN_TEMPO_ATTESA
Definition: Power_down.c:118
void preamplifier_set_reset_pre_gain(uint8_t scheda_su_scheda_giu, uint8_t canale, uint8_t set_2_reset_1_idle_0)
Here the 2 digital signals of the preamplifier's trimmer are exploited to set the gain of the preampl...
#define preamplifier_Gain_basso
. *‍/
#define preamplifier_Gain_alto
. *‍/
void instr_preamplifier_scrittura_lettura_trimmer_offset_function(void)
Content of the preamplifier trimmers can be read/write.
void instr_output_offset_to_be_set_function(void)
The detector bias will be set at the value set with the function instr_detector_Vbias_we_want_to_set_...
void instr_ouput_offset_we_want_to_set_function(void)
The preamplifier offset will be set at the value passed from the CAN with this function if,...
int32_t preamplifier_Output_offset_target[12]
Offset target values.
void Aspetta_tanti_ms(int millisecondi)
The timing function.
Definition: Timer.c:52
void instr_DetBias_trimmer_calibration_function(void)
The fine and coarse trimmers for Detector bias adjustment are calibrated here
Definition: Trimmer.c:480
void instr_offset_trimmer_calibration_function(void)
The fine and coarse trimmers for offset adjustment are calibrated here
Definition: Trimmer.c:320
void instr_inizializza_tutto_da_zero_function(void)
Start-up function.
void instr_periferal_reset_function(void)
I2C, SPI and CAN can be rest.
int wait_for_ADC_setting
settable from the can
bool flag_power_down
if flag_power_down is true the u-controller goes in power down after a while
volatile int8_t vettore_istruzioni[8]
This is a copy of the received 8 bytes from the CAN.
unsigned int FW_version
This is where the FW version is stored.
Definition: Gpio.h:13