Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[PIC] Delay routine drifting

Status
Not open for further replies.
I reviewed the thread, expecting to find a clear description of the observed "delay drifting" or how it's been detected, but to no avail.

May be you are chasing a phantom?
 

Ok guys let's restart.
I think was chaching phantoms.
Ive writen the following and i can't get a precise 1ms routine.
Can someone tell me whats wrong ?

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <pic12f1822.h>
 
 
/* Setup chip configuration */
__code int __at(_CONFIG1) __CONFIG = _FOSC_INTOSC & _WDTE_OFF & _PWRTE_OFF & _MCLRE_ON & _CP_OFF & _CPD_OFF & _BOREN_OFF & _CLKOUTEN_OFF & _IESO_OFF & _FCMEN_OFF;
 
void setup(void);
void delay_ms(int milis);
volatile unsigned char done;
 
 
static void isr(void) __interrupt 0 {
 
    if(TMR0IE==1 && TMR0IF==1)//timer 1 interrupt overflow detect
    {
        done = 1; //latch enable as soon as interrupt detected
        TMR0IF = 0;//clear overflow flag
    }
 
}
 
 
void main(void) {
 
    setup();
 
 
    while(1) {
 
        delay_ms(1000);
        LATA |= (1<<2);
        delay_ms(1000);
        LATA &= ~(1<<2);
}
 
}
 
void setup(void) {
 
    OSCCON = 0b01011000; // 1MHZ internal clock
    LATA = 0;
    TRISA = 0x00;
    PORTA = 0x00;
 
 
//  Clock source selection (Fosc/4)
    TMR0CS = 0;
    PSA = 1;    // No prescaler
 
//  Enable Timer1 interrupt
    TMR0IE = 1;
    GIE = 1;
}
 
void delay_ms(int milis) {
 
    while (milis--) {
 
        done = 0; //disable delay latch
        TMR0 = 250;  //
        while (!done);  //latch till 1ms interrupt happens
    }
}
 
}

 

hello,

FOSC=1Mhz => cycle=4µS
there is a mistake on timer0 init value ..

and reinit timer0 inside interrupt , else you will add some cycles (x * 4µs)


Code:
static void isr(void) __interrupt 0 {
 
    if(TMR0IE==1 && TMR0IF==1)//timer 1 interrupt overflow detect
    {  
       [B] TMR0 = 5[/B];  // reinit timer0 immediatly, because must continue to count elapsed time
        done = 1; //latch enable as soon as interrupt detected
        TMR0IF = 0;//clear overflow flag
    }
 
}
 
 
void main(void) {
 
    setup();
 
 
    while(1) {
 
        delay_ms(1000);
        LATA |= (1<<2);
        delay_ms(1000);
        LATA &= ~(1<<2);
}
 
}
 
void setup(void) {
 
    OSCCON = 0b01011000; // 1MHZ internal clock
    LATA = 0;
    TRISA = 0x00;
    PORTA = 0x00;
 
 
//  Clock source selection (Fosc/4)
    TMR0CS = 0;
    PSA = 1;    // No prescaler
    TMR0 = 5;  /
 
//  Enable Timer1 interrupt
    TMR0IE = 1;
    GIE = 1;
}
 
void delay_ms(int milis) {
 
    while (milis--) {
 
        done = 0; //disable delay latch
       // TMR0 = 250;  // allready done inside  interrupt
        while (!done);  //latch till 1ms interrupt happens
    }
}
 

My bad paulfjujo,
New code :

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <pic12f1822.h>
 
 
/* Setup chip configuration */
__code int __at(_CONFIG1) __CONFIG = _FOSC_INTOSC & _WDTE_OFF & _PWRTE_OFF & _MCLRE_ON & _CP_OFF & _CPD_OFF & _BOREN_OFF & _CLKOUTEN_OFF & _IESO_OFF & _FCMEN_OFF;
 
void setup(void);
void delay_ms(int milis);
 
 
 
void main(void) {
 
    setup();
 
 
    while(1) {
 
        delay_ms(1000);
        LATA |= (1<<2);
        delay_ms(1000);
        LATA &= ~(1<<2);
    }
 
}
 
void setup(void) {
 
    OSCCON = 0b01011000; // 1MHZ internal clock
    LATA = 0;
    TRISA = 0x00;
    PORTA = 0x00;
//  Clock source selection (Fosc/4)
    TMR0CS = 0;
    PSA = 1;    // No prescaler
 
//  Enable Timer1 interrupt
//  TMR0IE = 1;
//  GIE = 1;
}
 
void delay_ms(int milis) {
 
    while (milis--) {
 
        TMR0 = 6;  //
        while (!TMR0IF);  //latch till 1ms interrupt happens
        TMR0IF = 0;
    }
}

 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top