It is currently 26 May 2018 03:17

All times are UTC + 1 hour




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Timer1 program
PostPosted: 15 Jan 2018 16:29 
Offline

Joined: 30 Nov 2017 10:02
Posts: 5
Hello,

I am facing problem in running Timer 1 in 16F88. The task is very simple, just to turn-on and turn-off a LED which is connected to port B with one second delay. The program has an error which can be seen in attachment.


int Count = 0;

void main()
{
TRISB = 0; // Port B output
OSCCON = 0b01110010; // Internal oscillator @ 8 MHz, Internal RC is used for system clock.

Delay_ms(100); // Wait for stabilize


T1CON = 0b01000001;
while (1)
{
while ( ! TMR1IF);
TMR1IF = 0;
Count ++;
if (Count == 15)
{
PORTB.F0 = 1;
}
if (Count == 31)
{
PORTB.F0 = 0;
Count = 0;
}

}

}


Attachments:
Untitled01.png
Untitled01.png [ 9.61 KiB | Viewed 117 times ]
Top
 Profile  
 
 Post subject: Re: Timer1 program
PostPosted: 15 Jan 2018 16:41 
Offline

Joined: 27 Jun 2010 12:07
Posts: 917
Location: England
Try one of the options below:

int Count = 0;

void main(){
    TRISB = 0; // Port B output
    OSCCON = 0b01110010; // Internal oscillator @ 8 MHz, Internal RC is used for system clock.

    Delay_ms(100); // Wait for stabilize


    T1CON = 0b01000001;
    while (1){
        while ( ! PIR1.TMR1IF);                 // TMR1F is a bit witin PIR1 register - wait for timer 1 to overflow
        PIR1.TMR1IF = 0;                        // clear the overflow indication
        TMR1IF_bit = 0;                         // alternative to line above
        Count ++;
        if (Count == 15){
            PORTB.F0 = 1;
        }
        if (Count == 31){
            PORTB.F0 = 0;
            Count = 0;
        }

    }

}
Hopefully the comments explain your error.


Top
 Profile  
 
 Post subject: Re: Timer1 program
PostPosted: 15 Jan 2018 17:12 
Offline

Joined: 30 Nov 2017 10:02
Posts: 5
Yes now it works. Thanks.

Regarding timing, I am looking for the right formulae using which it is possible to calculate which numbers I really need to put in Count for one second on and one second off.


Top
 Profile  
 
 Post subject: Re: Timer1 program
PostPosted: 15 Jan 2018 17:14 
Offline

Joined: 27 Jun 2010 12:07
Posts: 917
Location: England
Maybe timer calculator utility will help....

https://libstock.mikroe.com/projects/view/398/timer-calculator


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: