Take input from button & generate output to LED in ASM

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Take input from button & generate output to LED in ASM

Jump to solution
1,667 Views
jameshayek
Contributor II

Hello all, thanks for the community, you guys are great, I was able to make progress from my last post.

I have a question in regard to my code for a project I started in school.

I was able to equate addresses, initialize the clock pins, and define the I/O pins for use.

I wrote a code that steps through the three colors on the on board LED. What I aim to do now, is to take input from a pin PORTA_PCR13 and set the led to one color when the button is not pressed, and another color when the button is pressed.

In C++ I would use an If, Then statement. However, I am unsure as to how I can implement this in ASM. Below is my code.

Any advice?

;Memory Address information found in the KL25P80M48SF0RM document |||||||||||||||| ;/////////////////////////////////////////////////////////////////////////////////

SIM_SCGC5   EQU  0x40048038     ;SIM_SCGC5 address            The control for each ports Clock Gate PORTB_PCR18 EQU  0x4004A000 + 4 * 18 ;PORTB_PCR18 address     LED Pin for RED

;Our added code to the example code found above in the lab manual PORTB_PCR19 EQU  0x4004A04C     ;PORTB_PCR19 address          LED Pin for GREEN PORTA_PCR13 EQU  0x40049034    ;PORTA_PCR13 address       Pin used for button PORTD_PCR1  EQU  0x4004C004   ;PORTD_PCR1 address           LED Pin for BLUE

GPIOA_PDDR  EQU  0x400FF014     ;GPIOA_PDDR Register Address  PDDR is Port Data Direction Register  GPIOB_PDDR  EQU  0x400FF054     ;GPIOA_PDDR Register Address  This sets the pin as input or output GPIOD_PDDR  EQU  0x400FF0D4     ;GPIOB_PDDR Register Address GPIOA_PDIR  EQU  0x400FF010  ;GPIOA_PDIR Register Address  This is the Port Data Input Register GPIOB_PDOR EQU   0x400FF040 GPIOD_PDOR EQU   0x400FF0C0 AREA asm_area, CODE, READONLY EXPORT  asm_main

asm_main  ;assembly entry point for C function, do not delete ; Add program code below here

;Initialize clock pins ||||||||||||||||||||||||||||||||||||||||||||||||||||||||||| ;/////////////////////////////////////////////////////////////////////////////////

BL  init_gpio

loop B loop init_gpio LDR R0,=SIM_SCGC5     ;Load address of SIM_SCGC5 to R0 LDR R1,[R0]           ;Put value of SIM_SCGC5 into R1 LDR R2,=0x00003E00    ;Load value to turn on all port clocks into R2 ORRS R1,R2            ;OR R2 into R1 for use of masking STR R1,[R0]           ;Put value back into SIM_SCGC5, ;This puts 0011111000000000, bianary of 3E, into the Sim_SCGC5 register ;Which turns on the port clocks A-E. See Onur Yilmaz - GPIO.ppt Slide 3 LDR R0,=PORTB_PCR18 ;Load address of PORTB_PCR18 to R0 LDR R1,=0x00000100 ;Load value to R1 STR R1,[R0]        ;Put value into PORTB_PCR18 LDR R0,=PORTB_PCR19 ;Load address of PORTB_PCR19 to R0 LDR R1,=0x00000100 ;Load value to R1 STR R1,[R0]        ;Put value into PORTB_PCR19

LDR R0,=PORTD_PCR1 ;Load address of PORTD_PCR1 to R0 LDR R1,=0x00000100 ;Load value to R1 STR R1,[R0]        ;Put value into PORTD_PCR1

LDR R0,=PORTA_PCR13 ;Load address of PORTA_PCR13 to R0 LDR R1,=0x00000102 ;Load value to R1 STR R1,[R0]        ;Put value into PORTA_PCR13 ;\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ ;Initialize GPIO's and setup I/O status |||||||||||||||||||||||||||||||||||||||||| ;/////////////////////////////////////////////////////////////////////////////////

LDR R0,=GPIOA_PDDR   ;Load address of GPIOA to R0 LDR R1,=0x00000000   ;Load value to R1     Set to ALL INPUTS. O input, 1 Output. Convert to Binary to see what PTB is set STR R1,[R0]          ;Put value into GPIOA           LDR R0,=GPIOB_PDDR   ;Load address of GPIOB to R0 LDR R1,=0x000C0000   ;Load value to R1      Outputs configured on PTB18 and PTB19, the LEDs on thos ports as outputs STR R1,[R0]          ;Put value into GPIOB LDR R0,=GPIOD_PDDR   ;Load address of GPIOD to R0 LDR R1,=0x00000002   ;Load value to R1      Output configured on PTC1 STR R1,[R0]          ;Put value into GPIOD ;this is where a loop would exist ;read the PDIR to take an input and branch to a location that will enable the color ;loop ;LDR R0,=GPIOA_PDIR ;LDR R1,[R0] ;LDR R2, =0x00002000         ;Must check this block of code    ;ANDS R1,R2     ;Not sure if it takes an input from pin A13 ;CMP R1,R2 ;BEQ loop LDR R0,=GPIOB_PDOR LDR R1,=0xFFFFFFFF STR R1,[R0] LDR R0,=GPIOD_PDOR LDR R1,=0xFFFFFFFF STR R1,[R0]   LDR R0,=GPIOB_PDOR LDR R1,=0x00040000 STR R1,[R0] LDR R0,=GPIOB_PDOR LDR R1,=0xFFFFFFFF STR R1,[R0] LDR R0,=GPIOB_PDOR LDR R1,=0x00080000 STR R1,[R0]

