void ADC1_Init(void) { INIT_ADC_GPIO(); ADC_DMA_Init(); ConfigADC(); timer3_init(500,(72-1));//500 memset(&adc_param,0,sizeof(adc_param)); ADC1_DMA_Start(); }
int main(void) { setup_systemclock(); //TODO:TimerInitに切り出し // Setup SysTick Timer to interrupt at 10 msec intervals SysTick_Config(CGU_GetPCLKFrequency(CGU_PERIPHERAL_M4CORE)/100); GPIO_SetDir(0,1<<8, 1); // GPIO0[8](LED)を出力に設定 GPIO_ClearValue(0,1<<8);// GPIO0[8](LED)出力L ////////////////////wave_gen/////////////////////////////// gen_dac_cfg_t cfg; cfg.amplitude=5000; cfg.dcOffset=0; cfg.frequency=150; cfg.waveform=GEN_DAC_CFG_WAVE_TRIANGLE; wave_gen(&cfg, &buf); ////////////////////fft/////////////////////////////// fft_gen(); ////////////////spi/////////////////////////////////// lcd_init(); lcd_clear(); ////////////////lcd/////////////////////////////////// // uint16_t lcd_x; // uint16_t lcd_y; // uint8_t scale_time = 2;//横軸データ数を2倍表示 // uint8_t scale_fft = 2;//横軸データ数を2倍表示 ////////////////////////////////////////////////////// // uint16_t i; // int j; ADC_DMA_Init(); NVIC_SetPriority(DMA_IRQn, ((0x01<<3)|0x01)); // Enter an infinite loop while(1) { //wav_genタイムドメイン表示 // for (i = 0; i < 240; i++) { // for (j = 0; j < 25; j++) { // lcd_data[i][j]=0x0000; // } // } // for (i = 0; i < 400*scale_time; i++ ){ // lcd_x = ((int16_t)(i/scale_time) -200) * -1 + 200;//左右逆転(LCD都合 // lcd_y = (buf.LUT_BUFFER[i]-2048)/17 + 120;//0~240に正規化 // lcd_data[lcd_y][lcd_x/16] = lcd_data[lcd_y][lcd_x/16] | 0x01<<(lcd_x%16); // } // lcd_write(lcd_data); systick_delay(50); } ADC_DMA_Exit(); return 0 ; }
/* * @brief Read the analog value of a pin. * Should return a 16-bit value, 0-65536 (0 = LOW, 65536 = HIGH) * Note: ADC is 12-bit. Currently it returns 0-4096 */ int32_t analogRead(uint16_t pin) { // Allow people to use 0-7 to define analog pins by checking to see if the values are too low. if (pin < FIRST_ANALOG_PIN) { pin = pin + FIRST_ANALOG_PIN; } // SPI safety check if (SPI.isEnabled() == true && (pin == SCK || pin == MOSI || pin == MISO)) { return LOW; } // I2C safety check if (Wire.isEnabled() == true && (pin == SCL || pin == SDA)) { return LOW; } // Serial1 safety check if (Serial1.isEnabled() == true && (pin == RX || pin == TX)) { return LOW; } if (pin >= TOTAL_PINS || PIN_MAP[pin].adc_channel == NONE ) { return LOW; } int i = 0; if (adcChannelConfigured != PIN_MAP[pin].adc_channel) { digitalPinModeSaved = PIN_MAP[pin].pin_mode; pinMode(pin, AN_INPUT); } if (adcInitFirstTime == true) { ADC_DMA_Init(); adcInitFirstTime = false; } if (adcChannelConfigured != PIN_MAP[pin].adc_channel) { // ADC1 regular channel configuration ADC_RegularChannelConfig(ADC1, PIN_MAP[pin].adc_channel, 1, ADC_Sample_Time); // ADC2 regular channel configuration ADC_RegularChannelConfig(ADC2, PIN_MAP[pin].adc_channel, 1, ADC_Sample_Time); // Save the ADC configured channel adcChannelConfigured = PIN_MAP[pin].adc_channel; } for(i = 0 ; i < ADC_DMA_BUFFERSIZE ; i++) { ADC_DualConvertedValues[i] = 0; } // Reset the number of data units in the DMA1 Channel1 transfer DMA_SetCurrDataCounter(DMA1_Channel1, ADC_DMA_BUFFERSIZE); // Enable ADC2 external trigger conversion ADC_ExternalTrigConvCmd(ADC2, ENABLE); // Enable DMA1 Channel1 DMA_Cmd(DMA1_Channel1, ENABLE); // Enable ADC1 DMA ADC_DMACmd(ADC1, ENABLE); // Start ADC1 Software Conversion ADC_SoftwareStartConvCmd(ADC1, ENABLE); // Test on Channel 1 DMA1_FLAG_TC flag while(!DMA_GetFlagStatus(DMA1_FLAG_TC1)); // Clear Channel 1 DMA1_FLAG_TC flag DMA_ClearFlag(DMA1_FLAG_TC1); // Disable ADC1 DMA ADC_DMACmd(ADC1, DISABLE); // Disable DMA1 Channel1 DMA_Cmd(DMA1_Channel1, DISABLE); uint16_t ADC1_ConvertedValue = 0; uint16_t ADC2_ConvertedValue = 0; uint32_t ADC_SummatedValue = 0; uint16_t ADC_AveragedValue = 0; for(int i = 0 ; i < ADC_DMA_BUFFERSIZE ; i++) { // Retrieve the ADC2 converted value and add to ADC_SummatedValue ADC2_ConvertedValue = ADC_DualConvertedValues[i] >> 16; ADC_SummatedValue += ADC2_ConvertedValue; // Retrieve the ADC1 converted value and add to ADC_SummatedValue ADC1_ConvertedValue = ADC_DualConvertedValues[i] & 0xFFFF; ADC_SummatedValue += ADC1_ConvertedValue; } ADC_AveragedValue = (uint16_t)(ADC_SummatedValue / (ADC_DMA_BUFFERSIZE * 2)); // Return ADC averaged value return ADC_AveragedValue; }
void ADC_exInit(void) { ADC_LP_UVP_NTC_Config(); ADC_CSOUT_ISMON_Config(); ADC_DMA_Init(); }
int main(void) { u8 i; u8 RecvBuf[32]; u8 SendBuf[32]; u8 offline = 0; u8 recv_flag = 0; u32 pd2ms=0,pd20ams=0,pd20bms=0,pd100ms=0; SystemInit(); RCC_Configuration(); NVIC_Configuration(); GPIO_Configuration(); USART1_Configuration(); dbgPrintf(" Init Ticktack !\r\n"); cycleCounterInit(); SysTick_Config(SystemCoreClock / 1000); for(i=0;i<2;i++) { OP_LED1;OP_LED2;OP_LED3;OP_LED4; delay_ms(500); OP_LED1;OP_LED2;OP_LED3;OP_LED4; delay_ms(500); } FilterInit(); controllerInit(); dbgPrintf(" Init eeprom!\r\n"); FLASH_Unlock(); EE_Init(); EE_Read_ACC_GYRO_Offset(); /* 如果PID丢失或者错误,将下面两行注释去掉,重新编译烧写,运行一遍,可将PID还原,然后重新注释,再烧写一遍 */ //EE_Write_PID(); //EE_Write_Rate_PID(); EE_Read_PID(); EE_Read_Rate_PID(); dbgPrintf(" Init adc!\r\n"); ADC_DMA_Init(); dbgPrintf(" Init NRF24L01 !\r\n"); SPI_NRF_Init(); Nrf24l01_Init(); NRF24l01_SetRxMode(); while(Nrf24l01_Check()) { dbgPrintf("NRF24L01 Fault !\r\n"); delay_ms(500); } dbgPrintf("NRF24L01 Is Detected !\r\n"); dbgPrintf("Init MPU6050...\r\n"); IIC_Init(); MPU6050_initialize(); dbgPrintf("Init Motor...\r\n"); Motor_Init(); Motor_SetPwm(0,0,0,0); pd20bms = TIMIRQCNT + 10*ITS_PER_MS; while(1) { if(TIMIRQCNT>pd2ms + 2*ITS_PER_MS-1) // every 4ms { GetEulerAngle(); if(lock_flag==UNLOCK) AttitudeToMotors(angle.y,angle.x,angle.z); else { MOTOR1=0; MOTOR2=0; MOTOR3=0; MOTOR4=0; } Motor_SetPwm(MOTOR1,MOTOR2,MOTOR3,MOTOR4); pd2ms = TIMIRQCNT; } if(TIMIRQCNT>pd20ams + 20*ITS_PER_MS-1) // every 20ms { if(NRF24l01_Recv(RecvBuf)>10) { if((RecvBuf[RecvBuf[2]+3]==CheckSum(RecvBuf, RecvBuf[2]+3))&&(RecvBuf[0]==0xAA)) { if(RecvBuf[1]!=0xC0) OP_LED1; offline=0; switch(RecvBuf[1]) { case 0xC0: //control Getdesireddata(RecvBuf); OP_LED2; break; case 0x10: //W PID SetPID(RecvBuf); break; case 0x11: //W Attitude SetAccGyroOffset(RecvBuf); break; case 0x12: //W Control offset break; case 0x14: //W Rate PID SetRatePID(RecvBuf); break; case 0x20: //R PID recv_flag = RESEND; GetPID(SendBuf); break; case 0x21: //R Attitude recv_flag = RESEND; GetAccGyroOffset(SendBuf); break; case 0x22: //R Control offset break; case 0x24: //R Rate PID recv_flag = RESEND; GetRatePID(SendBuf); break; case 0x40: //校准姿态 EnableCalibration(); break; case 0x41: //校准遥控器零点 break; default: break; } } } pd20ams = TIMIRQCNT; } if(TIMIRQCNT>pd20bms + 20*ITS_PER_MS-1) // every 20ms { if(recv_flag==0) GetState(SendBuf); else recv_flag--; NRF_SendData(SendBuf); OP_LED3; pd20bms = TIMIRQCNT; } if(TIMIRQCNT>pd100ms + 100*ITS_PER_MS-1) // every 100ms { if(offline>20) lock_flag = LOCK; offline++; // OP_LED4; pd100ms = TIMIRQCNT; } } }
/* * @brief Read the analog value of a pin. * Should return a 16-bit value, 0-65536 (0 = LOW, 65536 = HIGH) * Note: ADC is 12-bit. Currently it returns 0-4096 */ int32_t analogRead(uint16_t pin) { // Allow people to use 0-7 to define analog pins by checking to see if the values are too low. if (pin < FIRST_ANALOG_PIN) { pin = pin + FIRST_ANALOG_PIN; } // SPI safety check if (SPI.isEnabled() == true && (pin == SCK || pin == MOSI || pin == MISO)) { return -1; } // I2C safety check if (Wire.isEnabled() == true && (pin == SCL || pin == SDA)) { return -1; } // Serial1 safety check if (Serial1.isEnabled() == true && (pin == RX || pin == TX)) { return -1; } if (pin >= TOTAL_PINS || PIN_MAP[pin].adc_channel == NONE ) { return -1; } if (adcChannelConfigured != PIN_MAP[pin].adc_channel) { digitalPinModeSaved = PIN_MAP[pin].pin_mode; pinMode(pin, AN_INPUT); } if (adcInitFirstTime == true) { ADC_DMA_Init(); adcInitFirstTime = false; } if (adcChannelConfigured != PIN_MAP[pin].adc_channel) { // ADC1 regular channel configuration ADC_RegularChannelConfig(ADC1, PIN_MAP[pin].adc_channel, 1, ADC_SAMPLING_TIME); // ADC2 regular channel configuration ADC_RegularChannelConfig(ADC2, PIN_MAP[pin].adc_channel, 1, ADC_SAMPLING_TIME); adcChannelConfigured = PIN_MAP[pin].adc_channel; } // Start ADC1 Software Conversion ADC_SoftwareStartConvCmd(ADC1, ENABLE); // Test on Channel 1 DMA1_FLAG_TC flag while(!DMA_GetFlagStatus(DMA1_FLAG_TC1)); // Clear Channel 1 DMA1_FLAG_TC flag DMA_ClearFlag(DMA1_FLAG_TC1); uint16_t ADC_ConvertedValues[ADC_DMA_BUFFERSIZE * 2]; uint32_t ADC_SummatedValue = 0; uint16_t ADC_AveragedValue = 0; uint16_t j = 0; for(uint16_t i = 0 ; i < ADC_DMA_BUFFERSIZE ; i++) { // Fill the table with ADC2 converted values ADC_ConvertedValues[j++] = ADC_DualConvertedValues[i] >> 16; // Fill the table with ADC1 converted values ADC_ConvertedValues[j++] = ADC_DualConvertedValues[i] & 0xFFFF; } for(j = 0 ; j < (ADC_DMA_BUFFERSIZE * 2) ; j++) { ADC_SummatedValue += ADC_ConvertedValues[j]; } ADC_AveragedValue = (uint16_t)(ADC_SummatedValue / (ADC_DMA_BUFFERSIZE * 2)); // Return ADC averaged value return ADC_AveragedValue; }