CODE DOESNT STOP RUNNING ON mc9s12dp512

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

CODE DOESNT STOP RUNNING ON mc9s12dp512

4,287 Views
ibtisbr
Contributor II

hcs12(x) codewarrior v5.7 codewarrior for hcs12(x) microcontrollers v5.2 eval ultrasonic #range finder 

Hello ,

 

I'm working on an ultrasonic distance measurement circuit using mc9s12dp512 , the circuit is mainly consituted of an ultrasonic sensor (HC-SR04) and an LCD display , Port A of the MCU is used for data , and the 3 first pin of PORTB is to control the LCD , TC0 (sensor trigger) is used as an output compare to genrate a pulse of 10us and TC1 (sensor echo ) as input capture to measure the pulse width . when runinng the below code it doesnt stop , i have tried the breakpoints and nothing seems to have a problem , any idea where the problem might be ? 

 

code :

#include <hidef.h> /* common defines and macros */
#include "derivative.h" /* derivative-specific definitions */

#include "c:\Program Files (x86)\Freescale\CWS12v5.2\lib\hc12c\include\mc9s12dp512.h"





unsigned diff, edge1, overflow;
unsigned long pulse_width; unsigned long range ;
#define DelayHi 15 // high time of the pulses to be created
#define DelayLo 30000 // low time of the pulses to be created
#define NN 10 // number of pulses to be created
int pcnt;
#define data_port PORTA;


#define rs PORTA_BIT0;
#define rw PORTB_BIT1 ;
#define en PORTB_BIT2 ; // pulse count
char HiorLo;
extern void near tovisr(void);
extern void near tc0ISR(void);
#pragma CODE_SEG __NEAR_SEG NON_BANKED
#pragma CODE_SEG DEFAULT // Change code section to DEFAULT.
typedef void (*near tIsrFunc)(void);
const tIsrFunc _vect[] @0xFFDE =
{
tovisr,
tc0ISR };




void delay_ms(unsigned int msec) {
int i ;
{
while (msec != 0)
{
for (i=5; i >0;i--){};
break;
}

}

}
void lcd_cmd(unsigned char dat)
{

dat =PORTA & 0xFF;
PORTB=PORTB & 0xF1 ;// CLEARS rs=0
PORTB=PORTB & 0xF2;//rw 0;
PORTB=PORTB | 0xFB;//en 1 sets the bit 2 of PORTB to 1 ;
delay_ms(1);
PORTB=PORTB & 0xF4;

}

void lcd_data(unsigned char dat) // Function to send data to LCD
{

dat = PORTA & 0xFF;
PORTB=PORTB | 0xFE; // Rs=1
PORTB=PORTB & 0xF2; //RW=0
PORTB=PORTB | 0xFB;//EN=1
delay_ms(1);
PORTB=PORTB & 0xF4; //EN=0

}

void lcd_string( char *str) // Function to send string to LCD
{
int i=0;
while(str[i]!='\0')
{
lcd_data(str[i]);
i++;
delay_ms(1);
}
}

// flag to choose
void pulsetrig(void)
{

TSCR1= 0x90;
// enable TCNT and faster timer flag clear
TSCR2 = 0x04; // set TCNT clock input prescaler to 16
TFLG1 = TFLG1_C0F_MASK; // clear C0F flag
TIOS |= TIOS_IOS0 ; // enable OC0
TCTL2 = 0x03; // set OC0 pin action to be pull high
TC0 = TCNT+ 16;
while (!(TFLG1_C0F == 0)); // pull PT0 pin high quickly // “
pcnt = 2 * NN-1; // prepare to create NN pulses //(need to toggle 2*NN 2 1 times)
TCTL2 =0x01; // set OC0 pin action to be toggle
TC0 += DelayHi; // start the second OC0 operation
HiorLo = 0; // next time use DelayLo as delay count of OC0 operation
TIE |=TFLG1_C0F_MASK; // enable TC0 interrupt
asm("cli"); // “
// do nothing or do something else
}


interrupt void tc0ISR(void) {
if(HiorLo){
TC0 +=DelayHi;
HiorLo =0;
} else {
TC0 +=DelayLo;
HiorLo = 1;
}
pcnt--;
if(pcnt ==0){
TIE =0; // disable OC0 interrupt
TIOS &= 0xFE; // disable OC0
}
}


void echopulse(void)
{ pulsetrig();
overflow = 0;
TSCR1 = 0x90; // enable timer and fast flag clear
TSCR2 = 0x05; // set prescaler to 32, no timer overflow interrupt
TIOS &= TIOS_IOS1; // select input-capture 0
TCTL4 = 0x01; // prepare to capture the rising edge
TFLG1 = TFLG1_C1F_MASK; // clear C0F flag

while (!(TFLG1_C0F == 0)); // wait for the arrival of the rising edge
TFLG2 = 0x80; // clear TOF flag
TSCR2 |= 0x80; // enable TCNT overflow interrupt
asm("cli");
edge1 = TC1; // save the first edge
TCTL4 = 0x02;
while (!(TFLG1_C0F == 0)); // prepare to capture the falling edge
// wait for the arrival of the falling edge
diff = TC1 - edge1;
if (TC1 < edge1) ;

overflow -= 1;
pulse_width = (long)overflow * 65536u + (long)diff;
lcd_cmd(0xc0);

delay_ms(2);
lcd_string("Distance:");
lcd_cmd(0xc9);


if(pulse_width<35000) ; //actually you need to use 38000 but the sensor may not work at higher levels


range=(pulse_width/59 );

lcd_data(range+48); ;

lcd_string("cm");

}


interrupt void tovisr(void)
{
TFLG2 = 0x80; /* clear the TOF flag */
overflow++;
}

void main (void){
DDRB =0xFF;
DDRA= 0xFF;
echopulse();
delay_ms(2);
lcd_cmd(0x38);
lcd_cmd(0x0c);
delay_ms(2);
lcd_cmd(0x01);
delay_ms(2);
lcd_cmd(0x80);
delay_ms(2);

lcd_string("Range finder");
delay_ms(10);

TC1=0;
TC0=0;

while(1){

echopulse();
delay_ms(2);
break;
}

}

Original Attachment has been moved to: Project.rar

9 Replies

4,015 Views
ZhangJennie
NXP TechSupport
NXP TechSupport

Hi,

To investigate this problem, we need reproduce it first.

Thus we need your demo project.

Please upload it here thus I can investigate it further. Thanks.


Have a great day,
Jennie Zhang

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

4,015 Views
ibtisbr
Contributor II

hi , 

thanks for your response ,

I have modified my entry , please refer to the attached .rar file ,

Thank you

regards , 

Ibtissem

0 Kudos
Reply

4,015 Views
ZhangJennie
NXP TechSupport
NXP TechSupport

Hi.

I don't know why you add "break" in while loop.

pastedImage_1.png

please remove it thus your code can continue to run.


Have a great day,
Jennie Zhang

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

4,015 Views
ibtisbr
Contributor II

Hi , 

I did , but when it wont stop runing .

regards 

ibtissem

0 Kudos
Reply

4,015 Views
ZhangJennie
NXP TechSupport
NXP TechSupport

in your original code, you add "break"

pastedImage_1.png

thus after execution "break", your program goes to the end . then you will see "illegal BP" message.

when we burn a program into a chip,  we usually need it run until power down. if you add a "break", the program will execute for only once, practically we never do it.

Have a great day,
Jennie Zhang

 

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

4,015 Views
ibtisbr
Contributor II

Hi , 

I understand , but i removed the break  and i'm still having the same issue of endless runing .

regards , 

Ibtissem

0 Kudos
Reply

4,015 Views
ZhangJennie
NXP TechSupport
NXP TechSupport

Hi Ibtissem,

you mean you remove "break;", you still have illegal bp error?

Because I don't have exact your board, I tested your project (with removing "break") in full chip simulation mode, there is no illegal bp error. see attached video.


Have a great day,
Jennie Zhang

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

0 Kudos
Reply

4,015 Views
ibtisbr
Contributor II

Hi again Jennie , 

I m really sorry for the long Q&A but i dont have an illegal bp as seen in the video the simulation keeps running and doesnt return done , try to run it again in full chip simulation and you can notice that it doesnt stop runing for endless cpu cycles .

regards , 

ibtissem

0 Kudos
Reply

4,015 Views
ZhangJennie
NXP TechSupport
NXP TechSupport

I run it again but still can't see any problem.

Please make a video to show us your problem to avoid misunderstanding.

Thanks.

Jennie

0 Kudos
Reply