void Delay(u32 ticks) { SysTick_CounterCmd(SysTick_Counter_Enable); tick = ticks; while(tick); SysTick_CounterCmd(SysTick_Counter_Disable); /* Clear SysTick Counter */ SysTick_CounterCmd(SysTick_Counter_Clear); }
/******************************************************************************* * Function Name : delay_us * Description : * Input : - Nus: * Output : None * Return : None * Attention : *******************************************************************************/ void delay_us(u32 Nus) { SysTick_SetReload(delay_fac_us * Nus); SysTick_CounterCmd(SysTick_Counter_Clear); SysTick_CounterCmd(SysTick_Counter_Enable); do { Status = SysTick_GetFlagStatus(SysTick_FLAG_COUNT); }while (Status != SET); SysTick_CounterCmd(SysTick_Counter_Disable); SysTick_CounterCmd(SysTick_Counter_Clear); }
/******************************************************************************* * Function Name : delay_us * Description : 初始化延迟函数 * Input : - Nus: 延时us * Output : None * Return : None * Attention : 参数最大值为 0xffffff / (HCLK / 8000000) *******************************************************************************/ void delay_us(u32 Nus) { SysTick_SetReload(delay_fac_us * Nus); /* 时间加载 */ SysTick_CounterCmd(SysTick_Counter_Clear); /* 清空计数器 */ SysTick_CounterCmd(SysTick_Counter_Enable); /* 开始倒数 */ do { Status = SysTick_GetFlagStatus(SysTick_FLAG_COUNT); }while (Status != SET); /* 等待时间到达 */ SysTick_CounterCmd(SysTick_Counter_Disable); /* 关闭计数器 */ SysTick_CounterCmd(SysTick_Counter_Clear); /* 清空计数器 */ }
/******************************************************************************* * Function Name : Delay * Description : Inserts a delay time. * Input : nTime: specifies the delay time length, in milliseconds. * Output : None * Return : None *******************************************************************************/ void Delay(u32 nTime) { /* Enable the SysTick Counter */ SysTick_CounterCmd(SysTick_Counter_Enable); TimingDelay = nTime; while(TimingDelay != 0); /* Disable the SysTick Counter */ SysTick_CounterCmd(SysTick_Counter_Disable); /* Clear the SysTick Counter */ SysTick_CounterCmd(SysTick_Counter_Clear); }
void SysTick_Configuration(void) { SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8); SysTick_SetReload(9); SysTick_CounterCmd(SysTick_Counter_Enable); SysTick_ITConfig(ENABLE); }
/* init System Ticker with 1ms ----------------------------------------------*/ void initSysTick() { // config for systick - delay SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK); SysTick_SetReload(72000); SysTick_ITConfig(ENABLE); SysTick_CounterCmd(SysTick_Counter_Enable); }
void systime_initial(void) { /* SysTick end of count event each 10ms with input clock equal to 9MHz (HCLK/8, default) */ SysTick_SetReload(90000); /* Enable SysTick interrupt */ SysTick_ITConfig(ENABLE); SysTick_CounterCmd(SysTick_Counter_Enable); }
/******************************************************************************* * Function Name : delay_ms * Description : 初始化延迟函数 * Input : - nms: 延时ms * Output : None * Return : None * Attention : 参数最大值 nms<=0xffffff*8*1000/SYSCLK 对72M条件下,nms<=1864 *******************************************************************************/ void delay_ms(uint16_t nms) { uint32_t temp = delay_fac_ms * nms; if (temp > 0x00ffffff) { temp = 0x00ffffff; } SysTick_SetReload(temp); /* 时间加载 */ SysTick_CounterCmd(SysTick_Counter_Clear); /* 清空计数器 */ SysTick_CounterCmd(SysTick_Counter_Enable); /* 开始倒数 */ do { Status = SysTick_GetFlagStatus(SysTick_FLAG_COUNT); }while (Status != SET); /* 等待时间到达 */ SysTick_CounterCmd(SysTick_Counter_Disable); /* 关闭计数器 */ SysTick_CounterCmd(SysTick_Counter_Clear); /* 清空计数器 */ }
/******************************************************************************* * Function Name : delay_ms * Description : * Input : - nms: * Output : None * Return : None * Attention : *******************************************************************************/ void _delay_ms(uint16_t nms) { uint32_t temp = delay_fac_ms * nms; if (temp > 0x00ffffff) { temp = 0x00ffffff; } SysTick_SetReload(temp); SysTick_CounterCmd(SysTick_Counter_Clear); SysTick_CounterCmd(SysTick_Counter_Enable); do { Status = SysTick_GetFlagStatus(SysTick_FLAG_COUNT); }while (Status != SET); SysTick_CounterCmd(SysTick_Counter_Disable); SysTick_CounterCmd(SysTick_Counter_Clear); }
void SystickConfigure() { /* SysTick end of count event each 1ms with input clock equal to 9MHz (HCLK/8, default) */ SysTick_SetReload(9000); SysTick_ITConfig(ENABLE); SysTick_CounterCmd(SysTick_Counter_Enable); timer1 = 0; timer2 = 0; timerKW = 0; }
void SysTick_Configuration(void) { /* Configure HCLK clock as SysTick clock source */ SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK); SysTick_SetReload(72000); /* Enable the SysTick Interrupt */ SysTick_ITConfig(ENABLE); /* Enable the SysTick Counter */ SysTick_CounterCmd(SysTick_Counter_Enable); }
void SysTick_Init(void) { /* SysTick end of count event each 1ms with input clock equal to 9MHz (HCLK/8, default) */ SysTick_SetReload(9000); /* Enable SysTick interrupt */ SysTick_ITConfig(ENABLE); /* Enable the SysTick Counter */ SysTick_CounterCmd(SysTick_Counter_Enable); }
void initTimer(void) { SysTick_CounterCmd(SysTick_Counter_Disable); SysTick_ITConfig(DISABLE); SysTick->LOAD = 9000; SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK_Div8); SysTick_ITConfig(ENABLE); }
/******************************************************************************* * Function Name : main. * Description : Main routine. * Input : None. * Output : None. * Return : None. *******************************************************************************/ int main(void) { #ifdef DEBUG debug(); #endif Set_System(); Set_USBClock(); USB_Config(); USB_Init(); InitialIO(); InitialADC(); #ifdef JOYSTICK SSD1303_Init(); InitialSSD1306_controller(); #endif InitialProcTask(); //WaitConfig(); InitialDevice(&dev,&SSD1303_Prop,SongSmall5_DrawChar); SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK); SysTick_SetReload(72000000/20); SysTick_CounterCmd(SysTick_Counter_Enable); SysTick_ITConfig(ENABLE); CurrentProcess = JoystickProcess; CurrentSystick = JoystickSystick; JoystickUIInit(); void HidProcess(void); while (1) { HidProcess(); continue; static u8 lastCamOn = 0; if(lastCamOn != bCameraOn) { if(bCameraOn) { CurrentProcess = TetrisProcess; CurrentSystick = TetrisSystick; TetrisUIInit(); } else { CurrentProcess = JoystickProcess; CurrentSystick = JoystickSystick; JoystickUIInit(); } } lastCamOn = bCameraOn; CurrentProcess(); } }
/* ******************************************************************************** ** 函数名称 : SysTick_Configuration(void) ** 函数功能 : 时钟初始化 ** 输 入 : 无 ** 输 出 : 无 ** 返 回 : 无 ******************************************************************************** */ static void SysTick_Configuration(void) { RCC_ClocksTypeDef rcc_clocks; u32 cnts; RCC_GetClocksFreq(&rcc_clocks); cnts = (u32)rcc_clocks.HCLK_Frequency/OS_TICKS_PER_SEC; SysTick_SetReload(cnts); SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK); SysTick_CounterCmd(SysTick_Counter_Enable); SysTick_ITConfig(ENABLE); }
/** * @brief delay_us * @param u32 Nus * @retval None */ void delay_us(u32 us) { uint32_t temp = delay_fac_us * us; int16_t tick = us/1864135; if (temp > 0x00ffffff) temp = 0x00ffffff; do { SysTick_SetReload(temp); SysTick_CounterCmd(SysTick_Counter_Clear); SysTick_CounterCmd(SysTick_Counter_Enable); do { Status = SysTick_GetFlagStatus(SysTick_FLAG_COUNT); } while ((Status != SET) && (ButtonPush == B_RESET)); SysTick_CounterCmd(SysTick_Counter_Disable); SysTick_CounterCmd(SysTick_Counter_Clear); } while((tick-- > 0) && (ButtonPush == B_RESET)); }
int main (void) { unsigned long cnt; //unsigned char buf[6]; //unsigned char led_on=0; unsigned char pwr_sw; unsigned char sw_1; unsigned char sf_1; unsigned char sf_2; unsigned char sf_0; unsigned char pcm_dsd; unsigned char dsd64_128; /* SystemInit() is called by startup_stm32f10x.ld.s or you can use STM32F10X.s and call SetupClock() in here first */ /* SysTick event each 10 ms with input clock equal to 9MHz (HCLK/8) */ SysTick_SetReload(90000/2); /* Enable the SysTick Counter */ SysTick_CounterCmd(SysTick_Counter_Enable); /* Enable SysTick interrupt */ SysTick_ITConfig(ENABLE); GPIOA_Configure(); GPIOB_Configure(); //add_for_usart_in_the_main(); /* RESET USB */ GPIO_ResetBits(GPIOB, USB_RST_B6); Delay(10); GPIO_SetBits(GPIOB, USB_RST_B6); Delay(10); //printf(" main loop\n"); GPIO_ResetBits(GPIOA, ON_PWR_A11); /*ON_PWR*/ GPIO_SetBits(GPIOA, MUTE_A3); /*MUTE*/ GPIO_ResetBits(GPIOA, MUTE_RY_A9); /*MUTE_RY*/ GPIO_ResetBits(GPIOA, SEL_ANA_A8); /*SEL_ANA*/ state=USB_IN; current_event=INIT; current_state=IDLE_STATE; while(1) { Delay(2); sf_0 = GPIO_ReadInputDataBit(GPIOB,SF_0_B7); sf_1 = GPIO_ReadInputDataBit(GPIOB,SF_1_B8); sf_2 = GPIO_ReadInputDataBit(GPIOB,SF_2_B9); pcm_dsd = GPIO_ReadInputDataBit(GPIOB,PCM_DSD_B5); dsd64_128 = GPIO_ReadInputDataBit(GPIOA,DSD64_128_A2); state=get_state(); if(state != current_state ) { current_event=INIT; current_state=state; } switch(current_state) { case POWER_OFF: if(current_event==INIT) { dprint("power_off\n"); GPIO_SetBits(GPIOA, MUTE_A3); /*MUTE*/ GPIO_ResetBits(GPIOA, MUTE_RY_A9); /*MUTE_RY*/ EventTimeTick = TimeTick +10 ; current_event=EVENT1; } else if(current_event==EVENT1) { if(TimeTick > EventTimeTick) { GPIO_SetBits(GPIOA, ON_PWR_A11); /*ON_PWR*/ current_event=IDLE; } } else if(current_event == IDLE) { dprint("power_off - idle event\n"); } break; case USB_IN: if(current_event==INIT) { dprint("usb_in event0\n"); GPIO_SetBits(GPIOA, MUTE_RY_A9); /*MUTE_RY*/ GPIO_SetBits(GPIOA, MUTE_A3); /*MUTE*/ EventTimeTick = TimeTick +10 ; current_event=EVENT1; } else if(current_event==EVENT1) { dprint("usb_in event1\n"); if(TimeTick > EventTimeTick) { dprint("TimeTick_100m event1\n"); GPIO_ResetBits(GPIOA, SEL_ANA_A8); /*SEL_ANA*/ GPIO_ResetBits(GPIOA, ON_PWR_A11); /*ON_PWR*/ EventTimeTick = TimeTick + 10; current_event=EVENT2; } } else if(current_event ==EVENT2) { dprint("usb_in event2\n"); if(TimeTick > EventTimeTick) { dprint("TimeTick_100m event2\n"); GPIO_ResetBits(GPIOA, MUTE_A3); /*MUTE*/ current_event=IDLE; } } else if(current_event == IDLE) { dprint("usb in - idle event\n"); } break; case ANALOG_IN: if(current_event==INIT) { dprint("analog_in\n"); GPIO_SetBits(GPIOA, MUTE_RY_A9); /*MUTE_RY*/ GPIO_SetBits(GPIOA, MUTE_A3); /*MUTE*/ EventTimeTick = TimeTick +10 ; current_event=EVENT1; } else if(current_event==EVENT1) { dprint("analog event1\n"); if(TimeTick > EventTimeTick) { dprint("TimeTick_100m event1\n"); GPIO_SetBits(GPIOA, SEL_ANA_A8); /*SEL_ANA*/ GPIO_ResetBits(GPIOA, ON_PWR_A11); /*ON_PWR*/ EventTimeTick = TimeTick + 10; current_event=EVENT2; } } else if(current_event ==EVENT2) { dprint("analog event2\n"); if(TimeTick > EventTimeTick) { dprint("TimeTick_100m event2\n"); GPIO_ResetBits(GPIOA, MUTE_A3); /*MUTE*/ current_event=IDLE; } } else if(current_event == IDLE) { dprint("analog in - idle event\n"); } break; default: dprint("default\n"); break; } } }
/** * @brief Main program. * @param None * @retval None */ int main(void) { char test[25]; unsigned int an1, an2, an3, an4, an5; float Ta=0; int i; long msum[4]; float fm[4], T1, T2, T3, SP; unsigned int DSState = 0; unsigned long DSTimer=0, t0, t1, dt, HTimer, TTimer, t_sec, t_min; unsigned char data[2]; unsigned char out=0; unsigned int decval; volatile float pp, pi, pd, f_error, error_old=0, pid_out, pid_out_i, pid_out_p, pid_out_d, error_i=0; u8 Send_Buffer[25]; u8 tmp[4]; //char no_windup = 0; RCC_Configuration(); NVIC_Configuration(); /* SysTick end of count event each 1ms with input clock equal to 9MHz (HCLK/8, default) */ SysTick_SetReload(9000); /* Enable SysTick interrupt */ SysTick_ITConfig(ENABLE); /* Enable the SysTick Counter */ SysTick_CounterCmd(SysTick_Counter_Enable); GPIO_Setup(); RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI2, ENABLE); /* SPI2 Config -------------------------------------------------------------*/ SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex; SPI_InitStructure.SPI_Mode = SPI_Mode_Master; SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b; SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low; SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(SPI2, &SPI_InitStructure); /* Enable SPI1 */ SPI_CalculateCRC(SPI2, DISABLE); SPI_Cmd(SPI2, ENABLE); InitADC1(); // ADC1 Init OWInit(&OneWire, GPIOB, GPIO_Pin_8); /* Connect Key Button EXTI Line to Key Button GPIO Pin */ //GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_Pin_7); /* Configure Key Button EXTI Line to generate an interrupt on falling edge */ //EXTI_InitStructure.EXTI_Line = EXTI_Line7; //EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; //EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; //EXTI_InitStructure.EXTI_LineCmd = ENABLE; //EXTI_Init(&EXTI_InitStructure); /* Time base configuration */ TIM_TimeBaseStructure.TIM_Period = 55000; TIM_TimeBaseStructure.TIM_Prescaler = 12; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); /* TIM2 PWM2 Mode configuration: Channel1 */ TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2; TIM_OCInitStructure.TIM_Channel = TIM_Channel_1; TIM_OCInitStructure.TIM_Pulse = 1;//20000; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OCInit(TIM3, &TIM_OCInitStructure); /* TIM2 configuration in Input Capture Mode */ TIM_ICStructInit(&TIM_ICInitStructure); TIM_ICInitStructure.TIM_Channel = TIM_Channel_2; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Falling; TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; TIM_ICInitStructure.TIM_ICFilter = 0; TIM_ICInit(TIM3, &TIM_ICInitStructure); /* One Pulse Mode selection */ TIM_SelectOnePulseMode(TIM3, TIM_OPMode_Single); /* Input Trigger selection */ TIM_SelectInputTrigger(TIM3, TIM_TS_TI2FP2); /* Slave Mode selection: Trigger Mode */ TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Trigger); ST7565_st7565_init(); ST7565_st7565_command(CMD_SET_BIAS_9); ST7565_st7565_command(CMD_DISPLAY_ON); ST7565_st7565_command(CMD_SET_ALLPTS_NORMAL); ST7565_st7565_set_brightness(0x0C); OWSearch(&OneWire, addr); /*sprintf(test, "%02X %02X %02X %02X %02X %02X %02X %02X", addr[7],addr[6], addr[5],addr[4], addr[3],addr[2], addr[1],addr[0]); ST7565_drawstring(6, 6, test); */ USB_Init(); ST7565_display(); // show splashscreen t0 = millis(); HTimer = millis(); TTimer = millis(); pp = 10; pi = 0; pd = 150; while(1) { msum[0] = 0; msum[1] = 0; msum[2] = 0; msum[3] = 0; for (i = 0; i < 1000; i++) { an1 = GetADC1Channel(ADC_Channel_1); an2 = GetADC1Channel(ADC_Channel_2); an3 = GetADC1Channel(ADC_Channel_3); an4 = GetADC1Channel(ADC_Channel_4); an5 = GetADC1Channel(ADC_Channel_5); msum[0] += an1; msum[1] += an3 - an2; msum[2] += an4 - an2; msum[3] += an5 - an2; //DelayuS(333); } SP = round((msum[0] / 1000.0) * (60.0 / 4096)) * 5; fm[1] = (msum[1] / 1000.0); fm[2] = (msum[2] / 1000.0) + 12; fm[3] = (msum[3] / 1000.0) - 7; T1 = (T1 + Ta + Dac2Dt(fm[1])) / 2; T2 = (T2 + Ta + Dac2Dt(fm[2])) / 2; T3 = (T3 + Ta + Dac2Dt(fm[3])) / 2; t1 = millis(); dt = t1 - t0; t0 = t1; if (millis() - HTimer > 1000) { f_error = SP - T2; //if (noerror_i += error; pid_out_p = pp * f_error; pid_out_i = pi * error_i; pid_out_d = pd * (f_error - error_old); pid_out = pid_out_p + pid_out_i + pid_out_d; error_old = f_error; //out = pid_out; if (pid_out > 99) { out = 99; } else if (pid_out < 0) { out = 0; } else { out = round(pid_out); } TIM_SetCompare1(TIM3, 55000-PowerValues[out]); HTimer += 1000; //error_old = 10; sprintf(test, "T1 : %5.1f E %5.1f ", T1, f_error); ST7565_drawstring(6, 0, test); sprintf(test, "T2 : %5.1f ", T2); ST7565_drawstring(6, 1, test); sprintf(test, "T3 : %5.1f ", T3); ST7565_drawstring(6, 2, test); sprintf(test, "SP : %5.1f P %6.1f ", SP, pid_out_p); ST7565_drawstring(6, 3, test); sprintf(test, "Ta : %5.1f I %6.1f ", Ta, pid_out_i); ST7565_drawstring(6, 4, test); sprintf(test, "dt : %5lu D %6.1f ", dt, pid_out_d); ST7565_drawstring(6, 5, test); sprintf(test, "out: %3u %% %6.1f ", out, pid_out); ST7565_drawstring(6, 6, test); t_sec = (millis() - TTimer) / 1000; t_min = floor(t_sec / 60); t_sec %= 60; Send_Buffer[0] = 0x07; decval = round(T1 * 100); memcpy(&Send_Buffer[1], &decval, 2); decval = round(T2 * 100); memcpy(&Send_Buffer[3], &decval, 2); decval = round(T3 * 100); memcpy(&Send_Buffer[5], &decval, 2); decval = round(SP * 100); memcpy(&Send_Buffer[7], &decval, 2); memcpy(&Send_Buffer[9], &out, 1); //sprintf(test, "%02X %02X %02X %02X ", Send_Buffer[1], Send_Buffer[2], Send_Buffer[3], Send_Buffer[4]); //ST7565_drawstring(6, 7, test); UserToPMABufferCopy(Send_Buffer, ENDP1_TXADDR, 9); SetEPTxCount(ENDP1, 9); SetEPTxValid(ENDP1); ST7565_display(); } if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0) == 0) { TTimer = millis(); } //onewire switch (DSState){ case 0: OWReset(&OneWire); OWWrite(&OneWire, 0xCC); // skip ROM OWWrite(&OneWire, 0x44); // start conversion DSTimer = millis(); DSState++; break; case 1: if((millis() - DSTimer) >= 1000){ OWReset(&OneWire); OWSelect(&OneWire, addr); OWWrite(&OneWire, 0xBE); // Read Scratchpad data[0] = OWRead(&OneWire); data[1] = OWRead(&OneWire); Ta = ((data[1] << 8) | data[0]) / 16.0; DSState = 0; } break; } } }
/******************************************************************************* * Function Name : SYSCLOCK_Disable * Description : Disable the SYSTick Peripheral. * Input : None * Output : None * Return : None *******************************************************************************/ void SYSCLOCK_Disable() { /* Disable the SysTick Counter */ SysTick_CounterCmd(SysTick_Counter_Disable); }
/******************************************************************************* * Function Name : SYSCLOCK_Enable * Description : Enable the SYSTick Peripheral. * Input : None * Output : None * Return : None *******************************************************************************/ void SYSCLOCK_Enable() { /* Enable the SysTick Counter */ SysTick_CounterCmd(SysTick_Counter_Enable); }
/****************************************************************************** * Function Name : SysTick_Start * Description : None * Input : None * Output : None * Return : None ******************************************************************************/ void SysTick_Start(void) { SysTick_CounterCmd(SysTick_Counter_Clear); SysTick_CounterCmd(SysTick_Counter_Enable); }
void SysTickHandler(void) { GPIO_InitTypeDef GPIO_InitStructure; int sampCount = 0; int i; u16 max; u16 min; int maxIndex, minIndex; u32 avg; int count; u16 yTest; /* Disable SysTick Counter */ SysTick_CounterCmd(SysTick_Counter_Disable); /* Clear SysTick Counter */ SysTick_CounterCmd(SysTick_Counter_Clear); if( penDownCooldown < COOLDOWN_DELAY ) penDownCooldown++; if( penDown == 0 ) { // we think the pen is not down at all if( GPIO_ReadInputDataBit(GPIO_PNDWN_DET) == 0 ) { // 0 is the detected state, note inversion // this is the case that the pen goes down if( (penDown == 0) && (penDownCooldown >= COOLDOWN_DELAY) ) { // previously, we were not in a pen down if( powerState == 0 ) { cmdPowerUp(); // power on if we were previously off! } GPIO_WriteBit(GPIO_PNDWN_OUT, Bit_SET); penDownCooldown = 0; penDown = 1; sampleDisplay = 1; } } } else { // the pen was down, now we have to figure out if the pen is up // this is the case that the pen goes up ADC_RegularChannelConfig(ADC1, ADC_LCD_YP, 1, ADC_SampleTime_71Cycles5); ADC_SoftwareStartConvCmd(ADC1, ENABLE); while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET) ; yTest = ADC_GetConversionValue(ADC1); if( (yTest > PENUP_THRESH) && (penDownCooldown >= COOLDOWN_DELAY) ) { GPIO_WriteBit(GPIO_PNDWN_OUT, Bit_RESET); penDownCooldown = 0; sampleDisplay = 0; penDown = 0; } else if ( (yTest > PENUP_THRESH) && (penDownCooldown < COOLDOWN_DELAY) ) { // penDownCooldown++; // this is implicit from the very, very top statement sampleDisplay = 0; penDown = 1; /* Enable the SysTick Counter */ SysTick_CounterCmd(SysTick_Counter_Enable); return; // exit the routine if we think we have chatter... } else { sampleDisplay = 1; penDownCooldown = 0; // force the cooldown to zero penDown = 1; } } if( penDown ) { if( sampleDisplay ) { // kick off the sense routine // turn off the pullup on pin 3 so as not to bias readings // GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; // GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; // GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // drive it hard //////////////////// sample the Y value (assume: pins 4/5 set for AIN on entry) // set LCD_XM to 0, LCD_XP to 1 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_WriteBit(GPIO_LCD_XM, Bit_RESET); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_WriteBit(GPIO_LCD_XP, Bit_SET); settleDelay(); /* ADC1 regular channel1 configuration */ ADC_RegularChannelConfig(ADC1, ADC_LCD_YP, 1, ADC_SampleTime_71Cycles5); sampCount = 0; while( sampCount < NUMSAMPS ) { ADC_SoftwareStartConvCmd(ADC1, ENABLE); while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET) ; arrayY[sampCount++] = ADC_GetConversionValue(ADC1); } //////////////////// end sample the Y value //////////////////// sample the X value GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; GPIO_Init(GPIOA, &GPIO_InitStructure); // set LCD_YM to 0, LCD_YP to 1 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_WriteBit(GPIO_LCD_YM, Bit_RESET); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_WriteBit(GPIO_LCD_YP, Bit_SET); settleDelay(); ADC_RegularChannelConfig(ADC1, ADC_LCD_XP, 1, ADC_SampleTime_71Cycles5); sampCount = 0; while( sampCount < NUMSAMPS ) { ADC_SoftwareStartConvCmd(ADC1, ENABLE); while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET) ; arrayX[sampCount++] = ADC_GetConversionValue(ADC1); } //////////////////// end sample the X value } //////////////////// return to pendown sampling mode // drive LCD_YM to 1 to discharge the display capacitance GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_WriteBit(GPIO_LCD_YM, Bit_SET); settleDelay(); settleDelay(); /* Configure PA.04 as analog input */ // Y- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; GPIO_Init(GPIOA, &GPIO_InitStructure); /* Configure PA.05 as analog input */ // Y+ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; // toggle to GPIO_Mode_IN_FLOATING when sampling GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_WriteBit(GPIO_LCD_XM, Bit_RESET); //////////////////// end return to pendown sampling mode ///////// verify that the pen was still down after all of this, or else discard data. settleDelay(); ADC_RegularChannelConfig(ADC1, ADC_LCD_YP, 1, ADC_SampleTime_71Cycles5); ADC_SoftwareStartConvCmd(ADC1, ENABLE); while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET) ; yTest = ADC_GetConversionValue(ADC1); if( yTest > PENUP_THRESH ) { sampleDisplay = 0; // other counters will get reset the next systick, which is fine // but we want to prevent commiting any data at this point in time } ///////// if( sampleDisplay ) { // now perform data filtering max = 0; min = 65535; for( i = 0; i < NUMSAMPS; i++ ) { if( arrayX[i] > max ) { max = arrayX[i]; maxIndex = i; } if( arrayX[i] < min ) { min = arrayX[i]; minIndex = i; } } avg = 0; count = 0; for( i = 0; i < NUMSAMPS; i++ ) { if( (i != maxIndex) && (i != minIndex) ) { avg += arrayX[i]; count ++; // note that in the case that I have exactly the same samples i get a different count... } } avg /= count; xVal = avg; updated |= 1; // now do the same for y max = 0; min = 65535; for( i = 0; i < NUMSAMPS; i++ ) { if( arrayY[i] > max ) { max = arrayY[i]; maxIndex = i; } if( arrayY[i] < min ) { min = arrayY[i]; minIndex = i; } } avg = 0; count = 0; for( i = 0; i < NUMSAMPS; i++ ) { if( (i != maxIndex) && (i != minIndex) ) { avg += arrayY[i]; count ++; // note that in the case that I have exactly the same samples i get a different count... } } avg /= count; yVal = avg; updated |= 2; // the SPI routine should now be able to service with correct data } } else { // do nothing } /* Enable the SysTick Counter */ SysTick_CounterCmd(SysTick_Counter_Enable); }