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.
Solved! Go to Solution.
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
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
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
Thanks a lot Zhang :-)
you are welcome!
if my answer helps, please click on "correct answer" button. thanks!
Best Regards,
Zhang Jun
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