Advertisement
Kevin129

Kevin's stm32

Jul 28th, 2019
1,004
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;------------------------------------------------------------------------------------------        
  2. ; Discovery kit for STM32F407/11 lines
  3. ; LEDs:
  4. ;  LD3 ORANGE <---> PD.13
  5. ;  LD4 GREEN  <---> PD.12
  6. ;  LD5 RED    <---> PD.14
  7. ;  LD6 BLUE   <---> PD.15
  8. ;  LD7 GREEN LED indicates when VBUS is present on CN5 (pin PA.9)
  9. ;  LD8 RED LED indicates an overcurrent from VBUS of CN5 (pin PD.5)
  10. ;
  11. ; Pushbutton
  12. ;  B1 <---> PA.0
  13. ;  B2 <---> Reset
  14. ;*******************************************************************************************   
  15.                 INCLUDE core_cm4_constants.s        ; Load Constant Definitions
  16.                 INCLUDE stm32f401xc_constants.s     ;
  17. ;constants  
  18. SYSCFG          EQU 0x40013800
  19. EXTICR1         EQU 0x08
  20. EXTI            EQU 0x40013C00
  21. IMR             EQU 0x00
  22. RTSR            EQU 0x08
  23. FTSR            EQU 0X0C
  24. PR              EQU 0x14
  25. NVIC_ISER_0     EQU 0xE000E100
  26. NVIC_IPR_1      EQU 0xE000E404
  27. NIVC_ISER_1     EQU 0xE000E104
  28.  
  29. RCC             EQU 0x40023800
  30. TIM6            EQU 0x40001000
  31. TIM4            EQU 0x40000800
  32. APB1ENR         EQU 0x40
  33. PSC             EQU 0x28
  34. ARR             EQU 0x2C
  35. SR              EQU 0x10
  36. CR1             EQU 0x00
  37. DIER            EQU 0x0C
  38. CNT             EQU 0x24
  39. DAC             EQU 0x40007400
  40. CR              EQU 0x00
  41. GPIO_A          EQU 0x40020000
  42.  
  43.  
  44. ;Main function
  45.             AREA    main, CODE, READONLY
  46.             EXPORT  __main, EXTI0_IRQHandler, TIM4_IRQHandler           ; make __main visible to linker
  47.             ENTRY          
  48.                
  49. __main      PROC
  50.             BL LED_INI     
  51.             BL BUTTON_INI
  52.             BL INT_INI
  53.             BL TIM4_INI
  54.                
  55.             CPSIE I         ;Enable interrupts
  56.                
  57. stop        B stop          ;Wait for interrupts       
  58.             ENDP
  59.  
  60. ;\ --------- INT_INI SUBROUTINE ---------  
  61. ;\  Initializes the EXTI0 interrupt
  62. ;\  Sets the user button as interrupt
  63. ;\  sets the interrupt to read rising edge                                          
  64. INT_INI     PROC
  65.             PUSH{R0,R1}             ;put the resigters into stack to protect them
  66.        
  67.             ;enalbe the PA0 with interrupt
  68.             LDR R0,=SYSCFG          ;Pointer to SYSCFG in memory
  69.             LDRH R1,[R0,#EXTICR1]   ;Pull up the Px[0] interrupt register
  70.             BIC R1,#0x000F          ;Set the interrupt to PA 0; The low four bits =0 PAx, =1 PBX, =2,PCX
  71.             STRH R1,[R0,#EXTICR1]   ;Save the new setting
  72.            
  73.             ;configure the EXTI interrupt setting
  74.             LDR R0,=EXTI            ;Point to interrupt settings
  75.             MOV R1,#1              
  76.             STR R1,[R0,#RTSR]       ;Set rising edge interrupt
  77.             STR R1,[R0,#PR]         ;Clear the pending register
  78.             STR R1,[R0,#IMR]       
  79.            
  80.             ;enable the external interrupt
  81.             LDR R0,=NVIC_ISER_0     ;Points to the external interrupt enable
  82.             LDR R1,=0x00000040      ;Enables interrupts 6. (EXTI0)
  83.             STR R1,[R0]             ;Save enabled interrupts
  84.            
  85.             ;enalbe the timer 4 interrupt
  86.             LDR R0,=NVIC_ISER_0     ;Points to the 2nd External interrupt enable
  87.             LDR R1,=(1<<30)         ;Enables the 30th bit, TIM4 interrupt
  88.             STR R1,[R0]             ;Save enabled interrupt
  89.                        
  90.             POP{R0,R1}              ; restore the resgestors
  91.             BX LR
  92.             ENDP
  93.                
  94. ;\ ------------- TIM4_INI -------------  
  95. ;\  Initializes the TIM4 clock
  96. ;\  Sets the TIM4 to interrupt every sencond
  97. ;\  
  98. TIM4_INI PROC
  99.         PUSH{R0,R1}
  100.        
  101.         LDR R0,=TIM4            ;Points to TIMx register
  102.         LDR R1,=RCC             ;Points to RCC register
  103.        
  104.         ;Enable the colock to time 4
  105.         LDR R2,[R1,#APB1ENR]    ;Load timer register value,TIM2-3-4-5-6-7 on APB1
  106.         ;LDR R3,=0x00000004        
  107.         ;ORR R2,R3              ;Set TIM6 clock to on
  108.         LDR R2,=0X00000004      ;SET THE TIM CLK ON, TIM4 is controlled by the bit 2 of the APB1ENR (APB1ENR[2])
  109.         STR R2,[R1,#APB1ENR]    ;Store new clock settings
  110.        
  111.         ;set the Prescale Register, TIMx Counter +1 at evry PSC of system clock
  112.         LDRH R1,[R0,#PSC]       ;Load PSC register to R1
  113.         ;BIC R1,#0XFFFFFFFF         ;CLR PSC
  114.         MOV R1,#1599            ;Set pre-scale to count PSC = 1599+1
  115.         STRH R1,[R0,#PSC]       ;Save PSC value
  116.        
  117.         ;when the TIMx Counter reachs to ARR,the timer interrupts
  118.         LDRH R1,[R0,#ARR]       ;Load ARR register to R1
  119.         MOV R1,#4999            ;Set ARR to count 4999+1, COMBINE with PSC=1599+1, Interrupt every 0.5 second
  120.         STRH R1,[R0,#ARR]       ;Store new ARR value
  121.        
  122.         ;enable the timer interrupt
  123.         LDRH R1,[R0,#DIER]      ;Load DIER value
  124.         ORR R1,#0X01            ;Set bit-0 to 1
  125.         STRH R1,[R0,#DIER]      ;Store new DIER value
  126.        
  127.         ;clear the interrupt Flag
  128.         LDRH R1,[R0,#SR]        ;Load SR value
  129.         BIC R1,#0X01            ;Clear last bit
  130.         STRH R1,[R0,#SR]        ;Store bit-0 as 0
  131.        
  132.         ;enable the timer counter
  133.         LDR R1,[R0,#CR1]        ;Load CR1
  134.         BIC R1,#0XF             ;CLR THE FINAL 4 BITS
  135.         ORR R1,#0X01            ;Set bit-0 to 1
  136.         STR R1,[R0,#CR1]        ;Store new bit-0 value
  137.        
  138.         POP{R0,R1}
  139.         BX LR
  140.         ENDP
  141.                
  142.                
  143. ;\ --------- LED_INI SUBROUTINE ---------------------  
  144. ;\  Initializes the PD12-15 for output to operate the LED                                          
  145. LED_INI     PROC
  146.             ; Enable Clock of GPIO Port D
  147.             LDR r7, =RCC_BASE
  148.             LDR r1, [r7, #RCC_AHB1ENR]
  149.             ORR r1, r1,  #RCC_AHB1ENR_GPIODEN    
  150.             STR r1, [r7, #RCC_AHB1ENR]         
  151.        
  152.             LDR r7, =GPIOD_BASE                 ; Load GPIO Port D base address
  153.        
  154.             ; Set Pin 14 I/O direction as Digital Output
  155.             LDR r1, [r7, #GPIO_MODER]           ; r1 = GPIOD->MODER
  156.             ;BIC r1, r1, #(3 << 14*2)           ; Clear mode, their bit 27,28 of register GOPO_MODER to contorl PORT 14
  157.             ORR r1, r1, #(1 << 14*2)            ; PD 14, Input(00, reset), Output(01), Alternate Func(10), Analog(11)
  158.             ORR r1, r1, #(1 << 12*2)            ; PD 12, Input(00, reset), Output(01), Alternate Func(10), Analog(11)
  159.             ORR r1, r1, #(1 << 13*2)            ; PD 13,Input(00, reset), Output(01), Alternate Func(10), Analog(11)
  160.             ORR r1, r1, #(1 << 15*2)            ; PD 15,Input(00, reset), Output(01), Alternate Func(10), Analog(11)
  161.             STR r1, [r7, #GPIO_MODER]           ;      
  162.                
  163.             ;POP {R1}      
  164.             BX LR
  165.             ENDP
  166.                    
  167. ;\ ---------------------- INITBUTTON SUBROUTINE ------------------------------  
  168. ;\  Initializes GPIOA pins. Sets PA0(which connects to the Button) as an input
  169. BUTTON_INI  PROC
  170.             ; Enable Clock of GPIO Port A
  171.             LDR r7, =RCC_BASE
  172.             LDR r1, [r7, #RCC_AHB1ENR]
  173.             ORR r1, r1,  #RCC_AHB1ENR_GPIOAEN    
  174.             STR r1, [r7, #RCC_AHB1ENR]
  175.        
  176.             LDR r7, =GPIOA_BASE                 ; Load GPIO Port A base address
  177.        
  178.             ; Set Pin 0 I/O direction as Digital Iutput
  179.             LDR r1, [r7, #GPIO_MODER]           ; r1 = GPIOD->MODER
  180.             BIC r1, r1, #3                      ; Clear mode,and Input(00, reset), Output(01), Alternate Func(10), Analog(11)
  181.             STR r1, [r7, #GPIO_MODER]           ;      
  182.            
  183.             BX LR
  184.             ENDP
  185.                
  186. ;\ --------- LED_ON SUBROUTINE -------------
  187. ;\  switch on the led,
  188. ;\   r0, as the input to point the LED port
  189. ;         GPIO_ODR_ODR_12, to on GREEN LED
  190. ;         GPIO_ODR_ODR_13, to on ORANGE LED
  191. ;         GPIO_ODR_ODR_14, to on RED LED
  192. ;         GPIO_ODR_ODR_15, to on BLUE LED
  193. LED_ON  PROC
  194.             PUSH {R1}
  195.             ; Output 1
  196.             LDR r7, =GPIOD_BASE                 ; Load GPIO Port D base address
  197.             LDR r1, [r7, #GPIO_ODR]
  198.             ORR r1, r1,  r0
  199.             STR r1, [r7, #GPIO_ODR]
  200.             POP {R1}       
  201.             BX LR
  202.             ENDP
  203.  
  204. ;\ --------- LED_OFF SUBROUTINE ---------  
  205. ;\  switch off the led
  206. ;\   r0, as the input to point the LED port
  207. ;         GPIO_ODR_ODR_12, to off GREEN LED
  208. ;         GPIO_ODR_ODR_13, to off ORANGE LED
  209. ;         GPIO_ODR_ODR_14, to off RED LED
  210. ;         GPIO_ODR_ODR_15, to off BLUE LED
  211. LED_OFF PROC
  212.             PUSH {R1}
  213.             ; Output 1
  214.             LDR r7, =GPIOD_BASE                 ; Load GPIO Port D base address
  215.             LDR r1, [r7, #GPIO_ODR]
  216.             BIC r1, r1, r0
  217.             STR r1, [r7, #GPIO_ODR]
  218.  
  219.             POP {R1}       
  220.             BX LR
  221.             ENDP
  222.                
  223. ;\ --------- LED_OPERATION SUBROUTINE ---------  
  224. ;\  based on the vaule of LED_COUNTER to operate
  225. ;\ the LEDs: 0: off all LED, 1: Green ON,
  226. ;\           2: Green-Orange ON,3: Green-Orange-Red ON,
  227. ;\           4: Green-Orange-Red-Blue ON
  228. LED_OPERATION   PROC
  229.             PUSH {LR}       ;nest subroutine, need protect the LR
  230.             LDR r3,=LED_COUNTER
  231.             LDR r7,[r3]
  232.             CMP r7,#0       ; if LED_COUNTER == 0  
  233.             BEQ OFF_ALL
  234.             CMP r7,#1       ; if LED_COUNTER == 1
  235.             BEQ GREEN_ON
  236.             CMP r7,#2       ; if LED_COUNTER == 2
  237.             BEQ ORANGE_ON
  238.             CMP r7,#3       ; if LED_COUNTER == 3
  239.             BEQ RED_ON
  240.             CMP r7,#4       ; if LED_COUNTER == 3
  241.             BEQ BLUE_ON
  242. OFF_ALL    
  243.             MOV r0,#GPIO_ODR_ODR_12
  244.             BL LED_OFF
  245.             MOV r0,#GPIO_ODR_ODR_13
  246.             BL LED_OFF
  247.             MOV r0,#GPIO_ODR_ODR_14
  248.             BL LED_OFF
  249.             MOV r0,#GPIO_ODR_ODR_15
  250.             BL LED_OFF
  251.             B END_LEDOPERATION
  252. GREEN_ON
  253.             MOV r0,#GPIO_ODR_ODR_12
  254.             BL LED_ON
  255.             B END_LEDOPERATION
  256. ORANGE_ON
  257.             MOV r0,#GPIO_ODR_ODR_13
  258.             BL LED_ON
  259.             B END_LEDOPERATION
  260. RED_ON
  261.             MOV r0,#GPIO_ODR_ODR_14
  262.             BL LED_ON
  263.             B END_LEDOPERATION
  264. BLUE_ON
  265.             MOV r0,#GPIO_ODR_ODR_15
  266.             BL LED_ON
  267.             B END_LEDOPERATION
  268. END_LEDOPERATION
  269.             POP {LR} ;nest subroutine, need protect the LR
  270.             BX LR
  271.             ENDP
  272.                
  273. ;\------------- EXTI0_IRQHandler -------------
  274. ;\ The interrupt service rountine,
  275. ;\ if the KEY is 1, set KEY to 0
  276. ;\ if the KEY is 0, set KEY to 1
  277. EXTI0_IRQHandler PROC
  278.             LDR R5,=0x700000        ;Debounce Counter
  279. lpwait      SUBS R5,#1              ;Subtract 1 from R5, set flags
  280.             BNE lpwait              ;Keep subtracting until R5 == 0
  281.        
  282.             LDR R2,=EXTI            ;Point to EXTI settings
  283.             MOV R1,#1               ;Move a 1 to bit  0
  284.             STR R1,[R2,#PR]         ;Store 1, clearing pending interrupts
  285.        
  286.             LDR r5,=KEY
  287.             LDR r6,[r5]
  288.             CMP r6,#1       ;Compare r0 with 1
  289.             BCS CLEAR_KEY   ;if key highter than or same with 1 set it t0 0
  290.             MOV r7,#1
  291.             STR r7,[r5]
  292.             B END_HANDLER
  293. CLEAR_KEY   MOV r7,#0
  294.             STR r7,[r5]
  295. END_HANDLER BX LR
  296.             ENDP
  297.                
  298. ;\------------- TIM4_IRQHandler -------------------
  299. ;\ when KEY = 1, every interrupt light leds in turns
  300. ;\ when KEY = 0, off all LEDS
  301. TIM4_IRQHandler PROC
  302.             ;clear the interrupt flag
  303.             LDR R0,=TIM4            ;Point to timer 4 settings
  304.             LDRH R1,[R0,#SR]        ;Load SR value
  305.             BIC R1,#0x0001          ;Clear last bit
  306.             STRH R1,[R0,#SR]        ;Store bit-0 as 0
  307.            
  308.             ;PUSH{LR}
  309.             LDR r5,=KEY
  310.             LDR r6,[r5]
  311.             TST r6,#1           ;Compare r0 with 1
  312.             BEQ CLEAR_COUNTER   ; when KEY = 0, off all LED by clear LED_COUNTER
  313.            
  314.             ;when KEY !=0 add 1 to LED_COUNTER
  315.             LDR r3,=LED_COUNTER
  316.             LDR r7,[r3]
  317.             CMP r7,#4       ; if LED_COUNTER == 4
  318.             BNE ADD_COUTER  ; WHEN LED_COUNTER != 4 LED_COUNTER +=1
  319. CLEAR_COUNTER          
  320.             MOV r7,#0       ;if yes, set LED_COUNTER to 0
  321.             STR r7,[r3]     ; save to LED_COUNTER
  322.             B UPDATE_LED
  323. ADD_COUTER
  324.             ADD r7,#1
  325.             STR r7,[r3]     ; save to LED_COUNTER
  326. UPDATE_LED 
  327.             PUSH{LR}
  328.             BL LED_OPERATION
  329.             POP{LR}
  330. END_TIM_HANDLER
  331.             BX LR
  332.             ENDP
  333.                
  334.             AREA Data1,DATA
  335.             EXPORT KEY,LED_COUNTER
  336. KEY         DCB 0       ;Holds a KEY, when 1 enable the TIM4 to light the LED, when 0 off all LED
  337. LED_COUNTER DCB 0       ;0: off all LED, 1 Green ON, 2 Green-Orange ON,3 Green-Orange-Red ON,4 Green-Orange-Red-Blue ON  
  338.             END
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement