Thread: 1 flash per second , micocontrollers, while loop

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    147

    1 flash per second , micocontrollers, while loop

    H,i im using micro controllers ( the 68hc11 board) to make some lights flash ect and i have to have it

    1 fps – flashes per second. 1fps = 0.5 secs. on, 0.5 secs off,


    Code:
    //depending on the mask this will  make the lights flash on or be off
    			for (i = 0; i < 6; i++)
    			{
    				if ((counters[i] == delay) && (port & mask)) //delay = 500
    				{
    					PORTA ^= mask;
    					counters[i] = 0;
    				}//end of if
    				counters[i]++;
    				mask <<=1;	//check next bit
                }//end of for
    i have been using a while loop because that's all i can think im trying the value of delay at 500. I have herd you can use the hardware timer but i cant find any information about them.

    do you think having the delay set to 500 will give me 0.5 on 0.5 off ? im not at the board so i cant test plus i dont have a stopwatch

    thankyou
    Last edited by fortune2k; 03-16-2009 at 05:15 PM.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Is there a loop outside of the for-loop, somewhere?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    Quote Originally Posted by matsp View Post
    Is there a loop outside of the for-loop, somewhere?

    --
    Mats
    oh sorry yea its in a while(1) loop so its a infinity loop but it has loads of other bits and bobs in so its actually like this:

    Code:
    //depending on the mask this will  make the lights flash on or be off
    while(1)
    {
    			for (i = 0; i < 6; i++)
    			{
    				if ((counters[i] == delay) && (port & mask)) //delay = 500
    				{
    					PORTA ^= mask;
    					counters[i] = 0;
    				}//end of if
    				counters[i]++;
    				mask <<=1;	//check next bit
                }//end of for
    }//end of while

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    And what makes the inner loop take 1 ms? It looks to me like a 10MHz or so processor [guessing the speed of a HC11 here] would do that in about 50 clock-cycles -> 5 microseconds. [I'm guessing the speed and number of instructions too, but it's unlikeley that I'm a factor 200 off...

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The delay() function will give you at least 1/2 second delay, then your program will be again sharing the OS.

    If you use delay(500) however, there will be a problem that you have used 1/2 your second, and now must run something from your program, and have the OS do it's thing, AND then turn the led back on, for the other 1/2 second.

    That's not going to happen.

    What you're going to want is something like delay(430) to (480), so with the small amount of time left over, the OS can have some, your program can have some, and the delay will actually wind up being closer to 1/2 second.

    Have you tried having your program monitor the ticks of the clock, directly, as the program is running? Explicit control like that might give you finer timing control.

    It would be just like you were timing something normally, but you wouldn't divide the answering ticks by ticks_per_second. You'd want that fine tick count number.

    If you had the time, you could make your program self-adjusting, with this. So it would notice that the last led ON was on a 1/10th of a second too long, so it would shorten the next led period (the OFF), by 1/9th or 1/10th of a second.

  6. #6
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    Quote Originally Posted by Adak View Post
    The delay() function will give you at least 1/2 second delay, then your program will be again sharing the OS.

    If you use delay(500) however, there will be a problem that you have used 1/2 your second, and now must run something from your program, and have the OS do it's thing, AND then turn the led back on, for the other 1/2 second.

    That's not going to happen.

    What you're going to want is something like delay(430) to (480), so with the small amount of time left over, the OS can have some, your program can have some, and the delay will actually wind up being closer to 1/2 second.

    Have you tried having your program monitor the ticks of the clock, directly, as the program is running? Explicit control like that might give you finer timing control.

    It would be just like you were timing something normally, but you wouldn't divide the answering ticks by ticks_per_second. You'd want that fine tick count number.

    If you had the time, you could make your program self-adjusting, with this. So it would notice that the last led ON was on a 1/10th of a second too long, so it would shorten the next led period (the OFF), by 1/9th or 1/10th of a second.

    how would i use the delay() command in my example do i need any special header files? how would i call it ect ect??

  7. #7
    Malum in se abachler's Avatar
    Join Date
    Apr 2007
    Posts
    3,195
    set the prescalar to the appropraiate value for your crystal frequency, and then check the overflow bit on the main system timer. reference section 10.2.1.1 in revision 3 of the 68HC11 reference manual for more details, or ask if you have other questions, ill be glad to help you. I've bee using these for about 15 years.
    Last edited by abachler; 03-16-2009 at 07:35 PM.

  8. #8
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    This is a small example for delay(). It emits a tone for approx. 1/2 second, then stops for a half-second, four times in all.

    Code:
    #include <dos.h>
    
    int main(void)  {
    
       int i;
    
       for(i = 0; i < 4; i++)  {
    
          sound(440);
          delay(500);
          nosound();
          delay(500);   //Edit
       
       }
       printf(" \n\n\t\t\t\t   press enter when ready  ");
       i = getchar();
    
       return 0;
    By modifying this code you should be able to time it and find the value of delay that gets you closest to 1/2 second timing.

    I believe this is not the best way to do this however.
    Last edited by Adak; 03-17-2009 at 03:19 AM.

  9. #9
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Adak View Post
    This is a small example for delay(). It emits a tone for approx. 1/2 second, then stops.

    Code:
    #include <dos.h>
    
    int main(void)  {
    
       int i;
    
       for(i = 0; i < 4; i++)  {
    
          sound(440);
          delay(500);
          nosound();
       
       }
       printf(" \n\n\t\t\t\t   press enter when ready  ");
       i = getchar();
    
       return 0;
    By modifying this code you should be able to time it and find the value of delay that gets you closest to 1/2 second timing.

    I believe this is not the best way to do this however.
    And that works in 68HC11 world, because to me it looks like Turbo C.

    Edit: and I think it will make sound for about 2 seconds, possibly with a few glitches.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  10. #10
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Yeah, forgot the last delay() after nosound().

    Doesn't he have a small C compiler like Turbo C, for the 68HC11?

  11. #11
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    i use a .bat file for called CRAM which compiles my .c files and allows me to upload them to the board. Im going into school in a bit to test it out i shall let u know how i get on

    thankyou

  12. #12
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Adak View Post
    Yeah, forgot the last delay() after nosound().

    Doesn't he have a small C compiler like Turbo C, for the 68HC11?
    Sure, but delay() is a Turbo C specific extension, it's not a standard function, so there's absolutely no guarantee [in fact quite unlikely] that the HC11 has this function - I can't really tell, and it's certainly worth investigating. But I'm 99% sure that it's not running DOS, and doesn't have "dos.h".

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  13. #13
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    right i dont think i can use that way i get cannont open dosh.h sooo maybe a hardware timer should be used because sometimes i have 1 light running out of time

    i have found an example of a hardware timer for lights but i dont have a clue how it works and how i can use this to get my 0.5 on 0.5 off..

    Code:
    #include <stdio.h> 	/*standard i/o functions*/
    
    void timer(void);
    
    int hours, mins, secs, ticks, times;	/*Global Variables for returning values*/
    unsigned char *padr,*paddr, *tflg2, *pact1, *tmsk2;
    
    void main(void)
    {
    	padr=(unsigned char*)0x0;
    	paddr=(unsigned char*)0x1;
    	tmsk2=(unsigned char*)0x24;
    	tflg2=(unsigned char*)0x25;
    	pact1=(unsigned char*)0x26;
    	
    	*paddr=0xff;
    	*pact1=0x03;
    	*tmsk2=0x40;
    
    	for(;;)
    	{
    		if (ticks==0 && times==0){		/*print out once a sec*?*/
    		
    			printf("%2i:%2i:%2i\r", hours, mins, secs);
    			times=1;
    			}
    		if (ticks==1) times=0;
    	}
    }
    
    @interrupt void timer(void){
    		ticks++;
    		if (ticks==30){
    			ticks=0;
    			secs++;
    			if(*padr==0){
    				*padr=0xff;
    			}
    			else *padr=0x0;		/* Flash Lights */
    		}
    			if (secs==60){
    				secs=0;
    				mins++;
    			}
    			if (mins==60){
    				mins=0;
    				hours++;
    			}
    			if (hours==24){
    				hours=0;
    			}
    			*tflg2=0x40;		/*reset RTI flag**/
    
    		}
    can someone p[lease explain to me what this does

  14. #14
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Can you explain which part you do not understand?

    It also appears like it's missing something to assign the timer interrupt function into the interrupt vector table (or some such), but perhaps that is done somewhere else.

    The basic principle of an interrupt is that you have a signal that is driven by some device, in this case a timer, that tells the CPU "an event happened, go deal with it", at which point the processor will stop doing whatever it is doing, and run an "interrupt handler" - in this case the timer function [at least that's what we can presume from the source code shown here]. This appears to be set to 30Hz (because seconds get counted up every 30 ticks). When the interrupt routine returns (finishes), it goes on to do whatever it was doing.

    In the posted code, there is a loop polling (checking) if the ticks and times is zero, if so print the current time. 30 times a second, that gets interrupted by the timer, and ticks is counted up. Every 30 timer ticks, the seconds is counted up and ticks is reset to zero, and every 60 seconds, the minutes are counted up, etc, etc.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  15. #15
    Registered User
    Join Date
    Mar 2008
    Posts
    147
    well that code doesnt run sooo i cant see how its working whats missing then i can play around with it and maybe get somewhere

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 10:44 AM
  2. nested loop, simple but i'm missing it
    By big_brother in forum C Programming
    Replies: 19
    Last Post: 10-23-2006, 10:21 PM
  3. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 02:14 AM
  4. while loop help
    By bliznags in forum C Programming
    Replies: 5
    Last Post: 03-20-2005, 12:30 AM
  5. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM