0

I am a newbie in arm programming. I am using teensy 3.2 which has MK20DX256 microcontroller chip and the led is connected to PTC5 of MC.

I have written this code to blink the LED.

The project builds without error but HEX file does not blink led. I am using teensy loader to program the board with HEX file.

Here is my code:

#include "MK20D7.h"
void delay(unsigned int);

int main(void) {
  SystemCoreClockUpdate(); /* Get Core Clock Frequency */
  SysTick_Config(SystemCoreClock/1000); /* Generate interrupt each 1 ms */

    PORTC->PCR[5]= 256; //declared as GPIO
    PTC->PDDR=0x00000010;
    while(1){
      PTC->PDOR=0x00000010;
      delay(500);
      PTC->PDOR=0x00000000;
      delay(500);
    }
    
    void delay(unsigned int ms){
      unsigned int i,j;
    
      for(i=0;i<ms;i++)
      for(j=0;j<20000;j++);
    }
}

Please help me in finding what is wrong with this code

Roman Zh.
  • 985
  • 2
  • 6
  • 20
  • 1
    Are you sure that your loops in the `delay()` functions aren't optimized completely away? There are no side effects. – πάντα ῥεῖ Jan 10 '21 at 20:56
  • If you have an interrupt every 1ms you should use this interrupt to increment a counter rather than trying to time off of loops. If the loops don't get optimized out, they'll probably run far too fast. – user4581301 Jan 10 '21 at 21:09
  • I don't know the teensy system so I can't tell you how to install a (search keyterm incoming!) Interrupt Service Routine that will handle the interrupt and increment the counter, but the `delay` could be as simple as setting the counter to zero, then looping until the counter reaches `ms` and then returning. – user4581301 Jan 10 '21 at 21:14
  • you do not want to start with interrupts first, so you can remove that, and you dont need to mess with system clocks at all normally. you need to depending on the part, enable the gpio, make the gpio pins an output. and the do what you are doing with making the pin high and low with delays. – old_timer Jan 10 '21 at 22:39

1 Answers1

0

Try this first see if the led comes on and stays on

# include "MK20D7.h"
int main(void)
{
PORTC->PCR[5]= 256; //declared as GPIO
PTC->PDDR=0x00000010;
PTC->PDOR=0x00000010;
//PTC->PDOR=0x00000000;
while(1) continue;
}

If not then you need to disassemble, see below.

Then try this

# include "MK20D7.h"
int main(void)
{
PORTC->PCR[5]= 256; //declared as GPIO
PTC->PDDR=0x00000010;
//PTC->PDOR=0x00000010;
PTC->PDOR=0x00000000;
while(1) continue;
}

and see if it goes/stays off

Then try this

# include "MK20D7.h"
void delay(unsigned int );

int main(void)
{
PORTC->PCR[5]= 256; //declared as GPIO
PTC->PDDR=0x00000010;
while(1)
{
PTC->PDOR=0x00000010;
delay();
PTC->PDOR=0x00000000;
delay();
}

}

void delay(void)
{
volatile unsigned int j;
for(j=0;j<20000;j++);
}

Assuming you have the right header file and the binary is built and linked properly, etc. Ideally you want to disassemble the binary, examine vector table to insure its placement and proper contents. Examine the code after the main label and compare it to the documentation for the few registers used.

PSOR and PCOR are easier to use

PTC->PSOR=1<<5;
delay();
PTC->PCOR=1<<5;
delay();
old_timer
  • 69,149
  • 8
  • 89
  • 168
  • i am new to arm programming .i dont have any idea about disassembly.the solutions you suggested none of them works in my case .May be there is an issue in linking. when i try to debug,none of the regiters change their value.what should i do?please help me get started – blessed Jan 11 '21 at 07:49