int main(void) { // b. Umleiten der Standardausgabe stdout (Teil 2) //stdout = &mystdout; // Init everything // Init Touch & Potis DDRA = 0x00; // ADWandler-Pins auf Eingang schalten uint16_t ADC_val; ADC_Init(); // Init LED Matrix TLC5940_Init(); // Init SPI init_SPI(); // Init Timer timer_config(); TLC5940_SetAllDC(63); TLC5940_ClockInDC(); TLC5940_SetAllGS(0); // Init all 74hc595 init_74hc595(); // Init all 74hc165 init_74hc165(); // Enable Interrupts globally // TEMP TEMP TEMP DDRC |= 0b01000000; // Kalibriere Touchpanel calibrate(); sei(); // Init UART uart_init(); while (1) { static uint8_t current_potentiometer = 0; // POTENTIOMETER auslesen { /* switch( current_potentiometer ) { // case 1: // PORTC &= ~0b01000000; // break; case 2: PORTC |= 0b01000000; break; } */ // erstes Auslesen immer Fehlerhaft wegen Touchpanel evtl // zweiter Wert beinhaltet richtiges Ergebniss! // POTI_ADC_SAMPLES sollte daher 2 sein damit nach dem zweiten lesen in ADC_val das richtige ergebniss steht ADC_val = 0; for ( uint8_t count = 0 ; count < POTI_ADC_SAMPLES ; count++ ) ADC_val = ADC_Read(potentiometer[current_potentiometer].adc_channel); if( ADC_val > ( potentiometer[current_potentiometer].value + ADC_delta_for_change_poti ) || ( ADC_val < ( potentiometer[current_potentiometer].value - ADC_delta_for_change_poti ) ) ) // +- 8 von 1024 Quantisierungsstufen / 128 Midi Schritte . // if( ADC_val > ( potentiometer[current_potentiometer].value + 10 ) || ( ADC_val < ( potentiometer[current_potentiometer].value - 10 ) ) ) { potentiometer[current_potentiometer].value = ADC_val; controlChange(midi_channel, midi_poti_offset + current_potentiometer,ADC_val/8); //printf("%i. Poti %i\n", current_potentiometer , potentiometer[current_potentiometer].value ); } current_potentiometer++; if ( current_potentiometer == potentiometer_count) current_potentiometer = 0; } //Display_SetCross(4,2); // TOUCHPANEL auslesen read_touchscreen(); if(touchscreen.FLAG_Display_change) { TLC5940_SetAllGS(0); //Display_SetParabel(touchscreen.last_x , touchscreen.last_y ); Display_SetCross(touchscreen.last_LED_x,touchscreen.last_LED_y); touchscreen.FLAG_Display_change = 0; } } }
void ADC_DMA_Config(void) { ADC_InitTypeDef ADC_InitStructure; ADC_CommonInitTypeDef ADC_CommonInitStructure; DMA_InitTypeDef DMA_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; /* Enable ADC3, DMA2 and GPIO clocks ****************************************/ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2 | RCC_AHB1Periph_GPIOF, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC3, ENABLE); /* DMA2 Stream0 channel0 configuration **************************************/ DMA_InitStructure.DMA_Channel = DMA_Channel_2; DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC3->DR; DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&ADCConvertedValue; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; DMA_InitStructure.DMA_BufferSize = 4;//-------------- DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;//---------------- DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; DMA_InitStructure.DMA_Priority = DMA_Priority_Low; DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_Init(DMA2_Stream0, &DMA_InitStructure); DMA_Cmd(DMA2_Stream0, ENABLE); /* Configure ADC3 Channel12 pin as analog input ******************************/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10;//------------- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOF, &GPIO_InitStructure); /* ADC Common Init **********************************************************/ ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent; ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2; ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles; ADC_CommonInit(&ADC_CommonInitStructure); /* ADC3 Init ****************************************************************/ ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ScanConvMode = ENABLE;//------------------ ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfConversion = 4; ADC_Init(ADC3, &ADC_InitStructure); /* ADC3 regular channel12 configuration *************************************/ ADC_RegularChannelConfig(ADC3, ADC_Channel_5, 1, ADC_SampleTime_3Cycles); ADC_RegularChannelConfig(ADC3, ADC_Channel_6, 2, ADC_SampleTime_3Cycles); ADC_RegularChannelConfig(ADC3, ADC_Channel_7, 3, ADC_SampleTime_3Cycles); ADC_RegularChannelConfig(ADC3, ADC_Channel_8, 4, ADC_SampleTime_3Cycles); /* Enable DMA request after last transfer (Single-ADC mode) */ ADC_DMARequestAfterLastTransferCmd(ADC3, ENABLE); /* Enable ADC3 DMA */ ADC_DMACmd(ADC3, ENABLE); /* Enable ADC3 */ ADC_Cmd(ADC3, ENABLE); }
void Init_Task(uint32_t task_init_data) { int tester=0; //uint_8 sys=0; bool bInitOpen=FALSE; bool bInitStill=FALSE; bool bInitVLPS=FALSE; MQX_TICK_STRUCT ttt; _mqx_uint mqx_ret; trace_init(); show_version_information(); //////////////zga add //Set LPTMR to timeout about 5 seconds Lptmr_Init(1000, LPOCLK); ADC_Init(); Calibrate_ADC(); ADC_Init(); DMA1_Init(); //////////////zga add // clear flag APP_TRACE("start 1\n\r"); _task_create_at(0, SHELL_TASK, 0, shell_task_stack, SHELL_TASK_STACK_SIZE); _task_create_at(0, MMA8415_TASK, 0, mma8451_task_stack, MMA8451_TASK_STACK_SIZE); Lptmr_Start(); for(;;) { mqx_ret = _lwsem_wait(&g_lptmr_int_sem); // _time_delay_ticks(10); tester++; //_time_delay_ticks(10); //APP_TRACE("tester is: %d\r\n",tester); _time_get_elapsed_ticks(&ttt); APP_TRACE("high ttt %d, low ttt%d\r\n", ttt.TICKS[1],ttt.TICKS[0]); if(Measured) { Measured=0; APP_TRACE ("light: %d ,%d \r\n", (uint16_t) MeasuredValues[1],tester); } if((GetTouchON()==TRUE)) { SetSysStatus(ACTIVE_OPEN); } // for test SetSysStatus(ACTIVE_OPEN); switch (sysStatus) { case ACTIVE_OPEN: bInitStill=FALSE; bInitVLPS=FALSE; APP_TRACE ("ACTIVE_OPEN\r\n"); if(bInitOpen==FALSE) { bInitOpen=TRUE; putmma8451running(); SysTick_PDD_EnableDevice(SysTick_BASE_PTR, PDD_ENABLE); } break; case ACTIVE_STILL: bInitOpen=FALSE; bInitVLPS=FALSE; APP_TRACE ("ACTIVE_still\r\n"); if(bInitStill==FALSE) { bInitStill=TRUE; putmma8451detect(); } enter_vlps(); case VLPSMODE: bInitOpen=FALSE; bInitStill=FALSE; APP_TRACE ("vlpsmode\r\n"); if(bInitVLPS==FALSE) { bInitVLPS=TRUE; putmma8451standby(); } enter_vlps(); default: break; } } }
void main() { delay1s(); AUXR = AUXR|0x40; // T1, 1T Mode // IE2 |= ESPI; EXTI0_Init(); //4432的中断设置 UART_Init(); //波特率9600 SPI_Init(MASTER); delay1s(); SI4432_Init(); SI4432_SetRxMode(); //接收模式 ADC_Init(AFPORT_P1_4); delay1s(); #if MULTI_SENSOR SendString("ROMID Search...\r\n"); SendROMID(DS18B20_SearchRomID()); SendString("\r\n"); //调试信息时候用 #endif //----------------------------------------------------- EA = 1; //注意:外设初始化完再开中断! while(1) { if(Trans_RevPakFin) { Trans_RevPakFin = 0; //液位采集计算 ADC_STARTCOV(ADC_CH4,ADC_SPEED_540T); while(!(g_sensor_sta1&PRS_RDY)); //等待压力采集完成 //温度采集计算 // TemperDatHandle(); //液位开关采集 sensor_data.possw = POSSW; //流量开关采集 // sensor_data.flow = FLOW; //打包 if(1==Pak_Handle()) { g_sensor_sta1 = 0; //清除所有传感器标志位 LED2 = 0; SendString("valid cmd received.\r\n"); delay200ms(); LED2 = 1; } } //********************code for test************************************** // DATA_Cmd_ACK(); // LED2 = 0; // SendString("valid cmd received.\r\n"); // delay200ms(); // LED2 = 1; // ADC_STARTCOV(ADC_CH4,ADC_SPEED_540T); // while(!(g_sensor_sta1&PRS_RDY)); //等待压力采集完成 // sprintf("level: %d:\r\n",); // SendString("level: %s:\r\n"); // sensor_data.flow = FLOW; // SendString("flow data:\r\n"); // SendByteASCII(sensor_data.flow); // SendString("\r\n"); // delay1s(); // delay1s(); //********************end of code for test************************************** }//end of while }//end of main
void adcInit(drv_adc_config_t *init) { ADC_InitTypeDef ADC_InitStructure; ADC_CommonInitTypeDef ADC_CommonInitStructure; DMA_InitTypeDef DMA_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; bool multiChannel = init->powerAdcChannel > 0; /* Enable ADC1, DMA2 clocks *************************************************/ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); /* DMA2 Stream0 channel0 configuration **************************************/ DMA_DeInit(DMA2_Stream0); DMA_InitStructure.DMA_Channel = DMA_Channel_0; DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR; DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)adcValues; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; DMA_InitStructure.DMA_BufferSize = multiChannel ? 2 : 1; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_Init(DMA2_Stream0, &DMA_InitStructure); DMA_Cmd(DMA2_Stream0, ENABLE); /* Configure ADC3 Channel12 pin as analog input ******************************/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOC, &GPIO_InitStructure); /* ADC Common Init **********************************************************/ ADC_DeInit(); ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent; ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2; ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_20Cycles; ADC_CommonInit(&ADC_CommonInitStructure); /* ADC1 Init ****************************************************************/ ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ScanConvMode = multiChannel ? ENABLE : DISABLE; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; ADC_InitStructure.ADC_ExternalTrigConv = 0; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfConversion = multiChannel ? 2 : 1; ADC_Init(ADC1, &ADC_InitStructure); /* Enable ADC1 DMA */ //ADC_DMACmd(ADC1, ENABLE); ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 1, ADC_SampleTime_28Cycles); if (multiChannel) ADC_RegularChannelConfig(ADC1, init->powerAdcChannel, 2, ADC_SampleTime_28Cycles); ADC_DMACmd(ADC1, ENABLE); ADC_Cmd(ADC1, ENABLE); // Calibrate ADC //ADC_ResetCalibration(ADC1); //while(ADC_GetResetCalibrationStatus(ADC1)); // ADC_StartCalibration(ADC1); // while(ADC_GetCalibrationStatus(ADC1)); // Fire off ADC ADC_SoftwareStartConv(ADC1); }
int main(void) { char temp, i; LCD_Initialize(); DDRB = 0b00000000; PORTB = 0b00001111; DDRA = 0xFF; ADC_Init(); int value = 0; int calculations = 0; char dzialanie = 0; int digit = 0; do{ int digit = getADC(0); char sw0 = PINB & 0b00000001; char sw1 = PINB & 0b00000010; if(sw0 != 0b00000001) { state++; _delay_ms(300); } char str[15]; sprintf(str, "%15d", lastValue); LCD_GoTo(1,0); LCD_WriteText(str); switch(state){ case 0: if(sw1 != 0b00000010){ setNewValue(mappingLogToLinear(digit, digitMap, 10)); _delay_ms(300); } sprintf(str, "%15d", mappingLogToLinear(digit, digitMap,10)); LCD_GoTo(1,1); LCD_WriteText(str); break; case 1: if(sw1 != 0b00000010){ setSign(mappingLogToLinear(digit, signMap, 2)); power = 0; _delay_ms(300); } switch(mappingLogToLinear(digit, signMap,2)){ case 0: LCD_GoTo(1,1); LCD_WriteText("-"); break; case 1: LCD_GoTo(1,1); LCD_WriteText("+"); break; } break; case 2: if(sw1 != 0b00000010){ doCalculations(mappingLogToLinear(digit, expressionMap, 4)); power = 0; newValue=0; state = 0; _delay_ms(300); } switch(mappingLogToLinear(digit, expressionMap,4)){ case 0: LCD_GoTo(1,1); LCD_WriteText("+"); break; case 1: LCD_GoTo(1,1); LCD_WriteText("-"); break; case 2: LCD_GoTo(1,1); LCD_WriteText("*"); break; case 3: LCD_GoTo(1,1); LCD_WriteText("/"); break; } break; } /* char sw0 = PINB & 0b00000001; char sw1 = PINB & 0b00000010; char sw2 = PINB & 0b00000100; char sw3 = PINB & 0b00001000; if(sw0 != 0b00000001) value++; if(sw1 != 0b00000010) value--; if(sw2 != 0b00000100) { if(dzialanie == 0) calculations += value; if(dzialanie == 1) calculations -= value; if(dzialanie == 2) calculations /= value; if(dzialanie == 3) calculations *= value; value = 0; } if(sw3 != 0b00001000) { dzialanie++; dzialanie = dzialanie % 4; } char str[15]; sprintf(str, "%15d", calculations); LCD_GoTo(1,0); LCD_WriteText(str); sprintf(str, "%15d", value); LCD_GoTo(1,1); LCD_WriteText(str); _delay_ms(300); if(dzialanie == 0){ LCD_GoTo(0,0); LCD_WriteText("+"); } if(dzialanie == 1){ LCD_GoTo(0,0); LCD_WriteText("-"); } if(dzialanie == 2){ LCD_GoTo(0,0); LCD_WriteText("/"); } if(dzialanie == 3){ LCD_GoTo(0,0); LCD_WriteText("*"); }*/ }while(1); return 0; }
int hal_adc_open(HAL_ADC_HANDLE *handle, void *params) { ADC_InitTypeDef ADC_InitStructure; ADC_CommonInitTypeDef ADC_CommonInitStructure; DMA_InitTypeDef DMA_InitStructure; UNUSED(params); /* DMA2_Stream0 channel0 configuration **************************************/ DMA_DeInit(DMA2_Stream0); DMA_InitStructure.DMA_Channel = DMA_Channel_0; DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC1_DR_Address; DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)convertedValues; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; DMA_InitStructure.DMA_BufferSize = 1; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_Init(DMA2_Stream0, &DMA_InitStructure); /* DMA2_Stream0 enable */ DMA_Cmd(DMA2_Stream0, ENABLE); /* ADC Common Init **********************************************************/ ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent; ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2; ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_20Cycles; ADC_CommonInit(&ADC_CommonInitStructure); /* ADC1 Init ****************************************************************/ ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ScanConvMode = DISABLE; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfConversion = 1; ADC_Init(ADC1, &ADC_InitStructure); /* Enable ADC1 DMA */ ADC_DMACmd(ADC1, ENABLE); /* ADC1 regular channel18 (VBAT) configuration ******************************/ ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 1, ADC_SampleTime_480Cycles); /* Enable DMA request after last transfer (Single-ADC mode) */ ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE); /* Enable ADC1 **************************************************************/ ADC_Cmd(ADC1, ENABLE); ADC_SoftwareStartConv(ADC1); *handle = (void *)convertedValues; return HAL_ADC_E_SUCCESS; }
/******************************************************************************* * Function Name : main * Description : Main program * Input : None * Output : None * Return : None *******************************************************************************/ int main(void) { /* define local values */ u16 DataValue = 0x0; u16 Keta = 0; u16 cnt = 0; // counter u32 wcnt = 0; // counter for wait u16 Temperature = 0 ; u8 a, b, c, d; #ifdef DEBUG debug(); #endif /* System Clocks Configuration */ RCC_Configuration(); /* NVIC configuration */ NVIC_Configuration(); /* Configure the GPIO ports */ GPIO_Configuration(); // ADC用のGPIOの設定はしなくていいよね。 /* USART2 configuration ------------------------------------------------------*/ /* USART2 configured as follow: - BaudRate = 115200 baud - Word Length = 8 Bits - One Stop Bit - No parity - Hardware flow control enabled (RTS and CTS signals) - Receive and transmit enabled */ USART_InitStructure.USART_BaudRate = 115200; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No ; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_RTS_CTS; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_Init(USART2, &USART_InitStructure); /* Enable the USART2 */ USART_Cmd(USART2, ENABLE); /* ADC1 configuration ------------------------------------------------------*/ ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; ADC_InitStructure.ADC_ScanConvMode = ENABLE; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfChannel = 1; ADC_Init(ADC1, &ADC_InitStructure); /* ADC1 regular channels configuration */ ADC_RegularChannelConfig(ADC1, ADC_Channel_16, 1, ADC_SampleTime_239Cycles5); ADC_RegularChannelConfig(ADC1, ADC_Channel_17, 2, ADC_SampleTime_239Cycles5); /* Enable ADC1 DMA */ // ADC_DMACmd(ADC1, ENABLE); /* Enable Vrefint channel17 */ ADC_TempSensorVrefintCmd(ENABLE); /* Enable ADC1 */ ADC_Cmd(ADC1, ENABLE); /* Enable ADC1 reset calibaration register */ ADC_ResetCalibration(ADC1); /* Check the end of ADC1 reset calibration register */ while(ADC_GetResetCalibrationStatus(ADC1)); /* Start ADC1 calibaration */ ADC_StartCalibration(ADC1); /* Check the end of ADC1 calibration */ while(ADC_GetCalibrationStatus(ADC1)); /* Start ADC1 Software Conversion */ ADC_SoftwareStartConvCmd(ADC1, ENABLE); /* Test on DMA1 channel1 transfer complete flag */ //while(!DMA_GetFlagStatus(DMA1_FLAG_TC1)); /* Clear DMA1 channel1 transfer complete flag */ //DMA_ClearFlag(DMA1_FLAG_TC1); /* DEBUG MARKER */ /* ************* */ /* Turn on led connected to PC.06 pin */ //GPIO_SetBits(GPIOC, GPIO_Pin_6); /* Turn off led connected to PC.06 pin */ GPIO_ResetBits(GPIOC, GPIO_Pin_6); /* Communication hyperterminal-USART2 using hardware flow control -------------*/ /* Send a buffer from USART to hyperterminal */ while(NbrOfDataToTransfer--) { USART_SendData(USART2, TxBuffer[TxCounter++]); while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); } /* ************* */ while (1){ //while( ADC_GetFlagStatus( ADC1, ADC_FLAG_EOC) == RESET ); //DataValue = ADCConvertedValue; while( ADC_GetFlagStatus( ADC1, ADC_FLAG_EOC) == RESET ); while( ( wcnt++ % 0x400000) != 0x0 ){ // wait loop DataValue = ADC_GetConversionValue(ADC1); } Temperature= (1.42 - DataValue*3.3/4096)*1000/4.35 + 25; DataValue = Temperature; // rotate value. Keta = ( DataValue & 0xf000 ) / 0x1000 ; Keta = (( DataValue & 0x0f00 ) / 0x10 ) + Keta ; Keta = (( DataValue & 0x00f0 ) * 0x10 ) + Keta ; Keta = (( DataValue & 0x000f ) * 0x1000 ) + Keta ; DataValue = Keta ; // printf ( "%s", DataValue ); for ( cnt = 0 ; cnt < 4 ; cnt++ ){ Keta = 0 ; if ( DataValue % 16 >= 10 ){ Keta = 'A' + ( DataValue % 16 - 10 ); } else { Keta = '0' + ( DataValue % 16 ); } DataValue = DataValue /16 ; USART_SendData(USART2, Keta ); while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); } // printf( "\n\r" ); USART_SendData(USART2, '\n'); while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); USART_SendData(USART2, '\r'); while(USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET); } /* Receive a string (Max RxBufferSize bytes) from the Hyperterminal ended by '\r' (Enter key) */ do { if((USART_GetFlagStatus(USART2, USART_FLAG_RXNE) != RESET)&&(RxCounter < RxBufferSize)) { RxBuffer[RxCounter] = USART_ReceiveData(USART2); USART_SendData(USART2, RxBuffer[RxCounter++]); } } while((RxBuffer[RxCounter - 1] != '\r')&&(RxCounter != RxBufferSize)); while (1) { } }
/*----------------------------------------------------------------------------*/ void sys_adc_init() { GPIO_InitTypeDef GPIO_InitStructure; ADC_InitTypeDef ADC_InitStructure; DMA_InitTypeDef DMA_InitStructure; /* Enable DMA1 clock */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1, ENABLE); #define DIV RCC_PCLK2_Div2 //#define DIV RCC_PCLK2_Div4 //#define DIV RCC_PCLK2_Div6 //#define DIV RCC_PCLK2_Div8 RCC_ADCCLKConfig(DIV); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); /* GPIO */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; //GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOC, &GPIO_InitStructure); /* DMA1 channel1 configuration ----------------------------------------------*/ DMA_DeInit(DMA1_Channel1); DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&adc_buffer; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = ADC_BUFFER_SIZE; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA1_Channel1, &DMA_InitStructure); /* Enable DMA1 channel1 */ DMA_Cmd(DMA1_Channel1, ENABLE); /* ADC1 configuration ------------------------------------------------------*/ ADC_Cmd(ADC1, DISABLE); ADC_DeInit(ADC1); ADC_StructInit(&ADC_InitStructure); ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; ADC_InitStructure.ADC_ScanConvMode = ENABLE; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfChannel = 1; ADC_Init(ADC1, &ADC_InitStructure); /* ADC1 regular channel12 configuration */ //#define SAMPLE_TIME ADC_SampleTime_1Cycles5 //#define SAMPLE_TIME ADC_SampleTime_7Cycles5 //#define SAMPLE_TIME ADC_SampleTime_13Cycles5 #define SAMPLE_TIME ADC_SampleTime_28Cycles5 //#define SAMPLE_TIME ADC_SampleTime_41Cycles5 //#define SAMPLE_TIME ADC_SampleTime_55Cycles5 //#define SAMPLE_TIME ADC_SampleTime_71Cycles5 //#define SAMPLE_TIME ADC_SampleTime_239Cycles5 ADC_RegularChannelConfig(ADC1, ADC_Channel_12, 1, SAMPLE_TIME); /* Enable ADC1 DMA */ ADC_DMACmd(ADC1, ENABLE); /* Enable ADC1 */ ADC_Cmd(ADC1, ENABLE); sys_sleep(2); /* Enable ADC1 reset calibration register */ ADC_ResetCalibration(ADC1); /* Check the end of ADC1 reset calibration register */ while(ADC_GetResetCalibrationStatus(ADC1)); /* Start ADC1 calibration */ ADC_StartCalibration(ADC1); /* Check the end of ADC1 calibration */ while(ADC_GetCalibrationStatus(ADC1)); /* Start ADC1 Software Conversion */ ADC_SoftwareStartConvCmd(ADC1, ENABLE); #ifdef USE_CONSOLE console_cmd_init(&adc_console1); adc_console1.cmd = "adc"; adc_console1.help = "adc - ADC read ADC DMA statistic"; adc_console1.handler = adc_cmd1; console_add_cmd(&adc_console1); #endif //USE_CONSOLE }
/** * @brief Main program * @param None * @retval : None */ int main(void) { /* System Clocks Configuration */ RCC_Configuration(); /* Configure the GPIO ports */ GPIO_Configuration(); /* DMA1 Channel5 configuration ----------------------------------------------*/ DMA_DeInit(DMA1_Channel5); DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)TIM1_CCR1_Address; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)ADC1_DR_Address; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralDST; DMA_InitStructure.DMA_BufferSize = 1; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA1_Channel5, &DMA_InitStructure); /* Enable DMA1 Channel5 */ DMA_Cmd(DMA1_Channel5, ENABLE); /* ADC1 configuration ------------------------------------------------------*/ ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; ADC_InitStructure.ADC_ScanConvMode = DISABLE; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfChannel = 1; ADC_Init(ADC1, &ADC_InitStructure); /* ADC1 RegularChannelConfig Test */ ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 1, ADC_SampleTime_55Cycles5); /* TIM1 configuration ------------------------------------------------------*/ /* Time Base configuration */ TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Period = 0xFF0; TIM_TimeBaseStructure.TIM_Prescaler = 0x0; TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); /* Channel1 Configuration in PWM mode */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OC1Init(TIM1, &TIM_OCInitStructure); /* Enable TIM1 */ TIM_Cmd(TIM1, ENABLE); /* Enable TIM1 outputs */ TIM_CtrlPWMOutputs(TIM1, ENABLE); /* Enable TIM1 DMA interface */ TIM_DMACmd(TIM1, TIM_DMA_Update, ENABLE); /* Enable ADC1 */ ADC_Cmd(ADC1, ENABLE); /* Enable ADC1 reset calibaration register */ ADC_ResetCalibration(ADC1); /* Check the end of ADC1 reset calibration register */ while(ADC_GetResetCalibrationStatus(ADC1)); /* Start ADC1 calibaration */ ADC_StartCalibration(ADC1); /* Check the end of ADC1 calibration */ while(ADC_GetCalibrationStatus(ADC1)); /* Start ADC1 conversion */ ADC_SoftwareStartConvCmd(ADC1, ENABLE); while (1) { } }
//============================================================================== // ADC 初始化 //============================================================================== void F_InitialADC(void) { ADC_InitTypeDef ADC_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; // 聲明定時器初始化結構體 TIM_OCInitTypeDef TIM_OCInitStructure; /* GPIOC Periph clock enable */ RCC_AHBPeriphClockCmd(P_VR2_GPIO_CLK, ENABLE); /* ADC1 Periph clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); /* TIM3 Periph clock enable */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); /* Configure ADC Channel0 as analog input */ GPIO_InitStructure.GPIO_Pin = P_VR2_PIN ; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(P_VR2_GPIO_PORT, &GPIO_InitStructure); /* TIM3 Configuration *******************************************************/ TIM_DeInit(TIM3); TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_OCStructInit(&TIM_OCInitStructure); /* Time base configuration */ TIM_TimeBaseStructure.TIM_Period = 0xFF; TIM_TimeBaseStructure.TIM_Prescaler = 0x0; TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); /* TIM3 TRGO selection */ TIM_SelectOutputTrigger(TIM3, TIM_TRGOSource_Update); /* ADC2 Configuration *******************************************************/ /* ADCs DeInit */ ADC_DeInit(ADC1); /* Configure the ADC1 in continous mode withe a resolutuion equal to 8 bits*/ ADC_InitStructure.ADC_Resolution = ADC_Resolution_8b; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_Rising; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T3_TRGO; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Upward; ADC_Init(ADC1, &ADC_InitStructure); /* Convert the ADC1 Channel 0 with 239.5 Cycles as sampling time */ ADC_ChannelConfig(ADC1, P_VR2_Channel , ADC_SampleTime_28_5Cycles); /* ADC Calibration */ ADC_GetCalibrationFactor(ADC1); /* Enable the auto delay feature */ ADC_WaitModeCmd(ADC1, ENABLE); /* Enable the Auto power off mode */ ADC_AutoPowerOffCmd(ADC1, ENABLE); /* Enable ADCperipheral[PerIdx] */ ADC_Cmd(ADC1, ENABLE); /* Wait the ADCEN falg */ while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADEN)); /* TIM2 enable counter */ TIM_Cmd(TIM3, ENABLE); /* ADC1 regular Software Start Conv */ ADC_StartOfConversion(ADC1); }
int main(void){ int i = 0; //Score set score = 0; PLL_Init(); //Sound init DAC_Init(); Timer0A_Init(Sound_Update, 1000000/11025); // Timer0B_Init(updateXAxis, 1000000/11025); // Timer1B_Init(updateYAxis, 1000000/11025); //Input ADC_Init(); portD_Init(); SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOG); GPIOPinTypeGPIOInput(GPIO_PORTG_BASE, (GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7)); GPIOPinTypeGPIOOutput(GPIO_PORTG_BASE, GPIO_PIN_2); GPIOPadConfigSet(GPIO_PORTG_BASE, (GPIO_PIN_3 | GPIO_PIN_4 | GPIO_PIN_5 | GPIO_PIN_6 | GPIO_PIN_7), GPIO_STRENGTH_2MA, GPIO_PIN_TYPE_STD_WPU); SysTick_Init(50000000/400); Output_Init(); Output_Color(15); SysTick_IntEnable(); EnableInterrupts(); //Set flags gFlags = 0; HWREGBITW(&gFlags, TITLE_SCREEN) = True; //Math rand set seed while(HWREGBITW(&gFlags, SELECT_DOWN) == 0 && ((GPIO_PORTG_DATA_R & 0x80) != 0)) { } while(HWREGBITW(&gFlags, SELECT_DOWN) == 1 || ((GPIO_PORTG_DATA_R & 0x80) == 0)) { } setSeed(NVIC_ST_CURRENT_R); //Game set setGraphics(1);//the lm3s can't handle more than 2 rocks at graphics level 3. gameInit(); gameSet(0); while(1) { //Only draw to buffer when it has been output to the screen if(HWREGBITW(&gFlags, FRAME_BUFFER_READY) == False) { /*if(gameLevel == -2) { drawString(myMsgs[1], makePoint(50, 40)); while ((GPIO_PORTG_DATA_R & 0x80) != 0 && HWREGBITW(&gFlags, SELECT_DOWN) == False) { } while ((GPIO_PORTG_DATA_R & 0x80) == 0 && HWREGBITW(&gFlags, SELECT_DOWN) == True) { } //Reset game */ //Check for level completion, aka all rocks and enemies are //TODO: enemies if(HWREGBITW(&gFlags, LEVEL_COMPLETE) == True) { gameSet(++gameLevel); } //Redraw the screen from scratch. clearBuffer(); //Draw the player. if(gPlayer.status == ALIVE) { drawPlayer(makePoint((int)gPlayer.x, (int)gPlayer.y), gPlayer.angle, gPlayer.exhaustOn); } for(i = 0; i < MAX_ROCKS; i++) { if(gRocks[i].status == ALIVE) { drawRock(makePoint(gRocks[i].x, gRocks[i].y), gRocks[i].rockType, gRocks[i].rockSize); } } //Draw allied bullets. for(i = 0; i < MAX_PLAYER_BULLETS; i++) { if(gPlayerBullets[i].status == ALIVE) { drawBullet(makePoint(gPlayerBullets[i].x, gPlayerBullets[i].y)); } } //Draw enemy bullets. for(i = 0; i < MAX_ENEMY_BULLETS; i++) { if(gEnemyBullets[i].status == ALIVE) { drawBullet(makePoint(gEnemyBullets[i].x, gEnemyBullets[i].y)); } } //Draw explosions. for(i = 0; i < MAX_EXPLOSIONS; i++) { if(gExplosions[i].status == ALIVE) { drawExplosion(gExplosions[i].pos, gExplosions[i].current); } } // drawNumber(score, makePoint(2,2)); drawNumber(gameLevel, makePoint(128/2-6,2)); if(HWREGBITW(&gFlags, GAME_OVER) == True) { drawString("GAME OVER", makePoint(40, 38)); if((GPIO_PORTG_DATA_R & 0x80)) { //reset game } gameUpdate(); } else if(HWREGBITW(&gFlags, TITLE_SCREEN) == True) { drawString("ASTEROIDS", makePoint(40, 38)); gameLevel = 0; } updateXAxis(); gameUpdate(); HWREGBITW(&gFlags, FRAME_BUFFER_READY) = True; } } }
int main(void) { /*Ждем пока все включится*/ _delay_ms(100); /*Настраиваем порты ввода-вывода*/ DDRB = 1<<PORTB0|1<<PORTB1|1<<PORTB2|1<<PORTB3|1<<PORTB4|1<<PORTB5|1<<PORTB6|1<<PORTB7; DDRC = 1<<PORTC0|1<<PORTC1|1<<PORTC2|0<<PORTC3|0<<PORTC4|0<<PORTC5|0<<PORTC6|0<<PORTC7; DDRD = 0<<PORTD0|0<<PORTD1|0<<PORTD2|0<<PORTD3|1<<PORTD4|0<<PORTD5|0<<PORTD6|1<<PORTD7; PORTB = 1; PORTD = 1 << PORTD2; /*Тяга двигателей на минимум*/ for(uint8_t k = 0; k < CHANNELS_COUNT; ++k) { counter[k] = LOW; } /*Настраиваем I2C*/ TWSR = 0x00; TWBR = ((F_CPU / I2C_SPEED) - 16) / 2; _delay_us(10); /*Включаем Таймер0*/ TCCR0 = 1<<CS02 | 0<<CS01 | 0<<CS00; /*Включаем Таймер1*/ OCR1A=HIGH; //TOP TCCR1A=0<<COM1A1|0<<COM1A0|1<<COM1B1|0<<COM1B0|0<<FOC1A|0<<FOC1B|1<<WGM11|1<<WGM10; TCCR1B=0<<ICNC1|0<<ICES1|1<<WGM13|1<<WGM12|0<<CS12|0<<CS11|1<<CS10; TIMSK= 1<<TOIE2 | 1<<OCIE1A|1<<OCIE1B|0<<TOIE1|1<<TOIE0|0<<OCIE0; OCR1B=LOW; /*Включаем АЦП*/ ADC_Init(); /*Включаем прерывание INT0(высотомер)*/ INT0_Init(); /*Разрешаем работу прерываний*/ sei(); /*Настраиваем Modbus*/ eMBErrorCode eStatus = eMBInit( MB_RTU, 0x01, 0, 57600, MB_PAR_NONE ); eStatus = eMBEnable(); /*Настраиваем сенсоры*/ SensorsInit(); /*Загружаем в Holding Registers и в массив параметров значения из EEPROM*/ ModbusInitValues(); filterInit(); while(1) { /*Актуализируем значения Modbus-регистров в соответствии со значениями параметров*/ ModbusLoader(); /*Актуализируем значения параметров в соответствии со значениями Holding Registers*/ ModbusSaver(); /*Итерация Modbus*/ eMBPoll(); /*Ресурсоемкий расчет курса*/ Course_Calc(); } }
int main(void) { CanMessage tmp; /* Local Variables */ #if ( TERMINAL == 1 ) UART_Init(); /* UART */ ADC_Init(); /* ADC */ #endif GPIO_Init(); /* GPIO */ ExtINT_Init(); /* External Interrupt */ Timer_Init(); /* Timers */ SPI_Init(); /* SPI */ CanStatus res = CAN_Init(CAN_SPEED); /* Start CAN */ #if ( TERMINAL == 1 ) term_Start(res); /* Start Terminal */ #endif Msg_Init(); /* Construct Messages to be sent */ CAN_BufInit( &RxBuffer, CAN_RX_BUFFER_SIZE ); /* Initialize Receive Buffer */ CAN_BufInit( &TxBuffer, CAN_TX_BUFFER_SIZE ); /* Initialize Transmit Buffer */ sei(); /* Enable Global Interrupts */ /* ---------------------------*/ while(1){ wdt_enable(WDTO_1S); /* Enable Watchdog Timer for 2 second */ /* ------------------------------------------ */ /* Send Messages */ // Get Data ATOMIC_BLOCK( ATOMIC_FORCEON ){ if( CAN_BufState( &TxBuffer ) != BUFFER_EMPTY ){/* Check if empty */ CAN_BufDeq( &TxBuffer, &tmp ); /* Dequeue */ CAN_SendMsg( &tmp ); /* Send */ } } /* ------------------------------------------ */ /* Receive Messages */ ATOMIC_BLOCK( ATOMIC_FORCEON ){ /* Read Interrupt variables */ if( CAN_BufState( &RxBuffer ) != BUFFER_EMPTY ){/* Check if not empty */ CAN_BufDeq( &RxBuffer, &tmp ); /* Dequeue */ Msg_Chk( &tmp ); /* Check Received Message */ #if ( TERMINAL == 1 ) if( strm == MS_STREAM ) /* Enable Terminal Message Stream */ term_RxMsg( &tmp ); #endif } } /* ------------------------------------------ */ #if ( TERMINAL == 1 ) term_Main(); /* TERMINAL FOR DEBUGGING */ #endif } wdt_reset(); /* Reset Watchdog */ wdt_disable(); return 0; }
void init( void ) { SystemInit(); // Set Systick to 1ms interval, common to all SAM3 variants if (SysTick_Config(SystemCoreClock / 1000)) { // Capture error while (true); } /* Configure the SysTick Handler Priority: Preemption priority and subpriority */ NVIC_SetPriority(SysTick_IRQn, 15); // Disable watchdog //WDT_Disable(WDT); // Initialize C library __libc_init_array(); // default 13pin led will off. pinMode(13,OUTPUT); digitalWrite(13, LOW); /* // Enable parallel access on PIo output data registers PIOA->PIO_OWER = 0xFFFFFFFF; PIOB->PIO_OWER = 0xFFFFFFFF; PIOC->PIO_OWER = 0xFFFFFFFF; PIOD->PIO_OWER = 0xFFFFFFFF; // Initialize Serial port U(S)Art pins PIO_Configure( g_APinDescription[PINS_UART].pport, g_APinDescription[PINS_UART].ulpintype, g_APinDescription[PINS_UART].ulpin, g_APinDescription[PINS_UART].ulpinconfiguration); digitalWrite(0, HIGH); // Enable pullup for rx0 PIO_Configure( g_APinDescription[PINS_USART0].pport, g_APinDescription[PINS_USART0].ulpintype, g_APinDescription[PINS_USART0].ulpin, g_APinDescription[PINS_USART0].ulpinconfiguration); PIO_Configure( g_APinDescription[PINS_USART1].pport, g_APinDescription[PINS_USART1].ulpintype, g_APinDescription[PINS_USART1].ulpin, g_APinDescription[PINS_USART1].ulpinconfiguration); PIO_Configure( g_APinDescription[PINS_USART3].pport, g_APinDescription[PINS_USART3].ulpintype, g_APinDescription[PINS_USART3].ulpin, g_APinDescription[PINS_USART3].ulpinconfiguration); // Initialize USB pins PIO_Configure( g_APinDescription[PINS_USB].pport, g_APinDescription[PINS_USB].ulpintype, g_APinDescription[PINS_USB].ulpin, g_APinDescription[PINS_USB].ulpinconfiguration); // Initialize CAN pins PIO_Configure( g_APinDescription[PINS_CAN0].pport, g_APinDescription[PINS_CAN0].ulpintype, g_APinDescription[PINS_CAN0].ulpin, g_APinDescription[PINS_CAN0].ulpinconfiguration); PIO_Configure( g_APinDescription[PINS_CAN1].pport, g_APinDescription[PINS_CAN1].ulpintype, g_APinDescription[PINS_CAN1].ulPin, g_APinDescription[PINS_CAN1].ulPinConfiguration); */ //disable JTAG-DP,release pin 29(PB3),30(PB4),23(PA15) RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE); //GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable,ENABLE); ////remap Timer4 //GPIO_PinRemapConfig(GPIO_Remap_TIM4,ENABLE); ////remap USART3 //GPIO_PinRemapConfig(GPIO_FullRemap_USART3,ENABLE); ////remap USART2 //GPIO_PinRemapConfig(GPIO_Remap_USART2,ENABLE); ////remap CAN1,to PD0,PD1 //GPIO_PinRemapConfig(GPIO_Remap2_CAN1,ENABLE); // Initialize Analog Controller ADC_InitTypeDef ADC_InitStructure; ADC_CommonInitTypeDef ADC_CommonInitStructure; //RCC_AHB1PeriphClockCmd( RCC_AHB1Periph_GPIOC, ENABLE); // Enable ADC1 clock RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); ADC_DeInit(); ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent; ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_6Cycles; ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2; ADC_CommonInit(&ADC_CommonInitStructure); ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; /* ADC1 regular channel 12 configuration ************************************/ ADC_InitStructure.ADC_ScanConvMode = DISABLE; ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; //ADC_InitStructure.ADC_ExternalTrigConv = ; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfConversion = 1; ADC_Init(ADC1, &ADC_InitStructure); // Enable ADC1 ADC_Cmd(ADC1, ENABLE); // Enable ADC1 reset calibration register //ADC_ResetCalibration(ADC1); // Check the end of ADC1 reset calibration register while(ADC_GetSoftwareStartConvStatus(ADC1)); // Start ADC1 calibration //ADC_StartCalibration(ADC1); // Check the end of ADC1 calibration //while(ADC_GetCalibrationStatus(ADC1)); // Initialize analogOutput module analogOutputInit(); /* Configure the NVIC Preemption Priority Bits */ /* 4 bits for pre-emption priority(0-15 PreemptionPriority) and 0 bits for subpriority(0 SubPriority) */ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_4); }
/*stm32 iic³õʼ»¯*/ void pin_init1() { GPIO_InitTypeDef GPIO_InitStructure; ADC_InitTypeDef ADC_InitStructure; /* Enable GPIOB,E,F,G clock */ //DEBUG("power pin init 2\r\n"); RCC_ADCCLKConfig(RCC_PCLK2_Div4); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOG, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC3, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); /* Enable I2C2 clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE); GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE); GPIO_PinRemapConfig(GPIO_Remap_SPI3, ENABLE); GPIO_PinRemapConfig(GPIO_FullRemap_USART3, ENABLE); GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD; GPIO_InitStructure.GPIO_Pin = BATS_I2C_SCL_PIN; GPIO_Init(BATS_I2C_SCL_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = BATS_I2C_SDA_PIN; GPIO_Init(BATS_I2C_SDA_PORT, &GPIO_InitStructure); /* Config pin */ GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Pin = BATS_I2C_SEL_PIN; GPIO_Init(BATS_I2C_SEL_PORT, &GPIO_InitStructure); // GPIO_InitStructure.GPIO_Pin = BATS_SEL_C_PIN; // GPIO_Init(BATS_SEL_C_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = BATS_SEL_B_PIN; GPIO_Init(BATS_SEL_B_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = BATS_SEL_A_PIN; GPIO_Init(BATS_SEL_A_PORT, &GPIO_InitStructure); //GPIO_InitStructure.GPIO_Pin = BATS_SEL_STAC_PIN; //GPIO_Init(BATS_SEL_STAC_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = BATS_SEL_STAB_PIN; GPIO_Init(BATS_SEL_STAB_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = BATS_SEL_STAA_PIN; GPIO_Init(BATS_SEL_STAA_PORT, &GPIO_InitStructure); //GPIO_InitStructure.GPIO_Pin = BATS_C_CHARGE_CTL_PIN; //GPIO_Init(BATS_C_CHARGE_CTL_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = BATS_ABC_CHARGE_FAULT_PIN; GPIO_Init(BATS_ABC_CHARGE_FAULT_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = BATS_ABC_CHARGE_CHRG_PIN; GPIO_Init(BATS_ABC_CHARGE_CHRG_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = PG_3V3_PIN; GPIO_Init(PG_3V3_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = BATS_A_CHARGE_STAT_PIN; GPIO_Init(BATS_A_CHARGE_STAT_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = BATS_A_CHARGE_CTL_PIN; GPIO_Init(BATS_A_CHARGE_CTL_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = BATS_B_CHARGE_STAT_PIN; GPIO_Init(BATS_B_CHARGE_STAT_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = BATS_B_CHARGE_CTL_PIN; GPIO_Init(BATS_B_CHARGE_CTL_PORT, &GPIO_InitStructure); //GPIO_InitStructure.GPIO_Pin = BATS_C_CHARGE_STAT_PIN; //GPIO_Init(BATS_C_CHARGE_STAT_PORT, &GPIO_InitStructure); //GPIO_InitStructure.GPIO_Pin = BATS_SEL_C_PIN; //GPIO_Init(BATS_SEL_C_PORT, &GPIO_InitStructure); //ADC channel config GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; GPIO_InitStructure.GPIO_Pin = BATS_AB_MON_I_PIN; GPIO_Init(BATS_AB_MON_I_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = V3P3_MON_V_PIN; GPIO_Init(V3P3_MON_V_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = BATS_A_V_MON_PIN; GPIO_Init(BATS_A_V_MON_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = BATS_B_V_MON_PIN; GPIO_Init(BATS_B_V_MON_PORT, &GPIO_InitStructure); //GPIO_InitStructure.GPIO_Pin = BATS_C_V_MON_PIN; //GPIO_Init(BATS_C_V_MON_PORT, &GPIO_InitStructure); //I2C Config I2C_SoftwareResetCmd(I2C2,ENABLE); I2C_SoftwareResetCmd(I2C2,DISABLE); I2C_Cmd(I2C2, ENABLE); I2C_InitStructure.I2C_Mode = I2C_Mode_SMBusHost; I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2; I2C_InitStructure.I2C_OwnAddress1 = 0x79; I2C_InitStructure.I2C_Ack = I2C_Ack_Enable; I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit; I2C_InitStructure.I2C_ClockSpeed = 50000; I2C_Init(I2C2, &I2C_InitStructure); //DEBUG("power pin init 1\r\n"); I2C_CalculatePEC(I2C2, ENABLE); ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; ADC_InitStructure.ADC_ScanConvMode = DISABLE; ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfChannel = 1; ADC_Init(ADC3, &ADC_InitStructure); ADC_Cmd(ADC3, ENABLE); ADC_ResetCalibration(ADC3); while(ADC_GetResetCalibrationStatus(ADC3)); ADC_StartCalibration(ADC3); while(ADC_GetCalibrationStatus(ADC3)); //DEBUG("power pin init over\r\n"); }
void _mathworks_task(void) { ADC_InitTypeDef ADC_InitStructure; ADC_CommonInitTypeDef ADC_CommonInitStructure; int val; GPIO_InitTypeDef GPIO_InitStructure; /* Enable ADC3, GPIO clocks ****************************************/ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC3, ENABLE); /* Configure ADC3 Channel7 pin as analog input ******************************/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOF, &GPIO_InitStructure); /* ADC Common Init **********************************************************/ ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent; ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2; ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles; ADC_CommonInit(&ADC_CommonInitStructure); /* ADC3 Init ****************************************************************/ ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ScanConvMode = DISABLE; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfConversion = 1; ADC_Init(ADC3, &ADC_InitStructure); /* ADC3 regular channel7 configuration *************************************/ ADC_RegularChannelConfig(ADC3, ADC_Channel_5, 1, ADC_SampleTime_3Cycles); /* Enable ADC3 */ ADC_Cmd(ADC3, ENABLE); ShieldedPlug* DataBase = SP_getDatabase(ID); os_itv_set (5); /* Start ADC3 Software Conversion */ ADC_SoftwareStartConv(ADC3); for (;;) { /* Start ADC3 Software Conversion */ /* ADC_SoftwareStartConv(ADC3); while (!ADC_GetFlagStatus(ADC3,ADC_FLAG_EOC) ) { }*/ val= ADC_GetConversionValue(ADC3)*2/10; write_SP(DataBase,COMMANDE,val); //To write in Database /* Start ADC3 Software Conversion */ /* ADC_SoftwareStartConv(ADC3); while (!ADC_GetFlagStatus(ADC3,ADC_FLAG_EOC)) { }*/ int val= ADC_GetConversionValue(ADC3)/10; write_SP(DataBase,MEASURE,val); os_itv_wait (); } }
/******************************************************************************* *Функция инициализации портов кнопок ******************************************************************************/ void keyInit(uint8_t mode) { #ifdef SYSTEM_STM32 GPIO_InitTypeDef GPIO_InitStructure; //Структура настройки GPIO EXTI_InitTypeDef EXTI_InitStructure; ADC_InitTypeDef ADC_InitStructure;//Структура настройки АЦП NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);//Включаем порт А if(mode == MODE_ADC) { //Настроим пин джойстика АЦП GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;//Это свободный вход GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;// Это ADC2 нога PA0 GPIO_Init(GPIOA, &GPIO_InitStructure); RCC_ADCCLKConfig(RCC_PCLK2_Div2);//Частота ADC (max 14MHz --> 72/2=9MHz) RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC2, ENABLE);//Включаем тактирование АЦП //Определяем конфигурацию ADC ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; ADC_InitStructure.ADC_ScanConvMode = DISABLE; ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;//Работаем в режиме одноразового преобразования ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfChannel = 1; ADC_RegularChannelConfig(ADC2, ADC_Channel_0, 1, ADC_SampleTime_1Cycles5);//Время выборки АЦП ADC_Init(ADC2, &ADC_InitStructure);//Применить конфигурацию ADC2 ADC_Cmd(ADC2, ENABLE);//Включаем ADC. Необходимо для калибровки // ADC calibration (optional, but recommended at power on) ADC_ResetCalibration(ADC2);// Reset previous calibration while (ADC_GetResetCalibrationStatus(ADC2)); ADC_StartCalibration(ADC2);// Start new calibration (ADC must be off at that time) while (ADC_GetCalibrationStatus(ADC2)); // start conversion ADC_Cmd(ADC2, ENABLE);//enable ADC2 ADC_SoftwareStartConvCmd(ADC2, ENABLE);// start conversion (will be endless as we are in continuous mode) SysTick_task_add(&readKey, 10); //Заряжаем таймер на чтение кнопок через каждые 10 миллисекунд } else if(mode == MODE_INT) { SysTick_task_del(&readKey); //Удаляем задачу чтения нажатий RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE);//Включаем тактирование порта A и альтернативной функции GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; //Это свободный вход GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;// GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;// Это PA0 GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0);// выбор порта на котором хотим получить внешнее прерывание EXTI_InitStructure.EXTI_Line = EXTI_Line0;// выбираем линию порта EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;// настраиваем на внешнее прерывание EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); //далее идут настройки приоритета прерываний. NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 13; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 15; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); NVIC_EnableIRQ(EXTI0_IRQn);//разрешаем прерывание } state.button = BUTTON_LOCK; //Блокируем первое нажатие клавиши #endif }
void adc_init(void) { ADC_InitTypeDef ADC_InitStructure; { GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = BATTERYPIN ; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(BATTERYPORT, &GPIO_InitStructure); } RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1 , ENABLE); { DMA_InitTypeDef DMA_InitStructure; DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)0x40012440; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)adcarray; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = 2; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA1_Channel1, &DMA_InitStructure); } ADC_DMARequestModeConfig(ADC1, ADC_DMAMode_Circular); ADC_DMACmd(ADC1, ENABLE); ADC_StructInit(&ADC_InitStructure); ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Backward; ADC_Init(ADC1, &ADC_InitStructure); ADC_ChannelConfig(ADC1, ADC_Channel_Vrefint , ADC_SampleTime_239_5Cycles); ADC_ChannelConfig(ADC1, BATTERY_ADC_CHANNEL , ADC_SampleTime_239_5Cycles); ADC_VrefintCmd(ENABLE); ADC_GetCalibrationFactor(ADC1); ADC_Cmd(ADC1, ENABLE); ADC_StartOfConversion(ADC1); DMA_Cmd(DMA1_Channel1, ENABLE); // reference is measured a 3.3v, we are powered by 2.8, so a 1.17 multiplier // different vccs will translate to a different adc scale factor, // so actual vcc is not important as long as the voltage is correct in the end vref_cal = 1.17857f * (float) ( adcref_read ((adcrefcal *) 0x1FFFF7BA) ); }
/** * @brief ADC1 channel with DMA configuration * @param None * @retval None */ void ADC1_CH_DMA_Config(void) { ADC_InitTypeDef ADC_InitStructure; DMA_InitTypeDef DMA_InitStructure; /* ADC1 DeInit */ ADC_DeInit(ADC1); /* ADC1 Periph clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); /* DMA1 clock enable */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1 , ENABLE); /* DMA1 Channel1 Config */ DMA_DeInit(DMA1_Channel1); DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC1_DR_Address; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)RegularConvData_Tab; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = 2; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA1_Channel1, &DMA_InitStructure); /* DMA1 Channel1 enable */ DMA_Cmd(DMA1_Channel1, ENABLE); /* ADC DMA request in circular mode */ ADC_DMARequestModeConfig(ADC1, ADC_DMAMode_Circular); /* Enable ADC_DMA */ ADC_DMACmd(ADC1, ENABLE); /* Initialize ADC structure */ ADC_StructInit(&ADC_InitStructure); /* Configure the ADC1 in continous mode withe a resolutuion equal to 12 bits */ ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_ScanDirection = ADC_ScanDirection_Backward; ADC_Init(ADC1, &ADC_InitStructure); /* Convert the ADC1 temperature sensor with 55.5 Cycles as sampling time */ ADC_ChannelConfig(ADC1, ADC_Channel_TempSensor , ADC_SampleTime_55_5Cycles); ADC_TempSensorCmd(ENABLE); /* Convert the ADC1 Vref with 55.5 Cycles as sampling time */ ADC_ChannelConfig(ADC1, ADC_Channel_Vrefint , ADC_SampleTime_55_5Cycles); ADC_VrefintCmd(ENABLE); /* ADC Calibration */ ADC_GetCalibrationFactor(ADC1); /* Enable ADC1 */ ADC_Cmd(ADC1, ENABLE); /* Wait the ADCEN falg */ while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_ADEN)); /* ADC1 regular Software Start Conv */ ADC_StartOfConversion(ADC1); }
void adcInit(const adcConfig_t *config) { ADC_InitTypeDef ADC_InitStructure; DMA_InitTypeDef DMA_InitStructure; uint8_t adcChannelCount = 0; memset(&adcOperatingConfig, 0, sizeof(adcOperatingConfig)); if (config->vbat.enabled) { adcOperatingConfig[ADC_BATTERY].tag = config->vbat.ioTag; } if (config->rssi.enabled) { adcOperatingConfig[ADC_RSSI].tag = config->rssi.ioTag; //RSSI_ADC_CHANNEL; } if (config->external1.enabled) { adcOperatingConfig[ADC_EXTERNAL1].tag = config->external1.ioTag; //EXTERNAL1_ADC_CHANNEL; } if (config->current.enabled) { adcOperatingConfig[ADC_CURRENT].tag = config->current.ioTag; //CURRENT_METER_ADC_CHANNEL; } ADCDevice device = adcDeviceByInstance(ADC_INSTANCE); if (device == ADCINVALID) return; #ifdef ADC24_DMA_REMAP SYSCFG_DMAChannelRemapConfig(SYSCFG_DMARemap_ADC2ADC4, ENABLE); #endif adcDevice_t adc = adcHardware[device]; bool adcActive = false; for (int i = 0; i < ADC_CHANNEL_COUNT; i++) { if (!adcVerifyPin(adcOperatingConfig[i].tag, device)) { continue; } adcActive = true; IOInit(IOGetByTag(adcOperatingConfig[i].tag), OWNER_ADC_BATT + i, 0); IOConfigGPIO(IOGetByTag(adcOperatingConfig[i].tag), IO_CONFIG(GPIO_Mode_AN, 0, GPIO_OType_OD, GPIO_PuPd_NOPULL)); adcOperatingConfig[i].adcChannel = adcChannelByTag(adcOperatingConfig[i].tag); adcOperatingConfig[i].dmaIndex = adcChannelCount++; adcOperatingConfig[i].sampleTime = ADC_SampleTime_601Cycles5; adcOperatingConfig[i].enabled = true; } if (!adcActive) { return; } if ((device == ADCDEV_1) || (device == ADCDEV_2)) { // enable clock for ADC1+2 RCC_ADCCLKConfig(RCC_ADC12PLLCLK_Div256); // 72 MHz divided by 256 = 281.25 kHz } else { // enable clock for ADC3+4 RCC_ADCCLKConfig(RCC_ADC34PLLCLK_Div256); // 72 MHz divided by 256 = 281.25 kHz } RCC_ClockCmd(adc.rccADC, ENABLE); dmaInit(dmaGetIdentifier(adc.DMAy_Channelx), OWNER_ADC, 0); DMA_DeInit(adc.DMAy_Channelx); DMA_StructInit(&DMA_InitStructure); DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&adc.ADCx->DR; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)adcValues; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = adcChannelCount; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = adcChannelCount > 1 ? DMA_MemoryInc_Enable : DMA_MemoryInc_Disable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(adc.DMAy_Channelx, &DMA_InitStructure); DMA_Cmd(adc.DMAy_Channelx, ENABLE); // calibrate ADC_VoltageRegulatorCmd(adc.ADCx, ENABLE); delay(10); ADC_SelectCalibrationMode(adc.ADCx, ADC_CalibrationMode_Single); ADC_StartCalibration(adc.ADCx); while (ADC_GetCalibrationStatus(adc.ADCx) != RESET); ADC_VoltageRegulatorCmd(adc.ADCx, DISABLE); ADC_CommonInitTypeDef ADC_CommonInitStructure; ADC_CommonStructInit(&ADC_CommonInitStructure); ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent; ADC_CommonInitStructure.ADC_Clock = ADC_Clock_SynClkModeDiv4; ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_1; ADC_CommonInitStructure.ADC_DMAMode = ADC_DMAMode_Circular; ADC_CommonInitStructure.ADC_TwoSamplingDelay = 0; ADC_CommonInit(adc.ADCx, &ADC_CommonInitStructure); ADC_StructInit(&ADC_InitStructure); ADC_InitStructure.ADC_ContinuousConvMode = ADC_ContinuousConvMode_Enable; ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ExternalTrigConvEvent = ADC_ExternalTrigConvEvent_0; ADC_InitStructure.ADC_ExternalTrigEventEdge = ADC_ExternalTrigEventEdge_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_OverrunMode = ADC_OverrunMode_Disable; ADC_InitStructure.ADC_AutoInjMode = ADC_AutoInjec_Disable; ADC_InitStructure.ADC_NbrOfRegChannel = adcChannelCount; ADC_Init(adc.ADCx, &ADC_InitStructure); uint8_t rank = 1; for (int i = 0; i < ADC_CHANNEL_COUNT; i++) { if (!adcOperatingConfig[i].enabled) { continue; } ADC_RegularChannelConfig(adc.ADCx, adcOperatingConfig[i].adcChannel, rank++, adcOperatingConfig[i].sampleTime); } ADC_Cmd(adc.ADCx, ENABLE); while (!ADC_GetFlagStatus(adc.ADCx, ADC_FLAG_RDY)); ADC_DMAConfig(adc.ADCx, ADC_DMAMode_Circular); ADC_DMACmd(adc.ADCx, ENABLE); ADC_StartConversion(adc.ADCx); }
int ybs_drv_init(void) { GPIO_InitTypeDef GPIO_InitStructure; ADC_InitTypeDef ADC_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; NVIC_InitTypeDef NVIC_InitStructure; //mos ctrl, PB0, high off, low on RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz; GPIO_Init(GPIOB, &GPIO_InitStructure); //adc, PA1, ADC12_IN1 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE); RCC_ADCCLKConfig(RCC_PCLK2_Div8); /*72Mhz/8 = 9Mhz*/ RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; GPIO_Init(GPIOA, &GPIO_InitStructure); ADC_DeInit(ADC1); ADC_StructInit(&ADC_InitStructure); ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; ADC_InitStructure.ADC_ScanConvMode = DISABLE; ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_Init(ADC1, &ADC_InitStructure); ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 1, ADC_SampleTime_71Cycles5); //9Mhz/(71.5 + 12.5) = 107.1Khz ADC_Cmd(ADC1, ENABLE); /* Enable ADC1 reset calibaration register */ ADC_ResetCalibration(ADC1); while(ADC_GetResetCalibrationStatus(ADC1)); ADC_StartCalibration(ADC1); while (ADC_GetCalibrationStatus(ADC1)); ADC_Cmd(ADC1, ENABLE); ADC_SoftwareStartConvCmd(ADC1, ENABLE); //TIM4 TIMEBASE RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); TIM_TimeBaseStructure.TIM_Period = YBS_US - 1; TIM_TimeBaseStructure.TIM_Prescaler = 72 - 1; //Fapb1 = TIM_clk = 72Mhz, Tick = 1us TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseStructure.TIM_RepetitionCounter = 0; TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //TIM_ARRPreloadConfig(TIM4, ENABLE); TIM_Cmd(TIM4, DISABLE); TIM_ClearFlag(TIM4, TIM_FLAG_Update); TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE); //IRQ OF TIM4 NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); //DAC static const struct ad5663_cfg_s ad5663_cfg = { .spi = &spi1, .gpio_cs = SPI_1_NSS, .gpio_ldac = SPI_CS_PA2, .gpio_clr = SPI_CS_PA3, }; dev_register("ad5663", &ad5663_cfg); ybs_fd_dac = dev_open("dac0", 0); dev_ioctl(ybs_fd_dac, DAC_SET_CH, 1); //for fast ch1 write purpose return 0; }
/* ********************************************************************************************************* * 函 数 名: bsp_InitADC * 功能说明: ADC初始化 * 形 参: 无 * 返 回 值: 无 ********************************************************************************************************* */ void bsp_InitADC(void) { ADC_InitTypeDef ADC_InitStructure; ADC_CommonInitTypeDef ADC_CommonInitStructure; DMA_InitTypeDef DMA_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; /* 使能外设时钟 */ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2 | RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB| RCC_AHB1Periph_GPIOC, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOB, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOC, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOC, &GPIO_InitStructure); /* DMA2 Stream0 channel0 配置-------------------------------------------------- */ DMA_InitStructure.DMA_Channel = DMA_Channel_0; DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC1_DR_ADDRESS; DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&uhADCConvertedValue;; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; DMA_InitStructure.DMA_BufferSize = 4; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_Init(DMA2_Stream0, &DMA_InitStructure); /* DMA2_Stream0 enable */ DMA_Cmd(DMA2_Stream0, ENABLE); /**************************************************************************** PCLK2 = HCLK / 2 下面选择的是2分频 ADCCLK = PCLK2 /8 = HCLK / 8 = 168 / 8 = 21M ADC采样频率: Sampling Time + Conversion Time = 480 + 12 cycles = 492cyc Conversion Time = 21MHz / 492cyc = 42.6ksps. *****************************************************************************/ /* ADC Common 配置 ----------------------------------------------------------*/ ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent; ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2; ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles; ADC_CommonInit(&ADC_CommonInitStructure); /* ADC1 regular channel 12 configuration ************************************/ ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ScanConvMode = ENABLE; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfConversion = 4; ADC_Init(ADC1, &ADC_InitStructure); /* Enable ADC1 DMA */ ADC_DMACmd(ADC1, ENABLE); ADC_RegularChannelConfig(ADC1, ADC_Channel_1, 5, ADC_SampleTime_15Cycles); /* ADC1 regular channel18 (VBAT) configuration ******************************/ ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 1, ADC_SampleTime_15Cycles); /* ADC1 regular channel18 (VBAT) configuration *****************************/ ADC_RegularChannelConfig(ADC1, ADC_Channel_9, 2, ADC_SampleTime_15Cycles); /* ADC1 regular channels 10, 11 configuration */ ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 3, ADC_SampleTime_15Cycles); ADC_RegularChannelConfig(ADC1, ADC_Channel_15, 4, ADC_SampleTime_15Cycles); // /* Enable VBAT channel */ // ADC_VBATCmd(ENABLE); // // ADC_TempSensorVrefintCmd(ENABLE); /* Enable DMA request after last transfer (Single-ADC mode) */ ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE); /* Enable ADC1 **************************************************************/ ADC_Cmd(ADC1, ENABLE); /* Start ADC1 Software Conversion */ ADC_SoftwareStartConv(ADC1); }
/** * @brief Main program * @param None * @retval None */ int main(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f10x_xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f10x.c file */ /* System clocks configuration ---------------------------------------------*/ RCC_Configuration(); /* GPIO configuration ------------------------------------------------------*/ GPIO_Configuration(); /* DMA1 channel1 configuration ----------------------------------------------*/ DMA_DeInit(DMA1_Channel1); DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC1_DR_Address; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)ADC_DualConvertedValueTab; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = 16; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_Word; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_Word; DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA1_Channel1, &DMA_InitStructure); /* Enable DMA1 Channel1 */ DMA_Cmd(DMA1_Channel1, ENABLE); /* ADC1 configuration ------------------------------------------------------*/ ADC_InitStructure.ADC_Mode = ADC_Mode_RegSimult; ADC_InitStructure.ADC_ScanConvMode = ENABLE; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfChannel = 2; ADC_Init(ADC1, &ADC_InitStructure); /* ADC1 regular channels configuration */ ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 1, ADC_SampleTime_239Cycles5); ADC_RegularChannelConfig(ADC1, ADC_Channel_17, 2, ADC_SampleTime_239Cycles5); /* Enable ADC1 DMA */ ADC_DMACmd(ADC1, ENABLE); /* ADC2 configuration ------------------------------------------------------*/ ADC_InitStructure.ADC_Mode = ADC_Mode_RegSimult; ADC_InitStructure.ADC_ScanConvMode = ENABLE; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfChannel = 2; ADC_Init(ADC2, &ADC_InitStructure); /* ADC2 regular channels configuration */ ADC_RegularChannelConfig(ADC2, ADC_Channel_11, 1, ADC_SampleTime_239Cycles5); ADC_RegularChannelConfig(ADC2, ADC_Channel_12, 2, ADC_SampleTime_239Cycles5); /* Enable ADC2 external trigger conversion */ ADC_ExternalTrigConvCmd(ADC2, ENABLE); /* Enable ADC1 */ ADC_Cmd(ADC1, ENABLE); /* Enable Vrefint channel17 */ ADC_TempSensorVrefintCmd(ENABLE); /* Enable ADC1 reset calibaration register */ ADC_ResetCalibration(ADC1); /* Check the end of ADC1 reset calibration register */ while(ADC_GetResetCalibrationStatus(ADC1)); /* Start ADC1 calibaration */ ADC_StartCalibration(ADC1); /* Check the end of ADC1 calibration */ while(ADC_GetCalibrationStatus(ADC1)); /* Enable ADC2 */ ADC_Cmd(ADC2, ENABLE); /* Enable ADC2 reset calibaration register */ ADC_ResetCalibration(ADC2); /* Check the end of ADC2 reset calibration register */ while(ADC_GetResetCalibrationStatus(ADC2)); /* Start ADC2 calibaration */ ADC_StartCalibration(ADC2); /* Check the end of ADC2 calibration */ while(ADC_GetCalibrationStatus(ADC2)); /* Start ADC1 Software Conversion */ ADC_SoftwareStartConvCmd(ADC1, ENABLE); /* Test on DMA1 channel1 transfer complete flag */ while(!DMA_GetFlagStatus(DMA1_FLAG_TC1)); /* Clear DMA1 channel1 transfer complete flag */ DMA_ClearFlag(DMA1_FLAG_TC1); while (1) { } }
/** * @brief Main program * @param None * @retval None */ int main(void) { /*!< At this stage the microcontroller clock setting is already configured, this is done through SystemInit() function which is called from startup file (startup_stm32f10x_xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f10x.c file */ /* System clocks configuration ---------------------------------------------*/ RCC_Configuration(); /* NVIC configuration ------------------------------------------------------*/ NVIC_Configuration(); /* GPIO configuration ------------------------------------------------------*/ GPIO_Configuration(); /* TIM1 configuration ------------------------------------------------------*/ /* Time Base configuration */ TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Period = 0xFF; TIM_TimeBaseStructure.TIM_Prescaler = 0x4; TIM_TimeBaseStructure.TIM_ClockDivision = 0x0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); /* TIM1 channel1 configuration in PWM mode */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = 0x7F; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low; TIM_OC1Init(TIM1, &TIM_OCInitStructure); /* DMA1 Channel1 Configuration ----------------------------------------------*/ DMA_DeInit(DMA1_Channel1); DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)ADC_RegularConvertedValueTab; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = 32; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; DMA_InitStructure.DMA_Mode = DMA_Mode_Normal; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA1_Channel1, &DMA_InitStructure); /* Enable DMA1 channel1 */ DMA_Cmd(DMA1_Channel1, ENABLE); /* ADC1 configuration ------------------------------------------------------*/ ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; ADC_InitStructure.ADC_ScanConvMode = DISABLE; ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfChannel = 1; ADC_Init(ADC1, &ADC_InitStructure); /* ADC1 regular channel14 configuration */ ADC_RegularChannelConfig(ADC1, ADC_Channel_14, 1, ADC_SampleTime_13Cycles5); /* Set injected sequencer length */ ADC_InjectedSequencerLengthConfig(ADC1, 1); /* ADC1 injected channel Configuration */ ADC_InjectedChannelConfig(ADC1, ADC_Channel_11, 1, ADC_SampleTime_71Cycles5); /* ADC1 injected external trigger configuration */ ADC_ExternalTrigInjectedConvConfig(ADC1, ADC_ExternalTrigInjecConv_None); /* Enable automatic injected conversion start after regular one */ ADC_AutoInjectedConvCmd(ADC1, ENABLE); /* Enable ADC1 DMA */ ADC_DMACmd(ADC1, ENABLE); /* Enable ADC1 external trigger */ ADC_ExternalTrigConvCmd(ADC1, ENABLE); /* Enable JEOC interrupt */ ADC_ITConfig(ADC1, ADC_IT_JEOC, ENABLE); /* Enable ADC1 */ ADC_Cmd(ADC1, ENABLE); /* Enable ADC1 reset calibration register */ ADC_ResetCalibration(ADC1); /* Check the end of ADC1 reset calibration register */ while(ADC_GetResetCalibrationStatus(ADC1)); /* Start ADC1 calibration */ ADC_StartCalibration(ADC1); /* Check the end of ADC1 calibration */ while(ADC_GetCalibrationStatus(ADC1)); /* TIM1 counter enable */ TIM_Cmd(TIM1, ENABLE); /* TIM1 main Output Enable */ TIM_CtrlPWMOutputs(TIM1, ENABLE); /* Test on channel1 transfer complete flag */ while(!DMA_GetFlagStatus(DMA1_FLAG_TC1)); /* Clear channel1 transfer complete flag */ DMA_ClearFlag(DMA1_FLAG_TC1); /* TIM1 counter disable */ TIM_Cmd(TIM1, DISABLE); while (1) { } }
void ADCInit() { ADC_Init(LPC_ADC0, 200000, 10); ADC_IntConfig(LPC_ADC0,ADC_ADINTEN1,DISABLE); }
Status qAnalog_Init(){ ADC_Init(LPC_ADC, 200000); return SUCCESS; }
/*********************************************************************//** * @brief c_entry: Main ADC program body * @param[in] None * @return int **********************************************************************/ int c_entry(void) { PINSEL_CFG_Type PinCfg; uint32_t adc_value, tmp; /* Initialize debug via UART0 * – 115200bps * – 8 data bit * – No parity * – 1 stop bit * – No flow control */ debug_frmwrk_init(); // print welcome screen print_menu(); /* Initialize ADC ----------------------------------------------------*/ /* Because the potentiometer on different boards (MCB & IAR) connect * with different ADC channel, so we have to configure correct ADC channel * on each board respectively. * If you want to check other ADC channels, you have to wire this ADC pin directly * to potentiometer pin (please see schematic doc for more reference) */ #ifdef MCB_LPC_1768 /* * Init ADC pin connect * AD0.2 on P0.25 */ PinCfg.Funcnum = 1; PinCfg.OpenDrain = 0; PinCfg.Pinmode = 0; PinCfg.Pinnum = 25; PinCfg.Portnum = 0; PINSEL_ConfigPin(&PinCfg); #elif defined (IAR_LPC_1768) /* * Init ADC pin connect * AD0.5 on P1.31 */ PinCfg.Funcnum = 3; PinCfg.OpenDrain = 0; PinCfg.Pinmode = 0; PinCfg.Pinnum = 31; PinCfg.Portnum = 1; PINSEL_ConfigPin(&PinCfg); #endif /* Configuration for ADC : * Select: ADC channel 2 (if using MCB1700 board) * ADC channel 5 (if using IAR-LPC1768 board) * ADC conversion rate = 200Khz */ ADC_Init(LPC_ADC, 200000); ADC_IntConfig(LPC_ADC,_ADC_INT,DISABLE); ADC_ChannelCmd(LPC_ADC,_ADC_CHANNEL,ENABLE); while(1) { // Start conversion ADC_StartCmd(LPC_ADC,ADC_START_NOW); //Wait conversion complete while (!(ADC_ChannelGetStatus(LPC_ADC,_ADC_CHANNEL,ADC_DATA_DONE))); adc_value = ADC_ChannelGetData(LPC_ADC,_ADC_CHANNEL); //Display the result of conversion on the UART0 #ifdef MCB_LPC_1768 _DBG("ADC value on channel 2: "); #elif defined (IAR_LPC_1768) _DBG("ADC value on channel 5: "); #endif _DBD32(adc_value); _DBG_(""); //delay for(tmp = 0; tmp < 1000000; tmp++); } ADC_DeInit(LPC_ADC); return 1; }
/** * @brief ADC1 Channel Vbat configuration * @note This function Configure the ADC peripheral 1) Enable peripheral clocks 2) DMA2_Stream0 channel 0 configuration 3) Configure ADC1 Channel18 (VBAT) * @param None * @retval None */ static void ADC_Config(void) { ADC_InitTypeDef ADC_InitStructure; ADC_CommonInitTypeDef ADC_CommonInitStructure; DMA_InitTypeDef DMA_InitStructure; /* Enable peripheral clocks *************************************************/ RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2, ENABLE); RCC_APB2PeriphClockCmd(ADCx_CLK, ENABLE); /* DMA2_Stream0 channel0 configuration **************************************/ DMA_DeInit(DMA2_Stream0); DMA_InitStructure.DMA_Channel = DMA_CHANNELx; DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADCx_DR_ADDRESS; DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&uhADCConvertedValue; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory; DMA_InitStructure.DMA_BufferSize = 1; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable; DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull; DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single; DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_Init(DMA_STREAMx, &DMA_InitStructure); /* DMA2_Stream0 enable */ DMA_Cmd(DMA_STREAMx, ENABLE); /* ADC Common Init **********************************************************/ ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent; ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2; ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled; ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles; ADC_CommonInit(&ADC_CommonInitStructure); /* ADC1 Init ****************************************************************/ ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ScanConvMode = DISABLE; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfConversion = 1; ADC_Init(ADCx, &ADC_InitStructure); /* Enable ADC1 DMA */ ADC_DMACmd(ADCx, ENABLE); /* ADC1 regular channel18 (VBAT) configuration ******************************/ ADC_RegularChannelConfig(ADCx, ADC_Channel_Vbat, 1, ADC_SampleTime_15Cycles); /* Enable VBAT channel */ ADC_VBATCmd(ENABLE); /* Enable DMA request after last transfer (Single-ADC mode) */ ADC_DMARequestAfterLastTransferCmd(ADCx, ENABLE); /* Enable ADC1 **************************************************************/ ADC_Cmd(ADCx, ENABLE); }
void adcInit(drv_adc_config_t *init) { ADC_InitTypeDef ADC_InitStructure; DMA_InitTypeDef DMA_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; uint8_t i; uint8_t adcChannelCount = 0; memset(&adcConfig, 0, sizeof(adcConfig)); GPIO_StructInit(&GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; adcConfig[ADC_BATTERY].adcChannel = ADC_Channel_6; adcConfig[ADC_BATTERY].dmaIndex = adcChannelCount; adcConfig[ADC_BATTERY].sampleTime = ADC_SampleTime_601Cycles5; adcConfig[ADC_BATTERY].enabled = true; adcChannelCount++; if (init->enableCurrentMeter) { GPIO_InitStructure.GPIO_Pin |= GPIO_Pin_1; adcConfig[ADC_CURRENT].adcChannel = ADC_Channel_7; adcConfig[ADC_CURRENT].dmaIndex = adcChannelCount; adcConfig[ADC_CURRENT].sampleTime = ADC_SampleTime_601Cycles5; adcConfig[ADC_CURRENT].enabled = true; adcChannelCount++; } if (init->enableRSSI) { GPIO_InitStructure.GPIO_Pin |= GPIO_Pin_2; adcConfig[ADC_RSSI].adcChannel = ADC_Channel_8; adcConfig[ADC_RSSI].dmaIndex = adcChannelCount; adcConfig[ADC_RSSI].sampleTime = ADC_SampleTime_601Cycles5; adcConfig[ADC_RSSI].enabled = true; adcChannelCount++; } adcConfig[ADC_EXTERNAL1].adcChannel = ADC_Channel_9; adcConfig[ADC_EXTERNAL1].dmaIndex = adcChannelCount; adcConfig[ADC_EXTERNAL1].sampleTime = ADC_SampleTime_601Cycles5; adcConfig[ADC_EXTERNAL1].enabled = true; adcChannelCount++; RCC_ADCCLKConfig(RCC_ADC12PLLCLK_Div256); // 72 MHz divided by 256 = 281.25 kHz RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1 | RCC_AHBPeriph_ADC12, ENABLE); DMA_DeInit(DMA1_Channel1); DMA_StructInit(&DMA_InitStructure); DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)&ADC1->DR; DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)adcValues; DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC; DMA_InitStructure.DMA_BufferSize = adcChannelCount; DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable; DMA_InitStructure.DMA_MemoryInc = adcChannelCount > 1 ? DMA_MemoryInc_Enable : DMA_MemoryInc_Disable; DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord; DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord; DMA_InitStructure.DMA_Mode = DMA_Mode_Circular; DMA_InitStructure.DMA_Priority = DMA_Priority_High; DMA_InitStructure.DMA_M2M = DMA_M2M_Disable; DMA_Init(DMA1_Channel1, &DMA_InitStructure); DMA_Cmd(DMA1_Channel1, ENABLE); GPIO_Init(GPIOC, &GPIO_InitStructure); // calibrate ADC_VoltageRegulatorCmd(ADC1, ENABLE); delay(10); ADC_SelectCalibrationMode(ADC1, ADC_CalibrationMode_Single); ADC_StartCalibration(ADC1); while(ADC_GetCalibrationStatus(ADC1) != RESET); ADC_VoltageRegulatorCmd(ADC1, DISABLE); ADC_CommonInitTypeDef ADC_CommonInitStructure; ADC_CommonStructInit(&ADC_CommonInitStructure); ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent; ADC_CommonInitStructure.ADC_Clock = ADC_Clock_SynClkModeDiv4; ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_1; ADC_CommonInitStructure.ADC_DMAMode = ADC_DMAMode_Circular; ADC_CommonInitStructure.ADC_TwoSamplingDelay = 0; ADC_CommonInit(ADC1, &ADC_CommonInitStructure); ADC_StructInit(&ADC_InitStructure); ADC_InitStructure.ADC_ContinuousConvMode = ADC_ContinuousConvMode_Enable; ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; ADC_InitStructure.ADC_ExternalTrigConvEvent = ADC_ExternalTrigConvEvent_0; ADC_InitStructure.ADC_ExternalTrigEventEdge = ADC_ExternalTrigEventEdge_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_OverrunMode = ADC_OverrunMode_Disable; ADC_InitStructure.ADC_AutoInjMode = ADC_AutoInjec_Disable; ADC_InitStructure.ADC_NbrOfRegChannel = adcChannelCount; ADC_Init(ADC1, &ADC_InitStructure); uint8_t rank = 1; for (i = 0; i < ADC_CHANNEL_COUNT; i++) { if (!adcConfig[i].enabled) { continue; } ADC_RegularChannelConfig(ADC1, adcConfig[i].adcChannel, rank++, adcConfig[i].sampleTime); } ADC_Cmd(ADC1, ENABLE); while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_RDY)); ADC_DMAConfig(ADC1, ADC_DMAMode_Circular); ADC_DMACmd(ADC1, ENABLE); ADC_StartConversion(ADC1); }