
GPIOC的13脚为用户按键,GPIOA的5脚为用户LED,当按键按下时LED灯亮,按键松开后,LED灯灭,用查询方式。以下为main()函数。, W1 r5 u/ P+ _: A /** ****************************************************************************** * File Name : main.c * Date : 26/12/2014 13:21:00+ s$ Q3 O3 p8 \3 x3 b9 j * Description : Main program body ******************************************************************************* x. k- L' Q$ x/ D- r: G1 g! o *+ R# @2 N0 t# Y9 `& X' l, ^/ q2 | * COPYRIGHT(c) 2014 STMicroelectronics' ^) m) y: ~* K/ N( C *' J( }9 ]. ~. Z- N$ j7 M. ^' G * Redistribution and use in source and binary forms, with or without modification, * are permitted provided that the following conditions are met: * 1. Redistributions of source code must retain the above copyright notice,4 ^, `( I- e5 N, y1 v: J2 p. e * this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice,3 O3 O: `6 y' h! D * this list of conditions and the following disclaimer in the documentation1 n! s* d3 V9 K! D * and/or other materials provided with the distribution." [6 D6 ~1 F# ^1 b& i, v5 o * 3. Neither the name of STMicroelectronics nor the names of its contributors* S" J. ?& |2 P3 X$ n; j9 z0 i) f * may be used to endorse or promote products derived from this software * without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"2 q# q( t7 f s2 `* c * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE! c) v* N* I" j * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER4 X% H- r, z& } * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE) @( T- Z4 m& q * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * ******************************************************************************; G" O1 {" V( T! V ]- W! L, x8 l */ /* Includes ------------------------------------------------------------------*/ #include "stm32f0xx_hal.h" 9 D8 |( f* L' \( B' t6 X0 i4 Q7 x /* USER CODE BEGIN Includes */3 u3 O7 H' I" m/ E1 y$ X* o /* USER CODE END Includes */8 m( ?1 K+ \8 u, ? * j) l. |4 A$ |( Y /* Private variables ---------------------------------------------------------*/ /* USER CODE BEGIN PV */( e$ w$ b; l: S: v5 ^: |. r /* USER CODE END PV */ /* Private function prototypes -----------------------------------------------*/. A- {0 t( X R" h void SystemClock_Config(void); static void MX_GPIO_Init(void);5 x- ^9 ~/ J& s) H: ?: y /* USER CODE BEGIN PFP */3 _1 r9 b2 A; Y0 ]5 A* @/ } /* USER CODE END PFP */ % n" V+ s' R1 p- G- ^9 E /* USER CODE BEGIN 0 */ /* USER CODE END 0 */ + w/ q% t; S* b; X, O3 B3 @. e int main(void) { : H- n- z4 f; J7 v /* USER CODE BEGIN 1 */ 0 p) a8 T2 C' R* v7 Z# R /* USER CODE END 1 */ /* MCU Configuration----------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* Configure the system clock */ SystemClock_Config(); ' j; N( E: E7 \9 H# V /* Initialize all configured peripherals */ MX_GPIO_Init();9 c! T" _. g- r6 e1 m/ v 2 P) M( X& [. W9 S) B /* USER CODE BEGIN 2 */$ |! g% p6 F1 h3 U& n( [- n : U- K+ Q9 C& V9 V /* USER CODE END 2 */ /* USER CODE BEGIN 3 */ /* Infinite loop */ 5 A8 t) ^- V+ L: f //ͨ¹ý¶ÁÈ¡PC13Òý½ÅµÄ״̬¿ØÖÆPA5 LEDµÄÁÁÃð 4 H9 I i' `) [/ r5 j; i& m! n ( {) ]! h+ ^: Y) L( _, H }! c 1 p! K; ?6 l+ J/ S while (1) { 9 P# T1 b+ h7 z; Q& q" F if(HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_13)==0)+ _% m+ C. {" M U |) t3 n$ V {8 ?$ z, `& w3 j) u+ }5 m* U HAL_Delay(10); if(HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_13)==0)% s0 a9 L9 p t9 }$ A' V7 K HAL_GPIO_WritePin(GPIOA,GPIO_PIN_5,GPIO_PIN_SET); }8 F6 R: i9 G4 Z& s" v% r, g else$ R4 J6 q9 g- u% S" o9 X HAL_GPIO_WritePin(GPIOA,GPIO_PIN_5,GPIO_PIN_RESET); % [) }( b3 Y# ^, r, P } 3 t$ }) C4 O- e |& Q } /* USER CODE END 3 */ + l# m5 e- B3 a * d* o! l) [/ n' W# e0 M /** System Clock Configuration */ void SystemClock_Config(void) K- M, n2 ^" u' f# j# K { - K U+ `3 w; F* R RCC_OscInitTypeDef RCC_OscInitStruct;6 U+ t) ~8 d ?4 B9 i$ Z; H RCC_ClkInitTypeDef RCC_ClkInitStruct;8 o$ x2 B5 Z. Z4 p8 j RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;4 a+ ? G& H1 ~8 h! X9 i RCC_OscInitStruct.HSIState = RCC_HSI_ON;/ w# {6 D5 ?- w& ?! _& L b RCC_OscInitStruct.HSICalibrationValue = 16;- B/ T1 \ r L RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; HAL_RCC_OscConfig(&RCC_OscInitStruct);. x, n3 M" L6 Q) [0 @ ( T8 { O+ a2 K' G- e& y) S1 w0 x RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_SYSCLK; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;' t/ _ C9 V8 y+ l8 P# `% l. G HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0); __SYSCFG_CLK_ENABLE();6 r; ~, K8 |8 J1 B& Y O 5 s4 n6 ?" |+ y2 U3 h }6 n1 O5 o2 T$ x$ \ # t7 a2 {) C4 I N) W /** Configure pins as * Analog + A3 s# h$ `: {: ?+ f2 T0 o1 k& e& K * Input * Output6 n% N9 E! j+ [$ n& Q * EVENT_OUT * EXTI */ void MX_GPIO_Init(void)6 T- }6 Z; A8 B {5 i9 C9 N2 o1 M! `5 j) O# R9 ^7 y 5 Q( U3 @4 l4 K, z( G7 J7 ~ GPIO_InitTypeDef GPIO_InitStruct; ( t7 j4 c6 b7 u5 r. t t; v /* GPIO Ports Clock Enable */ __GPIOC_CLK_ENABLE();, ~: ~' `% x! `1 x9 G __GPIOA_CLK_ENABLE(); /*Configure GPIO pin : PC13 */+ ?4 Y( L D+ O5 d GPIO_InitStruct.Pin = GPIO_PIN_13; GPIO_InitStruct.Mode = GPIO_MODE_INPUT;5 R- W1 ^( i" i, D3 K GPIO_InitStruct.Pull = GPIO_NOPULL;: b( }- _( `( C HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); # W/ i! p- p4 Y1 e* M5 \! D /*Configure GPIO pin : PA5 */# u6 `1 ]! B. Y GPIO_InitStruct.Pin = GPIO_PIN_5; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;! y$ Q2 o- j/ z7 _% ^ GPIO_InitStruct.Pull = GPIO_NOPULL;( S* P$ m1 D: i T GPIO_InitStruct.Speed = GPIO_SPEED_LOW; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);& N; Z* m! f8 r* V * K4 a8 M5 Q. D; U }. W5 W. W6 }% P ^ 0 Q. a1 n' c& ?" n# s /* USER CODE BEGIN 4 */# Z: i- i. r' T /* USER CODE END 4 */ : J" Z, j, w# c8 ~( y9 d: m. C. ` #ifdef USE_FULL_ASSERT /** * @brief Reports the name of the source file and the source line number * where the assert_param error has occurred. * @param file: pointer to the source file name1 O; C1 p1 S' A$ g, M1 C% Z * @param line: assert_param error line source number* T; ]1 Z; M7 ] * @retval None& w& N& e' u4 ~, U: L */5 `% K9 B8 p- r m void assert_failed(uint8_t* file, uint32_t line) {" u& P+ d9 h1 T( y- U0 Q. j /* USER CODE BEGIN 6 */+ M7 C$ O; X# I K2 F /* User can add his own implementation to report the file name and line number,( x0 ~7 b! }& I0 p4 P7 e: U ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ /* USER CODE END 6 */" D# E. z/ ^: V, b1 | }! ?4 J9 Q% H% o1 B" L9 x0 W : i) b) [7 q& T/ h; c #endif / A4 V( m. Q- w3 Z: y* p /** * @} */ # z% J9 }# \* ]. I 2 j* N( ]! m' Y! ]0 Y /**5 @$ W; m9 k# |6 I * @}. E1 h* L7 j/ D7 X2 v; b1 d */ ) |1 ?1 P) O& r4 g 0 H6 K( V) V1 G/ Y! j3 l, V /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ ) A& z( j$ h6 ? |
谢谢分享 |