double HY3131DMMLib::Measure2(int8_t index){ busyState=true; RawData=0.0;ADCDigital=0; if(index==AC50mV || index==AC500mV ||index==AC5V || index==AC50V ||index==AC500V ||index==AC1000V || index==AC500uA || index==AC5mA ||index==AC50mA || index==AC500mA || index==AC10A){ if(minus==true) minus=false; RMSData=readRMS(); if(reg4>0)// && index!=AC5V && index!=AC500uA && index!=AC5mA && index!=AC50mA && index!=AC500mA && index!=AC10A) return 999999999; else RawData = (double)RMSData; } else{ ADCDigital=readADC1(); if ((ADCDigital < 0) || (ADCDigital > 0x800000)) { ADCDigital = ~(ADCDigital | 0xFF000000); minus=true; }else minus=false; if(ADCDigital==0x7ffffe) return 999999999; else RawData=(double)ADCDigital; } if(minus==true) RawData=RawData*-1; busyState=false; qDebug()<<"Raw Data- Before GAin & Offset:"<<RawData; return RawData; }
uint8_t readDIPSwitch(void) { /* Voltage step for each ADC division (4095 divisions) = 0.00080586 V VOLTAGE = ADC*(3.3/4095.0) ADC resolution is 12bit => 4096 values Dipswitch with 3 contact 000 = 0 = 3.3 V => readADC = 4095 001 = 1 = 2.27 V => readADC = 2816 010 = 2 = 1.96 V => readADC = 2432 011 = 3 = 1.54 V => readADC = 1911 100 = 4 = 1.32 V => readADC = 1638 101 = 5 = 1.12 V => readADC = 1389 110 = 6 = 1.04 V => readADC = 1290 111 = 7 = 0.9075 V => readADC = 1126 */ uint32_t k=0; uint8_t i; // Average 30 readings for (i=0; i<30; i++) k += readADC1(ADC_Channel_0); k /= 30; if (k>3500) return 0; if ((k>2750)&&(k<3500)) return 1; if ((k>2200)&&(k<2750)) return 2; if ((k>1800)&&(k<2200)) return 3; if ((k>1550)&&(k<1800)) return 4; if ((k>1350)&&(k<1550)) return 5; if ((k>1250)&&(k<1350)) return 6; if (k<1250) return 7; // Else return value 8, ERROR return 8; }
int main(void) { leds_init(); adc_init(); unsigned int bin_code; double voltage_x,voltage_y,voltage_z; do { bin_code = readADC1(ADC_Channel_1); voltage_x = bin_code*2.96 / 0xfff; //Delay(500000); bin_code = readADC1(ADC_Channel_3); voltage_y = bin_code*2.96 / 0xfff; //Delay(500000); bin_code = readADC1(ADC_Channel_5); voltage_z = bin_code*2.96 / 0xfff; //Delay(500000); } while (1); }
float readPorcentajeADC1(){ uint16_t valorVoltaje; float valorPorcentaje; valorVoltaje= readADC1(); valorPorcentaje= ((float)valorVoltaje/VALOR_MAX_ADC )*100; return valorPorcentaje; return valorVoltaje; }
void TIM2_IRQHandler() { char str[10]; uint16_t data; if(TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) { TIM_ClearITPendingBit(TIM2, TIM_IT_Update); GPIOB->ODR ^= 0x02; data = readADC1(10); sprintf(str, "%d\n", data); UART_puts(str); } }
//---------------------------------------------------------------------------------------------------- int main(void) { Initialization_All(); uint8_t pat_count, counter1, meandre_ok = 0; SysTick_Config(SystemCoreClock / 1000);//200000 if (Button_test() == false) state_err[4] = 0x04; //button on test if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_0) == 0) state_err[6] = 0x06; //LV1 error if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1) == 0) state_err[8] = 0x08; //LV2 error if (meandr_test(GPIOB, GPIO_Pin_0) == true) state_err[7] = 0x07; //short-circuit K1 if (meandr_test(GPIOB, GPIO_Pin_1) == true) {state_err[9] = 0x09; //emission in process \/ monoblok isn't connected Emission(true); Beeper(true);} //вкл индикатор излучение, вкл бипер Display(&display, struct_sample.voltage_correction*struct_sample.man_exposition*20);//длительность экспозиции мс //main loop while(1) { wait_label: // while B_SAFE and B_FILM is ON, do get voltage and output to display if (Button_OnClick(B_SAFE) && Button_OnClick(B_FILM)) { DI_Display(&display, readADC1(), 0); } meandr_auto(); // Выбор пациента if(Button_OnClick(B_PAT) == 1) ++pat_count; if (pat_count > 3) pat_count = 1; switch(pat_count) { case 1: {GPIO_SetBits(GPIOB, GPIO_Pin_3); GPIO_ResetBits(GPIOD, GPIO_Pin_6); GPIO_ResetBits(GPIOD, GPIO_Pin_4); \ DI_Display(&display, 3, 0);\ struct_sample.patient_type = PAT1;} break; case 2: {GPIO_ResetBits(GPIOB, GPIO_Pin_3); GPIO_SetBits(GPIOD, GPIO_Pin_6); GPIO_ResetBits(GPIOD, GPIO_Pin_4);\ DI_Display(&display, 33, 0);\ struct_sample.patient_type = PAT2;} break; case 3: {GPIO_ResetBits(GPIOB, GPIO_Pin_3); GPIO_ResetBits(GPIOD, GPIO_Pin_6); GPIO_SetBits(GPIOD, GPIO_Pin_4);\ DI_Display(&display, 111, 0);\ struct_sample.patient_type = PAT3;} break; default:{GPIO_ResetBits(GPIOB, GPIO_Pin_3); GPIO_ResetBits(GPIOD, GPIO_Pin_6); GPIO_ResetBits(GPIOD, GPIO_Pin_4);} break; } //Выбор пленки if(Button_OnClick(B_FILM) == 1) ++film_count; if (film_count > 3) film_count = 1; switch(pat_count) { case 1: {GPIO_SetBits(GPIOA, GPIO_Pin_15); GPIO_ResetBits(GPIOA, GPIO_Pin_13); GPIO_ResetBits(GPIOA, GPIO_Pin_9);\ struct_sample.film = FILM_1;} break; case 2: {GPIO_ResetBits(GPIOA, GPIO_Pin_15);GPIO_SetBits(GPIOA, GPIO_Pin_13); GPIO_ResetBits(GPIOA, GPIO_Pin_9);\ struct_sample.film = FILM_2;} break; case 3: {GPIO_ResetBits(GPIOA, GPIO_Pin_15);GPIO_ResetBits(GPIOA, GPIO_Pin_13); GPIO_SetBits(GPIOA, GPIO_Pin_9);\ struct_sample.film = FILM_3;} break; default:{GPIO_ResetBits(GPIOA, GPIO_Pin_15);GPIO_ResetBits(GPIOA, GPIO_Pin_13); GPIO_ResetBits(GPIOA, GPIO_Pin_9);} break; } // Кнопка безопасности if(Button_OnClick(B_SAFE) == 1) { if (!b_Safe) { GPIO_SetBits(GPIOC, GPIO_Pin_9); b_Safe = 1;// safe } else if(b_Safe) { GPIO_ResetBits(GPIOC, GPIO_Pin_9); b_Safe = 0;// safe } } //buttons released Button_OnClick(B_EXP)? (b_Exp = 1):(b_Exp = 0); Button_OnClick(B_PLUS)? (b_Plus = 1):(b_Plus = 0); Button_OnClick(B_MINUS)?(b_Minus = 1):(b_Minus = 0); Button_OnClick(B_FILM)? (b_Film = 1):(b_Film = 0); Button_OnClick(B_UIN)? (b_Uin = 1):(b_Uin = 0); /*if (Button_OnClick(B_EXP) == 1) b_Exp = 1; if (Button_OnClick(B_PLUS) == 1) b_Plus = 1; if (Button_OnClick(B_MINUS) == 1) b_Minus = 1; if (Button_OnClick(B_FILM) == 1) b_Film = 1; if (Button_OnClick(B_UIN) == 1) b_Uin = 1; */ //Выбор зуба if (Button_OnClick(B_UPM) == 1){ TOOTH_IND_ON('upm') ; struct_sample.tooth = UPM; } if (Button_OnClick( B_UMO) == 1){ TOOTH_IND_ON('umo') ; struct_sample.tooth = UMO; } if (Button_OnClick(B_DIN) == 1){ TOOTH_IND_ON('din') ; struct_sample.tooth = DIN; } if (Button_OnClick(B_DPM) == 1){ TOOTH_IND_ON('dpm') ; struct_sample.tooth = DPM; } if (Button_OnClick(B_UIN) == 1){ TOOTH_IND_ON('uin') ; struct_sample.tooth = UIN; } if (Button_OnClick(B_DMO) == 1){ TOOTH_IND_ON('dmo') ; struct_sample.tooth = DMO; } //==================================================================================== // starting mode //==================================================================================== if (mode_emit == starting) { //ошибка при преждевременном отпускании кнопки экспозиции if (Button_OnClick(B_EXP) == 1){ uint8_t i = 0; for(i = 0; i <= 5; i++){ voltage += readADC1(); delay_ms(20); } if ((voltage = voltage/i) < 2)//volt < 192V {state_err[1] = 0x01; mode_emit = idle; goto wait_label;} if ((voltage = voltage/i) > 3)//volt > 247V {state_err[2] = 0x02; mode_emit = idle; goto wait_label;} delay_ms(100); if (meandr_test(GPIOB, GPIO_Pin_0) == false) {state_err[1] = 0x01; mode_emit = idle; goto wait_label;} if (GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1) == 0) {state_err[6] = 0x06; mode_emit = idle; goto wait_label;} if (meandr_test(GPIOB, GPIO_Pin_1) == 1) {state_err[9] = 0x09; mode_emit = idle; Emission(true); Beeper(true);}//light ON, beeper ON if ((GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_1) == 1)&(meandr_test(GPIOB, GPIO_Pin_0) == true)) {mode_emit = emit;} } } // starting mode if (mode_emit == emit) { Display(&display, struct_sample.voltage_correction); if (cntr_imp >= imp_len) { // stopEmission(); mode_emit = idle; goto wait_label; } } }//while(1) }//main()
u_int32_t HY3131DMMLib::readPKHMAX(){ return (readADC1(0x11,0x12,0x13)); }
u_int32_t HY3131DMMLib::readPKHMIN(){ return (readADC1(0x0E,0x0F,0x10)); }
u_int32_t HY3131DMMLib::readLPF(){ return (readADC1(0x06,0x07,0x08)); }
u_int32_t HY3131DMMLib::readADC2(){ return (readADC1(0x03,0x04,0x05)); }
int main(void) { Delay_ms(100); Periph_clock_enable(); GPIO_Config(); Usart4Init(); I2C_Config(); ADC_Config(); MPU6050_Init(); Timer1_Config(); Timer8_Config(); Timer2_Config(); Timer5_Config(); Timer4_Config(); Timer3_Config();//RC control timer NVIC_Configuration(); EXTI_Config(); TIM_Cmd(TIM5, ENABLE); TIM_CtrlPWMOutputs(TIM5, ENABLE); for (i = 1 ; i < 1 ; i++) ; //small delay before starting Timer4 TIM_Cmd(TIM4, ENABLE); TIM_CtrlPWMOutputs(TIM4, ENABLE); Delay_ms(100); for (i = 0; i < configDataSize; i++) //reads configuration from eeprom { ReadFromEEPROM(i); configData[i] = EepromData; Delay_ms(5); } I2C_AcknowledgeConfig(I2C2, ENABLE); Delay_ms(100); while (1) { LEDon; DEBUG_LEDon; while (ConfigMode == 1) { TimerOff(); //Configuration loop } MPU6050_ACC_get();//Getting Accelerometer data acc_roll_angle = -(atan2(accADC_x, accADC_z)) + (configData[11] - 50.00) * 0.0035; //Calculating pitch ACC angle+callibration acc_pitch_angle = +(atan2(accADC_y, accADC_z)); //Calculating roll ACC angle MPU6050_Gyro_get();//Getting Gyroscope data acc_roll_angle_vid = ((acc_roll_angle_vid * 99.00) + acc_roll_angle) / 100.00; //Averaging pitch ACC values acc_pitch_angle_vid = ((acc_pitch_angle_vid * 99.00) + acc_pitch_angle) / 100.00; //Averaging roll ACC values sinus = sinusas[(int)(rc4)]; //Calculating sinus cosinus = sinusas[90 - (int)(rc4)]; //Calculating cosinus ROLL = -gyroADC_z * sinus + gyroADC_y * cosinus; roll_angle = (roll_angle + ROLL * dt) + 0.0002 * (acc_roll_angle_vid - roll_angle); //Roll Horizon //ROLL=-gyroADC_z*sinus+gyroADC_y*cosinus; yaw_angle = (yaw_angle + gyroADC_z * dt); //Yaw pitch_angle_true = ((pitch_angle_true + gyroADC_x * dt) + 0.0002 * (acc_pitch_angle_vid - pitch_angle_true)); //Pitch Horizon ADC1Ch1_vid = ((ADC1Ch1_vid * 99.00) + (readADC1(1) / 4000.00)) / 100.00; //Averaging ADC values ADC1Ch1_vid = 0.00; rc4_avg = ((rc4_avg * 499.00) + (rc4)) / 500.00; //Averaging RC4 values pitch_angle = pitch_angle_true - rc4_avg / 57.3; //Adding angle pitch_angle_correction = pitch_angle * 150.0; if (pitch_angle_correction > 2.0) { pitch_angle_correction = 2.0; } if (pitch_angle_correction < -2.0) { pitch_angle_correction = -2.0; } pitch_setpoint = pitch_setpoint + pitch_angle_correction; //Pitch return to zero after collision roll_angle_correction = roll_angle * 200.0; if (roll_angle_correction > 2.0) { roll_angle_correction = 2.0; } if (roll_angle_correction < -2.0) { roll_angle_correction = -2.0; } roll_setpoint = roll_setpoint + roll_angle_correction; //Roll return to zero after collision ADC1Ch13_vid = ((ADC1Ch13_vid * 99.00) + ((readADC1(13) - 2000) / 4000.00)) / 100.00; //Averaging ADC values if (configData[10] == '0') { yaw_angle = (yaw_angle + gyroADC_z * dt) + 0.01 * (ADC1Ch13_vid - yaw_angle); //Yaw AutoPan } if (configData[10] == '1') { yaw_angle = (yaw_angle + gyroADC_z * dt); //Yaw RCPan } yaw_angle_correction = yaw_angle * 50.0; if (yaw_angle_correction > 1.0) { yaw_angle_correction = 1.0; } if (yaw_angle_correction < -1.0) { yaw_angle_correction = -1.0; } yaw_setpoint = yaw_setpoint + yaw_angle_correction; //Yaw return to zero after collision pitch_PID();//Pitch axis pid roll_PID(); //Roll axis pid yaw_PID(); //Yaw axis pid printcounter++; //Print data to UART if (printcounter >= 100) { //sprintf (buff, " %d %d %c Labas\n\r", ACCread[0], ACCread[1], ACCread[2]); //sprintf (buff, " %x %x %x %x %x %x Labas\n\r", ACCread[0], ACCread[1], ACCread[2], ACCread[3], ACCread[4], ACCread[5]); //sprintf (buff, "Labas %d %d\n\r", ACCread[0], ACCread[1]); //sprintf (buff, "%3.1f %f\n\r", ADC1Ch1_vid*57.3, sinus); //sprintf (buff, "Labas %f %f %f \n\r", accADC_x, accADC_y, accADC_z); //sprintf (buff, "%3.1f %3.1f \n\r", acc_roll_angle_vid*57.3, acc_pitch_angle_vid *57.3); //sprintf (buff, "%3.1f %3.1f \n\r", pitch_angle*57.3, roll_angle*57.3); //sprintf (buff, "%d\n\r", rc4); //USART_PutString(buff); printcounter = 0; } stop = 0; LEDoff; watchcounter = 0; while (stop == 0) {} //Closed loop waits for interrupt } }