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

Tick timer issue

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



Joined: 10 Dec 2011
Posts: 376
Location: Sofiq,Bulgariq

View user's profile Send private message

Tick timer issue
PostPosted: Thu Dec 25, 2014 10:30 am     Reply with quote

Hello everyone.
I am doing a program for START-STOP button for car ignition.
So in this line:
Code:
            while ( (get_ticks() < 4000) ||  (!GEN_SIGNAL))    //Wait 4secs or generator signal, whichever appear first
               ;

I expect after 4 seconds to exit the while loop and to stop ignition. But indeed the loop iterates forever. get_ticks() never exceeds 4000ms and the processor never exits the while loop.
This is my entire code:

Code:

#include <16F628A.h>
#fuses NOWDT, NOPUT, INTRC_IO, NOMCLR, NOBROWNOUT, NOLVP, NOPROTECT
#use delay(clock = 4000000)
#USE TIMER(TIMER=1,TICK=1ms,BITS=16,NOISR)

#define CONTACT         pin_a1
#define IGNITION        pin_a0
#define BUTT            pin_b5
#define BUZZ            pin_a3
#define BREAKS          pin_b3
#define GENERATOR       pin_b2
#define BUTT_PRESSED    !input(BUTT)
#define CONTACT_ON      output_high(CONTACT)
#define CONTACT_OFF     output_low(CONTACT)
#define IGNITION_ON     output_high(IGNITION)
#define IGNITION_OFF    output_low(IGNITION)
#define BREAK_SIGNAL    input(BREAKS)
#define GEN_SIGNAL      input(GENERATOR)

#byte OPTION_REG = 0x81    //bit 7 enables the portB pullups

volatile int * const porta = 0x05;

void chip_init();
void enable_portb_pull_ups();
unsigned int1 contact_is_on();

void main()
{
enum engine_state {OFF, ON} engine = OFF;
unsigned int1 flag = 1;
   chip_init();
   while(true)
   {
      if ( BUTT_PRESSED && (engine == OFF) )
      {
      delay_ms(20);
         if ( (BUTT_PRESSED) && !(contact_is_on()) && flag )
         {
            CONTACT_ON;
            flag = 0;
         }
         if ( (BUTT_PRESSED) &&  (contact_is_on()) && (!BREAK_SIGNAL) && flag )
         {
            CONTACT_OFF;
            flag = 0;
         }
         if ( (BUTT_PRESSED) &&  (contact_is_on()) && (BREAK_SIGNAL) && (flag) )
         {
            IGNITION_ON;
            flag = 0;
            set_ticks(0);
            while ( (get_ticks() < 4000) ||  (!GEN_SIGNAL))    //Wait 4secs or generator signal, whichever appear first
               ;
             IGNITION_OFF;
         }
         
         if ( !BUTT_PRESSED )
         {
            flag = 1;
         }
      }

   }
}

void chip_init()
{
   IGNITION_OFF;
   CONTACT_OFF;
   enable_portb_pull_ups();
}

void enable_portb_pull_ups()
{
   set_tris_b(1 << 5);
   OPTION_REG = OPTION_REG & ( ~(1 << 7) );
}

unsigned int1 contact_is_on()
{
   return !!(*porta & (1 << 1));
}

_________________
A person who never made a mistake never tried anything new.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Dec 25, 2014 10:52 am     Reply with quote

What if you put a printf() in there and dump out the value of get_ticks() ?
What do you see ?

Also what's your compiler version ?
rikotech8



Joined: 10 Dec 2011
Posts: 376
Location: Sofiq,Bulgariq

View user's profile Send private message

PostPosted: Fri Dec 26, 2014 4:15 am     Reply with quote

I found where I am wrong. The tick timer works well. I just had to change the line to this:
Code:
while ( ((get_ticks() < 4000) && (!GEN_SIGNAL)) )

I placed && instead of || and now it works as I wish.
Thank you.
_________________
A person who never made a mistake never tried anything new.
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