게시글 본문내용
|
|
다음검색
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | #include "led.h" #include "key.h" #include "usart.h" #include "clock.h" #include "interrupt.h" #include "timer.h" int main(void) { LED_init(); KEY_init(); USART_1_Init(); CLOCK_init(); TIMER_init(); NVIC_Configuration(); // exti 0~3 setting ↓ EXTI_Configuration(); // set key 0~3 to exti TIM_Configuration(); Sensor_Init(); Motor_Init(); Motor_Stop(Motor_all); /* boot */ LED_On_LED0(); delay_1_second(); LED_On_LED1(); delay_1_second(); LED_On_LED2(); delay_1_second(); LED_On_LED3(); delay_1_second(); LED_Off_All(); /* run */ Motor_Go(Motor_all); Sensor_Start(); while(1) { int i=0, j=0; for(i=0;i<10;i++) { delay_1_second(); } } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 | ;/******************** (C) COPYRIGHT 2009 STMicroelectronics ******************** ;* File Name : startup_stm32f10x_md.s ;* Author : MCD Application Team ;* Version : V3.1.1 ;* Date : 07/27/2009 ;* Description : STM32F10x Medium Density Devices vector table for ;* EWARM5.x toolchain. ;* This module performs: ;* - Set the initial SP ;* - Set the initial PC == __iar_program_start, ;* - Set the vector table entries with the exceptions ISR ;* address. ;* After Reset the Cortex-M3 processor is in Thread mode, ;* priority is Privileged, and the Stack is set to Main. ;******************************************************************************** ;* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS ;* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE TIME. ;* AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY DIRECT, ;* INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE ;* CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE CODING ;* INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS. ;*******************************************************************************/ ; ; ; The modules in this file are included in the libraries, and may be replaced ; by any user-defined modules that define the PUBLIC symbol _program_start or ; a user defined start symbol. ; To override the cstartup defined in the library, simply add your modified ; version to the workbench project. ; ; The vector table is normally located at address 0. ; When debugging in RAM, it can be located in RAM, aligned to at least 2^6. ; The name "__vector_table" has special meaning for C-SPY: ; it is where the SP start value is found, and the NVIC vector ; table register (VTOR) is initialized to this address if != 0. ; ; Cortex-M version ; MODULE ?cstartup ;; Forward declaration of sections. SECTION CSTACK:DATA:NOROOT(3) SECTION .intvec:CODE:NOROOT(2) EXTERN __iar_program_start PUBLIC __vector_table DATA __vector_table DCD sfe(CSTACK) DCD __iar_program_start DCD NMI_Handler ; NMI Handler DCD HardFault_Handler ; Hard Fault Handler DCD MemManage_Handler ; MPU Fault Handler DCD BusFault_Handler ; Bus Fault Handler DCD UsageFault_Handler ; Usage Fault Handler DCD 0 ; Reserved DCD 0 ; Reserved DCD 0 ; Reserved DCD 0 ; Reserved DCD SVC_Handler ; SVCall Handler DCD DebugMon_Handler ; Debug Monitor Handler DCD 0 ; Reserved DCD PendSV_Handler ; PendSV Handler DCD SysTick_Handler ; SysTick Handler ; External Interrupts DCD WWDG_IRQHandler ; Window Watchdog DCD PVD_IRQHandler ; PVD through EXTI Line detect DCD TAMPER_IRQHandler ; Tamper DCD RTC_IRQHandler ; RTC DCD FLASH_IRQHandler ; Flash DCD RCC_IRQHandler ; RCC DCD EXTI0_IRQHandler ; EXTI Line 0 DCD EXTI1_IRQHandler ; EXTI Line 1 DCD EXTI2_IRQHandler ; EXTI Line 2 DCD EXTI3_IRQHandler ; EXTI Line 3 DCD EXTI4_IRQHandler ; EXTI Line 4 DCD DMA1_Channel1_IRQHandler ; DMA1 Channel 1 DCD DMA1_Channel2_IRQHandler ; DMA1 Channel 2 DCD DMA1_Channel3_IRQHandler ; DMA1 Channel 3 DCD DMA1_Channel4_IRQHandler ; DMA1 Channel 4 DCD DMA1_Channel5_IRQHandler ; DMA1 Channel 5 DCD DMA1_Channel6_IRQHandler ; DMA1 Channel 6 DCD DMA1_Channel7_IRQHandler ; DMA1 Channel 7 DCD ADC1_2_IRQHandler ; ADC1 & ADC2 DCD USB_HP_CAN1_TX_IRQHandler ; USB High Priority or CAN1 TX DCD USB_LP_CAN1_RX0_IRQHandler ; USB Low Priority or CAN1 RX0 DCD CAN1_RX1_IRQHandler ; CAN1 RX1 DCD CAN1_SCE_IRQHandler ; CAN1 SCE DCD EXTI9_5_IRQHandler ; EXTI Line 9..5 DCD TIM1_BRK_IRQHandler ; TIM1 Break DCD TIM1_UP_IRQHandler ; TIM1 Update DCD TIM1_TRG_COM_IRQHandler ; TIM1 Trigger and Commutation DCD TIM1_CC_IRQHandler ; TIM1 Capture Compare DCD TIM2_IRQHandler ; TIM2 DCD TIM3_IRQHandler ; TIM3 DCD TIM4_IRQHandler ; TIM4 DCD I2C1_EV_IRQHandler ; I2C1 Event DCD I2C1_ER_IRQHandler ; I2C1 Error DCD I2C2_EV_IRQHandler ; I2C2 Event DCD I2C2_ER_IRQHandler ; I2C2 Error DCD SPI1_IRQHandler ; SPI1 DCD SPI2_IRQHandler ; SPI2 DCD USART1_IRQHandler ; USART1 DCD USART2_IRQHandler ; USART2 DCD USART3_IRQHandler ; USART3 DCD EXTI15_10_IRQHandler ; EXTI Line 15..10 DCD RTCAlarm_IRQHandler ; RTC Alarm through EXTI Line DCD USBWakeUp_IRQHandler ; USB Wakeup from suspend ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; ;; Default interrupt handlers. ;; THUMB PUBWEAK NMI_Handler SECTION .text:CODE:REORDER:NOROOT(1) NMI_Handler B NMI_Handler PUBWEAK HardFault_Handler SECTION .text:CODE:REORDER:NOROOT(1) HardFault_Handler B HardFault_Handler PUBWEAK MemManage_Handler SECTION .text:CODE:REORDER:NOROOT(1) MemManage_Handler B MemManage_Handler PUBWEAK BusFault_Handler SECTION .text:CODE:REORDER:NOROOT(1) BusFault_Handler B BusFault_Handler PUBWEAK UsageFault_Handler SECTION .text:CODE:REORDER:NOROOT(1) UsageFault_Handler B UsageFault_Handler PUBWEAK SVC_Handler SECTION .text:CODE:REORDER:NOROOT(1) SVC_Handler B SVC_Handler PUBWEAK DebugMon_Handler SECTION .text:CODE:REORDER:NOROOT(1) DebugMon_Handler B DebugMon_Handler PUBWEAK PendSV_Handler SECTION .text:CODE:REORDER:NOROOT(1) PendSV_Handler B PendSV_Handler PUBWEAK SysTick_Handler SECTION .text:CODE:REORDER:NOROOT(1) SysTick_Handler B SysTick_Handler PUBWEAK WWDG_IRQHandler SECTION .text:CODE:REORDER:NOROOT(1) WWDG_IRQHandler B WWDG_IRQHandler PUBWEAK PVD_IRQHandler SECTION .text:CODE:REORDER:NOROOT(1) PVD_IRQHandler B PVD_IRQHandler PUBWEAK TAMPER_IRQHandler SECTION .text:CODE:REORDER:NOROOT(1) TAMPER_IRQHandler B TAMPER_IRQHandler PUBWEAK RTC_IRQHandler SECTION .text:CODE:REORDER:NOROOT(1) RTC_IRQHandler B RTC_IRQHandler PUBWEAK FLASH_IRQHandler SECTION .text:CODE:REORDER:NOROOT(1) FLASH_IRQHandler B FLASH_IRQHandler PUBWEAK RCC_IRQHandler SECTION .text:CODE:REORDER:NOROOT(1) RCC_IRQHandler B RCC_IRQHandler PUBWEAK EXTI0_IRQHandler SECTION .text:CODE:REORDER:NOROOT(1) EXTI0_IRQHandler B EXTI0_IRQHandler PUBWEAK EXTI1_IRQHandler SECTION .text:CODE:REORDER:NOROOT(1) EXTI1_IRQHandler B EXTI1_IRQHandler PUBWEAK EXTI2_IRQHandler SECTION .text:CODE:REORDER:NOROOT(1) EXTI2_IRQHandler B EXTI2_IRQHandler PUBWEAK EXTI3_IRQHandler SECTION .text:CODE:REORDER:NOROOT(1) EXTI3_IRQHandler B EXTI3_IRQHandler PUBWEAK EXTI4_IRQHandler SECTION .text:CODE:REORDER:NOROOT(1) EXTI4_IRQHandler B EXTI4_IRQHandler PUBWEAK DMA1_Channel1_IRQHandler SECTION .text:CODE:REORDER:NOROOT(1) DMA1_Channel1_IRQHandler B DMA1_Channel1_IRQHandler PUBWEAK DMA1_Channel2_IRQHandler SECTION .text:CODE:REORDER:NOROOT(1) DMA1_Channel2_IRQHandler B DMA1_Channel2_IRQHandler PUBWEAK DMA1_Channel3_IRQHandler SECTION .text:CODE:REORDER:NOROOT(1) DMA1_Channel3_IRQHandler B DMA1_Channel3_IRQHandler PUBWEAK DMA1_Channel4_IRQHandler SECTION .text:CODE:REORDER:NOROOT(1) DMA1_Channel4_IRQHandler B DMA1_Channel4_IRQHandler PUBWEAK DMA1_Channel5_IRQHandler SECTION .text:CODE:REORDER:NOROOT(1) DMA1_Channel5_IRQHandler B DMA1_Channel5_IRQHandler PUBWEAK DMA1_Channel6_IRQHandler SECTION .text:CODE:REORDER:NOROOT(1) DMA1_Channel6_IRQHandler B DMA1_Channel6_IRQHandler PUBWEAK DMA1_Channel7_IRQHandler SECTION .text:CODE:REORDER:NOROOT(1) DMA1_Channel7_IRQHandler B DMA1_Channel7_IRQHandler PUBWEAK ADC1_2_IRQHandler SECTION .text:CODE:REORDER:NOROOT(1) ADC1_2_IRQHandler B ADC1_2_IRQHandler PUBWEAK USB_HP_CAN1_TX_IRQHandler SECTION .text:CODE:REORDER:NOROOT(1) USB_HP_CAN1_TX_IRQHandler B USB_HP_CAN1_TX_IRQHandler PUBWEAK USB_LP_CAN1_RX0_IRQHandler SECTION .text:CODE:REORDER:NOROOT(1) USB_LP_CAN1_RX0_IRQHandler B USB_LP_CAN1_RX0_IRQHandler PUBWEAK CAN1_RX1_IRQHandler SECTION .text:CODE:REORDER:NOROOT(1) CAN1_RX1_IRQHandler B CAN1_RX1_IRQHandler PUBWEAK CAN1_SCE_IRQHandler SECTION .text:CODE:REORDER:NOROOT(1) CAN1_SCE_IRQHandler B CAN1_SCE_IRQHandler PUBWEAK EXTI9_5_IRQHandler SECTION .text:CODE:REORDER:NOROOT(1) EXTI9_5_IRQHandler B EXTI9_5_IRQHandler PUBWEAK TIM1_BRK_IRQHandler SECTION .text:CODE:REORDER:NOROOT(1) TIM1_BRK_IRQHandler B TIM1_BRK_IRQHandler PUBWEAK TIM1_UP_IRQHandler SECTION .text:CODE:REORDER:NOROOT(1) TIM1_UP_IRQHandler B TIM1_UP_IRQHandler PUBWEAK TIM1_TRG_COM_IRQHandler SECTION .text:CODE:REORDER:NOROOT(1) TIM1_TRG_COM_IRQHandler B TIM1_TRG_COM_IRQHandler PUBWEAK TIM1_CC_IRQHandler SECTION .text:CODE:REORDER:NOROOT(1) TIM1_CC_IRQHandler B TIM1_CC_IRQHandler PUBWEAK TIM2_IRQHandler SECTION .text:CODE:REORDER:NOROOT(1) TIM2_IRQHandler B TIM2_IRQHandler PUBWEAK TIM3_IRQHandler SECTION .text:CODE:REORDER:NOROOT(1) TIM3_IRQHandler B TIM3_IRQHandler PUBWEAK TIM4_IRQHandler SECTION .text:CODE:REORDER:NOROOT(1) TIM4_IRQHandler B TIM4_IRQHandler PUBWEAK I2C1_EV_IRQHandler SECTION .text:CODE:REORDER:NOROOT(1) I2C1_EV_IRQHandler B I2C1_EV_IRQHandler PUBWEAK I2C1_ER_IRQHandler SECTION .text:CODE:REORDER:NOROOT(1) I2C1_ER_IRQHandler B I2C1_ER_IRQHandler PUBWEAK I2C2_EV_IRQHandler SECTION .text:CODE:REORDER:NOROOT(1) I2C2_EV_IRQHandler B I2C2_EV_IRQHandler PUBWEAK I2C2_ER_IRQHandler SECTION .text:CODE:REORDER:NOROOT(1) I2C2_ER_IRQHandler B I2C2_ER_IRQHandler PUBWEAK SPI1_IRQHandler SECTION .text:CODE:REORDER:NOROOT(1) SPI1_IRQHandler B SPI1_IRQHandler PUBWEAK SPI2_IRQHandler SECTION .text:CODE:REORDER:NOROOT(1) SPI2_IRQHandler B SPI2_IRQHandler PUBWEAK USART1_IRQHandler SECTION .text:CODE:REORDER:NOROOT(1) USART1_IRQHandler B USART1_IRQHandler PUBWEAK USART2_IRQHandler SECTION .text:CODE:REORDER:NOROOT(1) USART2_IRQHandler B USART2_IRQHandler PUBWEAK USART3_IRQHandler SECTION .text:CODE:REORDER:NOROOT(1) USART3_IRQHandler B USART3_IRQHandler PUBWEAK EXTI15_10_IRQHandler SECTION .text:CODE:REORDER:NOROOT(1) EXTI15_10_IRQHandler B EXTI15_10_IRQHandler PUBWEAK RTCAlarm_IRQHandler SECTION .text:CODE:REORDER:NOROOT(1) RTCAlarm_IRQHandler B RTCAlarm_IRQHandler PUBWEAK USBWakeUp_IRQHandler SECTION .text:CODE:REORDER:NOROOT(1) USBWakeUp_IRQHandler B USBWakeUp_IRQHandler END /******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/ | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | #ifndef _PLATFORM_CONFIG_H_ #define _PLATFORM_CONFIG_H_ #include "stdio.h" #include <stdint.h> #define PERIPH_BASE ((uint32_t)0x40000000) #define APB1PERIPH_BASE PERIPH_BASE #define APB2PERIPH_BASE (PERIPH_BASE + 0x10000) #define AHBPERIPH_BASE (PERIPH_BASE + 0x20000) #define RCC_BASE (AHBPERIPH_BASE + 0x1000) #define RCC ((RCC_TypeDef*)RCC_BASE) #define __I volatile /* read only permissions */ #define __O volatile /* write only permissions */ #define __IO volatile /* read / write permissions */ #define AFIO_BASE (APB2PERIPH_BASE + 0x0000) #define EXTI_BASE (APB2PERIPH_BASE + 0x0400) #define GPIO_Pin_0 ((uint16_t)0x0001) #define GPIO_Pin_1 ((uint16_t)0x0002) #define GPIO_Pin_2 ((uint16_t)0x0004) #define GPIO_Pin_3 ((uint16_t)0x0008) #define GPIO_Pin_4 ((uint16_t)0x0010) #define GPIO_Pin_5 ((uint16_t)0x0020) #define GPIO_Pin_6 ((uint16_t)0x0040) #define GPIO_Pin_7 ((uint16_t)0x0080) #define GPIO_Pin_8 ((uint16_t)0x0100) #define GPIO_Pin_9 ((uint16_t)0x0200) #define GPIO_Pin_10 ((uint16_t)0x0400) #define GPIO_Pin_11 ((uint16_t)0x0800) #define GPIO_Pin_12 ((uint16_t)0x1000) #define GPIO_Pin_13 ((uint16_t)0x2000) #define GPIO_Pin_14 ((uint16_t)0x4000) #define GPIO_Pin_15 ((uint16_t)0x8000) #define GPIO_Pin_All ((uint16_t)0xFFFF) #define GPIOA_BASE (APB2PERIPH_BASE + 0x0800) #define GPIOB_BASE (APB2PERIPH_BASE + 0x0C00) #define GPIOC_BASE (APB2PERIPH_BASE + 0x1000) #define GPIOD_BASE (APB2PERIPH_BASE + 0x1400) #define GPIOE_BASE (APB2PERIPH_BASE + 0x1800) #define GPIOF_BASE (APB2PERIPH_BASE + 0x1C00) #define GPIOG_BASE (APB2PERIPH_BASE + 0x2000) #define GPIOA ((GPIO_TypeDef *) GPIOA_BASE) #define GPIOB ((GPIO_TypeDef *) GPIOB_BASE) #define GPIOC ((GPIO_TypeDef *) GPIOC_BASE) #define GPIOD ((GPIO_TypeDef *) GPIOD_BASE) #define GPIOE ((GPIO_TypeDef *) GPIOE_BASE) #define GPIOF ((GPIO_TypeDef *) GPIOF_BASE) #define GPIOG ((GPIO_TypeDef *) GPIOG_BASE) #define GPIO_PortSourceGPIOA ((uint8_t)0x00) #define GPIO_PortSourceGPIOB ((uint8_t)0x01) #define GPIO_PortSourceGPIOC ((uint8_t)0x02) #define GPIO_PortSourceGPIOD ((uint8_t)0x03) #define GPIO_PortSourceGPIOE ((uint8_t)0x04) #define GPIO_PortSourceGPIOF ((uint8_t)0x05) #define GPIO_PortSourceGPIOG ((uint8_t)0x06) #define GPIO_PinSource0 ((uint8_t)0x00) #define GPIO_PinSource1 ((uint8_t)0x01) #define GPIO_PinSource2 ((uint8_t)0x02) #define GPIO_PinSource3 ((uint8_t)0x03) #define GPIO_PinSource4 ((uint8_t)0x04) #define GPIO_PinSource5 ((uint8_t)0x05) #define GPIO_PinSource6 ((uint8_t)0x06) #define GPIO_PinSource7 ((uint8_t)0x07) #define GPIO_PinSource8 ((uint8_t)0x08) #define GPIO_PinSource9 ((uint8_t)0x09) #define GPIO_PinSource10 ((uint8_t)0x0A) #define GPIO_PinSource11 ((uint8_t)0x0B) #define GPIO_PinSource12 ((uint8_t)0x0C) #define GPIO_PinSource13 ((uint8_t)0x0D) #define GPIO_PinSource14 ((uint8_t)0x0E) #define GPIO_PinSource15 ((uint8_t)0x0F) #define EXTI_Line0 ((uint32_t)0x00001) /*< External interrupt line 0 */ #define EXTI_Line1 ((uint32_t)0x00002) /*< External interrupt line 1 */ #define EXTI_Line2 ((uint32_t)0x00004) /*< External interrupt line 2 */ #define EXTI_Line3 ((uint32_t)0x00008) /*< External interrupt line 3 */ #define EXTI_Line4 ((uint32_t)0x00010) /*< External interrupt line 4 */ #define EXTI_Line5 ((uint32_t)0x00020) /*< External interrupt line 5 */ #define EXTI_Line6 ((uint32_t)0x00040) /*< External interrupt line 6 */ #define EXTI_Line7 ((uint32_t)0x00080) /*< External interrupt line 7 */ #define EXTI_Line8 ((uint32_t)0x00100) /*< External interrupt line 8 */ #define EXTI_Line9 ((uint32_t)0x00200) /*< External interrupt line 9 */ #define EXTI_Line10 ((uint32_t)0x00400) /*< External interrupt line 10 */ #define EXTI_Line11 ((uint32_t)0x00800) /*< External interrupt line 11 */ #define EXTI_Line12 ((uint32_t)0x01000) /*< External interrupt line 12 */ #define EXTI_Line13 ((uint32_t)0x02000) /*< External interrupt line 13 */ #define EXTI_Line14 ((uint32_t)0x04000) /*< External interrupt line 14 */ #define EXTI_Line15 ((uint32_t)0x08000) /*< External interrupt line 15 */ #define EXTI_Line16 ((uint32_t)0x10000) /*< External interrupt line 16 Connected to the PVD Output */ #define EXTI_Line17 ((uint32_t)0x20000) /*< External interrupt line 17 Connected to the RTC Alarm event */ #define EXTI_Line18 ((uint32_t)0x40000) /*< External interrupt line 18 Connected to the USB Device/USB OTG FS Wakeup from suspend event */ #define EXTI_Line19 ((uint32_t)0x80000) /*< External interrupt line 19 Connected to the Ethernet Wakeup event */ #define RCC_APB2Periph_AFIO ((uint32_t)0x00000001) #define RCC_APB2Periph_GPIOA ((uint32_t)0x00000004) #define RCC_APB2Periph_GPIOB ((uint32_t)0x00000008) #define RCC_APB2Periph_GPIOC ((uint32_t)0x00000010) #define RCC_APB2Periph_GPIOD ((uint32_t)0x00000020) #define RCC_APB2Periph_GPIOE ((uint32_t)0x00000040) #define RCC_APB2Periph_GPIOF ((uint32_t)0x00000080) #define RCC_APB2Periph_GPIOG ((uint32_t)0x00000100) #define RCC_APB2Periph_ADC1 ((uint32_t)0x00000200) #define RCC_APB2Periph_ADC2 ((uint32_t)0x00000400) #define RCC_APB2Periph_TIM1 ((uint32_t)0x00000800) #define RCC_APB2Periph_SPI1 ((uint32_t)0x00001000) #define RCC_APB2Periph_TIM8 ((uint32_t)0x00002000) #define RCC_APB2Periph_USART1 ((uint32_t)0x00004000) #define RCC_APB2Periph_ADC3 ((uint32_t)0x00008000) typedef struct { __IO uint32_t CRL; __IO uint32_t CRH; __IO uint32_t IDR; __IO uint32_t ODR; __IO uint32_t BSRR; __IO uint32_t BRR; __IO uint32_t LCKR; } GPIO_TypeDef; typedef enum { GPIO_Input_Mode = 0, GPIO_Speed_10MHz = 1, GPIO_Speed_2MHz, GPIO_Speed_50MHz }GPIOSpeed_TypeDef; typedef enum { GPIO_Mode_AIN = 0x0, GPIO_Mode_IN_FLOATING = 0x04, GPIO_Mode_IPD = 0x28, GPIO_Mode_IPU = 0x48, GPIO_Mode_Out_OD = 0x14, GPIO_Mode_Out_PP = 0x10, GPIO_Mode_AF_OD = 0x1C, GPIO_Mode_AF_PP = 0x18 }GPIOMode_TypeDef; typedef struct { uint16_t GPIO_Pin; GPIOSpeed_TypeDef GPIO_Speed; GPIOMode_TypeDef GPIO_Mode; }GPIO_InitTypeDef; typedef enum { Bit_RESET = 0, Bit_SET }BitAction; typedef enum { RESET = 0, SET = !RESET } FlagStatus, ITStatus; typedef struct { __IO uint32_t CR; __IO uint32_t CFGR; __IO uint32_t CIR; __IO uint32_t APB2RSTR; __IO uint32_t APB1RSTR; __IO uint32_t AHBENR; __IO uint32_t APB2ENR; __IO uint32_t APB1ENR; __IO uint32_t BDCR; __IO uint32_t CSR; }RCC_TypeDef; typedef enum {DISABLE = 0, ENABLE = !DISABLE} FunctionalState; static void delay_int_count(volatile unsigned int nTime); // ↓↓↓↓↓ Not precise if "clock.h" is included void delay_1_second(void); // use delay_1_second_it() void delay_100_milli_second(void); // use delay_100_milli_second_it() void _delay_ms_(int count); // ↑↑↑↑↑ use Delay(int count) void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct); #endif | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | #include "platform_config.h" #define DELAY_COUNT 2000000 //8065960 static void delay_int_count(volatile unsigned int nTime) { for(;nTime>0;nTime--); } void delay_1_second(void) { delay_int_count(DELAY_COUNT); } void delay_100_milli_second(void) { delay_int_count(DELAY_COUNT/10); } void _delay_ms_(int count) { int tmp = count * DELAY_COUNT / 1000; delay_int_count(tmp); } void GPIO_Init(GPIO_TypeDef* GPIOx, GPIO_InitTypeDef* GPIO_InitStruct) { uint32_t currentmode = 0x00, currentpin = 0x00, pinpos = 0x00, pos = 0x00; uint32_t tmpreg = 0x00, pinmask = 0x00; /*---------------------------- GPIO Mode Configuration -----------------------*/ currentmode = ((uint32_t)GPIO_InitStruct->GPIO_Mode) & ((uint32_t)0x0F); if ((((uint32_t)GPIO_InitStruct->GPIO_Mode) & ((uint32_t)0x10)) != 0x00) { /* Output mode */ currentmode |= (uint32_t)GPIO_InitStruct->GPIO_Speed; } /*---------------------------- GPIO CRL Configuration ------------------------*/ /* Configure the eight low port pins */ if (((uint32_t)GPIO_InitStruct->GPIO_Pin & ((uint32_t)0x00FF)) != 0x00) { tmpreg = GPIOx->CRL; for (pinpos = 0x00; pinpos < 0x08; pinpos++) { pos = ((uint32_t)0x01) << pinpos; /* Get the port pins position */ currentpin = (GPIO_InitStruct->GPIO_Pin) & pos; if (currentpin == pos) { pos = pinpos << 2; /* Clear the corresponding low control register bits */ pinmask = ((uint32_t)0x0F) << pos; tmpreg &= ~pinmask; /* Write the mode configuration in the corresponding bits */ tmpreg |= (currentmode << pos); /* Reset the corresponding ODR bit */ if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPD) { GPIOx->BRR = (((uint32_t)0x01) << pinpos); } else { /* Set the corresponding ODR bit */ if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPU) { GPIOx->BSRR = (((uint32_t)0x01) << pinpos); } } } } GPIOx->CRL = tmpreg; } /*---------------------------- GPIO CRH Configuration ------------------------*/ /* Configure the eight high port pins */ if (GPIO_InitStruct->GPIO_Pin > 0x00FF) { tmpreg = GPIOx->CRH; for (pinpos = 0x00; pinpos < 0x08; pinpos++) { pos = (((uint32_t)0x01) << (pinpos + 0x08)); /* Get the port pins position */ currentpin = ((GPIO_InitStruct->GPIO_Pin) & pos); if (currentpin == pos) { pos = pinpos << 2; /* Clear the corresponding high control register bits */ pinmask = ((uint32_t)0x0F) << pos; tmpreg &= ~pinmask; /* Write the mode configuration in the corresponding bits */ tmpreg |= (currentmode << pos); /* Reset the corresponding ODR bit */ if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPD) { GPIOx->BRR = (((uint32_t)0x01) << (pinpos + 0x08)); } /* Set the corresponding ODR bit */ if (GPIO_InitStruct->GPIO_Mode == GPIO_Mode_IPU) { GPIOx->BSRR = (((uint32_t)0x01) << (pinpos + 0x08)); } } } GPIOx->CRH = tmpreg; } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | #ifndef _LED_H_ #define _LED_H_ #include "platform_config.h" #define GPIO_LED GPIOC #define GPIO_LED0_PIN GPIO_Pin_6 #define GPIO_LED1_PIN GPIO_Pin_7 #define GPIO_LED2_PIN GPIO_Pin_8 #define GPIO_LED3_PIN GPIO_Pin_9 #define GPIO_LED_All_PIN GPIO_LED0_PIN|GPIO_LED1_PIN|GPIO_LED2_PIN|GPIO_LED3_PIN void LED_init(void); void LED_On_LED0(void); void LED_Off_LED0(void); void LED_On_LED1(void); void LED_Off_LED1(void); void LED_On_LED2(void); void LED_Off_LED2(void); void LED_On_LED3(void); void LED_Off_LED3(void); void LED_On_All(void); void LED_Off_All(void); void LED_OnOff_All(uint32_t count, int time_ms); #endif | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | #include "led.h" void LED_init(void){ RCC->APB2ENR |= RCC_APB2Periph_GPIOC; GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = GPIO_LED_All_PIN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIO_LED,&GPIO_InitStructure); } void LED_On_LED0(void){ GPIO_LED->BSRR |= GPIO_LED0_PIN; } void LED_Off_LED0(void){ GPIO_LED->BRR |= GPIO_LED0_PIN; } void LED_On_LED1(void){ GPIO_LED->BSRR |= GPIO_LED1_PIN; } void LED_Off_LED1(void){ GPIO_LED->BRR |= GPIO_LED1_PIN; } void LED_On_LED2(void){ GPIO_LED->BSRR |= GPIO_LED2_PIN; } void LED_Off_LED2(void){ GPIO_LED->BRR |= GPIO_LED2_PIN; } void LED_On_LED3(void){ GPIO_LED->BSRR |= GPIO_LED3_PIN; } void LED_Off_LED3(void){ GPIO_LED->BRR |= GPIO_LED3_PIN; } void LED_On_All(void){ LED_On_LED0(); LED_On_LED1(); LED_On_LED2(); LED_On_LED3(); } void LED_Off_All(void){ LED_Off_LED0(); LED_Off_LED1(); LED_Off_LED2(); LED_Off_LED3(); } void LED_OnOff_All(uint32_t count, int time_ms){ for(;count>0;count--){ LED_On_All(); _delay_ms_(time_ms); LED_Off_All(); _delay_ms_(time_ms); } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | #ifndef _KEY_H_ #define _KEY_H_ #include "platform_config.h" #define GPIO_KEY GPIOC #define GPIO_KEY0_PIN GPIO_Pin_0 #define GPIO_KEY1_PIN GPIO_Pin_1 #define GPIO_KEY2_PIN GPIO_Pin_2 #define GPIO_KEY3_PIN GPIO_Pin_3 #define GPIO_KEY_ALL_PIN GPIO_KEY0_PIN|GPIO_KEY1_PIN|GPIO_KEY2_PIN|GPIO_KEY3_PIN void KEY_init(void); uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin); int KEY_GetKey(void); #endif | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | #include "key.h" void KEY_init(void){ RCC->APB2ENR |= RCC_APB2Periph_GPIOB; GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = GPIO_KEY_ALL_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIO_KEY,&GPIO_InitStructure); } uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin){ uint8_t bitstatus = 0x00; if((GPIOx->IDR & GPIO_Pin) != (uint32_t)Bit_RESET){ bitstatus = (uint8_t) Bit_SET; } else{ bitstatus = (uint8_t) Bit_RESET; } return bitstatus; } int KEY_GetKey(void){ int data =0; if(GPIO_ReadInputDataBit(GPIO_KEY,GPIO_KEY0_PIN) == Bit_SET)data |= 0x01; else data &= ~0x01; if(GPIO_ReadInputDataBit(GPIO_KEY,GPIO_KEY1_PIN) == Bit_SET)data |= 0x02; else data &= ~0x02; if(GPIO_ReadInputDataBit(GPIO_KEY,GPIO_KEY2_PIN) == Bit_SET)data |= 0x04; else data &= ~0x04; if(GPIO_ReadInputDataBit(GPIO_KEY,GPIO_KEY3_PIN) == Bit_SET)data |= 0x08; else data &= ~0x08; return data; } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 | #ifndef _USART_H_ #define _USART_H_ #include "platform_config.h" #define USART1_BASE (APB2PERIPH_BASE + 0x3800) #define USART1 ((USART_TypeDef*)USART1_BASE) #define GPIO_USART GPIOA #define GPIO_USART_Rx_Pin GPIO_Pin_10 #define GPIO_USART_Tx_Pin GPIO_Pin_9 #define USART_WordLength_8b ((uint16_t)0x0000) #define USART_WordLength_9b ((uint16_t)0x1000) #define USART_StopBits_1 ((uint16_t)0x0000) #define USART_StopBits_0_5 ((uint16_t)0x1000) #define USART_StopBits_2 ((uint16_t)0x2000) #define USART_StopBits_1_5 ((uint16_t)0x3000) #define USART_Parity_No ((uint16_t)0x0000) #define USART_Parity_Even ((uint16_t)0x0400) #define USART_Parity_Odd ((uint16_t)0x0600) #define USART_HardwareFlowControl_None ((uint16_t)0x0000) #define USART_HardwareFlowControl_RTS ((uint16_t)0x0100) #define USART_HardwareFlowControl_CTS ((uint16_t)0x0200) #define USART_HardwareFlowControl_RTS_CTS ((uint16_t)0x0300) #define USART_Mode_Rx ((uint16_t)0x0004) #define USART_Mode_Tx ((uint16_t)0x0008) #define CR1_UE_Set ((uint16_t)0x2000) #define CR2_STOP_CLEAR_Mask ((uint16_t)0xCFFF) #define CR1_CLEAR_Mask ((uint16_t)0xE9F3) #define CR3_CLEAR_Mask ((uint16_t)0xFCFF) #define USART_FLAG_CTS ((uint16_t)0x0200) #define USART_FLAG_LBD ((uint16_t)0x0100) #define USART_FLAG_TXE ((uint16_t)0x0080) #define USART_FLAG_TC ((uint16_t)0x0040) #define USART_FLAG_RXNE ((uint16_t)0x0020) #define USART_FLAG_IDLE ((uint16_t)0x0010) #define USART_FLAG_ORE ((uint16_t)0x0008) #define USART_FLAG_NE ((uint16_t)0x0004) #define USART_FLAG_FE ((uint16_t)0x0002) #define USART_FLAG_PE ((uint16_t)0x0001) typedef struct { __IO uint16_t SR; uint16_t RESERVED0; __IO uint16_t DR; uint16_t RESERVED1; __IO uint16_t BRR; uint16_t RESERVED2; __IO uint16_t CR1; uint16_t RESERVED3; __IO uint16_t CR2; uint16_t RESERVED4; __IO uint16_t CR3; uint16_t RESERVED5; __IO uint16_t GTPR; uint16_t RESERVED6; }USART_TypeDef; typedef struct { uint32_t USART_BaudRate; uint16_t USART_WordLength; uint16_t USART_StopBits; uint16_t USART_Parity; uint16_t USART_Mode; uint16_t USART_HardwareFlowControl; } USART_InitTypeDef; void USART_1_Init(void); void USART_Init(USART_TypeDef* USARTx, USART_InitTypeDef* USART_InitStruct); void Serial_PutString(uint8_t *s); void USART_SendData(USART_TypeDef* USARTx, uint16_t Data); FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint16_t USART_FLAG); void SerialPutChar(uint8_t c); int fputc(int ch, FILE *f); uint16_t USART_RecieveData(USART_TypeDef* USARTx); uint8_t USART_GetCharacter(USART_TypeDef* usart_p); #endif | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | #include "usart.h" void USART_1_Init(void) { /* *** GPIO setting *** */ RCC->APB2ENR |= RCC_APB2Periph_GPIOA; RCC->APB2ENR |= RCC_APB2Periph_USART1; GPIO_InitTypeDef GPIO_InitStructure; //Tx GPIO_InitStructure.GPIO_Pin = GPIO_USART_Tx_Pin; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIO_USART,&GPIO_InitStructure); //Rx GPIO_InitStructure.GPIO_Pin = GPIO_USART_Rx_Pin; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIO_USART,&GPIO_InitStructure); /* *** USART setting *** */ USART_InitTypeDef USART_InitStructure; 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_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; /*Configure the USARTx*/ USART_Init(USART1, &USART_InitStructure); /*Enable the USART1*/ USART1->CR1 |= CR1_UE_Set; } void USART_Init(USART_TypeDef* USARTx, USART_InitTypeDef* USART_InitStruct) { uint32_t tmpreg = 0x00; uint32_t apbclock = 72000000; uint32_t integerdivider = 0x00; uint32_t fractionaldivider = 0x00; /* --- USART CR2 Configuration --- set 1 stop bit */ tmpreg = USARTx->CR2; /* Clear STOP[13:12] bits */ tmpreg &= CR2_STOP_CLEAR_Mask; tmpreg |= (uint32_t)USART_InitStruct->USART_StopBits; USARTx->CR2 = (uint16_t)tmpreg; /* --- USART CR1 Configuration --- set WordLength,Parity,Mode */ tmpreg = USARTx->CR1; /* Clear M, PCE, PS, TE and RE bits */ tmpreg &= CR1_CLEAR_Mask; tmpreg |= (uint32_t) USART_InitStruct->USART_WordLength | USART_InitStruct->USART_Parity | USART_InitStruct->USART_Mode; USARTx->CR1 = (uint16_t)tmpreg; /* --- USART CR3 Configuration --- set Flow Control */ tmpreg = USARTx->CR3; /* Clear CTSE and RTSE bits */ tmpreg &= CR3_CLEAR_Mask; tmpreg |= USART_InitStruct->USART_HardwareFlowControl; USARTx->CR3 = (uint16_t)tmpreg; /* USART BRR Configuration : Baud rate config */ /* intager part*/ integerdivider = ((0x19 * apbclock) / (0x04 * (USART_InitStruct->USART_BaudRate))); tmpreg = (integerdivider / 0x64) << 0x04; /*fractional part*/ fractionaldivider = integerdivider - (0x64 * (tmpreg >> 0x04)); tmpreg |= ((((fractionaldivider * 0x10) + 0x32) / 0x64 )) & ((uint8_t)0x0F); USARTx->BRR = (uint16_t)tmpreg; } void Serial_PutString(uint8_t *s) { while(*s != '\0') { SerialPutChar(*s); s++; } } void USART_SendData(USART_TypeDef* USARTx, uint16_t Data) { USARTx->DR = (Data & (uint16_t)0x01FF); } FlagStatus USART_GetFlagStatus(USART_TypeDef* USARTx, uint16_t USART_FLAG) { FlagStatus bitstatus = RESET; if ((USARTx->SR & USART_FLAG) != (uint16_t)RESET) { bitstatus = SET; } else { bitstatus = RESET; } return bitstatus; } void SerialPutChar(uint8_t c) { USART_SendData(USART1, c); while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); } int fputc(int ch, FILE *f) { if(ch == '\n') { USART_SendData(USART1, '\r'); while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); USART_SendData(USART1, '\n'); } else { USART_SendData(USART1, (uint8_t) ch); } while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); return ch; } uint16_t USART_RecieveData(USART_TypeDef* USARTx) { return (uint16_t)(USARTx->DR & ((uint16_t)0x01FF)); } uint8_t USART_GetCharacter(USART_TypeDef* usart_p) { uint8_t data; while(USART_GetFlagStatus(usart_p, USART_FLAG_RXNE) == RESET); data = USART_RecieveData(usart_p); /* Echo */ USART_SendData(usart_p, data); while(USART_GetFlagStatus(usart_p, USART_FLAG_TXE) == RESET); if(data == '\r') {return (int)('\n');} else {return data;} } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | #ifndef _CLOCK_H_ #define _CLOCK_H_ #include "platform_config.h" #define FLASH_R_BASE (AHBPERIPH_BASE + 0x2000) #define FLASH ((FLASH_TypeDef *) FLASH_R_BASE) #define RCC_CR_HSEON ((uint32_t)0x00010000) /* high speed clock enable */ #define RCC_CR_HSERDY ((uint32_t)0x00020000) /* ↑ ready flag */ #define HSEStartUp_TimeOut ((uint16_t)0x0500) /* ↑ check ready flag */ #define FLASH_ACR_LATENCY_2 ((uint8_t)0x02) #define RCC_CFGR_HPRE_DIV1 ((uint32_t)0x00000000) /*!< SYSCLK not divided */ #define RCC_CFGR_PPRE2_DIV1 ((uint32_t)0x00000000) /*!< HCLK not divided */ #define RCC_CFGR_PPRE1_DIV2 ((uint32_t)0x00000400) /*!< HCLK divided by 2 */ #define RCC_CFGR_PLLSRC ((uint32_t)0x00010000) /*!< PLL entry clock source */ #define RCC_CFGR_PLLXTPRE ((uint32_t)0x00020000) /*!< HSE divider for PLL entry */ #define RCC_CFGR_PLLMULL ((uint32_t)0x003C0000) /*!< PLLMUL[3:0] bits (PLL multiplication factor) */ #define RCC_CFGR_PLLSRC_HSE ((uint32_t)0x00010000) /*!< HSE clock selected as PLL entry clock source */ #define RCC_CFGR_PLLMULL6 ((uint32_t)0x00100000) /*!< PLL input clock*6 */ #define RCC_CR_PLLON ((uint32_t)0x01000000) /*!< PLL enable */ #define RCC_CR_PLLRDY ((uint32_t)0x02000000) /*!< PLL clock ready flag */ #define RCC_CFGR_SW ((uint32_t)0x00000003) /*!< SW[1:0] bits (System clock Switch) */ #define RCC_CFGR_SW_PLL ((uint32_t)0x00000002) /*!< PLL selected as system clock */ #define RCC_CFGR_SWS ((uint32_t)0x0000000C) /*!< SWS[1:0] bits (System Clock Switch Status) */ #define CFGR_SWS_Mask ((uint32_t)0x0000000C) #define CFGR_PLLMull_Mask ((uint32_t)0x003C0000) #define CFGR_PLLSRC_Mask ((uint32_t)0x00010000) #define CFGR_PLLXTPRE_Mask ((uint32_t)0x00020000) #define CFGR_HPRE_Set_Mask ((uint32_t)0x000000F0) #define CFGR_PPRE1_Set_Mask ((uint32_t)0x00000700) #define CFGR_PPRE2_Set_Mask ((uint32_t)0x00003800) #define CFGR_ADCPRE_Set_Mask ((uint32_t)0x0000C000) #define HSI_Value ((uint32_t)8000000) #define HSE_Value ((uint32_t)12000000) static __IO uint8_t APBAHBPrescTable[16] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 2, 3, 4, 6, 7, 8, 9}; static __IO uint8_t ADCPrescTable[4] = {2, 4, 6, 8}; typedef struct { __IO uint32_t ACR; __IO uint32_t KEYR; __IO uint32_t OPTKEYR; __IO uint32_t SR; __IO uint32_t CR; __IO uint32_t AR; __IO uint32_t RESERVED; __IO uint32_t OBR; __IO uint32_t WRPR; } FLASH_TypeDef; typedef struct { uint32_t SYSCLK_Frequency; /*!< returns SYSCLK clock frequency expressed in Hz */ uint32_t HCLK_Frequency; /*!< returns HCLK clock frequency expressed in Hz */ uint32_t PCLK1_Frequency; /*!< returns PCLK1 clock frequency expressed in Hz */ uint32_t PCLK2_Frequency; /*!< returns PCLK2 clock frequency expressed in Hz */ uint32_t ADCCLK_Frequency; /*!< returns ADCCLK clock frequency expressed in Hz */ }RCC_ClocksTypeDef; void CLOCK_init(void); void RCC_GetClocksFreq(RCC_ClocksTypeDef* RCC_Clocks); // *************************200304 void RCC_APB1PeriphClockCmd(uint32_t RCC_APB1Periph, FunctionalState NewState); #endif | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 | #include "clock.h" uint16_t StartUpCounter = 0; void CLOCK_init(void) { __IO uint32_t HSEStatus = 0; /* SYSCLK, HCLK, PCLK2 and PCLK1 configuration ---------------------------*/ /* Enable HSE */ RCC->CR |= ((uint32_t)RCC_CR_HSEON); /* Wait till HSE is ready and if Time out is reached exit */ do { HSEStatus = RCC->CR & RCC_CR_HSERDY; StartUpCounter++; } while((HSEStatus == 0) && (StartUpCounter != HSEStartUp_TimeOut)); /* Flash 2 wait state */ FLASH->ACR |= (uint32_t)FLASH_ACR_LATENCY_2; /* HCLK = SYSCLK */ RCC->CFGR |= (uint32_t)RCC_CFGR_HPRE_DIV1; /* PCLK2 = HCLK */ RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE2_DIV1; /* PCLK1 = HCLK */ RCC->CFGR |= (uint32_t)RCC_CFGR_PPRE1_DIV2; /* PLL configuration: PLLCLK = HSE * 9 = 72 MHz */ RCC->CFGR |= (uint32_t)(RCC_CFGR_PLLSRC_HSE | RCC_CFGR_PLLMULL6); /* Enable PLL */ RCC->CR |= RCC_CR_PLLON; /* Wait till PLL is ready */ while((RCC->CR & RCC_CR_PLLRDY) == 0){;} /* Select PLL as system clock source */ RCC->CFGR |= (uint32_t)RCC_CFGR_SW_PLL; /* Wait till PLL is used as system clock source */ while ((RCC->CFGR & (uint32_t)RCC_CFGR_SWS) != (uint32_t)0x08){;} } void RCC_GetClocksFreq(RCC_ClocksTypeDef* RCC_Clocks) { uint32_t tmp = 0, pllmull = 0, pllsource = 0, presc = 0; /* Get SYSCLK source -------------------------------------------------------*/ tmp = RCC->CFGR & CFGR_SWS_Mask; switch (tmp) { case 0x00: /* HSI used as system clock */ RCC_Clocks->SYSCLK_Frequency = HSI_Value; break; case 0x04: /* HSE used as system clock */ RCC_Clocks->SYSCLK_Frequency = HSE_Value; break; case 0x08: /* PLL used as system clock */ /* Get PLL clock source and multiplication factor ----------------------*/ pllmull = RCC->CFGR & CFGR_PLLMull_Mask; pllsource = RCC->CFGR & CFGR_PLLSRC_Mask; pllmull = ( pllmull >> 18) + 2; if (pllsource == 0x00) {/* HSI oscillator clock divided by 2 selected as PLL clock entry */ RCC_Clocks->SYSCLK_Frequency = (HSI_Value >> 1) * pllmull; } else {/* HSE selected as PLL clock entry */ if ((RCC->CFGR & CFGR_PLLXTPRE_Mask) != (uint32_t)RESET) {/* HSE oscillator clock divided by 2 */ RCC_Clocks->SYSCLK_Frequency = (HSE_Value >> 1) * pllmull; } else { RCC_Clocks->SYSCLK_Frequency = HSE_Value * pllmull; } } break; default: RCC_Clocks->SYSCLK_Frequency = HSI_Value; break; } /* Compute HCLK, PCLK1, PCLK2 and ADCCLK clocks frequencies ----------------*/ /* Get HCLK prescaler */ tmp = RCC->CFGR & CFGR_HPRE_Set_Mask; tmp = tmp >> 4; presc = APBAHBPrescTable[tmp]; /* HCLK clock frequency */ RCC_Clocks->HCLK_Frequency = RCC_Clocks->SYSCLK_Frequency >> presc; /* Get PCLK1 prescaler */ tmp = RCC->CFGR & CFGR_PPRE1_Set_Mask; tmp = tmp >> 8; presc = APBAHBPrescTable[tmp]; /* PCLK1 clock frequency */ RCC_Clocks->PCLK1_Frequency = RCC_Clocks->HCLK_Frequency >> presc; /* Get PCLK2 prescaler */ tmp = RCC->CFGR & CFGR_PPRE2_Set_Mask; tmp = tmp >> 11; presc = APBAHBPrescTable[tmp]; /* PCLK2 clock frequency */ RCC_Clocks->PCLK2_Frequency = RCC_Clocks->HCLK_Frequency >> presc; /* Get ADCCLK prescaler */ tmp = RCC->CFGR & CFGR_ADCPRE_Set_Mask; tmp = tmp >> 14; presc = ADCPrescTable[tmp]; /* ADCCLK clock frequency */ RCC_Clocks->ADCCLK_Frequency = RCC_Clocks->PCLK2_Frequency / presc; } // ******************************200304 void RCC_APB1PeriphClockCmd(uint32_t RCC_APB1Periph, FunctionalState NewState) { if (NewState != DISABLE) { RCC->APB1ENR |= RCC_APB1Periph; } else { RCC->APB1ENR &= ~RCC_APB1Periph; } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 | #ifndef _INTERRUPT_H_ #define _INTERRUPT_H_ #include "platform_config.h" #include "clock.h" #define SCS_BASE (0xE000E000) /*!< System Control Space Base Address */ #define SysTick_BASE (SCS_BASE + 0x0010) /*!< SysTick Base Address */ #define NVIC_BASE (SCS_BASE + 0x0100) /*!< NVIC Base Address */ #define SCB_BASE (SCS_BASE + 0x0D00) /*!< System Control Block Base Address */ #define SCB ((SCB_Type *) SCB_BASE) /*!< SCB configuration struct */ #define SysTick ((SysTick_Type *) SysTick_BASE) /*!< SysTick configuration struct */ #define NVIC ((NVIC_Type *) NVIC_BASE) /*!< NVIC configuration struct */ #define NVIC_VectTab_RAM ((uint32_t)0x20000000) #define NVIC_VectTab_FLASH ((uint32_t)0x08000000) #define SYSTICK_ENABLE 0 /* Config-Bit to start or stop the SysTick Timer */ #define SYSTICK_TICKINT 1 /* Config-Bit to enable or disable the SysTick interrupt */ #define SYSTICK_CLKSOURCE 2 /* Clocksource has the offset 2 in SysTick Control and Status Register */ #define SYSTICK_MAXCOUNT ((1<<24) -1) /* SysTick MaxCount */ #define __NVIC_PRIO_BITS 4 /*!< STM32 uses 4 Bits for the Priority Levels */ typedef struct { __I uint32_t CPUID; /*!< CPU ID Base Register */ __IO uint32_t ICSR; /*!< Interrupt Control State Register */ __IO uint32_t VTOR; /*!< Vector Table Offset Register */ __IO uint32_t AIRCR; /*!< Application Interrupt / Reset Control Register */ __IO uint32_t SCR; /*!< System Control Register */ __IO uint32_t CCR; /*!< Configuration Control Register */ __IO uint8_t SHP[12]; /*!< System Handlers Priority Registers (4-7, 8-11, 12-15) */ __IO uint32_t SHCSR; /*!< System Handler Control and State Register */ __IO uint32_t CFSR; /*!< Configurable Fault Status Register */ __IO uint32_t HFSR; /*!< Hard Fault Status Register */ __IO uint32_t DFSR; /*!< Debug Fault Status Register */ __IO uint32_t MMFAR; /*!< Mem Manage Address Register */ __IO uint32_t BFAR; /*!< Bus Fault Address Register */ __IO uint32_t AFSR; /*!< Auxiliary Fault Status Register */ __I uint32_t PFR[2]; /*!< Processor Feature Register */ __I uint32_t DFR; /*!< Debug Feature Register */ __I uint32_t ADR; /*!< Auxiliary Feature Register */ __I uint32_t MMFR[4]; /*!< Memory Model Feature Register */ __I uint32_t ISAR[5]; /*!< ISA Feature Register */ } SCB_Type; typedef struct { __IO uint32_t CTRL; /*!< SysTick Control and Status Register */ __IO uint32_t LOAD; /*!< SysTick Reload Value Register */ __IO uint32_t VAL; /*!< SysTick Current Value Register */ __I uint32_t CALIB; /*!< SysTick Calibration Register */ } SysTick_Type; typedef enum IRQn { /****** Cortex-M3 Processor Exceptions Numbers ***************************************************/ NonMaskableInt_IRQn = -14, /*!< 2 Non Maskable Interrupt */ MemoryManagement_IRQn = -12, /*!< 4 Cortex-M3 Memory Management Interrupt */ BusFault_IRQn = -11, /*!< 5 Cortex-M3 Bus Fault Interrupt */ UsageFault_IRQn = -10, /*!< 6 Cortex-M3 Usage Fault Interrupt */ SVCall_IRQn = -5, /*!< 11 Cortex-M3 SV Call Interrupt */ DebugMonitor_IRQn = -4, /*!< 12 Cortex-M3 Debug Monitor Interrupt */ PendSV_IRQn = -2, /*!< 14 Cortex-M3 Pend SV Interrupt */ SysTick_IRQn = -1, /*!< 15 Cortex-M3 System Tick Interrupt */ /****** STM32 specific Interrupt Numbers *********************************************************/ WWDG_IRQn = 0, /*!< Window WatchDog Interrupt */ PVD_IRQn = 1, /*!< PVD through EXTI Line detection Interrupt */ TAMPER_IRQn = 2, /*!< Tamper Interrupt */ RTC_IRQn = 3, /*!< RTC global Interrupt */ FLASH_IRQn = 4, /*!< FLASH global Interrupt */ RCC_IRQn = 5, /*!< RCC global Interrupt */ EXTI0_IRQn = 6, /*!< EXTI Line0 Interrupt */ EXTI1_IRQn = 7, /*!< EXTI Line1 Interrupt */ EXTI2_IRQn = 8, /*!< EXTI Line2 Interrupt */ EXTI3_IRQn = 9, /*!< EXTI Line3 Interrupt */ EXTI4_IRQn = 10, /*!< EXTI Line4 Interrupt */ DMA1_Channel1_IRQn = 11, /*!< DMA1 Channel 1 global Interrupt */ DMA1_Channel2_IRQn = 12, /*!< DMA1 Channel 2 global Interrupt */ DMA1_Channel3_IRQn = 13, /*!< DMA1 Channel 3 global Interrupt */ DMA1_Channel4_IRQn = 14, /*!< DMA1 Channel 4 global Interrupt */ DMA1_Channel5_IRQn = 15, /*!< DMA1 Channel 5 global Interrupt */ DMA1_Channel6_IRQn = 16, /*!< DMA1 Channel 6 global Interrupt */ DMA1_Channel7_IRQn = 17, /*!< DMA1 Channel 7 global Interrupt */ ADC1_2_IRQn = 18, /*!< ADC1 and ADC2 global Interrupt */ USB_HP_CAN1_TX_IRQn = 19, /*!< USB Device High Priority or CAN1 TX Interrupts */ USB_LP_CAN1_RX0_IRQn = 20, /*!< USB Device Low Priority or CAN1 RX0 Interrupts */ CAN1_RX1_IRQn = 21, /*!< CAN1 RX1 Interrupt */ CAN1_SCE_IRQn = 22, /*!< CAN1 SCE Interrupt */ EXTI9_5_IRQn = 23, /*!< External Line[9:5] Interrupts */ TIM1_BRK_IRQn = 24, /*!< TIM1 Break Interrupt */ TIM1_UP_IRQn = 25, /*!< TIM1 Update Interrupt */ TIM1_TRG_COM_IRQn = 26, /*!< TIM1 Trigger and Commutation Interrupt */ TIM1_CC_IRQn = 27, /*!< TIM1 Capture Compare Interrupt */ TIM2_IRQn = 28, /*!< TIM2 global Interrupt */ TIM3_IRQn = 29, /*!< TIM3 global Interrupt */ TIM4_IRQn = 30, /*!< TIM4 global Interrupt */ I2C1_EV_IRQn = 31, /*!< I2C1 Event Interrupt */ I2C1_ER_IRQn = 32, /*!< I2C1 Error Interrupt */ I2C2_EV_IRQn = 33, /*!< I2C2 Event Interrupt */ I2C2_ER_IRQn = 34, /*!< I2C2 Error Interrupt */ SPI1_IRQn = 35, /*!< SPI1 global Interrupt */ SPI2_IRQn = 36, /*!< SPI2 global Interrupt */ USART1_IRQn = 37, /*!< USART1 global Interrupt */ USART2_IRQn = 38, /*!< USART2 global Interrupt */ USART3_IRQn = 39, /*!< USART3 global Interrupt */ EXTI15_10_IRQn = 40, /*!< External Line[15:10] Interrupts */ RTCAlarm_IRQn = 41, /*!< RTC Alarm through EXTI Line Interrupt */ USBWakeUp_IRQn = 42, /*!< USB Device WakeUp from suspend through EXTI Line Interrupt */ } IRQn_Type; typedef struct { __IO uint32_t ISER[8]; /*!< Interrupt Set Enable Register */ uint32_t RESERVED0[24]; __IO uint32_t ICER[8]; /*!< Interrupt Clear Enable Register */ uint32_t RSERVED1[24]; __IO uint32_t ISPR[8]; /*!< Interrupt Set Pending Register */ uint32_t RESERVED2[24]; __IO uint32_t ICPR[8]; /*!< Interrupt Clear Pending Register */ uint32_t RESERVED3[24]; __IO uint32_t IABR[8]; /*!< Interrupt Active bit Register */ uint32_t RESERVED4[56]; __IO uint8_t IP[240]; /*!< Interrupt Priority Register, 8Bit wide */ uint32_t RESERVED5[644]; __O uint32_t STIR; /*!< Software Trigger Interrupt Register */ } NVIC_Type; void INTERRUPT_delay_init(void); void NVIC_SetVectorTable(uint32_t NVIC_VectTab, uint32_t Offset); void NVIC_Configuration(void); uint32_t SysTick_Config(uint32_t ticks); void NVIC_SetPriority(IRQn_Type IRQn, uint32_t priority); void SysTick_Handler(void); void Delay(__IO uint32_t nTime); void TimingDelay_Decrement(void); void delay_1_second_it(void); void delay_100_milli_second_it(void); #define NVIC_PriorityGroup_0 ((uint32_t)0x700) /*< 0 bits for pre-emption priority 4 bits for subpriority */ #define AIRCR_VECTKEY_MASK ((uint32_t)0x05FA0000) #define GPIO_PORTSOURCE_KEY GPIO_PortSourceGPIOC #define GPIO_PINSOURCE_KEY0 GPIO_PinSource0 #define GPIO_PINSOURCE_KEY1 GPIO_PinSource1 #define GPIO_PINSOURCE_KEY2 GPIO_PinSource2 #define GPIO_PINSOURCE_KEY3 GPIO_PinSource3 #define GPIO_EXTI_Line_KEY0 EXTI_Line0 #define GPIO_EXTI_Line_KEY1 EXTI_Line1 #define GPIO_EXTI_Line_KEY2 EXTI_Line2 #define GPIO_EXTI_Line_KEY3 EXTI_Line3 #define AFIO ((AFIO_TypeDef *) AFIO_BASE) #define EXTI ((EXTI_TypeDef *) EXTI_BASE) #define AFIO_BASE (APB2PERIPH_BASE + 0x0000) #define EXTI_BASE (APB2PERIPH_BASE + 0x0400) typedef struct { uint8_t NVIC_IRQChannel; uint8_t NVIC_IRQChannelPreemptionPriority; uint8_t NVIC_IRQChannelSubPriority; FunctionalState NVIC_IRQChannelCmd; } NVIC_InitTypeDef; typedef struct { __IO uint32_t EVCR; __IO uint32_t MAPR; __IO uint32_t EXTICR[4]; } AFIO_TypeDef; typedef struct { __IO uint32_t IMR; __IO uint32_t EMR; __IO uint32_t RTSR; __IO uint32_t FTSR; __IO uint32_t SWIER; __IO uint32_t PR; } EXTI_TypeDef; typedef enum { EXTI_Mode_Interrupt = 0x00, EXTI_Mode_Event = 0x04 }EXTIMode_TypeDef; typedef enum { EXTI_Trigger_Rising = 0x08, EXTI_Trigger_Falling = 0x0C, EXTI_Trigger_Rising_Falling = 0x10 }EXTITrigger_TypeDef; typedef struct { uint32_t EXTI_Line; EXTIMode_TypeDef EXTI_Mode; EXTITrigger_TypeDef EXTI_Trigger; FunctionalState EXTI_LineCmd; }EXTI_InitTypeDef; void GPIO_EXTILineConfig(uint8_t GPIO_PortSource, uint8_t GPIO_PinSource); void EXTI_Configuration(void); void EXTI0_IRQHandler(void); /* key 0~3 */ void EXTI1_IRQHandler(void); void EXTI2_IRQHandler(void); void EXTI3_IRQHandler(void); void NVIC_PriorityGroupConfig(uint32_t NVIC_PriorityGroup); void NVIC_Init(NVIC_InitTypeDef* NVIC_InitStruct); void EXTI_ClearITPendingBit(uint32_t EXTI_Line); void EXTI_Init(EXTI_InitTypeDef* EXTI_InitStruct); ITStatus EXTI_GetITStatus(uint32_t EXTI_Line); #endif | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 | #include "interrupt.h" static volatile uint32_t TimingDelay; void INTERRUPT_delay_init(void) { RCC_ClocksTypeDef rcc_clocks;//test RCC_GetClocksFreq(&rcc_clocks); /* Setup SysTick Timer for 1 msec interrupts */ if (SysTick_Config(rcc_clocks.SYSCLK_Frequency / 1000)) { /* Capture error */ while (1); } } void NVIC_SetVectorTable(uint32_t NVIC_VectTab, uint32_t Offset){ SCB->VTOR = NVIC_VectTab | (Offset & (uint32_t)0x1FFFFF80); } void NVIC_Configuration(void) { NVIC_InitTypeDef NVIC_InitStructure; /* Set the Vector Table base location at 0x08000000 */ NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0); /* Configure one bit for preemption priority */ NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); /* Enable the EXTI0 Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* Enable the EXTI1 Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* Enable the EXTI2 Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = EXTI2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* Enable the EXTI3 Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = EXTI3_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* ************* NVIC_Configuration for TIMER 2 ~4 ************** */ /* Enable the TIM2 Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* Enable the TIM3 Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); /* Enable the TIM4 Interrupt */ 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); } uint32_t SysTick_Config(uint32_t ticks) { if (ticks > SYSTICK_MAXCOUNT) return (1); /* Reload value impossible */ SysTick->LOAD = (ticks & SYSTICK_MAXCOUNT) - 1; /* set reload register */ NVIC_SetPriority (SysTick_IRQn, (1<<__NVIC_PRIO_BITS) - 1); /* set Priority for Cortex-M0 System Interrupts */ SysTick->VAL = (0x00); /* Load the SysTick Counter Value */ SysTick->CTRL = (1 << SYSTICK_CLKSOURCE) | (1<<SYSTICK_ENABLE) | (1<<SYSTICK_TICKINT); /* Enable SysTick IRQ and SysTick Timer */ return (0); /* Function successful */ } void NVIC_SetPriority(IRQn_Type IRQn, uint32_t priority) { if(IRQn < 0) { SCB->SHP[((uint32_t)(IRQn) & 0xF)-4] = ((priority << (8 - __NVIC_PRIO_BITS)) & 0xff); } /* set Priority for Cortex-M3 System Interrupts */ else { NVIC->IP[(uint32_t)(IRQn)] = ((priority << (8 - __NVIC_PRIO_BITS)) & 0xff); } /* set Priority for device specific Interrupts */ } void SysTick_Handler(void) { TimingDelay_Decrement(); } void Delay(__IO uint32_t nTime) { TimingDelay = nTime; while(TimingDelay != 0); } void TimingDelay_Decrement(void) { if (TimingDelay != 0x00) { TimingDelay--; } } void delay_1_second_it(void) { Delay(1000); } void delay_100_milli_second_it(void) { Delay(100); } void EXTI0_IRQHandler(void) { if(EXTI_GetITStatus(GPIO_EXTI_Line_KEY0) != RESET) { EXTI_ClearITPendingBit(GPIO_EXTI_Line_KEY0); printf("key 0 Press\n"); } } void EXTI1_IRQHandler(void) { if(EXTI_GetITStatus(GPIO_EXTI_Line_KEY1) != RESET) { EXTI_ClearITPendingBit(GPIO_EXTI_Line_KEY1); printf("key 1 Press\n"); } } void EXTI2_IRQHandler(void) { if(EXTI_GetITStatus(GPIO_EXTI_Line_KEY2) != RESET) { EXTI_ClearITPendingBit(GPIO_EXTI_Line_KEY2); printf("key 2 Press\n"); } } void EXTI3_IRQHandler(void) { if(EXTI_GetITStatus(GPIO_EXTI_Line_KEY3) != RESET) { EXTI_ClearITPendingBit(GPIO_EXTI_Line_KEY3); printf("key 3 Press\n"); } } void NVIC_PriorityGroupConfig(uint32_t NVIC_PriorityGroup) { /* Set the PRIGROUP[10:8] bits according to NVIC_PriorityGroup value */ SCB->AIRCR = AIRCR_VECTKEY_MASK | NVIC_PriorityGroup; } void NVIC_Init(NVIC_InitTypeDef* NVIC_InitStruct) { uint32_t tmppriority = 0x00, tmppre = 0x00, tmpsub = 0x0F; if (NVIC_InitStruct->NVIC_IRQChannelCmd != DISABLE) { /* Compute the Corresponding IRQ Priority --------------------------------*/ tmppriority = (0x700 - ((SCB->AIRCR) & (uint32_t)0x700))>> 0x08; tmppre = (0x4 - tmppriority); tmpsub = tmpsub >> tmppriority; tmppriority = (uint32_t)NVIC_InitStruct->NVIC_IRQChannelPreemptionPriority << tmppre; tmppriority |= NVIC_InitStruct->NVIC_IRQChannelSubPriority & tmpsub; tmppriority = tmppriority << 0x04; NVIC->IP[NVIC_InitStruct->NVIC_IRQChannel] = tmppriority; /* Enable the Selected IRQ Channels --------------------------------------*/ NVIC->ISER[NVIC_InitStruct->NVIC_IRQChannel >> 0x05] = (uint32_t)0x01 << (NVIC_InitStruct->NVIC_IRQChannel & (uint8_t)0x1F); } else { /* Disable the Selected IRQ Channels -------------------------------------*/ NVIC->ICER[NVIC_InitStruct->NVIC_IRQChannel >> 0x05] = (uint32_t)0x01 << (NVIC_InitStruct->NVIC_IRQChannel & (uint8_t)0x1F); } } void EXTI_Configuration(void) { RCC->APB2ENR |= RCC_APB2Periph_AFIO; EXTI_InitTypeDef EXTI_InitStructure; /* Configure gpio as input : Key0 */ GPIO_EXTILineConfig(GPIO_PORTSOURCE_KEY, GPIO_PINSOURCE_KEY0); /* Configure EXTI Line to generate an interrupt */ EXTI_InitStructure.EXTI_Line = GPIO_EXTI_Line_KEY0; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); /* Configure gpio as input : Key1 */ GPIO_EXTILineConfig(GPIO_PORTSOURCE_KEY, GPIO_PINSOURCE_KEY1); EXTI_InitStructure.EXTI_Line = GPIO_EXTI_Line_KEY1; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); /* Configure gpio as input : Key2 */ GPIO_EXTILineConfig(GPIO_PORTSOURCE_KEY, GPIO_PINSOURCE_KEY2); EXTI_InitStructure.EXTI_Line = GPIO_EXTI_Line_KEY2; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); /* Configure gpio as input : Key3 */ GPIO_EXTILineConfig(GPIO_PORTSOURCE_KEY, GPIO_PINSOURCE_KEY3); EXTI_InitStructure.EXTI_Line = GPIO_EXTI_Line_KEY3; EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling; EXTI_InitStructure.EXTI_LineCmd = ENABLE; EXTI_Init(&EXTI_InitStructure); } void GPIO_EXTILineConfig(uint8_t GPIO_PortSource, uint8_t GPIO_PinSource) { uint32_t tmp = 0x00; tmp = ((uint32_t)0x0F) << (0x04 * (GPIO_PinSource & (uint8_t)0x03)); AFIO->EXTICR[GPIO_PinSource >> 0x02] &= ~tmp; AFIO->EXTICR[GPIO_PinSource >> 0x02] |= (((uint32_t)GPIO_PortSource) << (0x04 * (GPIO_PinSource & (uint8_t)0x03))); } ITStatus EXTI_GetITStatus(uint32_t EXTI_Line) { ITStatus bitstatus = RESET; uint32_t enablestatus = 0; enablestatus = EXTI->IMR & EXTI_Line; if (((EXTI->PR & EXTI_Line) != (uint32_t)RESET) && (enablestatus != (uint32_t)RESET)) { bitstatus = SET; } else { bitstatus = RESET; } return bitstatus; } void EXTI_ClearITPendingBit(uint32_t EXTI_Line) { EXTI->PR = EXTI_Line; } void EXTI_Init(EXTI_InitTypeDef* EXTI_InitStruct) { uint32_t tmp = 0; tmp = (uint32_t)EXTI_BASE; if (EXTI_InitStruct->EXTI_LineCmd != DISABLE) { /* Clear EXTI line configuration */ EXTI->IMR &= ~EXTI_InitStruct->EXTI_Line; EXTI->EMR &= ~EXTI_InitStruct->EXTI_Line; tmp += EXTI_InitStruct->EXTI_Mode; *(__IO uint32_t *) tmp |= EXTI_InitStruct->EXTI_Line; /* Clear Rising Falling edge configuration */ EXTI->RTSR &= ~EXTI_InitStruct->EXTI_Line; EXTI->FTSR &= ~EXTI_InitStruct->EXTI_Line; /* Select the trigger for the selected external interrupts */ if (EXTI_InitStruct->EXTI_Trigger == EXTI_Trigger_Rising_Falling) { /* Rising Falling edge */ EXTI->RTSR |= EXTI_InitStruct->EXTI_Line; EXTI->FTSR |= EXTI_InitStruct->EXTI_Line; } else { tmp = (uint32_t)EXTI_BASE; tmp += EXTI_InitStruct->EXTI_Trigger; *(__IO uint32_t *) tmp |= EXTI_InitStruct->EXTI_Line; } } else { tmp += EXTI_InitStruct->EXTI_Mode; /* Disable the selected external lines */ *(__IO uint32_t *) tmp &= ~EXTI_InitStruct->EXTI_Line; } } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 | #ifndef _MOTOR_H_ #define _MOTOR_H_ #include "platform_config.h" #include "timer.h" #define GPIO_MOTOR GPIOB #define GPIO_Port_Mtr1 GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15 #define GPIO_Port_Mtr2 GPIO_Pin_11|GPIO_Pin_10|GPIO_Pin_9|GPIO_Pin_8 static uint16_t Mtr1_Data_CW[4] = {GPIO_Pin_12,GPIO_Pin_13,GPIO_Pin_14,GPIO_Pin_15}; static uint16_t Mtr1_Data_CCW[4] = {GPIO_Pin_15,GPIO_Pin_14,GPIO_Pin_13,GPIO_Pin_12}; static uint16_t Mtr2_Data_CW[4] = {GPIO_Pin_11,GPIO_Pin_10,GPIO_Pin_9,GPIO_Pin_8}; static uint16_t Mtr2_Data_CCW[4] = {GPIO_Pin_8,GPIO_Pin_9,GPIO_Pin_10,GPIO_Pin_11}; typedef enum { Motor1, Motor2, Motor_all }Motor_Num; typedef enum { CW, CCW }Motor_Dir; void Motor_Init(void); void Motor_Move(int Motor_Num, int Motor_Dir); void Motor_Go(int Motor_Num); void Motor_Stop(int Motor_Num); void Motor_Set_Speed(int Motor_Num, int speed); #endif | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | #include "motor.h" void Motor_Init(void) { /* motor test*/ RCC->APB2ENR |= RCC_APB2Periph_GPIOB; // ********************************************************* 0312 GPIO_InitTypeDef GPIO_InitStructureMtr; GPIO_InitStructureMtr.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10|GPIO_Pin_11| GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15; GPIO_InitStructureMtr.GPIO_Speed = GPIO_Speed_10MHz; GPIO_InitStructureMtr.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOB,&GPIO_InitStructureMtr); // ********************************************************* 0312 } void Motor_Move(int Motor_Num, int Motor_Dir) { static int Mtr1Val = 0; // 0~3 motor status value static int Mtr2Val = 0; if(Motor_Num == Motor1 && Motor_Dir == CW) { GPIO_MOTOR->BRR |= GPIO_Port_Mtr1; // clear last stat GPIO_MOTOR->BSRR |= Mtr1_Data_CW[Mtr1Val]; // write new stat (step motor) Mtr1Val++; if(Mtr1Val == 4) Mtr1Val = 0; } else if(Motor_Num == Motor1 && Motor_Dir == CCW) { GPIO_MOTOR->BRR |= GPIO_Port_Mtr1; GPIO_MOTOR->BSRR |= Mtr1_Data_CCW[Mtr1Val]; Mtr1Val++; if(Mtr1Val == 4) Mtr1Val = 0; } else if(Motor_Num == Motor2 && Motor_Dir == CW) { GPIO_MOTOR->BRR |= GPIO_Port_Mtr2; GPIO_MOTOR->BSRR |= Mtr2_Data_CW[Mtr2Val]; Mtr2Val++; if(Mtr2Val == 4) Mtr2Val = 0; } else if(Motor_Num == Motor2 && Motor_Dir == CCW) { GPIO_MOTOR->BRR |= GPIO_Port_Mtr2; GPIO_MOTOR->BSRR |= Mtr2_Data_CCW[Mtr2Val]; Mtr2Val++; if(Mtr2Val == 4) Mtr2Val = 0; } } void Motor_Go(int Motor_Num) { if(Motor_Num == Motor1) { TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); } else if(Motor_Num == Motor2) { TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE); } else if(Motor_Num == Motor_all) { TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE); } } void Motor_Stop(int Motor_Num) { if(Motor_Num == Motor1) { TIM_ITConfig(TIM2, TIM_IT_Update, DISABLE); GPIO_MOTOR->BRR |= GPIO_Port_Mtr1; } else if(Motor_Num == Motor2) { TIM_ITConfig(TIM3, TIM_IT_Update, DISABLE); GPIO_MOTOR->BRR |= GPIO_Port_Mtr2; } else if(Motor_Num == Motor_all) { TIM_ITConfig(TIM2, TIM_IT_Update, DISABLE); TIM_ITConfig(TIM3, TIM_IT_Update, DISABLE); GPIO_MOTOR->BRR |= GPIO_Port_Mtr1; GPIO_MOTOR->BRR |= GPIO_Port_Mtr2; } } void Motor_Set_Speed(int Motor_Num, int speed) { // Motor_Stop(Motor_Num); if(Motor_Num == Motor1) { ((TIM_TypeDef*)TIM2)->ARR = speed; } else if(Motor_Num == Motor2) { ((TIM_TypeDef*)TIM3)->ARR = speed; } // Motor_Go(Motor_Num); } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | #ifndef _SENSOR_H_ #define _SENSOR_H_ #include "platform_config.h" #include "timer.h" #include "key.h" #define GPIO_SENSOR GPIOB #define GPIO_SENSOR0_PIN GPIO_Pin_1 #define GPIO_SENSOR1_PIN GPIO_Pin_5 #define GPIO_SENSOR2_PIN GPIO_Pin_6 #define GPIO_SENSOR3_PIN GPIO_Pin_7 #define GPIO_SENSOR_ALL_PIN GPIO_SENSOR0_PIN|GPIO_SENSOR1_PIN|GPIO_SENSOR2_PIN|GPIO_SENSOR3_PIN void Sensor_Init(void); int Sensor_Get_Status(void); int Sensor_Get_Error(void); void Sensor_FeedBack(void); void Sensor_Start(void); #endif | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | #include "sensor.h" void Sensor_Init(void) { RCC->APB2ENR |= RCC_APB2Periph_GPIOB; GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = GPIO_SENSOR_ALL_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIO_SENSOR,&GPIO_InitStructure); } int Sensor_Get_Status(void) { int sensor_value = 0; if(GPIO_ReadInputDataBit(GPIO_SENSOR,GPIO_SENSOR0_PIN) == Bit_SET)sensor_value |= 0x01; else sensor_value &= ~0x01; if(GPIO_ReadInputDataBit(GPIO_SENSOR,GPIO_SENSOR1_PIN) == Bit_SET)sensor_value |= 0x02; else sensor_value &= ~0x02; if(GPIO_ReadInputDataBit(GPIO_SENSOR,GPIO_SENSOR2_PIN) == Bit_SET)sensor_value |= 0x04; else sensor_value &= ~0x04; if(GPIO_ReadInputDataBit(GPIO_SENSOR,GPIO_SENSOR3_PIN) == Bit_SET)sensor_value |= 0x08; else sensor_value &= ~0x08; return sensor_value; } int Sensor_Get_Error(void) { switch(Sensor_Get_Status()) { case 0 : return 0; case 1 : return 3; case 2 : return 1; case 3 : return 2; case 4 : return -1; case 5 : return 2; case 6 : return 0; case 7 : return 3; case 8 : return -3; case 9 : return 0; case 10 : return -2; case 11 : return 1; case 12 : return -2; case 13 : return -1; case 14 : return -3; case 15 : return 0; default: return 0; } } void Sensor_FeedBack(void) { int error = Sensor_Get_Error(); //printf("%d\n",error); #define BASE_SPEED 7 #define P_GAIN 3 TIM_Configuration(); switch(error){ case 0: Motor_Set_Speed(Motor1, BASE_SPEED+P_GAIN*0); Motor_Set_Speed(Motor2, BASE_SPEED+P_GAIN*0); break; case -1:Motor_Set_Speed(Motor1, BASE_SPEED+P_GAIN*1); Motor_Set_Speed(Motor2, BASE_SPEED+P_GAIN*0); break; case -2:Motor_Set_Speed(Motor1, BASE_SPEED+P_GAIN*2); Motor_Set_Speed(Motor2, BASE_SPEED+P_GAIN*0); break; case -3:Motor_Set_Speed(Motor1, BASE_SPEED+P_GAIN*3); Motor_Set_Speed(Motor2, BASE_SPEED+P_GAIN*0); break; case 1: Motor_Set_Speed(Motor1, BASE_SPEED+P_GAIN*0); Motor_Set_Speed(Motor2, BASE_SPEED+P_GAIN*1); break; case 2: Motor_Set_Speed(Motor1, BASE_SPEED+P_GAIN*0); Motor_Set_Speed(Motor2, BASE_SPEED+P_GAIN*2); break; case 3: Motor_Set_Speed(Motor1, BASE_SPEED+P_GAIN*0); Motor_Set_Speed(Motor2, BASE_SPEED+P_GAIN*3); break; } } void Sensor_Start(void) { TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE); } | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 | // ********************************************************* // timer , interrupt ,motor // // // // ********************************************************* #ifndef _TIMER_H_ #define _TIMER_H_ #include "platform_config.h" #include "clock.h" #include "interrupt.h" #include "led.h" #include "motor.h" #include "sensor.h" #define RCC_APB1Periph_TIM2 ((uint32_t)0x00000001) #define RCC_APB1Periph_TIM3 ((uint32_t)0x00000002) #define RCC_APB1Periph_TIM4 ((uint32_t)0x00000004) #define TIM_IT_Update ((uint16_t)0x0001) #define TIM1_BASE (APB2PERIPH_BASE + 0x2C00) #define TIM2_BASE (APB1PERIPH_BASE + 0x0000) #define TIM3_BASE (APB1PERIPH_BASE + 0x0400) #define TIM4_BASE (APB1PERIPH_BASE + 0x0800) #define TIM5_BASE (APB1PERIPH_BASE + 0x0C00) #define TIM6_BASE (APB1PERIPH_BASE + 0x1000) #define TIM7_BASE (APB1PERIPH_BASE + 0x1400) #define TIM8_BASE (APB2PERIPH_BASE + 0x3400) #define TIM9_BASE (APB2PERIPH_BASE + 0x4C00) #define TIM10_BASE (APB2PERIPH_BASE + 0x5000) #define TIM11_BASE (APB2PERIPH_BASE + 0x5400) #define TIM12_BASE (APB1PERIPH_BASE + 0x1800) #define TIM13_BASE (APB1PERIPH_BASE + 0x1C00) #define TIM14_BASE (APB1PERIPH_BASE + 0x2000) #define TIM15_BASE (APB2PERIPH_BASE + 0x4000) #define TIM16_BASE (APB2PERIPH_BASE + 0x4400) #define TIM17_BASE (APB2PERIPH_BASE + 0x4800) #define TIM1 ((TIM_TypeDef *) TIM1_BASE) #define TIM2 ((TIM_TypeDef *) TIM2_BASE) #define TIM3 ((TIM_TypeDef *) TIM3_BASE) #define TIM4 ((TIM_TypeDef *) TIM4_BASE) #define TIM5 ((TIM_TypeDef *) TIM5_BASE) #define TIM6 ((TIM_TypeDef *) TIM6_BASE) #define TIM7 ((TIM_TypeDef *) TIM7_BASE) #define TIM8 ((TIM_TypeDef *) TIM8_BASE) #define TIM9 ((TIM_TypeDef *) TIM9_BASE) #define TIM10 ((TIM_TypeDef *) TIM10_BASE) #define TIM11 ((TIM_TypeDef *) TIM11_BASE) #define TIM12 ((TIM_TypeDef *) TIM12_BASE) #define TIM13 ((TIM_TypeDef *) TIM13_BASE) #define TIM14 ((TIM_TypeDef *) TIM14_BASE) #define TIM15 ((TIM_TypeDef *) TIM15_BASE) #define TIM16 ((TIM_TypeDef *) TIM16_BASE) #define TIM17 ((TIM_TypeDef *) TIM17_BASE) #define TIM_CKD_DIV1 ((uint16_t)0x0000) #define TIM_CKD_DIV2 ((uint16_t)0x0100) #define TIM_CKD_DIV4 ((uint16_t)0x0200) #define TIM_CR1_CEN ((uint16_t)0x0001) /*!< Counter enable */ #define TIM_CR1_UDIS ((uint16_t)0x0002) /*!< Update disable */ #define TIM_CR1_URS ((uint16_t)0x0004) /*!< Update request source */ #define TIM_CR1_OPM ((uint16_t)0x0008) /*!< One pulse mode */ #define TIM_CR1_DIR ((uint16_t)0x0010) /*!< Direction */ #define TIM_CR1_CMS ((uint16_t)0x0060) /*!< CMS[1:0] bits (Center-aligned mode selection) */ #define TIM_CR1_CMS_0 ((uint16_t)0x0020) /*!< Bit 0 */ #define TIM_CR1_CMS_1 ((uint16_t)0x0040) /*!< Bit 1 */ #define TIM_CR1_CKD ((uint16_t)0x0300) /*!< CKD[1:0] bits (clock division) */ #define TIM_CR1_CKD_0 ((uint16_t)0x0100) /*!< Bit 0 */ #define TIM_CR1_CKD_1 ((uint16_t)0x0200) /*!< Bit 1 */ #define TIM_PSCReloadMode_Update ((uint16_t)0x0000) #define TIM_PSCReloadMode_Immediate ((uint16_t)0x0001) #define IS_TIM_PRESCALER_RELOAD(RELOAD) (((RELOAD) == TIM_PSCReloadMode_Update) || \ ((RELOAD) == TIM_PSCReloadMode_Immediate)) #define TIM_CounterMode_Up ((uint16_t)0x0000) #define TIM_CounterMode_Down ((uint16_t)0x0010) #define TIM_CounterMode_CenterAligned1 ((uint16_t)0x0020) #define TIM_CounterMode_CenterAligned2 ((uint16_t)0x0040) #define TIM_CounterMode_CenterAligned3 ((uint16_t)0x0060) #define IS_TIM_COUNTER_MODE(MODE) (((MODE) == TIM_CounterMode_Up) || \ ((MODE) == TIM_CounterMode_Down) || \ ((MODE) == TIM_CounterMode_CenterAligned1) || \ ((MODE) == TIM_CounterMode_CenterAligned2) || \ ((MODE) == TIM_CounterMode_CenterAligned3)) #define IS_TIM_ALL_PERIPH(PERIPH) (((PERIPH) == TIM1) || \ ((PERIPH) == TIM2) || \ ((PERIPH) == TIM3) || \ ((PERIPH) == TIM4) || \ ((PERIPH) == TIM5) || \ ((PERIPH) == TIM6) || \ ((PERIPH) == TIM7) || \ ((PERIPH) == TIM8) || \ ((PERIPH) == TIM9) || \ ((PERIPH) == TIM10)|| \ ((PERIPH) == TIM11)|| \ ((PERIPH) == TIM12)|| \ ((PERIPH) == TIM13)|| \ ((PERIPH) == TIM14)|| \ ((PERIPH) == TIM15)|| \ ((PERIPH) == TIM16)|| \ ((PERIPH) == TIM17)) #define TIM_FLAG_Update (1 << 0) typedef struct { __IO uint16_t CR1; uint16_t RESERVED0; __IO uint16_t CR2; uint16_t RESERVED1; __IO uint16_t SMCR; uint16_t RESERVED2; __IO uint16_t DIER; uint16_t RESERVED3; __IO uint16_t SR; uint16_t RESERVED4; __IO uint16_t EGR; uint16_t RESERVED5; __IO uint16_t CCMR1; uint16_t RESERVED6; __IO uint16_t CCMR2; uint16_t RESERVED7; __IO uint16_t CCER; uint16_t RESERVED8; __IO uint16_t CNT; uint16_t RESERVED9; __IO uint16_t PSC; uint16_t RESERVED10; __IO uint16_t ARR; uint16_t RESERVED11; __IO uint16_t RCR; uint16_t RESERVED12; __IO uint16_t CCR1; uint16_t RESERVED13; __IO uint16_t CCR2; uint16_t RESERVED14; __IO uint16_t CCR3; uint16_t RESERVED15; __IO uint16_t CCR4; uint16_t RESERVED16; __IO uint16_t BDTR; uint16_t RESERVED17; __IO uint16_t DCR; uint16_t RESERVED18; __IO uint16_t DMAR; uint16_t RESERVED19; } TIM_TypeDef; typedef struct { uint16_t TIM_Prescaler; /*!< Specifies the prescaler value used to divide the TIM clock. This parameter can be a number between 0x0000 and 0xFFFF */ uint16_t TIM_CounterMode; /*!< Specifies the counter mode. This parameter can be a value of @ref TIM_Counter_Mode */ uint16_t TIM_Period; /*!< Specifies the period value to be loaded into the active Auto-Reload Register at the next update event. This parameter must be a number between 0x0000 and 0xFFFF. */ uint16_t TIM_ClockDivision; /*!< Specifies the clock division. This parameter can be a value of @ref TIM_Clock_Division_CKD */ uint8_t TIM_RepetitionCounter; /*!< Specifies the repetition counter value. Each time the RCR downcounter reaches zero, an update event is generated and counting restarts from the RCR value (N). This means in PWM mode that (N+1) corresponds to: - the number of PWM periods in edge-aligned mode - the number of half PWM period in center-aligned mode This parameter must be a number between 0x00 and 0xFF. @note This parameter is valid only for TIM1 and TIM8. */ } TIM_TimeBaseInitTypeDef; void TIMER_init(void); void TIM2_IRQHandler(void); void TIM3_IRQHandler(void); void TIM4_IRQHandler(void); void TIM_ClearITPendingBit(TIM_TypeDef* TIMx, uint16_t TIM_IT); void TIM_Configuration(void); void TIM_TimeBaseInit(TIM_TypeDef* TIMx, TIM_TimeBaseInitTypeDef* TIM_TimeBaseInitStruct); void TIM_Cmd(TIM_TypeDef* TIMx, FunctionalState NewState); void TIM_PrescalerConfig(TIM_TypeDef* TIMx, uint16_t Prescaler, uint16_t TIM_PSCReloadMode); void TIM_ClearFlag(TIM_TypeDef* TIMx, uint16_t TIM_FLAG); void TIM_ITConfig(TIM_TypeDef* TIMx, uint16_t TIM_IT, FunctionalState NewState); #endif | cs |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 | #include "timer.h" void TIMER_init(void){ /* Enable TIM2 clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); /* Enable TIM3 clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); /* Enable TIM4 clock */ RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); } int led0 = 0, led1 = 0, led2 = 0; void TIM2_IRQHandler(void) { Motor_Move(Motor1,CCW); TIM_ClearITPendingBit(TIM2, TIM_IT_Update); // printf("t2\t"); } void TIM3_IRQHandler(void) { Motor_Move(Motor2,CCW); TIM_ClearITPendingBit(TIM3, TIM_IT_Update); // printf("t3\t"); } void TIM4_IRQHandler(void) { Sensor_FeedBack(); TIM_ClearITPendingBit(TIM4, TIM_IT_Update); //printf("t4\n"); } void TIM_ClearITPendingBit(TIM_TypeDef* TIMx, uint16_t TIM_IT) { /* Clear the IT pending Bit */ TIMx->SR = (uint16_t)~TIM_IT; } void TIM_Configuration(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; /* default timer setting 0xea5f -> 59999 +1 72M / 60K = 1200 */ TIM_TimeBaseStructure.TIM_Prescaler = 0xea5f; // TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseStructure.TIM_RepetitionCounter = 0x0000; /* TIM2 configuration */ TIM_TimeBaseStructure.TIM_Period = 12; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); /* TIM3 configuration */ TIM_TimeBaseStructure.TIM_Period = 12; // ( TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); /* TIM4 configuration */ TIM_TimeBaseStructure.TIM_Period = 119; // 0.1sec TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); /* TIM2~4 enable counter */ TIM_Cmd(TIM2, ENABLE); TIM_Cmd(TIM3, ENABLE); TIM_Cmd(TIM4, ENABLE); /* Immediate load of TIM2~4 Precaler value */ TIM_PrescalerConfig(TIM2, 0xEA5F, TIM_PSCReloadMode_Immediate); TIM_PrescalerConfig(TIM3, 0xEA5F, TIM_PSCReloadMode_Immediate); TIM_PrescalerConfig(TIM4, 0xEA5F, TIM_PSCReloadMode_Immediate); /* Clear TIM2~4 update pending flag */ TIM_ClearFlag(TIM2, TIM_FLAG_Update); TIM_ClearFlag(TIM3, TIM_FLAG_Update); TIM_ClearFlag(TIM4, TIM_FLAG_Update); /* Enable TIM2~4 Update interrupt */ // TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ // TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE); @@@@@@@@@@@@@ TIMER ENABLE @@@@@@@@@@@@@@@ // TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE); @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ } void TIM_TimeBaseInit(TIM_TypeDef* TIMx, TIM_TimeBaseInitTypeDef* TIM_TimeBaseInitStruct) { uint16_t tmpcr1 = 0; tmpcr1 = TIMx->CR1; if((TIMx == TIM1) || (TIMx == TIM8)|| (TIMx == TIM2) || (TIMx == TIM3)|| (TIMx == TIM4) || (TIMx == TIM5)) { /* Select the Counter Mode */ tmpcr1 &= (uint16_t)(~((uint16_t)(TIM_CR1_DIR | TIM_CR1_CMS))); tmpcr1 |= (uint32_t)TIM_TimeBaseInitStruct->TIM_CounterMode; } if((TIMx != TIM6) && (TIMx != TIM7)) { /* Set the clock division */ tmpcr1 &= (uint16_t)(~((uint16_t)TIM_CR1_CKD)); tmpcr1 |= (uint32_t)TIM_TimeBaseInitStruct->TIM_ClockDivision; } TIMx->CR1 = tmpcr1; /* Set the Autoreload value */ TIMx->ARR = TIM_TimeBaseInitStruct->TIM_Period ; /* Set the Prescaler value */ TIMx->PSC = TIM_TimeBaseInitStruct->TIM_Prescaler; if ((TIMx == TIM1) || (TIMx == TIM8)|| (TIMx == TIM15)|| (TIMx == TIM16) || (TIMx == TIM17)) { /* Set the Repetition Counter value */ TIMx->RCR = TIM_TimeBaseInitStruct->TIM_RepetitionCounter; } /* Generate an update event to reload the Prescaler and the Repetition counter values immediately */ TIMx->EGR = TIM_PSCReloadMode_Immediate; } void TIM_Cmd(TIM_TypeDef* TIMx, FunctionalState NewState) { if (NewState != DISABLE) { /* Enable the TIM Counter */ TIMx->CR1 |= TIM_CR1_CEN; } else { /* Disable the TIM Counter */ TIMx->CR1 &= (uint16_t)(~((uint16_t)TIM_CR1_CEN)); } } void TIM_PrescalerConfig(TIM_TypeDef* TIMx, uint16_t Prescaler, uint16_t TIM_PSCReloadMode) { /* Set the Prescaler value */ TIMx->PSC = Prescaler; /* Set or reset the UG Bit */ TIMx->EGR = TIM_PSCReloadMode; } void TIM_ClearFlag(TIM_TypeDef* TIMx, uint16_t TIM_FLAG) { /* Clear the flags */ TIMx->SR = (uint16_t)~TIM_FLAG; } void TIM_ITConfig(TIM_TypeDef* TIMx, uint16_t TIM_IT, FunctionalState NewState) { if (NewState != DISABLE) { /* Enable the Interrupt sources */ TIMx->DIER |= TIM_IT; } else { /* Disable the Interrupt sources */ TIMx->DIER &= (uint16_t)~TIM_IT; } } | cs |
pwm 10 ms
pwm 100 ms
라인트레이서(우)
라인트레이서(좌)
빠르게
|
|

첫댓글 너무 느려 좀 빠르게 해봐
넵 추가했습니다