Tải bản đầy đủ (.pdf) (22 trang)

Lập trình vi điều khiển STM32L152 bài tập chương 10 sách Embedded Systems with ARM CortexM Microcontrollers in Assembly Language and C (Third Edition – Dr Yifeng Zhu)

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (432.52 KB, 22 trang )

LẬP TRÌNH VI ĐIỀU KHIỂN STM32L152
(Sách Embedded Systems with ARM Cortex-M Microcontrollers
in Assembly Language and C (Third Edition) – Dr Yifeng Zhu )

BÀI TẬP CHƯƠNG 10 (Có code đính kèm)
1 XoaKyTuTrongChuoi_C Call
a. C Program
#include "stm32l1xx.h"
extern void loaikytu(char *str,unsigned char kytu);
char string[] = "lieneeeeeeechieeeeeeeeu\0";

int main(){
__asm{
nop
}
loaikytu(string,'e');
while(1);
}
b. Assemply Program
;******************** (C) Yifeng ZHU ********************
; @file

main.s

; @author Yifeng Zhu
; @version V1.0.0
; @date

May-5-2012



; @note
; @brief Assembly code for STM32L1xx Discovery Kit
; @note
;

This code is for the book "Embedded Systems with ARM Cortex-M3

;

Microcontrollers in Assembly Language and C, Yifeng Zhu,

;

ISBN-10: 0982692625.

; @attension
;

This code is provided for education purpose. The author shall not be

;

held liable for any direct, indirect or consequential damages, for any

;

reason whatever. More information can be found from book website:

;


/>
;********************************************************

; STM32L1xx Discovery Kit:
; - USER Pushbutton: connected to PA0 (GPIO Port A, PIN 0), CLK
RCC_AHBENR_GPIOAEN
; - RESET Pushbutton: connected RESET
; - GREEN LED: connected to PB7 (GPIO Port B, PIN 7), CLK
RCC_AHBENR_GPIOBEN
; - BLUE LED: connected to PB6 (GPIO Port B, PIN 6), CLK
RCC_AHBENR_GPIOBEN
; - Linear touch sensor/touchkeys: PA6, PA7 (group 2), PC4, PC5 (group 9), PB0,
PB1 (group 3)
INCLUDE stm32l1xx_constants.s

; Load Constant Definitions


INCLUDE stm32l1xx_tim_constants.s ; TIM Constants

AREA

removeCharacter, CODE

ALIGN

loaikytu
EXPORT

PROC

loaikytu

;loai 1 ky tu ra khoi 1 chuoi
;vao : r0 chua dia chi cua chuoi, r1 chua ky tu
;ra

: khong

PUSH

{lr,r4,r5}

loop

LDRB

r2,[r0],#1

CBZ r2,stop

;load string
;if 0 -> stop

CMP r2,r1

BNE loop

;if r2!= r1 continue

SUB r0,#1


;else loai ky tu ra khoi chuoi

MOV r4,r0
loai
LDRB
cac ky tu phia sau ve phia truoc
STRB r5,[r4],#1
CMP r5,#0
BEQ loop

