Hot!Alarm clock using pic16f886

Page: 123 > Showing page 1 of 3
Author
john_ive
Junior Member
  • Total Posts : 105
  • Reward points : 0
  • Joined: 2017/05/22 22:22:08
  • Location: 0
  • Status: offline
2018/08/23 01:29:14 (permalink)
0

Alarm clock using pic16f886

<code> 
OPTION_REG = 0X07;
    long long int m = 10;
    while (m) {
        while (!T0IF);
        T0IF = 0;
        count++;
        m--;
    }
</code>
 
This is the code which use in my project...I'm working in an stopwatch project.It working fine but the accuracy is low.Some delay occurring in pic timer when compared with real timer.I like to reduce that errors.
How to remove this error..?
 
I'm using 8MHZ external crystal in my application.
#1

46 Replies Related Threads

    qhb
    Superb Member
    • Total Posts : 7467
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: online
    Re: Alarm clock using pic16f886 2018/08/23 02:16:37 (permalink)
    0
    john_ive
    <code> 
    ..
    </code>

    Close.
    Use [] instead of <> around your code tags.
     

    This is the code which use in my project...I'm working in an stopwatch project.It working fine but the accuracy is low.Some delay occurring in pic timer when compared with real timer.I like to reduce that errors.
    How to remove this error..?

    This can't be all your code. Presumably your real code is sometime missing timer rollovers.
    Doing the counter increment inside a timer interrupt service would be more reliable.
     
    Also note, if you are using XC8 version 1.xx, there is no "long long" type. You will just get "long" i.e. 32 bits.
    Only XC8 version 2.xx in C99 mode supports "long long" to get a 64 bit integer.
     
    #2
    john_ive
    Junior Member
    • Total Posts : 105
    • Reward points : 0
    • Joined: 2017/05/22 22:22:08
    • Location: 0
    • Status: offline
    Re: Alarm clock using pic16f886 2018/08/23 02:50:13 (permalink)
    0
    qhb,Can you explain your point please.Sorry i can't understand..
    #3
    qhb
    Superb Member
    • Total Posts : 7467
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: online
    Re: Alarm clock using pic16f886 2018/08/23 03:01:14 (permalink)
    0
    For starters, please explain better what you are trying to do.
    You stated you are working in a stopwatch project. A stopwatch is used to measure time.
    All you posted is some code to wait for ten timer rollovers (using a ridiculously large variable for such a small value), with no indication of how you verified how long it took.
     
    It seems this is not the code you actually did the measurement with.
     
     
    #4
    john_ive
    Junior Member
    • Total Posts : 105
    • Reward points : 0
    • Joined: 2017/05/22 22:22:08
    • Location: 0
    • Status: offline
    Re: Alarm clock using pic16f886 2018/08/23 03:42:59 (permalink)
    0
    qhb


    #define    XTAL_FREQ 8MHZ
    #include <xc.h>
    #include "delay.h"
    #include "16x2.h"

    // CONFIG1
    #pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator: High-speed crystal/resonator on RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN)
    #pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
    #pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
    #pragma config MCLRE = ON       // RE3/MCLR pin function select bit (RE3/MCLR pin function is MCLR)
    #pragma config CP = OFF         // Code Protection bit (Program memory code protection is disabled)
    #pragma config CPD = OFF        // Data Code Protection bit (Data memory code protection is disabled)
    #pragma config BOREN = OFF      // Brown Out Reset Selection bits (BOR disabled)
    #pragma config IESO = OFF       // Internal External Switchover bit (Internal/External Switchover mode is disabled)
    #pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is disabled)
    #pragma config LVP = OFF        // Low Voltage Programming Enable bit (RB3 pin has digital I/O, HV on MCLR must be used for programming)

    // CONFIG2
    #pragma config BOR4V = BOR40V   // Brown-out Reset Selection bit (Brown-out Reset set to 4.0V)
    #pragma config WRT = OFF        // Flash Program Memory Self Write Enable bits (Write protection off)

    /**************************************************************
    |                   Initialise System
     ***************************************************************/

    int temp1, temp2, temp3, temp4;
    int sec, min;

    void Sys(void) {

        ADCON1 = 0b10000000;
        ADCON0bits.ADCS1 = 1;
        ADCON0bits.ADCS0 = 0;

        ANSELH = 0x00;

        TRISB = 0b00000000;
        PORTB = 0b00000000;

        TRISC = 0b01110000;
        PORTC = 0b01110000;

        TRISA = 0b00000111;
        PORTA = 0b00000111;

        ANSEL = 0b00001111;

    }

    void tm1_timer_fn() {//timer Function
        OPTION_REG = 0XC7;
        int m = 28;
        while (m) {
            while (!T0IF);
            T0IF = 0;
            m--;
        }
    }

    void main(void) {
        Sys();
        tm1_timer_fn();
        sec++;
        temp1 = sec / 10;
        temp2 = sec % 10;

        lcd_goto(1, 1);
        lcd_putch(temp1);
        lcd_putch(temp2);

        temp1 = temp2 = 0;

        if (sec == 60) {
            sec = 0;
            min++;
        }

        temp1 = min / 10;
        temp2 = min % 10;

        lcd_goto(1, 4);
        lcd_putch(temp1);
        lcd_putch(temp2);
        if (min == 10) {
            while (1) {

            }
        }
    }


     
     
    this my code...
    #5
    qhb
    Superb Member
    • Total Posts : 7467
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: online
    Re: Alarm clock using pic16f886 2018/08/23 04:06:04 (permalink)
    0
    What do you expect this code to do?
    At first glance, it appears it will delay about 0.9175 seconds, put a couple of garbage characters in the first two positions on your LCD,  put another couple of garbage characters a bit further along the LCD,
    then exit the main() function, causing a reset.
     
    I'll ask again, is this your real code, and what do you see when you run it?
     
    #6
    john_ive
    Junior Member
    • Total Posts : 105
    • Reward points : 0
    • Joined: 2017/05/22 22:22:08
    • Location: 0
    • Status: offline
    Re: Alarm clock using pic16f886 2018/08/23 04:20:12 (permalink)
    0
    yes this the real code when 10 mins reached an relay will turn on.When the relay turn ON the total hardware(Hardware in machine and controller) part will reset and the timer will start to run from first.Again it will turn OFF after 10minutes.Its an cyclic process...
     
    The issue is their is an error in total time.The cyclic timing is not accurate when compared with calibrated timer.I started the both timer by looping each other,to avoid error.
     
    "it appears it will delay about 0.9175 seconds" is this is the error for incorrect time...?
    #7
    qhb
    Superb Member
    • Total Posts : 7467
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: online
    Re: Alarm clock using pic16f886 2018/08/23 04:28:36 (permalink)
    0
    There is nothing in that code to control a relay.
    There is nothing in that code that will wait ten minutes. It will run through once and fall out, as the only endless loop is inside the
    if (min == 10)
    test that will never be true.
     
    #8
    NKurzman
    A Guy on the Net
    • Total Posts : 16555
    • Reward points : 0
    • Joined: 2008/01/16 19:33:48
    • Location: 0
    • Status: online
    Re: Alarm clock using pic16f886 2018/08/23 04:37:36 (permalink)
    0
    The is an error. In the total time? A second off? Or an hour off? Over what period?
    #9
    john_ive
    Junior Member
    • Total Posts : 105
    • Reward points : 0
    • Joined: 2017/05/22 22:22:08
    • Location: 0
    • Status: offline
    Re: Alarm clock using pic16f886 2018/08/23 04:49:19 (permalink)
    0
    qhb
    sec and min variables will increase based on tm1_timer_fn();
    Is that wrong..?
     
    Please advice to create an error free timer...
    Meanwhile i will try to resolve the control blocks.
    #10
    qhb
    Superb Member
    • Total Posts : 7467
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: online
    Re: Alarm clock using pic16f886 2018/08/23 04:55:23 (permalink)
    0
    john_ive
    qhb
    sec and min variables will increase based on tm1_timer_fn();
    Is that wrong..?

    No they won't.
    By my calculation, that function delays for 0.9175 seconds, then returns.
    You have no code looping back to call it again, so it is only ever called once.
     

    Please advice to create an error free timer...

    I already did. Use interrupts. Have you ever used interrupts on a microcontroller?
    #11
    john_ive
    Junior Member
    • Total Posts : 105
    • Reward points : 0
    • Joined: 2017/05/22 22:22:08
    • Location: 0
    • Status: offline
    Re: Alarm clock using pic16f886 2018/08/23 04:58:05 (permalink)
    0
    qhb
    how to increase 0.9175 seconds to 1 seconds..?
    Please advice how to use interrupt and how you calculated 0.9175 seconds(for learning i'm asking)...?
    F
    post edited by john_ive - 2018/08/23 05:04:29
    #12
    qhb
    Superb Member
    • Total Posts : 7467
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: online
    Re: Alarm clock using pic16f886 2018/08/23 05:16:39 (permalink)
    +1 (1)
    First, understand where the 0.9175 seconds comes from.
    If your crystal is 8MHz, the Fosc/4 is 2 MHz.
    You selected a prescaler of 1:256 by writing 0xC7 to the OPTION register (You really only need do that once, not every time you call that routine)
    So, TIMER0 is being clocked at 2MHz/256 = 7813 Hz
    The timer counts through 256 values before rolling over (and setting T0IF), so this will happen at 7813/256 = 30.52 Hz.
    You are counting 28 rollovers, so this will take 28/30.52 = 0.9175 seconds.
     
    As you can see from these numbers, it's not easy to get exactly one second from Timer-0.
    However, it is possible with Timer-2, which has much more versatile scalers.
    Try setting the Timer2 prescaler to 1:16, PR2 to 124, and Postscaler to 10.
    That will give a rollover rate of 2 MHz /16 /125 /10 = 100 Hz
    So you can count 100 of those rollovers for exactly one second.
     
    This only works if you are doing nothing but counting rollovers while you are waiting.
    If you need to do anything else, such as update the LCD, then yo need to use interrupts to count the rollovers.
     
    #13
    qhb
    Superb Member
    • Total Posts : 7467
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: online
    Re: Alarm clock using pic16f886 2018/08/23 05:20:26 (permalink)
    0
    Alternatively, setting PR2 to 249 will halve the rollover rate to 50Hz, so you only need to count 50 rollovers.
     
    #14
    john_ive
    Junior Member
    • Total Posts : 105
    • Reward points : 0
    • Joined: 2017/05/22 22:22:08
    • Location: 0
    • Status: offline
    Re: Alarm clock using pic16f886 2018/08/23 05:25:47 (permalink)
    0
    That means updating values to LCD or doing another operation will affect the timer accuracy...
    Shall we use RTC DS1307..?
    #15
    qhb
    Superb Member
    • Total Posts : 7467
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: online
    Re: Alarm clock using pic16f886 2018/08/23 05:29:51 (permalink)
    +1 (1)
    You keep ignoring my questons about interrupts.
    THAT is how you do this sort of thing accurately.
     
    And, you keep posting fragments of code that do not do anything like what you claim you are trying to do.
     
    #16
    katela
    Super Member
    • Total Posts : 982
    • Reward points : 0
    • Joined: 2013/06/11 05:25:18
    • Location: South Africa
    • Status: online
    Re: Alarm clock using pic16f886 2018/08/23 05:36:22 (permalink)
    0
    Are you sure this is the code that is running as you are explaining?
     
    if (min == 10) {
            while (1) {

            }
        }
     
    Are you sure this is the code that is running as you are explaining?
    If min is not = 10 what will happen? It will exit the main function and reset.
    I doubt this code will produce the results you are explaining. Unless this is not the full code.

    Free online Microcontroller Tutorials and Projects for Hobbyists and students. From beginners to advanced. Website: www.studentcompanion.co.za
    YouTube Tutorials: https://www.youtube.com/user/StudentCompanionSA
    #17
    qhb
    Superb Member
    • Total Posts : 7467
    • Reward points : 0
    • Joined: 2016/06/05 14:55:32
    • Location: One step ahead...
    • Status: online
    Re: Alarm clock using pic16f886 2018/08/23 05:44:05 (permalink)
    0
    That's what I've been saying. The code as posted is rubbish, and will just reset about once a second.
     
    #18
    john_ive
    Junior Member
    • Total Posts : 105
    • Reward points : 0
    • Joined: 2017/05/22 22:22:08
    • Location: 0
    • Status: offline
    Re: Alarm clock using pic16f886 2018/08/23 05:45:14 (permalink)
    0
    qhb
    I didn't no so much about interrupts,worked with external interrupts only.
    Can you explain how to use interrupts for this project..?
    #19
    katela
    Super Member
    • Total Posts : 982
    • Reward points : 0
    • Joined: 2013/06/11 05:25:18
    • Location: South Africa
    • Status: online
    Re: Alarm clock using pic16f886 2018/08/23 05:56:44 (permalink)
    +1 (1)
    john_ive
    Can you explain how to use interrupts for this project..?


    http://microchipdeveloper.com/8bit:emr-interrupts
     

    Free online Microcontroller Tutorials and Projects for Hobbyists and students. From beginners to advanced. Website: www.studentcompanion.co.za
    YouTube Tutorials: https://www.youtube.com/user/StudentCompanionSA
    #20
    Page: 123 > Showing page 1 of 3
    Jump to:
    © 2018 APG vNext Commercial Version 4.5