int main(void) { //init: DAC_DeInit(); init_GPIOA(); init_GPIOC(); init_GPIOD(); init_DMA1(); init_DMA2(); init_TIM2(); init_TIM3(); init_TIM4(); init_TIM6(); init_ADC3(); init_DAC(); init_filter(&ap_1); init_filter(&ap_2); init_filter(&ap_3); init_filter(&ap_4); ap_filter_coefs(&ap_1); ap_filter_coefs(&ap_2); ap_filter_coefs(&ap_3); ap_filter_coefs(&ap_4); ADC_SoftwareStartConv(ADC3); while (1) { counter++; } }
int main(void) { // initialize the GPIO pins we need initClock(); init_GPIO(); init_ADC(); init_DAC(); init_GYACC(); usb::init(); Herkulex hercules; GPIOE->ODR |= 0xC000; Tools::Delay(Tools::DELAY_AROUND_1S / 2); GPIOE->ODR &= ~0xC000; enable_ADC_watchdog(2760, 3870); // ~6.9V -- ~9.5V (V33 = 3.41V) hercules.setTorque(DEFAULT_ID, TORQUE_ON); unsigned debounce = 10000, oldb=0; unsigned g = GYACC_txrx(USE_GYRO, 0x8F00); unsigned a = GYACC_txrx(USE_ACC, 0xA000); for(;;) { char p[4]; uint32_t r = usb::read(p); for (int i=0; i < r; ++i) p[i] += 2; usb::write(p, r); if (r) GPIOE->ODR += 0x4000; if (!debounce--) { const unsigned b = ~GPIOC->IDR; const unsigned p = (b ^ oldb) & b; if (p & 2) { GPIOE->ODR ^= 0x8000; hercules.positionControl(DEFAULT_ID, 512 + 300, 40, 0); } else if (p & 4) { GPIOE->ODR ^= 0x4000; hercules.positionControl(DEFAULT_ID, 512, 40, 0); } else if (p & 8) { GPIOE->ODR -= 0x4000; hercules.positionControl(DEFAULT_ID, 512 - 300, 40, 0); } oldb = b; debounce = 10000; } } }
/********************************************Programme Principal************************************************/ int main(void) { init_OSC(); init_dsPIC(); init_DAC(); init_ADC(); while(1) { val=convertADC()*64; } return 0; }
void da_init() { RCC_PeriphClockCmd(RCC_DAC, ENABLE); init_DAC(DAC_Channel_1, DAC_Trigger_None, DAC_WaveGeneration_None, 0, DAC_OutputBuffer_Enable); }
void main (void) { /*开硬件浮点*/ SCB->CPACR |=((3UL << 10*2)|(3UL << 11*2)); /* set CP10 and CP11 Full Access */ uint16 flag; uint16 i,j; DisableInterrupts; LCD_init(1); Disp_single_colour(Black); LCD_PutString(10, 50,"Frequency: ", White, Black); LCD_PutString(145, 50," KHz", White, Black); LCD_PutString(10, 80,"Power: ", White, Black); LCD_PutString(145, 80," W", White, Black); LCD_PutString(10, 110,"Amplify: ", White, Black); LCD_PutString(165, 110,"Restrain: ", White, Black); init_ADC(); init_DAC(); init_DMA(); init_PDB(); init_PIT(); init_gpio_PE24(); EnableInterrupts; LPLD_LPTMR_DelayMs(100); flag = Result_flag; uint16 ShowAFlag = 0; uint16 ShowBFlag = 0; uint16 ShowCFlag = 0; arm_fir_init_f32(&S, NUM_TAPS, (float32_t *)&firCoeffs32[0], &firStateF32[0], blockSize); while(1) { if( flag==Result_flag && Result_flag == 0) { if(++ShowAFlag<10) { for(j = 0;j<LENGTH;j++) testInput_x[j*2] = Result_A[j]; for(j = 0;j<LENGTH;j++) testInput_x[j*2+1] = 0; arm_cfft_f32(&arm_cfft_sR_f32_len2048, testInput_x, ifftFlag, doBitReverse); /* Process the data through the Complex Magnitude Module for calculating the magnitude at each bin */ arm_cmplx_mag_f32(testInput_x, testOutput, fftSize); testOutput[0] = 0; /* Calculates maxValue and returns corresponding BIN value */ arm_max_f32(testOutput, fftSize, &maxValue, &testIndex); } else { ShowAFlag = 0; if(starfir !=2 ) LCD_Put_Float(100, 50,"",testIndex*40.0/2048, White, Black); } if(starfir == 1) { PTE24_O = 1; for(j = 0;j<LENGTH;j++) firInput[j] = Result_A[j]; inputF32 = &firInput[0]; outputF32 = &firOutput[0]; for(i=0; i < numBlocks; i++) arm_fir_f32(&S, inputF32 + (i * blockSize), outputF32 + (i * blockSize), blockSize); for(j = 0;j<LENGTH;j++) Result_A[j] = firOutput[j]; PTE24_O = 0; } flag = 1; } else if(flag==Result_flag && Result_flag == 1) { if(starfir !=2 ) { if(++ShowBFlag<10) { power = 0; for(i=0;i<LENGTH;i++) power+=((Result_B[i] - OFFEST)/1241.0)*((Result_B[i] - OFFEST)/1241.0)*90*MyDb/8.0; power = power/LENGTH; } else { ShowBFlag = 0; LCD_Put_Float(100, 80,"",power, White, Black); } } else { for(i = 0;i<160;i++) { FFT_RESULT_NEW[i] = testOutput[i*6]/FFT_VALUE; if(FFT_RESULT_NEW[i]>239) FFT_RESULT_NEW[i] = 239; } } // { // for(j = 0;j<LENGTH;j++) // testInput_x[j*2] = Result_B[j]; // for(j = 0;j<LENGTH;j++) // testInput_x[j*2+1] = 0; // // arm_cfft_f32(&arm_cfft_sR_f32_len2048, testInput_x, ifftFlag, doBitReverse); // // /* Process the data through the Complex Magnitude Module for // calculating the magnitude at each bin */ // arm_cmplx_mag_f32(testInput_x, testOutput, fftSize); // // testOutput[0] = 0; // /* Calculates maxValue and returns corresponding BIN value */ // arm_max_f32(testOutput, fftSize, &maxValue, &testIndex); // } if(starfir == 1) { PTE24_O = 1; for(j = 0;j<LENGTH;j++) firInput[j] = Result_B[j]; inputF32 = &firInput[0]; outputF32 = &firOutput[0]; for(i=0; i < numBlocks; i++) arm_fir_f32(&S, inputF32 + (i * blockSize), outputF32 + (i * blockSize), blockSize); for(j = 0;j<LENGTH;j++) Result_B[j] = firOutput[j]; PTE24_O = 0; } flag = 2; } else if(flag==Result_flag && Result_flag == 2) { // // { // for(j = 0;j<LENGTH;j++) // testInput_x[j*2] = Result_C[j]; // for(j = 0;j<LENGTH;j++) // testInput_x[j*2+1] = 0; // // arm_cfft_f32(&arm_cfft_sR_f32_len2048, testInput_x, ifftFlag, doBitReverse); // // /* Process the data through the Complex Magnitude Module for // calculating the magnitude at each bin */ // arm_cmplx_mag_f32(testInput_x, testOutput, fftSize); // // testOutput[0] = 0; // /* Calculates maxValue and returns corresponding BIN value */ // arm_max_f32(testOutput, fftSize, &maxValue, &testIndex); // } if(starfir == 1) { // PTE24_O = 1; for(j = 0;j<LENGTH;j++) firInput[j] = Result_C[j]; inputF32 = &firInput[0]; outputF32 = &firOutput[0]; for(i=0; i < numBlocks; i++) arm_fir_f32(&S, inputF32 + (i * blockSize), outputF32 + (i * blockSize), blockSize); for(j = 0;j<LENGTH;j++) Result_C[j] = firOutput[j]; // PTE24_O = 0; } if(starfir != 2) { if(++ShowCFlag<5) { } else { if(ShowMenu) { Disp_single_colour(Black); LCD_PutString(10, 50,"Frequency: ", White, Black); LCD_PutString(145, 50," KHz", White, Black); LCD_PutString(10, 80,"Power: ", White, Black); LCD_PutString(145, 80," W", White, Black); LCD_PutString(10, 110,"Amplify: ", White, Black); LCD_PutString(165, 110,"Restrain: ", White, Black); ShowMenu = 0; } LCD_Put_Float(100, 110,"",MyDb/0.5, White, Black); if(starfir) LCD_PutString(260, 110,"On ", White, Black); else LCD_PutString(260, 110,"Off", White, Black); } } else { if(ShowMenu) { Disp_single_colour(Black); ShowMenu = 0; } draw_fft(); } flag = 0; } } }
int main(void) { WWDG_SetPrescaler(WWDG_Prescaler_8); //init: for(k=0; k<VECSIZE; k++) //initialize output vector { DAC1ConvertedValue[k] = 0; DAC2ConvertedValue[k] = 0; } // init_filter(&ap_1); // init_filter(&ap_2); // init_filter(&ap_3); // init_filter(&ap_4); for(k = 0; k<nfilters; k++) { //init_filter(&filters[k]); } // ap_filter_coefs(&ap_1); // ap_filter_coefs(&ap_2); // ap_filter_coefs(&ap_3); // ap_filter_coefs(&ap_4); for(k = 0; k<nfilters; k++) { //ap_filter_coefs(&filters[k]); } for(k = 0; k<VECSIZE; k++) { vector_a[k] = 0.0; vector_b[k] = 0.0; } noise.add = 3; noise.mod = gsize; noise.mul = 1; noise.out = 0; noise2.add = 3; noise2.mod = gsize; noise2.mul = 1; noise2.out = 0; noise3.add = 3; noise3.mod = gsize; noise3.mul = 1; noise3.out = 0; saw.add = 1.0/800; saw.mul = 1; saw.out = 0; saw.prev_add = 0; saw.slew = 0.994; melody.add = 1.0/10000; melody.mul = 1; melody.out = 0; melody.prev_add = 0; melody.slew = 0.9994; harmony.add = 1.0/10000; harmony.mul = 1; harmony.out = 0; harmony.prev_add = 0; harmony.slew = 0.9994; drone.add = 1.0/10000; drone.mul = 1; drone.out = 0; drone.prev_add = 0; drone.slew = 0.999994; dronelo.add = 1.0/10000; dronelo.mul = 1; dronelo.out = 0; dronelo.prev_add = 0; dronelo.slew = 0.99994; for(k = 0; k<(nfilters*5); k++) //repeat coefs nfilters times { coefTable[k] = coef_single[(k%5)]; } arm_biquad_cascade_df2T_init_f32( &S1, nfilters, &coefTable, &biquadState); DAC_DeInit(); init_GPIOA(); init_GPIOC(); init_GPIOD(); init_DMA1(); init_DMA2(); init_TIM2(); init_TIM3(); init_TIM4(); init_TIM6(); init_ADC3(); init_DAC(); ADC_SoftwareStartConv(ADC3); while (1) { counter++; } }
int main(int argc, char** argv) { int one = 1, client_fd; int rdlen; struct sockaddr_in svr_addr, cli_addr; socklen_t sin_len = sizeof (cli_addr); struct tm *u; char *f; time_t timer; // Init DAC and SPI int ret = bcm2835_init(); if (ret != 1) { fprintf(stderr, "Error in bcm2835_init()\n"); return -1; } fprintf(stderr, "bcm2835_init()\n"); ret = bcm2835_spi_begin(); if (ret != 1) { fprintf(stderr, "Error in spi begin! ret code = %d\n", ret); return -1; } //setting SPI and CS bcm2835_spi_setBitOrder(BCM2835_SPI_BIT_ORDER_LSBFIRST); bcm2835_spi_setDataMode(BCM2835_SPI_MODE1); bcm2835_spi_setClockDivider(BCM2835_SPI_CLOCK_DIVIDER_1024); bcm2835_gpio_fsel(GPIO12, BCM2835_GPIO_FSEL_OUTP); bcm2835_gpio_write(GPIO12, HIGH); if ((argc >1) && (strncmp(argv[1], "init", 4) == 0)) { fprintf(stderr, "init 5790\n" ); init_DAC(); } else { fprintf(stderr, "start without init 5790\n" ); } int sock = socket(AF_INET, SOCK_STREAM, 0); if (sock < 0) err(1, "can't open socket"); setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &one, sizeof (int)); svr_addr.sin_family = AF_INET; svr_addr.sin_addr.s_addr = INADDR_ANY; svr_addr.sin_port = htons(port); if (bind(sock, (struct sockaddr *) &svr_addr, sizeof (svr_addr)) == -1) { close(sock); err(1, "Can't bind"); return 0; } double setval; uint16_t dsetval; listen(sock, 5); while (1) { client_fd = accept(sock, (struct sockaddr *) &cli_addr, &sin_len); // printf("got connection\n"); if (client_fd == -1) { // perror("Can't accept"); continue; } timer = time(NULL); u = localtime(&timer); f = settime(u); printf("\n####################################\n %s", f); rdlen = read(client_fd, answer, 2000); printf(" request: %s \n", answer); strstart = 0; strpos = 0; /* nword = ""; */ getword(); strcpy(metod, nword); getword(); strcpy(uri, nword); getword(); strcpy(protocol, nword); printf("metod='%s' uri='%s' protocol='%s'\n", metod, uri, protocol); memset(resp1, 0, 1100); strcpy(resp1, response); if (strncmp(uri, "/get", 4) == 0) { val = 123.876; sprintf(nword, "%f}", val); strcat(resp1, nword); printf("get=%s\n", nword); } if (strncmp(uri, "/setabs=", 7) == 0) { memset(nword, 0, 100); strncpy(nword, uri + 8, 20); strcat(resp1, nword); printf("setabs=%s\n", nword); long int dsetval; dsetval=0; sscanf(nword,"%d",&dsetval); printf("setabs u = %d \n", dsetval); // double Vout = atof(argv[1]) - 1e-6; // printf("1 u = \n"); // long int DAC_code = 1048576.0*(Vout + VREFN) / (VREFP - VREFN); // printf("2 u = %d \n", DAC_code); long int DAC_code = dsetval; printf("3 dac code = %d \n", DAC_code); char msb[3] = {0}; printf("4 u = %d \n", DAC_code); msb[0] = DAC_code / (256*256); msb[1] = (DAC_code - 256*256*msb[0])/256; msb[2] = (DAC_code - 256*256*msb[0] - 256*msb[1]); printf("5 u = %d \n", DAC_code); printf("msb = {%d %d %d}\n", msb[0], msb[1], msb[2]); set_V(msb); } write(client_fd, resp1, sizeof (resp1) - 1); /*-1:'\0'*/ close(client_fd); } }
int main(void) { PORTE.DIRSET |= 0xFF; PORTD.DIRSET = 0x00; PORTD.PIN1CTRL = 0b00011010; PORTC.DIRSET |= 0x01; while(1) { /* Read the next incoming event. Usually this is a blocking function. */ // EVENTS event = readEventFromMessageQueue(); if((flag.HARDWARE_ERROR_FLAG == 1 || flag.OVERTEMP_FLAG == 1) && flag.DISCHARGED_FLAG == 0) { state = DISCHARGE; } /* Finite State Machine */ switch(state) { case INIT: init_clock(); init(); interrupt_init(); UART_init(); AC_init(); init_DAC(); init_GPIO_interrupt(); // init_counter(); // Start by using capbank 1 flag.CAPBANK_FLAG = 0; PORTE.OUT = 0x00; //_delay_ms(4000); // comparator reference voltages // Desired voltage reference level expressed in Capacitor voltage // TODO: high_trigger_cap is based on user_settings.voltage_amplitude which is in percent but should be converted to absolute. double low_trigger_cap = 0.3; // Voltage divided, digital version of capacitor voltage uint16_t high_trigger_b; uint16_t low_trigger_b; // high_trigger_b = voltage_to_DAC(voltage_to_vdac(high_trigger_cap)); low_trigger_b = voltage_to_DAC(low_trigger_cap); state = STANDBY; break; case STANDBY: PORTE.OUT = 0x01; if (flag.PRECHARGE_FLAG) { state = PRECHARGE; // PORTE.OUT = 0x04; // _delay_ms(3000); break; } else if(flag.WRITE_FLAG) { state = WRITE; //delay_ms(3000); break; } else if(flag.READ_FLAG) { state = READ; // PORTE.OUT = 0x05; //_delay_ms(3000); break; } else if(!(PORTD.IN & (1 << 1))) { PORTE.OUT = 0x06; //_delay_ms(3000); if(user_settings.mode == 0) { state = SINGLE_PULSE; break; } else if(user_settings.mode == 1) { state = PAIRED_PULSE; break; } } else { state = STANDBY; break; } break; case PRECHARGE: // Clear precharge flag flag.PRECHARGE_FLAG = 0; flag.DISCHARGED_FLAG = 0; PORTE.OUT = 0x03; //_delay_ms(3000); // Set AC trigger high // write_DAC(high_trigger_b); write_DAC(voltage_to_DAC(voltage_to_vdac(user_settings.voltage_amplitude * V_MAX))); // Go to pulse state, single or paired depending on user settings if (user_settings.mode == 0) // single pulse mode { // Capbank 1 if (flag.CAPBANK_FLAG == 0) { // Close precharge switch Capbank 1 PRECHARGE1_ON; PORTC.OUT |= (1 << 0); // check if precharge reference triggered if(ACA.STATUS & (1 << 4) ) { // TODO: Open precharge switch PRECHARGE1_OFF; PORTC.OUT &= ~(1 << 0); PORTE.OUT = 0x04; //_delay_ms(3000); // Send precharge ready ACK USARTF0.DATA = 0x06; state = STANDBY; break; } } // Capbank 2 else if (flag.CAPBANK_FLAG == 1) { // Close precharge switch Capbank 2 PRECHARGE2_ON; //PORTC.OUT |= (1 << 0); // check if precharge reference triggered // if(ACA.STATUS & (1 << 5) ) if(ACA.STATUS & (1 << 4) ) { // TODO: Open precharge switch PRECHARGE2_OFF; //PORTC.OUT &= ~(1 << 0); PORTE.OUT = 0x04; //_delay_ms(3000); // Send precharge ready ACK USARTF0.DATA = 0x06; state = STANDBY; } } } else if (user_settings.mode == 1) // paired pulse mode { PRECHARGE1_ON; //PORTC.OUT |= (1 << 0); // check if precharge reference capbank 1 triggered if (ACA.STATUS & (1 << 4) ) { // Open precharge switch capbank 1 PRECHARGE1_OFF; //PORTC.OUT &= ~(1 << 0); PORTE.OUT = 0x04; //_delay_ms(1500); PRECHARGE2_ON; //PORTC.OUT |= (1 << 0); // check if precharge reference capbank 2 triggered // if (ACA.STATUS & (1 << 5) ) if (ACA.STATUS & (1 << 4) ) { // Open precharge switch capbank 2 PRECHARGE2_OFF; //PORTC.OUT &= ~(1 << 0); PORTE.OUT = 0x05; //_delay_ms(1500); // Send precharge ready ACK USARTF0.DATA = 0x06; state = STANDBY; } } } else { PORTE.OUT = 0x07; //_delay_ms(1500); // stay in precharge if trigger level not reached state = PRECHARGE; } break; case READ: //_delay_ms(3000); PORTE.OUT = 0x03; // read stuff here // TODO: read not possible yet state = STANDBY; break; case WRITE: PORTE.OUT = 0x02; // write stuff here bufferWrite(&UART_buffer_write, USARTF0.DATA); // Get char from RX buffer flag.WRITE_FLAG = 0; //_delay_ms(1500); state = STANDBY; break; case SINGLE_PULSE: // set reference for AC with DAC write_DAC(low_trigger_b); // single pulse here PORTE.OUT = 0x05; //_delay_ms(3000); if (flag.CAPBANK_FLAG == 0) { // Pulse using capbank 1 single_pulse(CAPBANK1); // TODO: Wait for pulse to damp out _delay_ms(1); } else { // Pulse using capbank 2 single_pulse(CAPBANK2); // TODO: Wait for pulse to damp out _delay_ms(1); } // Toggle capbank used flag.CAPBANK_FLAG ^= (1 << 0); break; case PAIRED_PULSE: // paired pulse here PORTE.OUT = 0x05; //_delay_ms(3000); // set reference for AC with DAC write_DAC(low_trigger_b); state = STANDBY; // Pulse using capbank 1 single_pulse(CAPBANK1); // _delay_us(50); delay_in_ms(user_settings.interstimulus_interval); // Pulse using capbank 2 single_pulse(CAPBANK2); _delay_us(50); // TODO: Wait for pulse to damp out //_delay_ms(1); // When finished, go to precharge state again to wait for new pulse state = STANDBY; break; case DISCHARGE: PORTE.OUT = 0x08; //_delay_ms(3000); // set reference for AC with DAC write_DAC(low_trigger_b); DISCHARGE1_ON; DISCHARGE2_ON; // Discharge complete when both capbank voltages below AC reference // if ( (ACA.STATUS & (1 << 4)) && (ACA.STATUS & (1 << 5)) ) if ( !(ACA.STATUS & (1 << 4))) { DISCHARGE1_OFF; DISCHARGE2_OFF; flag.DISCHARGED_FLAG = 1; flag.HARDWARE_ERROR_FLAG = 0; flag.OVERTEMP_FLAG = 0; // _delay_ms(5000); state = STANDBY; } else { // Keep discharging state = DISCHARGE; } break; } } }
void init_actuators(void){ (*IOp_DO).port=0; init_PWM(); // V3 board init_DAC(); }