KL25Z Traffic Light Program - Code and Breadboard Issues

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

KL25Z Traffic Light Program - Code and Breadboard Issues

2,729 Views
dc153boo
Contributor II

Essentially, what I'm trying to do is have lights cycle through 3 corresponding LEDs on the breadboard via some ARM assembly code on the KL25Z using Keil uVision 5 as my IDE. I've chosen PIN PTA2 to be my green LED, PTA4 to be my yellow, PTA5 to be my red, PCR7 to be emergency reset button which restarts the cycle at green (I haven't implemented this in code yet since I haven't even gotten the cycle to work) -- details are commented in the code. No LEDs are being lit up and I'm curious as to why. Here is my code:

SIM_SCGC5   EQU  0x40048038 ;SIM_SCGC5 address 

SIM_COPC     EQU  0x40048100 ;SIM_COPC (watchdog) address

   

PORTA_PCR2  EQU  0x40049008 ;PORTA_PCR2 (PTA2) address

PORTA_PCR4  EQU  0x40049010 ;PORTA_PCR4 (PTA4) address

PORTA_PCR5  EQU  0x40049014 ;PORTA_PCR5 (PTA5) address

PORTC_PCR7  EQU  0x4004B01C ;PORTA_PCR7 (PTC7) address

PORTA_PDDR  EQU 0x400FF014

PORTA_PDOR  EQU 0x400FF000

PORTC_PDDR  EQU 0x400FF094

PORTC_PDIR  EQU 0x400FF090

   

GREEN_MASK   EQU 0x00000004 ; PORTA_PIN 2

YELLOW_MASK  EQU 0x00000010 ; PORTA_PIN 4

RED_MASK     EQU 0x00000020 ; PORTA_PIN 5

INIT_MASKS   EQU 0x00000034 ; PORTA_PIN 2, 4 and 5

BUTTON_MASK  EQU 0X00000080 ; PORTC_PIN 7

DELAY_CNT    EQU 0X00800000

    AREA asm_area, CODE, READONLY

    EXPORT  asm_main

asm_main  ;assembly entry point for C function, do not delete

; Add program code here

    BL init_gpio 

loop

    BL greenon

    LDR R0, =DELAY_CNT

    BL  delay

    BL  redon

    LDR R0, =DELAY_CNT

    BL  delay

    BL  yellowon

    LDR R0, =DELAY_CNT

    BL  delay

    B   loop

greenon

    LDR R0,=PORTA_PDOR  ;Load address of PORTA_PDOR to R0

    LDR R1,=GREEN_MASK    ;Load value to R1

    STR R1,[R0]     ;Put value into PORTA_PDOR

    BX LR

redon

    LDR R0,=PORTA_PDOR  ;Load address of PORTA_PDOR to R0

    LDR R1,=RED_MASK    ;Load value to R1

    STR R1,[R0]     ;Put value into PORTA_PDOR

    BX LR

yellowon

    LDR R0,=PORTA_PDOR  ;Load address of PORTA_PDOR to R0

    LDR R1,=YELLOW_MASK    ;Load value to R1

    STR R1,[R0] ;Put value into PORTA_PDOR

    BX LR

delay

    SUBS R0, #10

    BNE  delay

    BX LR

init_gpio

    ;Disable watchdog timer (COP)

    LDR R0,=SIM_COPC

    LDR R1,=0x0

    STR R1,[R0]

   

    ;Turns on clocks for all ports

    LDR R0,=SIM_SCGC5    ;Load address of SIM_SCGC5 to R0

    LDR R1,[R0]          ;Get original value of SIM_SCGC5

    LDR R2,=0x00003E00   ;Load mask for bits to set to R2

    ORRS R1,R2           ;Set bits with OR of orig val and mask

    STR R1,[R0]          ;Put new value back into SIM_SCGC5

    ; Setup PORTA Pins 2,4 and 5 to be outputs

    LDR R0,=PORTA_PDDR   ;Load address of PORTA_PDDR to R0

    LDR R1,=INIT_MASKS    ;Load new value to R1

    STR R1,[R0]           ;Put value into PORTA_PDDR

    ; Setup PORTC Pin 7 to be an input

    LDR R0,=PORTC_PDDR ; Load address of PORTC_PDDR to R0

    LDR R1,=BUTTON_MASK     ;Load new value to R1

    STR R1,[R0]            ;Put value into PORTC_PDDR

    BX LR

    END


