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.

Strange Problem with Timer1 on 18F4550 using PICKIT3

Status
Not open for further replies.

ahmed osama

Full Member level 6
Joined
Jul 18, 2004
Messages
352
Helped
2
Reputation
4
Reaction score
0
Trophy points
1,296
Location
Cairo, Egypt, Egypt
Activity points
2,652
i have a Strange Problem with Timer1 on 18F4550 using PICKIT3 (internal CLock 8 Mhz after OscCon 100 became 1Mhz)

the following code working so fine as there is a led turned on at the starting ( PORTDbits.RD2 = 1;) and after 30 sec , the timer1 will turn it off , it worked so fine but in cond. that while(1) must have inside it delay(); anything else the if cond. inside the timer1 ISR doesn't work at all

i tried the following

Code:
while(1) { }
while(1);
while(1) { temp (); } //temp is just empty func.
for (1;;);

nothing makes the code run so well except when i use

Code:
while(1)
{
delay(); // !!!!!!
}

sorry i am some how new to PIC programming so kindly help me

i also clear all mem , config bits ,....
i also cleared the project and rebuild it


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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include<p18f4550.h>
#include<timers.h>
 
 
void high_ISR (void);
void timer1_isr(void);
 
#pragma interrupt high_ISR //is already high
 
void high_ISR (void)
{
_asm goto timer1_isr _endasm
}
 
void delay(void);
 
int x;
void timer1_isr(void)
{
x=x+1;
if (x == 3750)
{
PORTDbits.RD2 = 0;
x=0;
}
PIR1bits.TMR1IF = 0; //Reset Timer1 interrupt flag
WriteTimer1(0xFC17); // Give new start value to the timer1
INTCONbits.GIEH  =1;
}
 
void main(void)
{ 
 
x=0;
 
INTCONbits.GIEH  =1;
 
   
 
TRISDbits.TRISD2 = 0;            // set RB0
 
  PORTDbits.RD2 = 1;        //  ON RB0
 
RCONbits.IPEN = 1; //Enable priority levels on interrupts
RCONbits.SBOREN = 0; //Disable BOR
 
IPR1bits.TMR1IP = 1; //Timer1 interrupt priority high
PIE1bits.TMR1IE = 1; //Timer1 interrupt enable
 
 OpenTimer1( TIMER_INT_ON & // Interrupt enabled
T1_16BIT_RW & //set timer1 as one 16-bit registers
T1_SOURCE_INT & //choose internal clock source (TOSC)
T1_PS_1_2 & // Prescale Value: 1:2
T1_OSC1EN_OFF & //Disable Timer1 oscillator
T1_SYNC_EXT_OFF //Don’t sync external clock input
);
 
WriteTimer1(0xFC17); // set start value of timer,
 
 
 
while(1)
{
delay();  // must delay func. and thing else the if cond. in the timer1 isr doesn't work
}
 
 
}
 
 
void delay(void) {
 
    int f, k;
 
 
 
    for(f=0; f < 1000 ; f++)
 
        for(k=0; k< 2 ; k++ )
{
        }
 
    }

 
Last edited by a moderator:

hello


Yes your clock seems to be 1Mhz
so timer1 interrupt every 8 mS * 3450 => give 30 secondes .. normal

but when you wrote
while(1)
{};

did you check what is the ASM given by the complier ?
maybe you can get the equivalent as
goto $-1

so there is no gap to go out of the main programme and return inside
maybe it can work with some additional NOP like this..

ici:
nop
nop
goto ici



it is not a NORMAL SITUATION ..
the main job must be done by the main programme , not only by interrupts.
 

i have put nop inside while(1) and didn't worked , but there is another more strange when i changed the for loop inside the delay from 1000 to 10 it does not work !!!
 

hello

try this :
Raz the interrupt flag at the end of ISR
you don't need to arm GIE .

To do a tempo of 30 sec
maybe it's better to use the max capacity ot timer1
to reduce the number of interrups ;
for example :
with a value of 3035 (0x0BDB) and with prescaler=1/8 (instead of 1/2)
you get 2000mS instead of 8 ms
so count maxi= 30/2=15 to obtain 30 secondes

Code:
void timer1_isr(void)
{
x=x+1;
if (x == 3750)
{
PORTDbits.RD2 = 0;
x=0;
}

WriteTimer1(0xFC17); // Give new start value to the timer1
PIR1bits.TMR1IF = 0; //Reset Timer1 interrupt flag
}
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top