LDR R0,=GPIOB_PDOR LDR R1,=0xFFFFFFFF STR R1,[R0] LDR R0,=GPIOD_PDOR LDR R1,=0xFFFFFFFD STR R1,[R0] LDR R0,=GPIOD_PDOR LDR R1,=0xFFFFFFFF STR R1,[R0] BX LR

;Control indivual pins or LED's |||||||||||||||||||||||||||||||||||||||||||||||||| ;///////////////////////////////////////////////////////////////////////////////// LDR R2, =0x00000000  ;Counter to see Iif button is held down LDR R4,= 0x00000000  ;Counter for LED COLOR CYLE

;/////////////////////////////////////////////////// ; Put constants here

AREA data_area, DATA, READWRITE ; Put variables here END

Thanks in advance.

Labels (1)
Tags (2)
0 Kudos
1 Solution
1,264 Views
mjbcswitzerland
Specialist V

James

You will need to check the register contents and also the CCR flags that are affected when stepping.

- the CMP is not required because the AND already sets the CCR with the result of whether it was zero or not

- you certainly need a jump after the first block otherwise it just continues into the second and you always have the same result at the end

Regards

Mark

View solution in original post

0 Kudos
5 Replies
1,264 Views
mjbcswitzerland
Specialist V

James

Read the value to a register, mask the bit of interest and then do a compare.

CMP R0, #0

BEQ.N _label1

do things when true and return or jump to _label2

_label1

do things when false

_label2

continue

However you really should start using C asap because I don't think anyone is really interested with messing about in assembler (maybe back in the 1970s it would have been used). In any case just write what you want in C and see what the compiler spits out in assembled code since it is the easiest way to learn.

Regards

Mark

0 Kudos
1,264 Views
jameshayek
Contributor II

After reviewing your first comment and code I wrote the following (this was placed after my block of comments and before clearing GPIOB_PDOR):

LDR R0,=GPIOA_PDIR
LDR R1,[R0]
LDR R2, =0x00002000         ;Mask the bit of interest, this is for PTA13   
ANDS R1,R2
CMP R1,R2                         ;Compare the results
BEQ _onColor

_offColor

LDR R0,=GPIOB_PDOR
LDR R1,=0x00080000

STR R1,[R0]

_onColor

LDR R0,=GPIOD_PDOR
LDR R1,=0xFFFFFFFD
STR R1,[R0]

I shorted pin PTA13 to ground and when I step through the code, I expected to see the pin color I specified light up, yet it does not. This code chronologically steps throgh each line, dispite creating the labels _onColor and _offColor

.

I am not sure what I am doing wrong.

I did not know I could write in C then transcribe to ASM. I will try that for the next assignment, this ASM is 90% done. All I need is to get the button functionality for this project.

The class is 50% ASM and 50% C

0 Kudos
1,264 Views
yasuhikokoumoto
Senior Contributor I

Hello james,

I wrote a part of the code according to Mr. Mark's instructions.

It was a little modified to pass the GCC assembler.

LOOP: LDR R0,=GPIOA_PDIR LDR R1,[R0]  LDR R2, =0x00002000 AND R1,R2      BNE _onColor

_offColor:

LDR R0,=GPIOB_PDOR LDR R1,=0x00080000 STR R1,[R0] B  NEXT

_onColor: LDR R0,=GPIOD_PDO LDR R1,=0xFFFFFFFD  STR R1,[R0]

NEXT: /* do something?  */

B  LOOP

Can it help you?

Best regards,

Yasuhiko Koumoto.

1,264 Views
jameshayek
Contributor II

Thank you for everyone's help.

I was able to take information learned from Mark as well as a buddies sample code, and write this code which works like expected.

loop

  LDR R0,=GPIOA_PDIR

  LDR R1,[R0]

  LDR R2, =0x00002000         ;Mask the bit of interest   

  ANDS R1,R2

  LDR R6, =0x00000000;

  CMP R1, R6                  ;Test to see if button is pressed

  BNE _offColor

_onColor

  LDR R0,=GPIOD_PDOR

  LDR R1,=0xFFFFFFFF     ;Clears the registers

  STR R1,[R0]

  LDR R0,=GPIOB_PDOR

  LDR R1,=0xFFFFFFFF     ;Also clears the registers

  STR R1,[R0]

  LDR R1,=0x00080000

  STR R1,[R0]

  LDR R0,=GPIOD_PDOR

  LDR R1,=0xFFFFFFFD

  STR R1,[R0]

  B loop

_offColor

  LDR R0,=GPIOB_PDOR

  LDR R1,=0xFFFFFFFF     ;This clears the register

  STR R1,[R0]

  LDR R0,=GPIOD_PDOR

  LDR R1,=0xFFFFFFFF     ;This also clears the registers for the other color pin

  STR R1,[R0]

  LDR R0,=GPIOB_PDOR

  LDR R1,=0x00080000     ;This sets the color of the pin of choice.

  STR R1,[R0]

  B loop

yasuhikokoumoto, your code looks much more simpler then mine. I am going to try to rewrite it using your code and see if I can get it to work as well.

Thank you mjbcswitzerland

0 Kudos
1,265 Views
mjbcswitzerland
Specialist V

James

You will need to check the register contents and also the CCR flags that are affected when stepping.

- the CMP is not required because the AND already sets the CCR with the result of whether it was zero or not

- you certainly need a jump after the first block otherwise it just continues into the second and you always have the same result at the end

Regards

Mark

0 Kudos