View previous topic :: View next topic |
Author |
Message |
rikotech8
Joined: 10 Dec 2011 Posts: 376 Location: Sofiq,Bulgariq
|
Tick timer issue |
Posted: Thu Dec 25, 2014 10:30 am |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Dec 25, 2014 10:52 am |
|
|
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 ? |
|
![](templates/subSilver/images/spacer.gif) |
rikotech8
Joined: 10 Dec 2011 Posts: 376 Location: Sofiq,Bulgariq
|
|
Posted: Fri Dec 26, 2014 4:15 am |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
|