/*------------------------------------------------------------------------------------------------------------*/ void cgi_aio( void * pStruct ) { struct HTTP_REQUEST * http_request; http_request = (struct HTTP_REQUEST *) pStruct; char i; char gpio[10]; int temp; /* printf_P( PSTR( "<HTML>" "<HEAD>" "<TITLE>ADC</TITLE>" "</HEAD>" "<BODY>"*/ cgi_PrintHttpheaderStart(); printf_P( PSTR( "<form action=\"dio.cgi\">" "<table border=\"0\" cellpadding=\"5\" cellspacing=\"0\">" ) ); for( i = 0 ; i < MAX_ADC ; i++ ) { temp = ADC_mVolt( ADC_GetValue( i ) + 1 , 5000 ); printf_P( PSTR( "<tr>" "<td align=\"right\">ADC %d =</td>" "<td align=\"right\"> %d.%03dV</td>" "</tr>\r"), i, temp / 1000, temp % 1000 ); } printf_P( PSTR( "</table>" "</form>")); cgi_PrintHttpheaderEnd(); /* "</BODY>" "</HTML>\r\n" "\r\n")); */ }
void Key_Process(void) { Key = 0; ADC_Channel = KEY_ADC_CH; ADC_Result = 0; ADC_GetValue(); Key_Noise_Filter(); if(ADC_Result >0 ) { if((ADC_Result > KEY1_ACTIVE_ADC_VAL_LOW) && (ADC_Result<KEY1_ACTIVE_ADC_VAL_HIGH)) { Key = 0x01; } else if((ADC_Result > KEY2_ACTIVE_ADC_VAL_LOW) && (ADC_Result<KEY2_ACTIVE_ADC_VAL_HIGH)) { Key = 0x02; } else if((ADC_Result > KEY3_ACTIVE_ADC_VAL_LOW) && (ADC_Result<KEY3_ACTIVE_ADC_VAL_HIGH)) { Key = 0x03; } else if((ADC_Result > KEY4_ACTIVE_ADC_VAL_LOW) && (ADC_Result<KEY4_ACTIVE_ADC_VAL_HIGH)) { Key = 0x04; } // else if((ADC_Result > KEY5_ACTIVE_ADC_VAL_LOW) && (ADC_Result<KEY5_ACTIVE_ADC_VAL_HIGH)) { // Key = 0x10; } // else if((ADC_Result > KEY6_ACTIVE_ADC_VAL_LOW) && (ADC_Result<KEY6_ACTIVE_ADC_VAL_HIGH)) { // Key = 0x20; } } if(Comm_CurrentStatus == STATUS_CONNECTED) { if(Key || Throttle) { Comm_Data[0] = Throttle; Comm_Data[1] = Yaw; Comm_Data[2] = Roll; Comm_Data[3] = Pitch; Comm_Data[4] = Key; Comm_SendData(); } } }
static void battery_task(void) { uint16_t batRaw; portTickType lastWakeTime = os_getTime(); for (;;) { os_frequency(&lastWakeTime, 50); batRaw = ADC_GetValue(10); batRaw *= 39; batRaw /= 4; batRaw += 550; measures[nextPos] = batRaw; nextPos = (nextPos + 1) % AVERAGING_WINDOW_SIZE; } }
int cmd_adc( int argc, char ** argv ) { int channel; if ( !strcmp_P( argv[ 1 ], PSTR("get") ) ) { channel = atoi( argv[ 2 ] ); if ( channel < 8 && channel >= 0 ) printf_P( PSTR("ADC (%d) = %d\r\n"), channel, ADC_GetValue( channel ) ); else printf_P( PSTR("Error, %d ist kein gülter ADC-Eingang\r\n"), channel ); } else printf_P( PSTR("adc <get> <0..7>\r\n")); }
int is_bat_low(void) { int adc_value = 0; unsigned int comp_vbat = 0; int i; if(charger_connected()){ comp_vbat = get_bat_low_level_chg(); }else{ comp_vbat = get_bat_low_level(); } ADC_Init(); for(i=0;i<_BUF_SIZE;i++){ retry_adc: adc_value = ADC_GetValue(ADC_CHANNEL_VBAT, false); if(adc_value < 0){ printf("ADC read error\n"); udelay(10); goto retry_adc; }else{ put_vbat_value(adc_value); } } adc_value = get_vbat_value(); printf("is_bat_low adc_value:%d,comp_vbat:%d\n",adc_value,comp_vbat); if(CHGMNG_AdcvalueToVoltage (adc_value) < comp_vbat) return 1; else return 0; }
static uint32 ap_adc_calibration(uint32 channel, MSG_AP_ADC_CNF *pMsgADC) { volatile uint32 adc_channel = 0, adc_result = 0; int i = 0; adc_channel = channel; if (adc_channel <= 8) { if (adc_channel == 0) { adc_channel = 1; } adc_result = 0; for(; i < 16; i++) { adc_result += ADC_GetValue(adc_channel-1, 0); } adc_result >>= 4; pMsgADC->diag_ap_cnf.status = 0; pMsgADC->ap_adc_req. parameters[0]= (uint16)(adc_result&0xFFFF); } else {
void main() { u16 tmp; u8 tmp8, tmp8_A; static tCANMsg RxMsgBuff; CLK_Init(CLK_HSE); disableInterrupts(); GPIO_Init(); TIM4_Init(); // TIM2_Init(); TIM1_Init(); ADC_Init(); /* Configure CAN - Interface */ CAN_Init(); // init CAN - interface enableInterrupts(); CAN_Start(); CAN_TxMsg1.Length = 4; CAN_TxMsg1.Xtd = false; CAN_TxMsg1.rtr = false; CAN_TxMsg1.ID = 0x280; CAN_TxMsg1.Data[0] =0 ; CAN_TxMsg1.Data[1] =0; //PWM_SetFrequency(1); do { if ( IsCAN_MSG1_Send()) { tmp = ADC_GetValue(ADC_REV_CHANEL); tmp = tmp *24; if ( tmp < 100 ) { OIL_PRESS = false; } else { OIL_PRESS = true; } CAN_TxMsg1.Data[3] =(u8)(tmp >> 8) ; CAN_TxMsg1.Data[2] =(u8)(tmp & 0x00FF); CAN_Write(&CAN_TxMsg1); } if ( IsSpeedAdjustTime()) { tmp = ADC_GetValue(ADC_SPEED_CHANEL); tmp = tmp * 10; tmp = tmp/ 34; PWM_SetFrequency(tmp); // 1023 = 300 Hz } /* if( CAN_GetMsg(&RxMsgBuff)== RET_OK){ if (RxMsgBuff.Xtd ){ // EID tmp8 = (u8)(RxMsgBuff.timeStamp >>3) & 0xE0U; if (RxMsgBuff.rtr) tmp8 |= 0x10; tmp8 |= (RxMsgBuff.Length & 0x0F); tmp8_A = (RxMsgBuff.ID>>24) & 0x001f; tmp8_A |= (RxMsgBuff.FilterID << 5); USART_SendBytesMessage (CAN_MSG_EXT_1, tmp8,(u8)((RxMsgBuff.timeStamp) & 0x00FF), tmp8_A ,(RxMsgBuff.ID>>16) & 0x00ffU); USART_SendBytesMessage (CAN_MSG_EXT_2, (RxMsgBuff.ID>>8) & 0x00ffU,RxMsgBuff.ID & 0x00ffU,RxMsgBuff.Data[0], RxMsgBuff.Data[1] ); if (RxMsgBuff.Length > 2) USART_SendBytesMessage (CAN_MSG_EXT_3, RxMsgBuff.Data[2], RxMsgBuff.Data[3], RxMsgBuff.Data[4], RxMsgBuff.Data[5]); if (RxMsgBuff.Length > 6 ) USART_SendBytesMessage (CAN_MSG_EXT_4, RxMsgBuff.Data[6], RxMsgBuff.Data[7],0,0); }else{ // standard ID tmp8 = (u8)(RxMsgBuff.timeStamp >>3) & 0xE0U; if (RxMsgBuff.rtr) tmp8 |= 0x10; tmp8 |= (RxMsgBuff.Length & 0x0F); tmp8_A = (RxMsgBuff.ID>>8) & 0x007f; tmp8_A |= (RxMsgBuff.FilterID << 3); USART_SendBytesMessage (CAN_MSG_STD_1, tmp8,(u8)((RxMsgBuff.timeStamp) & 0x00FF), tmp8_A ,RxMsgBuff.ID & 0x00ff); if (RxMsgBuff.Length > 0) USART_SendBytesMessage (CAN_MSG_STD_2, RxMsgBuff.Data[0], RxMsgBuff.Data[1], RxMsgBuff.Data[2], RxMsgBuff.Data[3]); if (RxMsgBuff.Length > 4 ) USART_SendBytesMessage (CAN_MSG_STD_3, RxMsgBuff.Data[4], RxMsgBuff.Data[5], RxMsgBuff.Data[6], RxMsgBuff.Data[7]); } }*/ /** LED Flashing **/ if (GetLedState()) { LED_ON; } else { LED_OFF; } } while (1); }
void AppPong() { switch(appPongState) { case Normal: // Increment ball's position new_ball_X += ballVelocityX; new_ball_Y += ballVelocityY; // Check if the ball is colliding with the left paddle if (new_ball_X < (new_paddle0_X + paddleW) ) { // Check if ball is within paddle's height if ((new_ball_Y > new_paddle0_Y) && (new_ball_Y < new_paddle0_Y + paddleH)) { new_ball_X += ball_rad; // Move ball over one to the right ballVelocityX = -ballVelocityX; // Change velocity Beep(); } } // Check if the ball hit the right paddle if (new_ball_X + ball_rad > new_paddle1_X) { // Check if ball is within paddle's height if ((new_ball_Y > new_paddle1_Y) && (new_ball_Y < new_paddle1_Y + paddleH)) { new_ball_X -= ball_rad; // Move ball over one to the left ballVelocityX = -ballVelocityX; // change velocity Beep(); } } // Check if the ball hit the top or bottom if ((new_ball_Y <= borderWidth) || (new_ball_Y >= (GLCD_HEIGHT - ball_rad - 1))) { // Change up/down velocity direction ballVelocityY = -ballVelocityY; Beep(); } if(AUTOMATIC_CONTROL) { //Automatic Control new_paddle0_Y = new_ball_Y-paddleH/2; // Invincible paddle0 } else { //Controlled by potentiometer ADC_StartConversion(); new_paddle0_Y = map(ADC_GetValue(),0,4096,0,GLCD_HEIGHT); } // Automatic control new_paddle1_Y = new_ball_Y-paddleH/2; // Invincible paddle1 // Check if paddle0 is not out of the frame if(new_paddle0_Y < borderWidth) { // Top Frame new_paddle0_Y = borderWidth; } if(new_paddle0_Y + paddleH > GLCD_HEIGHT - borderWidth) { // Bottom Frame new_paddle0_Y = GLCD_HEIGHT - borderWidth - paddleH; } // Check if paddle1 is not out of the frame if(new_paddle1_Y < borderWidth) { // Top Frame new_paddle1_Y = borderWidth; } if(new_paddle1_Y + paddleH > GLCD_HEIGHT - borderWidth) { // Bottom Frame new_paddle1_Y = GLCD_HEIGHT - borderWidth - paddleH; } joyMsk = Joystick_GetState(); // Show joystick arrows if (joy ^ joyMsk) { joy = joyMsk; if(joy & JOYSTICK_LEFT) { if(ballVelocityX > 0) { ballVelocityX--; } else { ballVelocityX++; } if(ballVelocityY > 0) { ballVelocityY--; } else { ballVelocityY++; } } if(joy & JOYSTICK_RIGHT) { if(ballVelocityX > 0) { ballVelocityX++; } else { ballVelocityX--; } if(ballVelocityY > 0) { ballVelocityY++; } else { ballVelocityY--; } } if(joy & JOYSTICK_CENTER) { } if(joy & JOYSTICK_UP) { GLCD_SetForegroundColor(BackgroundColor); GLCD_DrawRectangle(old_ball_X, old_ball_Y, ball_rad, ball_rad); GLCD_SetForegroundColor(BallForegroundColor); ball_rad++; } if(joy & JOYSTICK_DOWN) { GLCD_SetForegroundColor(BackgroundColor); GLCD_DrawRectangle(old_ball_X, old_ball_Y, ball_rad, ball_rad); GLCD_SetForegroundColor(BallForegroundColor); ball_rad--; } } if(new_ball_X > GLCD_WIDTH) { score0++; appPongState = Lose; } if(new_ball_X < 0) { score1++; appPongState = Lose; } // Draw the Pong Field //STATIC OBJECTS // Draw an outline of the screen: GLCD_SetForegroundColor(ForegroundColor); GLCD_DrawRectangle(0, 0, GLCD_WIDTH - 1, GLCD_HEIGHT - 1); // Draw the center line GLCD_DrawRectangle(GLCD_WIDTH/2 - 1, 0, borderWidth, GLCD_HEIGHT); //MOVING OBJECTS GLCD_SetForegroundColor(BackgroundColor); // Erase the Paddles: GLCD_DrawRectangle(old_paddle0_X, old_paddle0_Y, paddleW, paddleH); GLCD_DrawRectangle(old_paddle1_X, old_paddle1_Y, paddleW, paddleH); // Erase the ball: GLCD_DrawRectangle(old_ball_X, old_ball_Y, ball_rad, ball_rad); old_paddle0_X = new_paddle0_X; old_paddle0_Y = new_paddle0_Y; old_paddle1_X = new_paddle1_X; old_paddle1_Y = new_paddle1_Y; old_ball_X = new_ball_X; old_ball_Y = new_ball_Y; // Draw the Paddles: GLCD_SetForegroundColor(LeftPaddleForegroundColor); GLCD_DrawRectangle(new_paddle0_X, new_paddle0_Y, paddleW, paddleH); GLCD_SetForegroundColor(RightPaddleForegroundColor); GLCD_DrawRectangle(new_paddle1_X, new_paddle1_Y, paddleW, paddleH); // Draw the ball: GLCD_SetForegroundColor(BallForegroundColor); GLCD_DrawRectangle(new_ball_X, new_ball_Y, ball_rad, ball_rad); sprintf (str_score0, "%1u", score0); //takes the numeric value and convert it to a char with the %1u format (1 unit unsigned) sprintf (str_score1, "%1u", score1); str_score0[sizeof(str_score0)-1] = '\0'; //indicate end of string str_score1[sizeof(str_score1)-1] = '\0'; GLCD_SetForegroundColor(LeftPaddleForegroundColor); GLCD_DrawString(5, 5, str_score0); GLCD_SetForegroundColor(RightPaddleForegroundColor); GLCD_DrawString((GLCD_WIDTH/2)+5, 5, str_score1); break; case Lose: if(score0 >= SCORE_MAX_PONG || score1 >= SCORE_MAX_PONG) { appPongState = GameOver; break; } InitializeAppPong(); break; case GameOver: GLCD_SetFont (&GLCD_Font_16x24); if(score0 > score1) { GLCD_SetForegroundColor(GLCD_COLOR_BLUE); GLCD_DrawString((((GLCD_WIDTH/2)-(16*6))/2), GLCD_HEIGHT/2, "WIN !"); GLCD_SetForegroundColor(GLCD_COLOR_GREEN); GLCD_DrawString((((GLCD_WIDTH/2)-(16*6))/2) + (GLCD_WIDTH/2), GLCD_HEIGHT/2, "LOSE !"); } else { GLCD_SetForegroundColor(GLCD_COLOR_GREEN); GLCD_DrawString((((GLCD_WIDTH/2)-(16*6))/2) + (GLCD_WIDTH/2), GLCD_HEIGHT/2, "WIN !"); GLCD_SetForegroundColor(GLCD_COLOR_BLUE); GLCD_DrawString((((GLCD_WIDTH/2)-(16*6))/2), GLCD_HEIGHT/2, "LOSE !"); } score0 = 0; score1 = 0; osDelay(2000); menuChoice = InitMenu; osThreadTerminate (idThreadBeep); break; } }
int main (void) { int32_t max_num = LED_GetCount() - 1; int32_t num = 0; int32_t dir = 1; uint32_t keyMsk, adcVal; int32_t key = -1; int32_t adc = -1; SystemCoreClockUpdate(); LED_Initialize(); /* LED Initialization */ ADC_Initialize(); /* A/D Converter Init */ Buttons_Initialize(); /* Button initialization */ GLCD_Initialize(); /* Initialize the GLCD */ SysTick_Config(SystemCoreClock/100); /* Generate interrupt each 10 ms */ GLCD_SetBackgroundColor (GLCD_COLOR_WHITE); GLCD_ClearScreen (); GLCD_SetBackgroundColor (GLCD_COLOR_BLUE); GLCD_SetForegroundColor (GLCD_COLOR_WHITE); GLCD_SetFont (&GLCD_Font_16x24); GLCD_DrawString (0*16, 0*24, " STM32303C-EVAL Demo"); GLCD_DrawString (0*16, 1*24, " Blinky Example "); GLCD_DrawString (0*16, 2*24, " www.keil.com "); GLCD_SetBackgroundColor (GLCD_COLOR_WHITE); GLCD_SetForegroundColor (GLCD_COLOR_BLUE); GLCD_DrawString (0*16, 5*24, "LEDs: "); GLCD_DrawString (0*16, 6*24, "AD value: "); GLCD_DrawString (0*16, 7*24, "Buttons : "); GLCD_SetForegroundColor (GLCD_COLOR_LIGHT_GREY); GLCD_DrawString (9*16, 5*24, "0123"); while (1) { /* Force refresh */ key = -1; adc = -1; if (LEDOn) { LEDOn = 0; LED_On (num); /* Turn specified LED on */ GLCD_SetForegroundColor (GLCD_COLOR_RED); GLCD_DrawChar ((9+num)*16, 5*24, numStr[num]); } if (LEDOff) { LEDOff = 0; LED_Off (num); /* Turn specified LED off */ GLCD_SetForegroundColor (GLCD_COLOR_LIGHT_GREY); GLCD_DrawChar ((9+num)*16, 5*24, numStr[num]); num += dir; /* Change LED number */ if (dir == 1 && num == max_num) { dir = -1; /* Change direction to down */ } else if (num == 0) { dir = 1; /* Change direction to up */ } } keyMsk = Buttons_GetState(); /* Show buttons state */ if (key ^ keyMsk) { GLCD_SetForegroundColor (GLCD_COLOR_BLACK); if (keyMsk & KEY_USER ) { GLCD_DrawString (9*16, 7*24, "Key"); } GLCD_SetForegroundColor (GLCD_COLOR_LIGHT_GREY); if (!(keyMsk & KEY_USER )) { GLCD_DrawString (9*16, 7*24, "Key"); } } ADC_StartConversion(); /* Show A/D conversion bargraph */ adcVal = ADC_GetValue(); if (adc ^ adcVal) { adc = adcVal; GLCD_SetForegroundColor (GLCD_COLOR_GREEN); GLCD_DrawBargraph (9*16, 6*24, 160, 20, (adcVal * 100) / ((1 << ADC_GetResolution()) - 1)); } } }
/** * @brief The specified ADC channel convert once in polling mode * @param[in] chan The specified ADC channel * @param[in] data Pointer to the output data * @param[in] msec Timeout value in millisecond of conversion * HAL_WAIT_FOREVER for no timeout * @retval HAL_Status, HAL_OK on success */ HAL_Status HAL_ADC_Conv_Polling(ADC_Channel chan, uint32_t *data, uint32_t msec) { unsigned long flags; ADC_Private *priv; uint32_t stopTime; uint8_t isTimeout; ADC_ASSERT_CHANNEL(chan); flags = HAL_EnterCriticalSection(); priv = &gADCPrivate; if (priv->state == ADC_STATE_READY) priv->state = ADC_STATE_BUSY; else priv = NULL; HAL_ExitCriticalSection(flags); if (priv == NULL) { HAL_WRN("ADC state: %d\n", gADCPrivate.state); return HAL_ERROR; } if ((!ADC_GetChanPinMux(chan)) && (chan != ADC_CHANNEL_8)) { HAL_BoardIoctl(HAL_BIR_PINMUX_INIT, HAL_MKDEV(HAL_DEV_MAJOR_ADC, chan), 0); ADC_SetChanPinMux(chan); } ADC_DisableAllChanSel(); ADC_DisableAllChanCmp(); ADC_DisableAllChanIRQ(); if (chan == ADC_CHANNEL_8) ADC_EnableVbatDetec(); ADC_EnableChanSel(chan); if (msec == HAL_WAIT_FOREVER) stopTime = 0xFFFFFFFF; else stopTime = HAL_TicksToMSecs(HAL_Ticks()) + msec; if (stopTime < msec) { HAL_ERR("stopTime overflow.\n"); return HAL_ERROR; } isTimeout = 1; ADC_EnableADC(); while (HAL_TicksToMSecs(HAL_Ticks()) <= stopTime) { if (HAL_GET_BIT(ADC_GetDataPending(), HAL_BIT(chan))) { *data = ADC_GetValue(chan); ADC_ClrDataPending(ADC_GetDataPending()); isTimeout = 0; break; } } ADC_DisableADC(); ADC_DisableChanSel(chan); if (chan == ADC_CHANNEL_8) ADC_DisableVbatDetec(); if (ADC_GetChanPinMux(chan) && (chan != ADC_CHANNEL_8)) { HAL_BoardIoctl(HAL_BIR_PINMUX_DEINIT, HAL_MKDEV(HAL_DEV_MAJOR_ADC, chan), 0); ADC_ClrChanPinMux(chan); } flags = HAL_EnterCriticalSection(); priv->state = ADC_STATE_READY; HAL_ExitCriticalSection(flags); if (isTimeout) { HAL_WRN("ADC timeout.\n"); return HAL_TIMEOUT; } else { return HAL_OK; } }
/** * @brief Get digital value of the specified ADC channel * @param[in] chan The specified ADC channel * @return Digital value converted by the specified ADC channel */ uint32_t HAL_ADC_GetValue(ADC_Channel chan) { ADC_ASSERT_CHANNEL(chan); return ADC_GetValue(chan); }
int ADC_Value() { ADC_Start(); return ADC_GetValue(); }