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.

Solution needed time delay..

Status
Not open for further replies.

akshat mishra

Newbie level 4
Newbie level 4
Joined
Jul 6, 2013
Messages
6
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Visit site
Activity points
51
I am using pic18f97j60

I want led to blink for 1 sec. Below i pasting my code i don't know what it is going wrong with that program

Timer frequency is 32 khz.

#include <p18f97j60.h>

unsigned int i=0;

void Delay_1s(void);
void init_timer1(void);

void main(void)
{
init_timer1();
TRISJbits.TRISJ1 = 0;
LATJbits.LATJ1=0;

while(1)
{
Delay_1s();
LATJbits.LATJ1 = 1;
Delay_1s();
LATJbits.LATJ1 = 0;
}
}

void Delay_1s()
{
unsigned int i = 0;

for (i=0; i<20; i++)
{
TMR1L = 55;
while(TMR1L != 255 );
}
}

void init_timer1()
{
T1CONbits.TMR1CS = 1;
T1CONbits.T1CKPS = 3;
T1CONbits.RD16 = 1;
T1CONbits.T1OSCEN = 1;
T1CONbits.T1SYNC = 1;
T1CONbits.T1RUN = 1;
T1CONbits.TMR1ON = 1;

}



Give me solution .
 
Last edited:

Hi there,

Make sure source clock is external or internal.

You enabled T1CONbits.RD16 = 1; means it is working as a 16 bit timer so 2^16=65536(0xFFFF).

I found some wrong setting in your timer1 control register. please study microcontroller datasheet and try.

change T1CON=0x39; and give a try.


also update your same code with
while initalization time turn off timer and with in 1s_delay function

T1CONbits.TMR1ON = 1; // turn on timer 1
for (i=0; i<20; i++)
{
TMR1L = 55;??
while(TMR1L != 255 );??

}
T1CONbits.TMR1ON = 0; // turn off timer 1
}

Best regards,
 

Hi there,

Make sure source clock is external or internal.

You enabled T1CONbits.RD16 = 1; means it is working as a 16 bit timer so 2^16=65536(0xFFFF).

I found some wrong setting in your timer1 control register. please study microcontroller datasheet and try.

change T1CON=0x39; and give a try.


also update your same code with
while initalization time turn off timer and with in 1s_delay function

T1CONbits.TMR1ON = 1; // turn on timer 1
for (i=0; i<20; i++)
{
TMR1L = 55;??
while(TMR1L != 255 );??

}
T1CONbits.TMR1ON = 0; // turn off timer 1
}

Best regards,


I am sorry I am new at this could you please send me the updated code.
It would be very helpful

Best regards
Akshat
 

that code i have update that code is working but i am giving 1 sec delay but it is not giving 1 sec delay.
 

Hi there

How much it giving?

Please check prescaler value , clock Fosc and Fcy??

update me

Best regards,
 

Hi,

Change your Delay function and write with simple logic, don't use timer for delay like this.

For Ex. used delay function like
void Delay_1s()
{
unsigned int i = 0, j = 0;

for (i=0; i<20; i++)
{
for(j = 0; j < 50000; j++);
}
}

Here change 50000 value up or down to adjust delay for 1 Sec.

Regards,
 

Hi,

If you want to use timer for delay function then use timer in interrupt mode not in polling mode as you used in previous post.

For ex.

timer interrupt()
{
variable++;
}
---------------
void Delay_1s()
{
unsigned int i = 0;

for (i=0; i<20; i++)
{
while(variable != 255 );
}
}


regards,
 

Hi there,

Steps to program Timer0 (16-bit mode)

1. Configure the T0CON register indicating which mode (8-bit or 16- bit) to be
used and the selected prescaler option.
2. Load register TMR0H followed by register TMR0L with initial count values.
3. Start the timer with the instruction “T0CONbits.TMR0ON = 1”
4. Keep monitoring the timer flag (T0CONbits.TMR0IF) to see if it is raised. Get
out of the loop when TMR0IF becomes high.
5. Stop the timer with the instruction “T0CONbits.TMR0ON = 0”.
6. Clear the TMR0IF flag for the next round (T0CONbits.TMR0IF = 0).
7. Go back to Step 2 to load TMR0H and TMR0L again.


Values for TMR0H and TMR0L
1. Divide the desired time delay by 0.4 μs.
2. Perform 65,536 — n, where n is the decimal value we got in Step 1.
3. Convert the result of Step 2 to hex, where yyxx is the initial hex value to be loaded into the timer’s registers.
4. Set TMR0H = yy and TMR0L = xx.


Steps to program Timer0 (8-bit mode)

1. Load the T0CON value register indicating 8-bit mode is selected.
2. Load the TMR0L registers with the initial count value.
3. Start the timer.
4. Keep monitoring the TMR0IF to see if it is raised. Get out of the loop when
TMR0IF becomes HIGH.
5. Clear the TMR0IF flag for the next round.
6. Start the timer.
7. Go back to Step 2 to load TMR0L again.

Notice that when we choose the 8-bit option, only the TMR0L register is used and the TMR0H has a zero value during the count up.

HTML:
void T0Delay(void);
void main(void)
{
TRISB=0; //PORTB output port
while (1) //repeat forever
{
PORTB=0x55; //toggle all bits of Port B
T0Delay(); //delay size unknown
PORTB 0xAA; //toggle all bits of Port B
T0Delay();
}
}
void T0Delay()
{
T0CON=0x08; //Timer0, 16-bit mode, no prescaler
TMR0H=0x35; //load TH0
TMR0L=0x00; //load TL0
T0CONbits.TMR0ON = 1; //turn on T0
while (INTCONbits.TMR0IF == 0); //wait for TF0 to roll over
T0CONbits TMR0ON = 0; //turn off T0
INTCONbits.TMR0IF = 0; //clear TF0
}


Best regards,

- - - Updated - - -

Try these also
HTML:
Write a C18 program to toggle only the PORTB.4 bit continuously every 50 ms.
Use Timer0, 16-bit mode, the 1:4 prescaler to create the delay. Assume XTAL = 10MHz.

FFFFh - 85EEH = 7A11H
= 31249 + 1 = 31250
Timer delay =
31250 x 4 x 0.4 ts = 50 ms



void T0Delay(void);
#define mybit PORTBbits.RB4
void main(void)
{
TRISBbits.TRISB4 = 0;
while repeat forever
{
mybit ^ = 1; //toggle using ex-or (^)
T0Delay();
}
}
void T0Delay()
{
T0CON=0x01; //Timer0, 16-bit mode, 1:4 prescaler
TMR0H=0x85; //load TH0
TMR0L=0xEE; //load TL0
T0CONbits.TMR0ON = 1; //turn on T0
while (INTCONbits.TMR0IF == 0); //wait for TF0 to roll over
T0CONbits TMR0ON = 0; //turn off T0
INTCONbits.TMR0IF = 0; //clear TF0
}
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top