MC9S08DN16 port toggle error

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

MC9S08DN16 port toggle error

Jump to solution
745 Views
vikassubramani
Contributor II

Hi every body i am new bie to this controller and i am facing some problem with my code,

 

i have written a small code to toggle port E I am able to toggle the port but when i run the code in debug mode and do step by step method the PC is not going set by step it is pointing to random instructions. I have included the code below please help.

 

 

 

 

/* [sample program to toggle the port E of the micro controller ] */

 

#include <hidef.h> /* for EnableInterrupts macro */

#include "derivative.h" /* include peripheral declarations */

void port_initialize(void);

void delay(void);

 

 

 

 

void main(void) {

EnableInterrupts;

port_initialize();

while(1)

{

PTED = 0x00;                          /* flashing 0's on to port E */

delay();                                    /* delay function */

PTED = 0xff;                               /* flashing 1's on to port E */

delay();                                    /* delay function */

PTED = 0x00;                          /* flashing 0's on to port E */

delay();                                      /* delay function */

PTED = 0xff;                               /* flashing 1's on to port E */

delay();                                         /* delay function */

PTED = 0x00;                               /* flashing 0's on to port E */

delay();                                           /* delay function */

PTED = 0xff;                                   /* flashing 1's on to port E */

delay();                                              /* delay function */

__RESET_WATCHDOG();

 

}

 

}

 

void port_initialize(void )

{

PTEDD = 0xff;                          /* port E data direction register configured as output */

  PTEPE = 0x00;                     /* Internal pull-up/pull-down device disabled for port E */

  PTESE = 0x00;                     /* Output slew rate control disabled for port E */

// PTEPE = 0x00;                     /* Port disabled as interrupt */

}

 

void delay()

{

unsigned int i,j;

for(i=0; i<255; i++)

for(j=0; j<255;j++);

}

 

Note when i use the port just once i.e make it high and then make it low and delete the rest of the code it works find but when i try to toggle the port again by includeing the port then i am facing this problem.

Labels (1)
0 Kudos
1 Solution
551 Views
ZhangJennie
NXP TechSupport
NXP TechSupport

hello Vikas,

the problem is due to optimization. the simple way to avoid optimization is to add "asm nop;"

   while (1)

{

PTAD_PTAD0 = 1;

asm nop;

delay();

PTAD_PTAD0 = 0;

asm nop;

delay();

PTAD_PTAD0 = 1;

asm nop;

delay();

PTAD_PTAD0 = 0;

asm nop;

delay();

                __Reset_Watchdog;

}

==============================================

this answer is for you. if it helps, please click on "correct answer" button. thanks!

Best Regards,

Zhang Jun

View solution in original post

0 Kudos
8 Replies
551 Views
ZhangJennie
NXP TechSupport
NXP TechSupport

hi vikas,

which line of you do you begin to have problem with? can you please send a screenshot of the probelm?

Best Regards,

Zhang Jun

0 Kudos
551 Views
vikassubramani
Contributor II

screen shot 1.pngscreen shot 2.png

0 Kudos
551 Views
vikassubramani
Contributor II

screen shot 3.png

0 Kudos
551 Views
vikassubramani
Contributor II

screen shot 4.png

0 Kudos
552 Views
ZhangJennie
NXP TechSupport
NXP TechSupport

hello Vikas,

the problem is due to optimization. the simple way to avoid optimization is to add "asm nop;"

   while (1)

{

PTAD_PTAD0 = 1;

asm nop;

delay();

PTAD_PTAD0 = 0;

asm nop;

delay();

PTAD_PTAD0 = 1;

asm nop;

delay();

PTAD_PTAD0 = 0;

asm nop;

delay();

                __Reset_Watchdog;

}

==============================================

this answer is for you. if it helps, please click on "correct answer" button. thanks!

Best Regards,

Zhang Jun

0 Kudos
551 Views
vikassubramani
Contributor II

Thanks a lot Zhang :-)

0 Kudos
551 Views
ZhangJennie
NXP TechSupport
NXP TechSupport

you are welcome!

if my answer helps, please click on "correct answer" button. thanks!

Best Regards,

Zhang Jun

0 Kudos
551 Views
vikassubramani
Contributor II

I have tried using SPOT1_COPT = 0; disabeling the watchdog timmer but the result is the same then i removed all the comment in the code and debug even then i am facing the problem

0 Kudos