volatile uint8_t adc_flag = 0; volatile uint16_t adc_val; volatile uint32_t adc_sum; volatile uint16_t adc_avg; volatile uint16_t adc_buffer[5]; volatile uint8_t adc_buffer_pointer; uint16_t adc_array[160]; uint8_t adc_array_pointer; volatile uint8_t data_valid=0; uint16_t adc_MIN; uint16_t adc_MAX; uint16_t adc_TRG; //trigger level = 2/3 of peak-peak value char bufferX[80]; float timm=0; unsigned long currentMillis =0; unsigned long previousMillis=0; HardwareSerial Serial1 (PA10_R, PA9_R);// UART na remap. pinech č. 5 a 6 na STM32G030J6 pouzdro SO8 // kanal UART1 je na branach PA10 a PA9, ale pro pouzdro SO8 musime oznacit, ze se ma pouzit remapovani #define LED PB5 // pin č. 8, PB5 SO8 STM32G030J6 void setup() { // put your setup code here, to run once: adc_buffer[0]=0; adc_buffer[1]=0; adc_buffer[2]=0; adc_buffer[3]=0; adc_buffer_pointer = 0; adc_array_pointer = 0; Serial1.begin(115200); pinMode(LED, OUTPUT); // PB5 pin. c.8 for(uint8_t t=0; t<150;t++){ adc_array[t]=0; } analogReadResolution(12); } void loop(){ // put your main code here, to run repeatedly: currentMillis = millis(); if(currentMillis - previousMillis >= 5){ //each 5 ms previousMillis = currentMillis; // save the last time adc_buffer[adc_buffer_pointer] = analogRead(PB7); // measure the voltage adc_buffer_pointer++; //and store it in buffer if(adc_buffer_pointer > 3){ //average 4 values to cancel 50Hz noise adc_buffer_pointer=0; adc_sum =0; for(uint8_t t=0; t<4;t++){ adc_sum += adc_buffer[t]; } adc_avg = (uint16_t)(adc_sum/4); //calculate average value adc_array[adc_array_pointer]=adc_avg; //add to array to calculate trigger level adc_array_pointer++; if(adc_array_pointer>149){adc_array_pointer=0;}; adc_MAX =0; //calculate MIN and MAX values adc_MIN= 65535; for(uint8_t a=0;a<150;a++){ if(adc_array[a]> adc_MAX){adc_MAX = adc_array[a];}; if(adc_array[a]< adc_MIN){adc_MIN = adc_array[a];}; };//for(uint8_t a=0;a<100;a++) //data_valid only if there is some significant peak-peak value, not only noise if((adc_MAX-adc_MIN)>100){data_valid=1;}else{data_valid=0;}; //calculate trigger value - 2/3 of peak-peak amplitude - there is smaller peak next to the big one //but it does not have high amplitude so 2/3 is safely above its value.. adc_TRG = (((adc_MAX-adc_MIN)/3)*2)+adc_MIN; if((adc_avg>adc_TRG)&&(data_valid)){ //blink with LED when current signal value is above trigger and the signal is valid digitalWrite(LED, HIGH); // LED ON (privede vysokou uroven napeti HIGH (cca 3,3 V) //analogWrite(LED, 200); }else{ digitalWrite(LED, LOW); // LED OFF (vypne LED) //analogWrite(LED,0); }; //send the data to PC - DataPlotter format timm +=0.02; if (timm > 9999){timm=0;}; //sprintf((char *)bufferX,"$$P-,%5.3f;",(float)(adc_avg/19859.0)); float tmpVal0 = timm; //----------------------------------time uint16_t tmpInt10 = tmpVal0; float tmpFrac0 = tmpVal0 - tmpInt10; uint16_t tmpInt20 = trunc(tmpFrac0 * 100); //------------------------------------------------OPTOPULSE float tmpVal1 = adc_avg/1241.0; uint16_t tmpInt11 = tmpVal1; float tmpFrac1 = tmpVal1 - tmpInt11; uint16_t tmpInt21 = trunc(tmpFrac1 * 1000); // float tmpVal2 = adc_TRG/1241.0; uint16_t tmpInt12 = tmpVal2; float tmpFrac2 = tmpVal2 - tmpInt12; uint16_t tmpInt22 = trunc(tmpFrac2 * 1000); //---------------------------------------------------------- / time / pulse / TRG pulse / sprintf((char *)bufferX,"$$P%04u.%02u,%01u.%03u,%01u.%03u;",tmpInt10,tmpInt20,tmpInt11,tmpInt21,tmpInt12,tmpInt22); // $$P0000.22,2.255,1.188; Serial1.print(bufferX); } } }