r5,[r4,#1]

;di chuyen


B

loai

stop

POP {lr,r4,r5}

BX

lr

ENDP
2 NoiChuoi_S Call

a. C Program
#include <stdint.h>
#include <stdio.h>
#include "stm32l1xx.h"

void strct(char * dst, char* src){
while(*dst++);
while(*dst++ == *src++);
}
b. Assemply Program
;******************** (C) Yifeng ZHU ********************
; @file

main.s

; @author Yifeng Zhu
; @version V1.0.0
; @date

May-5-2012

; @note
; @brief Assembly code for STM32L1xx Discovery Kit
; @note


;

This code is for the book "Embedded Systems with ARM Cortex-M3


;

Microcontrollers in Assembly Language and C, Yifeng Zhu,

;

ISBN-10: 0982692625.

; @attension
;

This code is provided for education purpose. The author shall not be

;

held liable for any direct, indirect or consequential damages, for any

;

reason whatever. More information can be found from book website:

;

/>
;********************************************************

; STM32L1xx Discovery Kit:
; - USER Pushbutton: connected to PA0 (GPIO Port A, PIN 0), CLK
RCC_AHBENR_GPIOAEN
; - RESET Pushbutton: connected RESET

; - GREEN LED: connected to PB7 (GPIO Port B, PIN 7), CLK
RCC_AHBENR_GPIOBEN
; - BLUE LED: connected to PB6 (GPIO Port B, PIN 6), CLK
RCC_AHBENR_GPIOBEN
; - Linear touch sensor/touchkeys: PA6, PA7 (group 2), PC4, PC5 (group 9), PB0,
PB1 (group 3)

INCLUDE stm32l1xx_constants.s

; Load Constant Definitions

INCLUDE stm32l1xx_tim_constants.s ; TIM Constants


IMPORT

strcat

AREA main, CODE, READONLY
EXPORT __main

; make __main visible to linker

ENTRY

__main

PROC

NOP

ldr

r0,=str1

ldr

r1,=str2

bl

strcat

stop

B

stop

ENDP

product
bx

PROC
lr

ENDP

AREA


myData, DATA, READWRITE

ALIGN
str1

DCB

trong

space 100

str2

DCB

"this is tha thu",0

" ma minh ve len thoi",0


END
3 DaoChuoi_C Call
a. C Program
#include "stm32l1xx.h"

extern void hoandoi(char** str1,char** str2);
char s1[] = "day la cai tha thu \0";
char s2[] = "ma minh ve len thoi\0";
char *str1 = s1;
char *str2 = s2;


int main(){
__asm{
nop
}
hoandoi(&str1,&str2);
str1[0] = '1';
str2[0] = '2';
while(1);
}
b. Assemply Program
;******************** (C) Yifeng ZHU ********************
; @file

main.s

; @author Yifeng Zhu


; @version V1.0.0
; @date

May-5-2012

; @note
; @brief Assembly code for STM32L1xx Discovery Kit
; @note
;

This code is for the book "Embedded Systems with ARM Cortex-M3


;

Microcontrollers in Assembly Language and C, Yifeng Zhu,

;

ISBN-10: 0982692625.

; @attension
;

This code is provided for education purpose. The author shall not be

;

held liable for any direct, indirect or consequential damages, for any

;

reason whatever. More information can be found from book website:

;

/>
;********************************************************

; STM32L1xx Discovery Kit:
; - USER Pushbutton: connected to PA0 (GPIO Port A, PIN 0), CLK
RCC_AHBENR_GPIOAEN

; - RESET Pushbutton: connected RESET
; - GREEN LED: connected to PB7 (GPIO Port B, PIN 7), CLK
RCC_AHBENR_GPIOBEN
; - BLUE LED: connected to PB6 (GPIO Port B, PIN 6), CLK
RCC_AHBENR_GPIOBEN


; - Linear touch sensor/touchkeys: PA6, PA7 (group 2), PC4, PC5 (group 9), PB0,
PB1 (group 3)
INCLUDE stm32l1xx_constants.s

; Load Constant Definitions

INCLUDE stm32l1xx_tim_constants.s ; TIM Constants

AREA bai10_3,code
ALIGN

hoandoi

PROC

EXPORT hoandoi
;ham hoan doi 2 chuoi (hoan doi con tro)
;vao : r0, r1 chua dia chi cua con tro tro den 2 chuoi
;ra : void
ldr r2,[r0]
ldr r3,[r1]
str r2,[r1]
str r3,[r0]


bx lr
ENDP
4 TimKyTuTrongChuoi_S Call
a. C Program
char *search(char * s, char c){
char *p = 0;


for(;*s;s++)
if(*s == c)
p = s;
return p;
}
b. Assemply Program
;******************** (C) Yifeng ZHU ********************
; @file

main.s

; @author Yifeng Zhu
; @version V1.0.0
; @date

May-5-2012

; @note
; @brief Assembly code for STM32L1xx Discovery Kit
; @note
;


This code is for the book "Embedded Systems with ARM Cortex-M3

;

Microcontrollers in Assembly Language and C, Yifeng Zhu,

;

ISBN-10: 0982692625.

; @attension
;

This code is provided for education purpose. The author shall not be

;

held liable for any direct, indirect or consequential damages, for any

;

reason whatever. More information can be found from book website:

;

/>
;********************************************************



; STM32L1xx Discovery Kit:
; - USER Pushbutton: connected to PA0 (GPIO Port A, PIN 0), CLK
RCC_AHBENR_GPIOAEN
; - RESET Pushbutton: connected RESET
; - GREEN LED: connected to PB7 (GPIO Port B, PIN 7), CLK
RCC_AHBENR_GPIOBEN
; - BLUE LED: connected to PB6 (GPIO Port B, PIN 6), CLK
RCC_AHBENR_GPIOBEN
; - Linear touch sensor/touchkeys: PA6, PA7 (group 2), PC4, PC5 (group 9), PB0,
PB1 (group 3)
INCLUDE stm32l1xx_constants.s

; Load Constant Definitions

INCLUDE stm32l1xx_tim_constants.s ; TIM Constants

IMPORT

search

AREA main, CODE, READONLY
EXPORT __main

; make __main visible to linker

ENTRY

__main
NOP
ldr r0,=str1

ldr r1,='u'
bl search

PROC


ldr r0,[r0]

stop

B stop

ENDP

product

PROC

bx lr
ENDP

AREA myData, DATA, READWRITE
ALIGN
str1

DCB "this is tha thu",0

trong

space 100


str2

DCB " ma minh ve len thoi",0

END
5 Struct_S Call
a. C Program
struct Student_T{char c1;char c2;int score;char c3;
}
students[10];
void khoiTaoStruct(){
int i;
for( i=0 ; i<10 ; i++ ){
students[i].score = i;


students[i].c1 = 'x';
}
}
b. Assemply Program
;******************** (C) Yifeng ZHU ********************
; @file

main.s

; @author Yifeng Zhu
; @version V1.0.0
; @date


May-5-2012

; @note
; @brief Assembly code for STM32L1xx Discovery Kit
; @note
;

This code is for the book "Embedded Systems with ARM Cortex-M3

;

Microcontrollers in Assembly Language and C, Yifeng Zhu,

;

ISBN-10: 0982692625.

; @attension
;

This code is provided for education purpose. The author shall not be

;

held liable for any direct, indirect or consequential damages, for any

;

reason whatever. More information can be found from book website:


;

/>
;********************************************************

; STM32L1xx Discovery Kit:


; - USER Pushbutton: connected to PA0 (GPIO Port A, PIN 0), CLK
RCC_AHBENR_GPIOAEN
; - RESET Pushbutton: connected RESET
; - GREEN LED: connected to PB7 (GPIO Port B, PIN 7), CLK
RCC_AHBENR_GPIOBEN
; - BLUE LED: connected to PB6 (GPIO Port B, PIN 6), CLK
RCC_AHBENR_GPIOBEN
; - Linear touch sensor/touchkeys: PA6, PA7 (group 2), PC4, PC5 (group 9), PB0,
PB1 (group 3)
INCLUDE stm32l1xx_constants.s

; Load Constant Definitions

INCLUDE stm32l1xx_tim_constants.s ; TIM Constants

IMPORT

students

IMPORT

khoiTaoStruct


AREA main, CODE, READONLY
EXPORT __main

; make __main visible to linker

ENTRY

__main

PROC

NOP

bl khoiTaoStruct

;khoi tao score cho mang struct

ldr r0,=students
mov r1,#0

;tong score


mov r2,#0

;bien lap tinh

mov r3,#0


;offset cua score

mov r4,#12

;moi bien struct co kich thuoc la 12 byte

lap

cmp r2,#10

;while r2<10

beq stop
mul r3,r2,r4

;lay dia chi cua phan tu thu r2 trong mang

add r3,#4

;dia chi offset cua score trong phan tu do

ldr r3,[r0,r3]

;cong score vao tong

add r1,r3

add r2,#1
b lap


stop

B stop

ENDP

AREA myData, DATA, READWRITE
ALIGN
str1

DCB "this is tha thu",0

trong

space 100

str2

DCB " ma minh ve len thoi",0


END
6 MaxFourNumber_C Call
a. C Program
#include "stm32L1xx.h"

extern int max4(int, int, int, int);
int ketqua = 0xffffffff;

int main(){

__asm{
nop
}
ketqua = max4(1,2,3,4);
ketqua = max4(6,3,-54,9);
ketqua = max4(1,3,6,-9);
ketqua = max4(-9,8,6,5);
ketqua = max4(1,2,3,-6);
ketqua = max4(6,-9,8,4);
while(1);
}
b. Assemply Program
;******************** (C) Yifeng ZHU ********************
; @file

main.s

; @author Yifeng Zhu


; @version V1.0.0
; @date

May-5-2012

; @note
; @brief Assembly code for STM32L1xx Discovery Kit
; @note
;


This code is for the book "Embedded Systems with ARM Cortex-M3

;

Microcontrollers in Assembly Language and C, Yifeng Zhu,

;

ISBN-10: 0982692625.

; @attension
;

This code is provided for education purpose. The author shall not be

;

held liable for any direct, indirect or consequential damages, for any

;

reason whatever. More information can be found from book website:

;

/>
;********************************************************

; STM32L1xx Discovery Kit:
; - USER Pushbutton: connected to PA0 (GPIO Port A, PIN 0), CLK

RCC_AHBENR_GPIOAEN
; - RESET Pushbutton: connected RESET
; - GREEN LED: connected to PB7 (GPIO Port B, PIN 7), CLK
RCC_AHBENR_GPIOBEN
; - BLUE LED: connected to PB6 (GPIO Port B, PIN 6), CLK
RCC_AHBENR_GPIOBEN


; - Linear touch sensor/touchkeys: PA6, PA7 (group 2), PC4, PC5 (group 9), PB0,
PB1 (group 3)
INCLUDE stm32l1xx_constants.s

; Load Constant Definitions

INCLUDE stm32l1xx_tim_constants.s ; TIM Constants

AREA bai10_4,code
ALIGN

max4

PROC

EXPORT max4
;ham tinh max 4 so nguyen co dau
;vao : r0-r4 chua 4 so nguyen
;ra : r0 chua max
cmp

r0,r1


movlt

r0,r1

cmp

r0,r2

movlt

r0,r2

cmp

r0,r3

movlt

r0,r3

bx lr
ENDP
7 TKiemTraDoiXung_C Call
a. C Program

;if(r0
r0 = r1


;if(r0
r0 = r2

;if(r0
r0 = r3


#include "stm32L1xx.h"
extern int sodoixung(int);
int ketqua = 0xffffffff;
int main(){
__asm{
nop
}
ketqua = sodoixung(121);
//ketqua = sodoixung(1121);
//ketqua = sodoixung(1234554321);
//ketqua = sodoixung(111011);
//ketqua = sodoixung(0);
while(1);
}
b. Assemply Program
;******************** (C) Yifeng ZHU ********************
; @file

main.s

; @author Yifeng Zhu

; @version V1.0.0
; @date

May-5-2012

; @note
; @brief Assembly code for STM32L1xx Discovery Kit
; @note
;

This code is for the book "Embedded Systems with ARM Cortex-M3


;

Microcontrollers in Assembly Language and C, Yifeng Zhu,

;

ISBN-10: 0982692625.

; @attension
;

This code is provided for education purpose. The author shall not be

;

held liable for any direct, indirect or consequential damages, for any


;

reason whatever. More information can be found from book website:

;

/>
;********************************************************

; STM32L1xx Discovery Kit:
; - USER Pushbutton: connected to PA0 (GPIO Port A, PIN 0), CLK
RCC_AHBENR_GPIOAEN
; - RESET Pushbutton: connected RESET
; - GREEN LED: connected to PB7 (GPIO Port B, PIN 7), CLK
RCC_AHBENR_GPIOBEN
; - BLUE LED: connected to PB6 (GPIO Port B, PIN 6), CLK
RCC_AHBENR_GPIOBEN
; - Linear touch sensor/touchkeys: PA6, PA7 (group 2), PC4, PC5 (group 9), PB0,
PB1 (group 3)
INCLUDE stm32l1xx_constants.s

; Load Constant Definitions

INCLUDE stm32l1xx_tim_constants.s ; TIM Constants

AREA bai10_7,code
ALIGN


sodoixung

EXPORT

PROC
sodoixung

;ham kiem tra so doi xung
;vao : r0 chua so can kiem tra
;ra : r0 = 1 neu la so doi xung va nguoc lai
push {lr,r4,r5,r6}
mov

r6,sp

;luu gia tri sp lai de tao bien cuc bo trong stack

cmp

r0,#0

;if(r0 == 0)

moveq

r0,#1

beq

ketthuc

return 1


sub r4,sp,#4

;luu vao r4 dia chi cua phan tu dau tien

mov r1,r0

;luu doi so vao r1, de r0 lam ket qua tra ve

mov r0,#1

;khoi tao ket qua tra ve la 1

mov r2,#10

;dung trong phep chia

lap

cmp r1,#0

;dua cac chu so cua r0 vao stack

beq

kiemtra

udiv r3,r1,r2
mls


;r3 = r1%r2

r3,r2,r3,r1

push {r3}

;luu vao trong stack de kiem tra doi xung


udiv

r1,r2

b

lap

kiemtra

mov r3,r4

;r3 lap tu dau day

mov

r2,sp

;r2 lap tu cuoi day

loop


cmp r2,r3

;while r2
bhs

ketthuc

ldr

r4,[r2]

ldr

r5,[r3]

cmp

r4,r5

movne

r0,#0

;r0 = 0

bne

ketthuc


;break

add

r2,#4

;else, tang r2, giam r3, tiep tuc lap

sub

r3,#4

b

loop

ketthuc

mov sp,r6

pop {lr,r4,r5,r6}
bx
ENDP

;if string[r1] != string[r2]

lr




×