/** * @brief Main program. * @param None * @retval None */ int main(void) { RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG , ENABLE); RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOC | RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_GPIOA, ENABLE ); RCC_APB1PeriphClockCmd( RCC_APB1Periph_TIM3 | RCC_APB1Periph_TIM3, ENABLE ); volatile int i; int n = 1; GPIO_StructInit(&GPIO_InitStructure); // Reset init structure GPIO_StructInit(&GPIO_InitStructure2); // Reset init structure GPIO_StructInit(&GPIO_InitStructure3); // Reset init structure GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_TIM3); GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_TIM3); GPIO_PinAFConfig(GPIOC, GPIO_PinSource8, GPIO_AF_TIM3); GPIO_PinAFConfig(GPIOC, GPIO_PinSource9, GPIO_AF_TIM3); //GPIO_PinAFConfig(GPIOB, GPIO_PinSource4, GPIO_AF_TIM3); //GPIO_PinAFConfig(GPIOB, GPIO_PinSource5, GPIO_AF_TIM3); /** PWM Output PortC 6 7 8 9 AF TIM3 **/ // Setup Servo on STM32-Discovery Board to use PWM. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7| GPIO_Pin_8| GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; // Alt Function - Push Pull GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init( GPIOC, &GPIO_InitStructure ); /** Button Output PortB 4 5 **/ /* GPIOA Configuration: CH1 (PB4) and CH2 (PB5) */ GPIO_InitStructure2.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 ; GPIO_InitStructure2.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure2.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure2.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure2.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOB, &GPIO_InitStructure2); GPIOB->BSRRL = GPIO_Pin_4; GPIOB->BSRRL = GPIO_Pin_5; /** Button Input PortA 2 3 **/ /* GPIOA Configuration: (PA2) and (PA3) */ //input GPIO_InitStructure2.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3 ; GPIO_InitStructure2.GPIO_Mode = GPIO_Mode_IN; GPIO_InitStructure2.GPIO_Speed = GPIO_Speed_100MHz; GPIO_InitStructure2.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure2.GPIO_PuPd = GPIO_PuPd_DOWN ; GPIO_Init(GPIOA, &GPIO_InitStructure2); /** Button A2 A3 Interrupt EXTI 2 3 **/ //清空中断标志 EXTI_ClearITPendingBit(EXTI_Line2); EXTI_ClearITPendingBit(EXTI_Line3); //选择中断管脚PA.2 PA.3 SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource2); SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource3); // SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA,EXTI_PinSource0); // SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA,EXTI_PinSource0); EXTI_InitStructure.EXTI_Line = EXTI_Line2 ; //选择中断线路2 EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; //设置为中断请求,非事件请求 EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; //设置中断触发方式为上下降沿触发 EXTI_InitStructure.EXTI_LineCmd = ENABLE; //外部中断使能 EXTI_Init(&EXTI_InitStructure); EXTI_GenerateSWInterrupt(EXTI_Line2); EXTI_InitStructure.EXTI_Line = EXTI_Line3 ; //选择中断线路2 EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; //设置为中断请求,非事件请求 EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; //设置中断触发方式为上下降沿触发 EXTI_InitStructure.EXTI_LineCmd = ENABLE; //外部中断使能 EXTI_Init(&EXTI_InitStructure); EXTI_GenerateSWInterrupt(EXTI_Line3); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); //选择中断分组2 NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn ; //选择中断通道2 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占式中断优先级设置为0 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //响应式中断优先级设置为0 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能中断 NVIC_Init(&NVIC_InitStructure); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); //选择中断分组2 NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQn; //选择中断通道2 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //抢占式中断优先级设置为0 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; //响应式中断优先级设置为0 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //使能中断 NVIC_Init(&NVIC_InitStructure); /** Timer setting Tim3 **/ PrescalerValue = (uint16_t) ((SystemCoreClock /4) / 100000) - 1; // Let PWM frequency equal 100Hz. // Let period equal 1000. Therefore, timer runs from zero to 1000. Gives 0.1Hz resolution. // Solving for prescaler gives 240. TIM_TimeBaseStructInit( &TIM_TimeBaseInitStruct ); TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInitStruct.TIM_Period = 3360 - 1; // 0..2000 TIM_TimeBaseInitStruct.TIM_Prescaler = 500; TIM_TimeBaseInit( TIM3, &TIM_TimeBaseInitStruct ); /** TIM_Prescaler = 500-1, TIM_Period = 1680-1 , TIMxCLK = 84MHz 輸出脈波週期 = (TIM_Period + 1) * (TIM_Prescaler + 1) / TIMxCLK = ( 1680 - 1 +1 ) * ( 500 - 1 + 1 ) / 84000000 = 0.01s(100Hz) **/ TIM_OCStructInit( &TIM_OCInitStruct ); TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1; // Initial duty cycle equals 0%. Value can range from zero to 1000. TIM_OCInitStruct.TIM_Pulse = 3360 -1; // 0 .. 1680 (0=Always Off, 1680=Always On) TIM_OC1Init( TIM3, &TIM_OCInitStruct ); // Channel 1 Servo TIM_OC2Init( TIM3, &TIM_OCInitStruct ); // Channel 2 Servo TIM_OC3Init( TIM3, &TIM_OCInitStruct ); // Channel 3 PB7 Servo L TIM_OC4Init( TIM3, &TIM_OCInitStruct ); // Channel 4 PB8 Servo R TIM_Cmd( TIM3, ENABLE ); /** Project Testing **/ while(1) // Do not exit { //Test 1 if(TIM3->CCR4>0){ for(i=0;i<16000000;i++); TIM3->CCR4 = 0; } /* //Test 2 TIM3->CCR4 = 168; for(i=0;i<16000000;i++); TIM3->CCR4 = 336; for(i=0;i<16000000;i++); TIM3->CCR4 = 252; for(i=0;i<16000000;i++); /* //Test 3 if (((brightness + n) >= 3000) || ((brightness + n) <= 0)) n = -n; // if brightness maximum/maximum change direction brightness += n; TIM3->CCR1 = brightness; // set brightness TIM3->CCR2 = 1000 - brightness; // set brightness TIM3->CCR3 = 3000 - brightness; // set brightness TIM3->CCR4 = 3000 - brightness; // set brightness for(i=0;i<10000;i++); // delay //Delay(250000); //Delay(1000000); //Delay(1000000); */ } return(0); // System will implode /** Others **/ if (STM_EVAL_PBGetState(BUTTON_USER) == Bit_SET) { if ((*(__IO uint32_t*) TESTRESULT_ADDRESS) == ALLTEST_PASS) { /* Waiting User Button is pressed or Test Program condition verified */ while ((STM_EVAL_PBGetState(BUTTON_USER) == Bit_SET)&&(TimingDelay != 0x00)) {} } else { /* Waiting User Button is Released or TimeOut*/ while ((STM_EVAL_PBGetState(BUTTON_USER) == Bit_SET)&&(TimingDelay != 0x00)) {} if (STM_EVAL_PBGetState(BUTTON_USER) == Bit_RESET) { } } if (TimingDelay == 0x00) { /* Turn off LEDs available on STM32F4-Discovery ------------------------*/ /* Waiting User Button is released */ while (STM_EVAL_PBGetState(BUTTON_USER) == Bit_SET) {} /* Unlocks the FLASH control register access */ FLASH_Unlock(); /* Move discovery kit to detect negative and positive acceleration values on X, Y and Z axis */ Accelerometer_MEMS_Test(); /* USB Hardware connection */ USB_Test(); /* Audio Hardware connection */ Audio_Test(); /* Microphone MEMS Hardware connection */ Microphone_MEMS_Test(); /* Write PASS code at last word in the flash memory */ FLASH_ProgramWord(TESTRESULT_ADDRESS, ALLTEST_PASS); while(1) { /* Toggle Green LED: signaling the End of the Test program */ STM_EVAL_LEDToggle(LED4); Delay(41999/2); } } else { Demo_Exec(); } } else { Demo_Exec(); } }
/** * @brief Main program. * @param None * @retval None */ int main(void) { RCC_ClocksTypeDef RCC_Clocks; /* Initialize LEDs and User_Button on STM32F4-Discovery --------------------*/ STM_EVAL_PBInit(BUTTON_USER, BUTTON_MODE_EXTI); STM_EVAL_LEDInit(LED4); STM_EVAL_LEDInit(LED3); STM_EVAL_LEDInit(LED5); STM_EVAL_LEDInit(LED6); /* SysTick end of count event each 10ms */ RCC_GetClocksFreq(&RCC_Clocks); SysTick_Config(RCC_Clocks.HCLK_Frequency / 100); if (STM_EVAL_PBGetState(BUTTON_USER) == Bit_SET) { /* Turn on LEDs available on STM32F4-Discovery ---------------------------*/ STM_EVAL_LEDOn(LED4); STM_EVAL_LEDOn(LED3); STM_EVAL_LEDOn(LED5); STM_EVAL_LEDOn(LED6); if ((*(__IO uint32_t*) TESTRESULT_ADDRESS) == ALLTEST_PASS) { TimingDelay = 300; /* Waiting User Button is pressed or Test Program condition verified */ while ((STM_EVAL_PBGetState(BUTTON_USER) == Bit_SET)&&(TimingDelay != 0x00)) {} } else { /* Waiting User Button is Released or TimeOut*/ TimingDelay = 300; while ((STM_EVAL_PBGetState(BUTTON_USER) == Bit_SET)&&(TimingDelay != 0x00)) {} if (STM_EVAL_PBGetState(BUTTON_USER) == Bit_RESET) { TimingDelay = 0x00; } } if (TimingDelay == 0x00) { /* Turn off LEDs available on STM32F4-Discovery ------------------------*/ STM_EVAL_LEDOff(LED4); STM_EVAL_LEDOff(LED3); STM_EVAL_LEDOff(LED5); STM_EVAL_LEDOff(LED6); /* Waiting User Button is released */ while (STM_EVAL_PBGetState(BUTTON_USER) == Bit_SET) {} /* Unlocks the FLASH control register access */ FLASH_Unlock(); /* Move discovery kit to detect negative and positive acceleration values on X, Y and Z axis */ Accelerometer_MEMS_Test(); /* USB Hardware connection */ USB_Test(); /* Audio Hardware connection */ Audio_Test(); /* Microphone MEMS Hardware connection */ Microphone_MEMS_Test(); /* Write PASS code at last word in the flash memory */ FLASH_ProgramWord(TESTRESULT_ADDRESS, ALLTEST_PASS); while(1) { /* Toggle Green LED: signaling the End of the Test program */ STM_EVAL_LEDToggle(LED4); Delay(10); } } else { Demo_Exec(); } } else { Demo_Exec(); } }
/** * @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_stm32f0xx.s) before to branch to application main. To reconfigure the default setting of SystemInit() function, refer to system_stm32f0xx.c file*/ /* Setup SysTick Timer for 1 msec interrupts.*/ if (SysTick_Config(SystemCoreClock / 1000)) { /* Capture error */ while (1); } /* Initialize LEDs, User Button on STM32F072B-DISCO board ***********/ STM_EVAL_PBInit(BUTTON_USER, BUTTON_MODE_EXTI); STM_EVAL_LEDInit(LED3); STM_EVAL_LEDInit(LED4); STM_EVAL_LEDInit(LED5); STM_EVAL_LEDInit(LED6); /* Initialize Mems Gyroscope */ Demo_GyroConfig(); /* Initialize USB Device */ USBD_Init(&USB_Device_dev, &USR_desc, &USBD_HID_cb, &USR_cb); /* Init STMTouch driver */ TSL_user_Init(); /* End of Initialisation */ /* Delay 1s to select Test Program or to go directly through the demo*/ Delay (1000); if (STM_EVAL_PBGetState(BUTTON_USER) == Bit_SET ) { /* Wait for User button is released */ while (STM_EVAL_PBGetState(BUTTON_USER) != Bit_RESET) {} /* Set ButtonPressed at 0 value */ ButtonPressed = 0; /* LED test : Blinking LEDs */ LED_Test(); /* Wait for User button to be pressed to switch to USB Test the cursor move in square path and led On corresponding to such direction */ USB_Test(); /* Move Discovery board to execute MEMS Test, Mems detect the angular rate and led On corresponding to such direction*/ MEMS_Test(); /* Wait for User button to be pressed to switch to Touch Sensor Test each TouchKey pointed correspond to specific Leds On, test can performed in both direction */ LTS_Test(); } /* Infinite loop */ while (1) { Demo(); } }