C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 03-11-2009, 07:08 AM   #1
Registered User
 
Join Date: Mar 2008
Posts: 147
C keep a loop going whilst program continues on

hi im making a c program for the 68hc11 and to out put flashing ect ect on 6 lights on a O/I board i need to use a while loop to keep my lights flashing but i need program to continue on

heres my code
Code:
#include <stdio.h> 	/*standard i/o functions*/
#include <stdlib.h>
#include <math.h>
#include <iof1.h>


void main()
{
    int choice,actizone;

	do{
		choice = menu(); /* Calls the menu and returns the users choice */

        switch(choice)
        {
        case 1: /* Disarm the alarm */
            {
                arm();
                break;
            }
        case 2: /* Arm the alarm */
            {
                
                break;
            }
        case 3: /* Exit */
            {
                exit(1);
                break;
            }
        }


	  }while(1);
}


menu()
{
    int choice, i;
   	printf("\n ===========================================");
    	printf("\n ----=====| Burgalar Alarm System |=====----");
	printf("\n ================++++++++++++===============");
	printf("\n 1. Arm Alarm");
	printf("\n 2. Disarm Alarm");
	printf("\n 3. Exit");
	printf("\n -------------------------------------------");
	printf("\n\n Please input your choice from the menu :> ");
	scanf("%d", &choice);
    printf("\n");

	 for(i=0;i<1;i++)
     {
	      if(choice < 1 | choice > 4) // if the menu choice is invalid the program will ask the user to re input it.
	       {
           printf("\nInvalid choice please re enter :> ");
	       scanf("%d", &choice);
           i--;
      	   }
     }


return choice;
}




int arm(int argc, char *argv)
{
   unsigned char mask, port;
   unsigned int run,i;
   unsigned int counters[6] = {0};

   PORTG = 63;
   DDRG = 0xff;	//set port to output

   run = 1;


   while (run)
   {
		mask = 1;	//reset mask to one before each iteration
		for (i = 0; i < 6; i++)
		{
			if ((counters[i] == 500) && (port & mask))
			{
				PORTG ^= mask;
				counters[i] = 0;
			}
			counters[i]++;
			mask <<=1;	//check next bit
		
		}
   }



   printf("\n");
   return 0;
}
the arm function makes the lights blink but i need them to stay blinking and not to keep my program in a infinate loop sooo i can select other things from the menu ect ect . Any ideas how i can do this
thankyou
fortune2k is offline   Reply With Quote
Old 03-11-2009, 07:14 AM   #2
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,946
if available: fork()
otherwise pthreads

The easiest way would be to write separate programs.
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is online now   Reply With Quote
Old 03-11-2009, 07:15 AM   #3
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
So, you either have to:
1. Use interrupts.
2. Check for keypress without WAITING for the keypress.

--
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.
matsp is offline   Reply With Quote
Old 03-11-2009, 07:16 AM   #4
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
Quote:
Originally Posted by MK27 View Post
if available: fork()
otherwise pthreads

The easiest way would be to write separate programs.
This is a microcontroller, it probably has 32KB of RAM. It is POSSIBLE to make threads in such a system, but I think it is beyond the original posters capabilities at this point in time.

--
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.
matsp is offline   Reply With Quote
Old 03-11-2009, 07:28 AM   #5
Registered User
 
Join Date: Mar 2008
Posts: 147
i am ment to make a simple burgalar alarm disarmd : all 6 zones (6lights 6 switches) off, armed : all the zones or 1 of them will have the lights flashing at 0.5 seconds constant, and then when a switch is flicked the alarm is triggerd soo the zone will flash at 0.25 seconds.

is there a way i can make the zone flash without using a infinite loop? so the lights are flashing and my program is not on a halt

i dont think were ment to use fork
fortune2k is offline   Reply With Quote
Old 03-11-2009, 07:40 AM   #6
Kernel hacker
 
Join Date: Jul 2007
Location: Farncombe, Surrey, England
Posts: 15,686
I have no idea what you are supposed to use, but there are, essentialy three ways of doing two things "at once" in a computer system:
1. Interrupts that happen at given intervals (periodic timer interrupts).
2. "Do not block on anything and keep the code running in a loop"
3. Use multiple threads/processes (e.g. fork, pthreads or whatever)

I find #3 unlikely in a 68HC11 system, so it's highly likely that you should use one of the other solutions.

You may want to confer with your tutor or some such to see what the right solution is in this case.

--
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.
matsp is offline   Reply With Quote
Old 03-11-2009, 08:44 AM   #7
Registered User
 
Join Date: Sep 2006
Posts: 2,512
You might be able to 'simulate' an interrupt timer. What I mean is make a global variable oldTime, which is your start time when the alarm first "goes off".

Now, as your programs runs, it periodically has a line of code that tests the newTime, and when the newTime - oldTime > some time interval, then it calls a function which will blink the lights one time, etc., and return where it left off.

A large function or loop, may need a couple of these lines of newTime - oldTime checks in them.

It's important to use these time checks carefully. Too many of them, and your other code will run very slowly, indeed. Not enough, and the lights will blink at too great of an interval.

Ideally, your board should have a "latch" state on it, so once the alarm state is entered, it needs no further "babysitting" by your software, but will stay "on", light a light bulb, once the switch is turned on.

If you can inline your function for the blinking light (assuming you find no "stay on" state for your board), it will work better.
Adak is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Using variables in system() Afro C Programming 8 07-03-2007 12:27 PM
BOOKKEEPING PROGRAM, need help! yabud C Programming 3 11-16-2006 11:17 PM
detect infinite loop in c program abhivyakat C Programming 19 10-01-2003 06:55 AM
fopen(); GanglyLamb C Programming 8 11-03-2002 12:39 PM
Need Help on Program Using For Loop Unregistered C++ Programming 10 02-26-2002 06:54 PM


All times are GMT -6. The time now is 10:46 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22