pic12f629 timer problem

Discussion in 'Programmer's Corner' started by koyaelektronic, Aug 5, 2013.

  1. koyaelektronic

    Thread Starter New Member

    Aug 1, 2013
    22
    0
    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.

    Code ( (Unknown Language)):
    1.  
    2. #include<12F629.h>
    3.  
    4. #define GP0 PIN_A0
    5. #define GP1 PIN_A1
    6. #define GP2 PIN_A2  
    7. #define GP3 PIN_A3  
    8. #define GP4 PIN_A4  
    9. #define GP5 PIN_A5  
    10.  
    11. #fuses NOMCLR,NOWDT,NOPROTECT,INTRC
    12.  
    13. #use delay(clock=4000000)
    14. #use fast_io(a)
    15.  
    16. #define LED GP0
    17. #define Trigger GP1
    18.  
    19. #define MAXRT 1000
    20.  
    21. void main()
    22.   {
    23.       unsigned int8 cnt_8ms;
    24.       setup_timer_0(RTCC_Internal|RTCC_Div_32);
    25.       set_tris_a(0b00000010);
    26.       while(TRUE)
    27.         {
    28.              output_a(0);
    29.              cnt_8ms = 0;
    30.              if(input(Trigger) == 0)
    31.                 {
    32.                while(input(Trigger) == 1 && cnt_8ms < 2000/8)
    33.                  {
    34.                     set_timer0(0);
    35.                     while(get_timer0() < 8000/32)
    36.                     ++cnt_8ms;
    37.                  }
    38.                if(cnt_8ms < MAXRT/8) //first event
    39.                  {
    40.                     output_high(LED);
    41.                     delay_ms(1000);
    42.                     output_low(LED);
    43.                     delay_ms(1000);
    44.                     output_high(LED);
    45.                     delay_ms(1000);
    46.                     output_low(LED);
    47.                  }
    48.                if(cnt_8ms > MAXRT/8) //second event
    49.                  {
    50.                     output_high(LED);
    51.                     delay_ms(2000);
    52.                     output_low(LED);
    53.                  }
    54.                delay_ms(1000);
    55.                              
    56. }
    57. }
    58.  
    It always triggers the first event,I can't get it to work properly.

    Kind regards,
    Sima
     
    Last edited by a moderator: Aug 5, 2013
  2. tracecom

    AAC Fanatic!

    Apr 16, 2010
    3,869
    1,393
    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.
     
  3. koyaelektronic

    Thread Starter New Member

    Aug 1, 2013
    22
    0
    My input is pulled up via 100k resistor.Because here logic 0 triggers my input.Will post schematic soon.
     
  4. koyaelektronic

    Thread Starter New Member

    Aug 1, 2013
    22
    0
    Here is my schematic,sorry for it being hand-drawn.For simplicity of understanding i put GP1 on the left,knowing it is on the right side of PIC.
     
  5. blueroomelectronics

    AAC Fanatic!

    Jul 22, 2007
    1,758
    98
    Run it through MPLAB's simulator, also the 12F629 has weak pull-ups so you probably don't need the 100K resistor.
     
    koyaelektronic likes this.
  6. koyaelektronic

    Thread Starter New Member

    Aug 1, 2013
    22
    0
    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.
     
  7. tshuck

    Well-Known Member

    Oct 18, 2012
    3,531
    675
    I suspect the same. What is the value of
    ?

    I typically set up my timers using the registers themselves, since I don't know what goes on in these compiler functions...
     
  8. blueroomelectronics

    AAC Fanatic!

    Jul 22, 2007
    1,758
    98
    If you're willing to convert it to XC8 I'll take a shot at it.
     
  9. koyaelektronic

    Thread Starter New Member

    Aug 1, 2013
    22
    0
    Do you mean my code,or you recode and give me in XC8?
     
  10. blueroomelectronics

    AAC Fanatic!

    Jul 22, 2007
    1,758
    98
    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: Aug 5, 2013
  11. koyaelektronic

    Thread Starter New Member

    Aug 1, 2013
    22
    0
    I will do my best to try,first time to use XC8 is now.Give me a couple of minutes
     
  12. blueroomelectronics

    AAC Fanatic!

    Jul 22, 2007
    1,758
    98
    The header will look something like this
    Code ( (Unknown Language)):
    1. #include <xc.h>
    2. #include <stdio.h>
    3. #pragma config FOSC = INTRCIO, WDTE = OFF
    4. #define _XTAL_FREQ 4000000 // internal osc frequency
    5. #define LED GP0
    6. #define Trigger GP1
    delay looks like this
    Code ( (Unknown Language)):
    1. __delay_ms(1000);
     
  13. koyaelektronic

    Thread Starter New Member

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

    Here it is :
    Code ( (Unknown Language)):
    1.  
    2. #include<xc.h>
    3. #include<stdint.h>
    4.  
    5. __CONFIG(MCLRE_OFF & CP_OFF & WDT_OFF & OSC_IntRC)
    6.  
    7. #define _XTAL_FREQ 4000000
    8.  
    9. #define LED GPIObits.GP0 //LED on GP0 output
    10. #define Trigger GPIObits.GP1 //Button on GP1 input
    11.  
    12. #define MAXRT 1000 //max reaction time in ms
    13.  
    14. void main()
    15.    {
    16.        uint8_t   cnt_8ms;
    17.        TRIS=0b00000010; //GP1 input,all other output
    18.        OPTION=0b11010100;
    19.        for(;;)
    20.           {
    21.           GPIO=0; // LED starts in OFF mode
    22.           cnt_8ms=0; //Counter reset
    23.           if(Trigger == 0) //If button is pressed
    24.             {
    25.                 TMR0 = 0; // Set timer0
    26.                 while(Trigger == 1 && cnt_8ms < 2000/8)
    27.                     {
    28.                         while(TMR0 < 8000/32)
    29.                            {
    30.                               ++cnt_8ms;
    31.                            }
    32.                     }
    33.                 if(cnt_8ms < MAXRT/8)
    34.                  {
    35.                      LED=1;
    36.                      __delay_ms(1000);
    37.                      LED=0;
    38.                      __delay_ms(1000);
    39.                      LED=1;
    40.                     __delay_ms(1000);
    41.                     LED=0;
    42.                  }
    43.               else
    44.                  {
    45.                     LED=1;
    46.                     __delay_ms(2000);
    47.                     LED=0;
    48.                  }
    49. }
    50. }
    51.  
     
  14. blueroomelectronics

    AAC Fanatic!

    Jul 22, 2007
    1,758
    98
    That was quick, but it's not quite XC8 and won't compile.
     
  15. koyaelektronic

    Thread Starter New Member

    Aug 1, 2013
    22
    0
    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.
     
  16. blueroomelectronics

    AAC Fanatic!

    Jul 22, 2007
    1,758
    98
    I'm going through your code now. Let's see what I come up with.
     
  17. blueroomelectronics

    AAC Fanatic!

    Jul 22, 2007
    1,758
    98
    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.
    Code ( (Unknown Language)):
    1.             LED = 1;
    2.           __delay_ms(1000);
    3.             LED = 0;
    4.                         __delay_ms(1000);
    5.             LED = 1;
    6.                         __delay_ms(1000);
    7.             LED = 0;
     
    Last edited: Aug 5, 2013
  18. koyaelektronic

    Thread Starter New Member

    Aug 1, 2013
    22
    0
    I will draw what I need,maybe you will understand then :D
     
  19. koyaelektronic

    Thread Starter New Member

    Aug 1, 2013
    22
    0
    Here is what I am trying to accomplish
     
  20. blueroomelectronics

    AAC Fanatic!

    Jul 22, 2007
    1,758
    98
    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
     
    koyaelektronic likes this.
Loading...