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

Timing problem, Need help, please survive

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



Joined: 10 Jul 2011
Posts: 54

View user's profile Send private message Send e-mail

Timing problem, Need help, please survive
PostPosted: Fri Aug 05, 2011 8:27 am     Reply with quote

Hi there.
I am new in PIC programming.
I've made a project but do not working properly

When LED 1 is active, LED 2 is not On until the delay time is over.
But I need to turn on the LED2 before the delay time. How can I do this ?

Actually I want to keep on LED1 for three or five minutes/seconds and during this time I want to execute other instruction of my program.

I post here the code so that you can understand how much I know and what I want to do.

Need your help.

My code as follows:

Code:

#include <16F877A.H>
#fuses XT, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock = 4000000)
#include <flex_LCD420.c>   
//===================================
void main()
{

// The lcd_init() function should always be called once,
// near the start of your program.
lcd_init();

// Clear the LCD.
printf(lcd_putc, "\f");
delay_ms(50);


   printf(lcd_putc, "\f  PIC Experiment");
   printf(lcd_putc, "\n--------------------");
   printf(lcd_putc, "\nBatch    Total ");
while(1)
  {
   if (!input(pin_D0))
   {output_high(pin_C0);
   delay_ms(15000);     // keep the LED1 on for this time
   output_low(pin_C0);}
   
   if (!input(pin_D1))
   {output_high(pin_C1);}    // to active another LED2
   if (!input(pin_D2))
   {output_low(pin_C1);}
  }
}


Last edited by freedom on Sun Aug 07, 2011 1:14 pm; edited 2 times in total
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

timer ints are your friend
PostPosted: Fri Aug 05, 2011 9:47 am     Reply with quote

So long as you are not going to do any RS-232 in software
you should investigate using timer #1 counting the master clock
and generating INTerrupts to decrement a 'tick' timer that will
tell you precisely when any amount of time has elapsed and
direct your LED actions with a simple flag var or state machine variable -
that is altered when the background timer count has elapsedl

Then you go about your business w/ using the delay function
which you rightly observe - locks you in place .... waiting.
freedom



Joined: 10 Jul 2011
Posts: 54

View user's profile Send private message Send e-mail

PostPosted: Fri Aug 05, 2011 10:07 am     Reply with quote

Thnx a lot asmboy.

Would you like to provide code example regarding on my problem.

I don't know how to use timer.

Thanks to all and waiting for your reply.
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Fri Aug 05, 2011 12:08 pm     Reply with quote

This is a quick combined hack from several other programs but it shows the basic ideas for a simple non interrupt way to do this. You can adjust the setup of timer0 to get a convenient 10ms or and many other tick granularities.

Code:

unsigned int16 MSecs=0;
// polled non interrupt way to get 1ms accuracy w/o using delay_ms
// part of your init
    setup_timer_0 (RTCC_DIV_4 );   
//     at 4 mhz  sec /tick we need to preload the counter with '6'
//     to get the best 1msec /tick accuracy
//
//      this will initiate the delay in millisecs you want
// call it like setdelay(1000) ;  for 1 second delay etc
//                                 
void setdelay(unsigned int16 howmanyms ){
     msecs=howmanyms;  // will cancel any partially completed delay too
     set_timer0(6);    // compensate to get pretty exact 1 msec
     T0IF=0;   // cancel OVF flag
}

void myeventhandler(void ){

// **  where you do your led on off logic or
// ** any other timer/delay , based event
// ** this is your state machine handler function

}

// this will implement a POLLED 1 ms 'tick' using timer 0
// with a max range of 65.535 seconds
// it is POLLED method in main
// add this section to your code
//
void main(void){

    // your init code

    while (1) // loop forever below here
    // ** ----------------------
    // your other code you want to loop thru and poll
    // but note: any code execution that takes more than 1 msec
    // will stretch your delay -- 
    // ** ----------------------

    // now  the timer minder
    //
    if (MSecs && T0IF) {
         set_timer0(6);
         T0IF=0; // kill OVF flag
        --MSecs; // 1 ms recurrent decrement
        if ( !MSecs) myeventhandler();
     }
}

freedom



Joined: 10 Jul 2011
Posts: 54

View user's profile Send private message Send e-mail

PostPosted: Fri Aug 05, 2011 2:38 pm     Reply with quote

Thnx a lot asmboy.

Sorry to say, still now I'm not clear. I've read CCS manual but I'm not really clear.

As I'm new in Programming PIC, so I've to disturb. Sorry for this.

If you see my code then you understand that first I turn on a LED and it will 'on' for mentioned time as i use delay_ms. But in this case other instruction doesn't execute until the delay time is over.

Hope, you would be kind enough to modify my code so that i can understand.
Ttelmah



Joined: 11 Mar 2010
Posts: 19327

View user's profile Send private message

PostPosted: Sat Aug 06, 2011 9:42 am     Reply with quote

This is the point about 'single threaded' applications. The same would happen on a PC, if you used a delay like this. Basically while doing one job, the code waits for each thing in that job.
Asmboy, has posted a way round this.
This is not a CCS question. You need to go away, and do some basic learning about how to do a task like this, and then come back to ask about the details of how to do this in CCS (there are three basic ways, the polling method shown by Asmboy, using a 'tick' interrupt - this is how you would probably do it on a PC - , or using the RTOS).
This forum is for specific things to do with CCS, _not_ 'how to program'. You need to do some basic learning first.

Best Wishes
freedom



Joined: 10 Jul 2011
Posts: 54

View user's profile Send private message Send e-mail

PostPosted: Sat Aug 06, 2011 10:06 am     Reply with quote

thanx Ttelmah

Sorry to say I'm very hopeless on your remark.

However, I'm in such place where is no teacher than can help me to understand. Only this forum and other internet based resources can help me.

As your attention, please remember a little example makes a man can solve a big problem.

I just expect a little example code that can help me to understand the procedure.

Hope you understand.

Thanks for reply
freedom



Joined: 10 Jul 2011
Posts: 54

View user's profile Send private message Send e-mail

PostPosted: Mon Aug 08, 2011 3:06 pm     Reply with quote

still now I'm waiting
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Aug 08, 2011 3:15 pm     Reply with quote

Here's another example program:
http://www.ccsinfo.com/forum/viewtopic.php?t=35429
We don't want to write your program for you. You need to learn C
and study the examples.
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