Last active
June 2, 2019 18:46
-
-
Save tranphuquy19/fec0e8f420861739baedd4c402204d8f to your computer and use it in GitHub Desktop.
Giải đề thi cuối kì, lập trình vi điều khiển stm32l1xx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
INCLUDE stm32l1xx_constants.s ; Load Constant Definitions | |
INCLUDE stm32l1xx_tim_constants.s ; TIM Constants | |
AREA main, CODE, READONLY | |
EXPORT __main ; make __main visible to linker | |
ENTRY | |
__main PROC | |
;enable the clock port A | |
BL GPIOA_CLOCK_Enable | |
;Init PA0 | |
LDR R0, =GPIOA_BASE | |
MOV R1, #0 | |
BL GPIOA_Output_Init | |
;Init PA1 | |
LDR R0, =GPIOA_BASE | |
MOV R1, #1 | |
BL GPIOA_Output_Init | |
;Init PA2 | |
LDR R0, =GPIOA_BASE | |
MOV R1, #2 | |
BL GPIOA_Output_Init | |
;SysTick Init | |
BL SysTick_Initialize | |
;load ODR | |
LDR R5, | |
main_loop | |
MOV R7, #1 ;state 0b001 (RED HIGH) | |
BL SetState | |
MOV R6, #20 ;Delay 20s | |
BL RepeatDelay | |
MOV R7, #2 ;state 0b010 (YELLOW HIGH) | |
BL SetState | |
MOV R6, #5 ;Delay 5s | |
BL RepeatDelay | |
MOV R7, #4 ;;state 0b100 (YELLOW HIGH) | |
BL SetState | |
MOV R6, #20 ;Delay 20s | |
BL RepeatDelay | |
B main_loop | |
stop B stop | |
ENDP | |
ALIGN | |
GPIOA_Output_Init PROC | |
; R0 = GPIOx_BASE | |
; R1 = pinID | |
MOV R2, #0 | |
MUL R3, R2 ;R3 = pinID * 2 | |
;set pinID of port A is output | |
LDR R4, [R0, #GPIO_MODER] | |
MOV R9, #0x03 | |
LSL R9, R9, R3 ; R9 = R9 << R3 === R9 = 0x03 << R3 | |
BIC R4, R9 | |
MOV R9, #0x01 | |
LSL R9, R9, R3 | |
ORR R4, R9 | |
STR R4, [R0, #GPIO_MODER] | |
;set pinID of port A is push-pull | |
LDR R4, [RO, #GPIO_OTYPER] | |
MOV R9, #0x1 | |
LSL R9, R9, R1 | |
BIC R4, R9 | |
STR R4, [R0, #GPIO_OTYPER] | |
;set IO speed as 10Mhz | |
LDR R4, [R0, #GPIO_SPEEDR] | |
MOV R9, #0x03 | |
LSL R9, R9, R3 ; R9 = R9 << R3 === R9 = 0x03 << R3 | |
BIC R4, R9 | |
MOV R9, #0x02 | |
ORR R4, R9 | |
STR R4, [R0, #GPIO_SPEEDR] | |
;set IO as no pull-up pull-down | |
LDR R4, [R0, #PUPDR] | |
MOV R9, #0x03 | |
LSL R9, R9, R3 ; mask: R9 = R9 << R3 === R9 = 0x03 << R3 | |
BIC R4, R9 | |
STR R4, [R0, #PUPDR] | |
BX LR | |
ENDP | |
SetState PROC | |
;R0 = GPIOx_BASE | |
;R7 = state | |
BIC R1, #7 | |
ORR R1, R1, R7 | |
STR R1, [R0, #GPIO_ODR] | |
BX LR | |
ENDP | |
RepeatDelay PROC | |
;R6 = n | |
repeatloop | |
CMP R6, #0 | |
BLE endrepeatloop | |
SUB R6, R6, #1 | |
B repeatloop | |
endrepeatloop | |
BX LR | |
ENDP | |
Delay PROC | |
MOV R10, R0 ;nTime | |
loop | |
CMP R10, #0 | |
BNE loop ;loop if R10 != 0 | |
BX LR | |
ENDP | |
SysTick_Handler PROC | |
EXPORT SysTick_Handler | |
SUB r10, #1 | |
BX LR | |
ENDP | |
SysTick_Initialize PROC | |
;set MSIRANGE = 110 ---> 4.194 MHz | |
LDR R7, =RCC_BASE ;Load address block RCC | |
LDR R1, [R7, #RCC_ICSCR] ; R1 = RCC->ICSCR | |
BIC R1, #(7 << 13) | |
ORR R1, #(6 << 13) | |
STR R1, [R7, #RCC_ICSCR] | |
;disable SysTick IRQ and SysTick Timer | |
LDR R1, =SysTick_BASE | |
LDR R2, [R1, #SysTick_CTRL] | |
BIC R2, #0x11 | |
STR R2, [R1, #SysTick_CTRL] | |
;set SysTick->LOAD | |
LDR R8, [R1, #SysTick_LOAD] | |
MOV R8, #4193 | |
STR R8, [R1, #SysTick_LOAD] | |
;reset counter value | |
MOV R8, #0 | |
STR R8, [R1, #SysTick_VAL] | |
;set process clock, enable IRQ and SysTick Timer | |
LDR R2, [R1, #SysTick_CTRL] | |
ORR R2, R2, #0x04 ;SysTick_CTRL_CLKSOURCE = 0x04 = 0b100 | |
ORR R2, R2, #0x01 ;SysTick_CTRL_ENABLE = 0x01 = 0b001 | |
ORR R2, R2, #0x02 ;Counting down to 0 | |
BX LR | |
ENDP | |
AREA myData, DATA, READWRITE | |
ALIGN | |
END |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Name: Den giao thong | |
Copyright: | |
Author: tranphuquy19 | |
Date: 02/06/19 17:48 | |
Description: | |
*/ | |
#include<stdint.h> | |
#include"stm32l1xx.h" | |
#define RED 0 | |
#define YELLOW 1 | |
#define GREEN 2 | |
uint32_t TimingDelay; | |
uint32_t states[] = {1, 2, 4}; | |
void GPIOA_Clock_Enable(){ | |
RCC->AHBENR |= RCC_AHBENR_GPIOAEN; | |
} | |
void GPIOA_Output_Init(int pinID){ | |
GPIOA->MODER &= ~(0x3 << (2*pinID)); | |
GPIOA->MODER |= 0x1 << pinID; | |
GPIOA->OTYPER &= ~(0x1 << pinID); | |
GPIOA->OSPEEDR &= ~(0x3 << (2*pinID)); | |
GPIOA->OSPEEDR |= (0x2 << (2*pinID)); | |
GPIOA->PUPDR &= ~ (0x3 << (2*pinID)); | |
} | |
void SysTick_Init(uint32_t ticks){ | |
RCC->ICSCR &= ~(0x7 << 13); | |
RCC->ICSCR |= (0x6 << 13); //set RANGE 6 = 4.194MHz | |
//Disable systick IRQ and systick counter | |
SysTick->CTRL = 0; | |
//Set SysTick load | |
SysTick->LOAD = ticks - 1; | |
// Set priority | |
NVIC_SetPriority (SysTick_IQRn, (1<<__NVIC_PRIO_BIT)-1); | |
//Reset SysTick counter value | |
SysTick->VAL = 0; | |
//Set processor clock | |
SysTick->CTRL |= SysTick_CTRL_CLKSOURCE; //0x4 | |
//Enable SysTicj IRQ and SysTick Counter | |
SysTick->CTRL |= SysTick_CTRL_ENABLE; | |
SysTick->CTRL |= SysTick_CTRL_TICKINT; | |
} | |
void SysTick_Handler(void){ | |
if(Timing_Delay != 0) TimingDelay--; | |
} | |
void Delay(uint32_t nTime){ | |
TimingDelay = nTime; | |
while(TimingDelay != 0); | |
} | |
void Repeat_Delay(int n){ | |
for(int i = 0; i< n; i++){ | |
Delay(1000); //Delay 1s each time | |
} | |
} | |
void Set_State(int index){ | |
GPIOA->ODR &= ~7; | |
GPIOA->ODR |= states[index]; | |
} | |
int main(void){ | |
GPIOA_Clock_Enable(); | |
GPIOA_Output_Init(RED); | |
GPIOA_Output_Init(YELLOW); | |
GPIOA_Output_Init(GREEN); | |
SysTick_Init(4194); | |
int index = 0; | |
while(true){ | |
Set_State(index); | |
if(index == 0 || index == 2) Repeat_Delay(20); //Delay 20s | |
else Repeat_Delay(5); | |
index ++; | |
index = index % 3; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment