Author Topic: How to quit loop after string finish print?  (Read 4337 times)

0 Members and 1 Guest are viewing this topic.

Offline singlarrysongTopic starter

  • Contributor
  • Posts: 34
How to quit loop after string finish print?
« on: May 01, 2015, 09:55:18 pm »
Below is my code, do anyone know how to stop print string after the given string had printed once? It just continue print right after each print.
And weird thing is once it enter while loop, it seems escape and start the main program once again although I had set while to infinity. I found this problem when I change the very first delay_ms(500) to other value.
I'm also not really understand the operator *x.

This is program is for 8bit VFD where I take a 4bit LCD code as reference, the sample code is from hitech C content folder.
The function DispData() is just playing with arrangement of the output as 8th bit not use as TRISA5 is input only.(I had clear it out from here)


#include <htc.h>
#include <pic.h>

__CONFIG (0x0FAC); //see "Config" > "Configuration bits" for detail
__CONFIG (0x1CFF); //as above

#define _XTAL_FREQ 16000000 //for __delay_ purpose

void main(void)
{
   OSCCON = 0b01111010; //16MHz Fosc, Int Osc module
   
   TRISB = 0b00000000;
   PORTB = 0;
   TRISA = 0b00100000;
   
   __delay_ms(500);   //Display Powerup delay

   const char *x = "Bye World =P";
   while(1)
   {      
         DispData(*x);
         RB0 = 1;
         __delay_us(280);
         RB0 = 0;
         __delay_us(280);

         *x++;
   }
}
 

Online Andy Watson

  • Super Contributor
  • ***
  • Posts: 2084
Re: How to quit loop after string finish print?
« Reply #1 on: May 01, 2015, 10:08:10 pm »
It just continue print right after each print.
What do you expect it to do? It is in a while loop - forever!  If you want it to "not" do it, you will have to give it something else to do - like another "while(1); "

Quote
And weird thing is once it enter while loop, it seems escape and start the main program once again
Is the processor being reset via the watchdog timer ?

Quote
I'm also not really understand the operator *x.

*x means "the value that x points to" - I assume you have "pointed" x to somewhere ?
 

Offline Rick Law

  • Super Contributor
  • ***
  • Posts: 3441
  • Country: us
Re: How to quit loop after string finish print?
« Reply #2 on: May 01, 2015, 10:31:40 pm »
A C-string should be null terminated.  So test for that condition.

   while(1)

   while (*x)
   {     
         DispData(*x);
         RB0 = 1;
         __delay_us(280);
         RB0 = 0;
         __delay_us(280);

         *x++;
   }
 

Offline singlarrysongTopic starter

  • Contributor
  • Posts: 34
Re: How to quit loop after string finish print?
« Reply #3 on: May 01, 2015, 10:35:06 pm »
I put on while loop after trying do while and for. What I wish to know is the condition that make it quit the loop once the string done printed.

About the WDT I think that is it.
I'll look for it ASAP.
 

Offline Rick Law

  • Super Contributor
  • ***
  • Posts: 3441
  • Country: us
Re: How to quit loop after string finish print?
« Reply #4 on: May 01, 2015, 10:37:37 pm »
I put on while loop after trying do while and for. What I wish to know is the condition that make it quit the loop once the string done printed.

About the WDT I think that is it.
I'll look for it ASAP.

I just show you that.  Instead of while(1), use while(*x).

So, while *xx!=0, the loop keeps going,
when at the end of the string, *x==0, (*x) is now false and while loop stops.
 

Offline singlarrysongTopic starter

  • Contributor
  • Posts: 34
Re: How to quit loop after string finish print?
« Reply #5 on: May 01, 2015, 10:39:05 pm »
I'll try it ASAP. It's mid night in UK now.

Thanks a lot :) (emoji error)
 

Online langwadt

  • Super Contributor
  • ***
  • Posts: 4421
  • Country: dk
Re: How to quit loop after string finish print?
« Reply #6 on: May 01, 2015, 10:41:48 pm »
I put on while loop after trying do while and for. What I wish to know is the condition that make it quit the loop once the string done printed.

About the WDT I think that is it.
I'll look for it ASAP.

as mentioned there is numerous way to do it, but then what?  In most embedded code you never return from main
 

Offline singlarrysongTopic starter

  • Contributor
  • Posts: 34
Re: How to quit loop after string finish print?
« Reply #7 on: May 01, 2015, 10:45:11 pm »
I need it at the moment cause I need print more different string. This is still in testing phase.
thank you :)
 

Offline AndreasF

  • Frequent Contributor
  • **
  • Posts: 251
  • Country: gb
    • mind-dump.net
Re: How to quit loop after string finish print?
« Reply #8 on: May 02, 2015, 08:50:41 am »
The end of the loop should look like this:

x++;

not

*x++;

since you want to increment the address, not it's content. (see K&R, p.98)
my random ramblings mind-dump.net
 

Offline mikerj

  • Super Contributor
  • ***
  • Posts: 3238
  • Country: gb
Re: How to quit loop after string finish print?
« Reply #9 on: May 02, 2015, 11:47:20 am »
The end of the loop should look like this:

x++;

not

*x++;

since you want to increment the address, not it's content. (see K&R, p.98)


*x++ is equivalent to *(x++)  so the current pointer value is first dereferenced, and then the pointer is incremented.  The dereferenced value is not changed.

Since there is no need to dereference the pointer in this case then x++ is clearly the correct construct to use, but *x++ would also work, though potentially creating some useless extra instructions.
 

Offline singlarrysongTopic starter

  • Contributor
  • Posts: 34
Re: How to quit loop after string finish print?
« Reply #10 on: May 02, 2015, 09:17:12 pm »
Hi everyone, the code is now below.

instead *x++ or *(x++), x++ is work for testing weather the loop quit or not, I put on a delay after the loop to observe. it did. [*x++ is which used in the sample code provided in hitech c folder]

BUT there is still a problem, where the program seems restarting

void main(void)
{
   OSCCON = 0b01111010; //16MHz Fosc, Int Osc module
   
   TRISB = 0b00000000;
   PORTB = 0;
   TRISA = 0b00100000;
   
   __delay_ms(500);   //Display Powerup delay

   const char *x = "Bye World =P";
   while(*x)
   {      
         DispData(*x);
         RB0 = 1;
         __delay_us(280);
         RB0 = 0;
         __delay_us(280);

         x++;
   }
   __delay_ms(2000);
}
 

Offline zapta

  • Super Contributor
  • ***
  • Posts: 6190
  • Country: us
Re: How to quit loop after string finish print?
« Reply #11 on: May 02, 2015, 10:02:36 pm »
If you want to iterate the characters of a string you can use a for loop


Code: [Select]
for(const char* x = "My String"; *x;  x++) {
  // the current character is available here as:  *x
}

 

Offline singlarrysongTopic starter

  • Contributor
  • Posts: 34
Re: How to quit loop after string finish print?
« Reply #12 on: May 02, 2015, 10:16:17 pm »
Thanks, for loop help save a few program memory.

Btw just to reply the comment before above, the program seems restart it self each time after the main had end. (WDT disable) I had attachment showing my device config
 

Offline mikerj

  • Super Contributor
  • ***
  • Posts: 3238
  • Country: gb
Re: How to quit loop after string finish print?
« Reply #13 on: May 03, 2015, 01:40:43 pm »
Thanks, for loop help save a few program memory.

Btw just to reply the comment before above, the program seems restart it self each time after the main had end. (WDT disable) I had attachment showing my device config

What do you think will happen when execution reaches the end of the main() function?  There is no operating system to return to, and the micro won't just stop executing instructions.  In this case, either the compiler puts an explicit jump back to the reset vector at the end of the main() function, or the micro may simply keep executing instructions in the memory after main until it reaches the end of the memory space, and rolls over back to zero.  The value held in a 'blank' program memory address is usually a valid instruction.

If you want to stop the micro exiting the main function, then you need to put an endless loop at the end of the function.  Either use for( ; ; ); or while(1);

Code: [Select]
void main(void)
{
   OSCCON = 0b01111010; //16MHz Fosc, Int Osc module
   
   TRISB = 0b00000000;
   PORTB = 0;
   TRISA = 0b00100000;
   
   __delay_ms(500);   //Display Powerup delay

   const char *x = "Bye World =P";
   while(*x)
   {     
         DispData(*x);
         RB0 = 1;
         __delay_us(280);
         RB0 = 0;
         __delay_us(280);

         x++;
   }
   
   for(;;);    /* Stop here */
}
 

Offline singlarrysongTopic starter

  • Contributor
  • Posts: 34
Re: How to quit loop after string finish print?
« Reply #14 on: May 03, 2015, 01:45:25 pm »
Thanks, I found that solution too. I Just don't care about the restart anymore because seldom have program that need to go up to ending and instead going in infinity loop after few setting of main
 

Offline Rick Law

  • Super Contributor
  • ***
  • Posts: 3441
  • Country: us
Re: How to quit loop after string finish print?
« Reply #15 on: May 04, 2015, 04:11:59 am »
Thanks, I found that solution too. I Just don't care about the restart anymore because seldom have program that need to go up to ending and instead going in infinity loop after few setting of main

Any microprocessor/mcu/cpu is always doing something.  From the moment of power up, it will be executing instructions given to it even if the instructions results in doing nothing useful.  To stop the CPU, you basically can power down, or to stop the CPU clock from ticking.

If it is a multi-processing/multi-treaded systems, one can say: this thread you are doing for me has nothing left to do, so terminate me.  The CPU terminates that thread and go do other active things - if such other active threads exists.  For a single-tasking system, when your main() is done, it got nothing to do other than looping waiting for an interrupt to occur in which case it would still be not doing anything of interest as you have no further instructions for it.

With typical MCU being single tasking, once you have no further instructions for it to do, it is done.  Empty loop, or loop forever to blick an LED, or HALT doesn't really matter.  If your hardware doesn't have a software triggerable power off, it will just have to bore itself to death.

One can of course implement a power save state or sleep state to save power.  But it adds cost and judging from its availability on typical MCU broads, apparently there is not enough of a market for it.
 

Offline singlarrysongTopic starter

  • Contributor
  • Posts: 34
Re: How to quit loop after string finish print?
« Reply #16 on: May 04, 2015, 08:58:49 am »
I see
Thanks for the information. :) have a nice day
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf