CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

2 Timers (multitasking) . . .

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
ivan_ult



Joined: 29 Oct 2008
Posts: 7

View user's profile Send private message

2 Timers (multitasking) . . .
PostPosted: Mon Nov 24, 2008 9:21 am     Reply with quote

Hi everyone, I have an application which controls 2 rs232 inputs, I need to read each rx (a "@" character), and send an output in a single pin, with 1 minute duration and after that send the pin to low.

I've done this with 1 rs232 and with 1 output_bit, but I need to do it with 2 rx and 2 outbits at the same.

Any suggestion.

Part of the code. . . .
Code:

myloop:
   restart_wdt();
   if (kbhit(PICCOM1))
      {
      PROGRAM_DATA=fgetc(PICCOM1);
      output_bit(LED,1);
      delay_ms(5);
      output_bit(LED,0);
      if (PROGRAM_DATA==64)
         {
         output_bit(LED,1);
         output_bit(RELAY_RS232,1);
         for (x=1;x<=45;++x)
            {
            delay_ms(1000);
            restart_wdt();
            };
         output_bit(LED,0);
         output_bit(RELAY_RS232,0);
         };
      }
   goto myloop;

Regards . . .

Ivan Sierra Flores
Matamoros MX
Ttelmah
Guest







PostPosted: Mon Nov 24, 2008 9:30 am     Reply with quote

First question. What PIC?.
You need ether a PIC with two hardware serial ports, or an external hardware UART. Otherwise this is basically impossible (not quite, but nearly...).

Then, do a search for 'timeslicing state machines'.
You would only use one timer. Have this provide a system heartbeat, and in this heartbeat, trigger counters when events are detected, which get decremented on the system heartbeat, with events triggered when this happens.

Best Wishes
ivan_ult



Joined: 29 Oct 2008
Posts: 7

View user's profile Send private message

PostPosted: Mon Nov 24, 2008 9:58 am     Reply with quote

Hi Ttelmah, first thanks for answer so fast, I'm using a 16F877A,
some suggestion with this.

Regards
Ttelmah
Guest







PostPosted: Mon Nov 24, 2008 10:15 am     Reply with quote

I'm afraid with that, you will need to add an external UART. Philips do some nice small SPI ones, or Maxim has their MAX3100. You'd be much better switching to a chip with dual UARTs.

Best Wsihes
andrewg



Joined: 17 Aug 2005
Posts: 316
Location: Perth, Western Australia

View user's profile Send private message Visit poster's website

PostPosted: Tue Nov 25, 2008 8:06 am     Reply with quote

1.Have your mainline sit in a loop with a software serial port that turns on the pin and sets a countdown when the character is received.

2. Set up a hardware serial interrupt handler that sets another countdown when the character is received and turns on the pin.

3. Set up a millisecond interrupt, counting down each of the counters to turn off the pins.
_________________
Andrew
John P



Joined: 17 Sep 2003
Posts: 331

View user's profile Send private message

PostPosted: Wed Nov 26, 2008 5:04 pm     Reply with quote

That overall loop with a label and a goto is awful C programming. Use
Code:

while(1)
{
      Everything inside the loop
};

And there is no need for a semicolon after a closing bracket (I put one there just to illustrate it). It will only cause trouble.

Speaking of trouble:
Code:

for (x=1;x<=45;++x)
{
     delay_ms(1000);
     restart_wdt();
};


You do understand that the watchdog will get reset once a second for 45 seconds, right?

It's perfectly possible to have one hardware UART and one software UART. But to make that work, you can't allow anything to hold up the program, as the delay_ms() instruction does. I agree with AndrewG, you have to set up a timer in an interrupt and operate the LEDs when it runs out, while polling your UARTs, or using separate interrupts for them. The choice there depends on what's most important, immediate response or accurate timing. But if all you want to do is flash lights for a human observer, it doesn't matter if there are small delays or if a "second" isn't exactly 1.000 seconds.

If there's a software UART involved, you might want to set the timer interrupt to the same frequency as the baud rate, assuming it isn't incredibly fast or slow. 9600 or 19200 are good choices.
ivan_ult



Joined: 29 Oct 2008
Posts: 7

View user's profile Send private message

PostPosted: Thu Nov 27, 2008 9:41 am     Reply with quote

Thanks a lot Surprised , for now I've use 2 877 (there is a rush in this project), but I will try to do your suggestions the next time (1 weeek Shocked ), so I'll keep you guys posted. And thanks again.

Ivan . . .
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group