Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8701287
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T02:21:41+00:00 2026-06-13T02:21:41+00:00

I am trying to figure out how to create a timer for my C8051F020

  • 0

I am trying to figure out how to create a timer for my C8051F020 MCU. The following code uses the value passed to init_Timer2() with the following formula:

65535-(0.1 / (12/2000000)=48868.

I set up the timer to count every time it executes and for every 10 counts, count one second. This is based on the above formula. 48868 when passed to init_Timer2 will produce a 0.1 second delay. It would take ten of them per second. However, when I test the timer it is a little fast. At ten seconds the timer reports 11 seconds, at 20 seconds the timer reports 22 seconds. I would like to get as close to a perfect second as I can.

Here is my code:

#include <compiler_defs.h>
#include <C8051F020_defs.h>

void init_Clock(void);
void init_Watchdog(void);
void init_Ports(void);
void init_Timer2(unsigned int counts);
void start_Timer2(void);
void timer2_ISR(void);

unsigned int timer2_Count;
unsigned int seconds;
unsigned int minutes;

int main(void)
{
    init_Clock();
    init_Watchdog();
    init_Ports();
    start_Timer2();

    P5 &= 0xFF;

    while (1);
}

//=============================================================
//Functions
//=============================================================
void init_Clock(void)
{
    OSCICN = 0x04;  //2Mhz
    //OSCICN = 0x07;    //16Mhz
}

void init_Watchdog(void)
{
    //Disable watchdog timer
    WDTCN = 0xDE;
    WDTCN = 0xAD;
}

void init_Ports(void)
{
    XBR0    = 0x00;
    XBR1    = 0x00;
    XBR2    = 0x40;

    P0      = 0x00;
    P0MDOUT = 0x00;
    P5      = 0x00; //Set P5 to 1111
    P74OUT  = 0x08; //Set P5 4 - 7 (LEDs) to push pull (Output)
}

void init_Timer2(unsigned int counts)
{
    CKCON = 0x00;   //Set all timers to system clock divided by 12

    T2CON = 0x00;   //Set timer 2 to timer mode

    RCAP2 = counts; 

    T2 = 0xFFFF;    //655535

    IE |= 0x20;     //Enable timer 2

    T2CON |= 0x04;  //Start timer 2
}

void start_Timer2(void)
{
    EA = 0;

    init_Timer2(48868);

    EA = 1;
}

void timer2_ISR(void) interrupt 5
{    
    T2CON &= ~(0x80);

        P5 ^= 0xF0;

        timer2_Count++;

        if(timer2_Count % 10 == 0)
        {
            seconds++;
        }

        if(seconds % 60 == 0  && seconds != 0)
        {
            minutes++;
        }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T02:21:42+00:00Added an answer on June 13, 2026 at 2:21 am

    I cannot take full credit for answering this question. Everyone contributed. I ended up using the external clock at 22.1184Mhz. The following snippets of code are the sections of the original post code that has changed to make it work:

    void init_Clock(void)
    {
        OSCXCN = 0x67;  //External 22.1184Mhz
    
        while ( !(OSCXCN & 0x80) );
    
        OSCICN = 0x88;
    }
    

    and

    void start_Timer2(void)
    {
        EA = 0;
    
        //External clock
        init_Timer2(47103);
    
        EA = 1;
    }
    

    From my tests this is very accurate.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to figure out how to create an 'if' statement that uses
So I'm trying to figure out how to create a timer, I came across
I'm trying to figure out the best solution to create a game-round timer in
I am trying to figure out how to create a temporary table that is
I am trying to figure out how to create an image gallery like the
I'm trying to figure out how to create a http CRAN-repository. I've tried to
i am trying to figure out how to create a javascript alert box asking
I am trying to figure out how to create 3 divs and have them
I'm trying to figure out how to create personalized urls for double-byte languages. For
I am very new to Python, and trying to figure out how to create

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.