pic12f629 timer problem

Thread Starter

koyaelektronic

Joined Aug 1, 2013
22
Hello,

I was wondering if someone can help me,to set timer0 for my program.
The situation is following:
GP1 input,all others output.
Logic 0 on GP1 starts the 2 seconds timer.
If in in those two seconds Logic 0 appears once more on GP1,then do first event.If not do second event.
So here is a code I have written.

Rich (BB code):
#include<12F629.h>

#define GP0 PIN_A0
#define GP1 PIN_A1
#define GP2 PIN_A2  
#define GP3 PIN_A3  
#define GP4 PIN_A4  
#define GP5 PIN_A5  

#fuses NOMCLR,NOWDT,NOPROTECT,INTRC

#use delay(clock=4000000)
#use fast_io(a)

#define LED GP0
#define Trigger GP1

#define MAXRT 1000

void main()
  {
      unsigned int8 cnt_8ms;
      setup_timer_0(RTCC_Internal|RTCC_Div_32);
      set_tris_a(0b00000010);
      while(TRUE)
        {
             output_a(0);
             cnt_8ms = 0;
             if(input(Trigger) == 0)
                {
               while(input(Trigger) == 1 && cnt_8ms < 2000/8)
                 {
                    set_timer0(0);
                    while(get_timer0() < 8000/32)
                    ++cnt_8ms;
                 }
               if(cnt_8ms < MAXRT/8) //first event
                 {
                    output_high(LED);
                    delay_ms(1000);
                    output_low(LED);
                    delay_ms(1000);
                    output_high(LED);
                    delay_ms(1000);
                    output_low(LED);
                 }
               if(cnt_8ms > MAXRT/8) //second event
                 {
                    output_high(LED);
                    delay_ms(2000);
                    output_low(LED);
                 }
               delay_ms(1000);
                              
}
}
It always triggers the first event,I can't get it to work properly.

Kind regards,
Sima
 
Last edited by a moderator:

tracecom

Joined Apr 16, 2010
3,944
I don't program in C, so I probably can't say anything worthwhile about your code. However, it looks like you are defining the pins backwards from the actual hardware configuration, and you are defining some of the pins twice. I don't understand why.

As I requested in your other thread, a schematic is needed to properly analyze your problem. And as I also suggested, my first guess is that you don't have pull-up resistors on your inputs. Floating inputs are unpredictable and unreliable.

Post a schematic.
 

Thread Starter

koyaelektronic

Joined Aug 1, 2013
22
I don't program in C, so I can't say anything worthwhile about your code.

As I requested in your other thread, a schematic is needed to properly analyze your problem. And as I also suggested, my first guess is that you don't have pull-down resistors on your inputs. Floating inputs are unpredictable and unreliable.

Post a schematic.
My input is pulled up via 100k resistor.Because here logic 0 triggers my input.Will post schematic soon.
 

Thread Starter

koyaelektronic

Joined Aug 1, 2013
22
Did that,and I also have Visual Pic Simulator called Real Pic simulator,both always do first event.I think my timer settings are the problem.
 
You'll have to convert it. I avoided C for ages, but recently changed my tone.

Where did you get the original program? There is no way for your program to check what GP1 is doing until the delay routines are complete and the program loops again. You could set an interrupt flag based on GP1 (those get set even while the CPU is tied up doing other things).

Now might be a good time to structure your code and break it up into subroutines & functions so it's easier to follow and modify.

It also looks ideal for an interrupt driven bit of code, timers love interrupts. GP2 can generate an edge driven interrupt, might be handy.

Something to remember, the delay functions effectively stop the CPU from doing anything (except interrupts) for their duration.
 
Last edited:
The header will look something like this
Rich (BB code):
#include <xc.h>
#include <stdio.h>
#pragma config FOSC = INTRCIO, WDTE = OFF
#define _XTAL_FREQ 4000000 // internal osc frequency
#define LED GP0
#define Trigger GP1
delay looks like this
Rich (BB code):
__delay_ms(1000);
 

Thread Starter

koyaelektronic

Joined Aug 1, 2013
22
I think i have converted my code to XC8

Here it is :
Rich (BB code):
#include<xc.h>
#include<stdint.h>

__CONFIG(MCLRE_OFF & CP_OFF & WDT_OFF & OSC_IntRC)

#define _XTAL_FREQ 4000000

#define LED GPIObits.GP0 //LED on GP0 output
#define Trigger GPIObits.GP1 //Button on GP1 input

#define MAXRT 1000 //max reaction time in ms

void main()
   {
       uint8_t   cnt_8ms;
       TRIS=0b00000010; //GP1 input,all other output
       OPTION=0b11010100;
       for(;;)
          {
          GPIO=0; // LED starts in OFF mode
          cnt_8ms=0; //Counter reset
          if(Trigger == 0) //If button is pressed
            {
                TMR0 = 0; // Set timer0
                while(Trigger == 1 && cnt_8ms < 2000/8)
                    {
                        while(TMR0 < 8000/32)
                           {
                              ++cnt_8ms;
                           }
                    }
                if(cnt_8ms < MAXRT/8)
                 {
                     LED=1;
                     __delay_ms(1000);
                     LED=0;
                     __delay_ms(1000);
                     LED=1;
                    __delay_ms(1000);
                    LED=0;
                 }
              else
                 {
                    LED=1;
                    __delay_ms(2000);
                    LED=0;
                 }
}
}
 

Thread Starter

koyaelektronic

Joined Aug 1, 2013
22
Well,I tried my best,really :D
What I wanted to say was:
If GP1 gets logical 0,start the timer for 2 seconds.
If in those two seconds,GP1 gets another logical zero do:
LED on 1 sec
LED off for 1 sec
LED on for 1 sec
LED off
If it doesnt receive another logical zero then:
LED on for 2 sec
LED off.
 
Well,I tried my best,really :D
What I wanted to say was:
If GP1 gets logical 0,start the timer for 2 seconds.
If in those two seconds,GP1 gets another logical zero do:
LED on 1 sec
LED off for 1 sec
LED on for 1 sec
LED off
If it doesnt receive another logical zero then:
LED on for 2 sec
LED off.
So when switch closed do nothing for two seconds? How does it get another switch closure during those two seconds? Do you have to release it and repress it?

If it's held down then what? What is this for and game controller?

Note: as written this bit of code and the other delay that follows it can do nothing else till they're completed. Therefore your "if in & if it" cannot be tested during.
Rich (BB code):
            LED = 1;
          __delay_ms(1000);
            LED = 0;
                        __delay_ms(1000);
            LED = 1;
                        __delay_ms(1000);
            LED = 0;
 
Last edited:
You'll have to think of it differently if you want to make it work.

On the Input low LED ON
1s
IF Input Low then LED OFF
1s
IF LED ON then LED OFF break (exit end of condition 2)
1s
LED ON
1s
LED OFF (end of condition 1)
repeat
 
Top