Any help/suggestions would be immensely appreciated! Thanks in advance.

8 Replies

1,944 Views
jordanliong95
Contributor I

Using the FRDM-KL25Z board, with uVision 5 Keil compiler. 2 ways traffic light with pedestrian crossing button traffic light program. C programming.can anyone help me?

Thank you

0 Kudos

1,944 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hi JODAN LEONG

  Please create your own question post, then we will help you in your new question post directly.

  Please don't just post questions in other's answered post, thank you!

 

Wish it helps you!

If you still have question, please contact with me!

 

 


Have a great day,
Jingjing

 

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos

1,944 Views
jordanliong95
Contributor I

Noted sir,

Thank you

0 Kudos

1,944 Views
kerryzhou
NXP TechSupport
NXP TechSupport

Hi Dan Cymbala,

      If your KL25's IO is enough,  I am not suggest you use the PTA4 as the normal IO, because it is the NMI pin in default, if you want to disable the NMI function, you need to modify the flash configuration field, then you should better to choose another IO.

    Actually, it is very easy to control the IO as the output.

    The relate register is:

  SIM_SCGC5: Port clock gate control

  PORTx_PCRn: configure MUX as 1 to realize the IO function,

  GPIOx_PDDR: port data direction, 1 ouput

  GPIOx_PSOR, GPIOx_PDOR, GPIOx_PCOR: control the output data.

Now take PTA2 as an example:

  SIM_SCGC5 |= SIM_SCGC5_PORTA_MASK; //open PTA port clock

  PORTA_PCR2 = PORT_PCR_MUX(1); // PTA2 as GPIO

  GPIOA_PDDR |= (1<<2);// PTA2 as output

GPIOA_PSOR = (1<<2));// PTA2 output high

GPIOA_PCOR = (1<<2); // PTA2 output low

Wish it helps you!

If you still have question, please contact with me!


Have a great day,
Jingjing

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

0 Kudos

1,944 Views
mjbcswitzerland
Specialist V

Dan

You have equates for PORTA_PCR2 etc. but haven't used them yet.

You need to configure the pin mux setting in these registers otherwise they are still in their default condition:

- PTA2 is disabled

- PTA4 is NMI input

- PTA5 is disabled

Write the MUX setting of each to 0x01 (ALT0, which is the GPIO setting) and then they will start operating as ports.

Regards

Mark

1,944 Views
dc153boo
Contributor II

Mark,

Also here is my updated code - I ran it, there were no results :

[ARM] SIM_SCGC5 EQU 0x40048038 ;SIM_SCGC5 address SIM_COPC EQU 0x40048100 ;SIM - Pastebin.com

0 Kudos

1,944 Views
mjbcswitzerland
Specialist V

Dan

You need to first read the data sheet before writing values to registers:

pastedImage_0.png

The MUX setting for GPIO is (0x01 << 8) [or 0x00000100] and not 0x01. There are also settings for drive strength, speed although these can be left at 0 for your case. Your code was just selecting a pull-up resistor on the pin.

You can write the register whenever you ant to change the setting (including setting back to its origianal configuration).

Regards

Mark

0 Kudos

1,945 Views
dc153boo
Contributor II

Mark,

How do I write to the MUX setting of the board? Do I do this in code or in uVision 5? Also, if I hypothetically wanted the board in it's original pin state, I'm assuming I would write 0x00 to the board's MUX settings, correct? And if I hypothetically wanted to write to the ALT2 GPIO setting to the MUX, I would write 0x02?

Please let me know as soon as possible.


P.S. Thank you for the speedy response!

Cheers,

Dan

0 Kudos