Keil Logo

How to call a function after a specific clock period

Next Thread | Thread List | Previous Thread Start a Thread | Settings

Details Message
Read-Only
Author
qazi Wajid
Posted
30-Sep-2011 07:44 GMT
Toolset
C51
New! How to call a function after a specific clock period

hi
I am using 89c51. for using serial port I am using these commands

SCON  = 0x50;                  /* SCON: mode 1, 8-bit UART, enable rcvr    */
TMOD  = 0x20;                  /* TMOD: timer 1, mode 2, 8-bit reload      */
TH1   = 0xfd;                  /* TH1:  reload value for 9600 baud         */
TR1   = 1;                     /* TR1:  timer 1 run                        */

these are working correctly
------------
but in main function i am calling 2 functions

for example
---------------------

void main()
{
    while(1)
    {
        if(RI)
        {
            GetData();
        }
        if(??????????)
        {
            DisplayData();
        }
    }
}


At the location "??????????" I want to call the DisplayData() after a specific clock period, for example after every 1000 clocks. is it possible to set the timer0 of 89c51 for this purpose.

Read-Only
Author
Andrew Neil
Posted
30-Sep-2011 08:17 GMT
Toolset
C51
New! RE: How to call a function after a specific clock period

Set up a timer to generate an interrupt at the required rate;

In the timer's Interrupt Service Routine, set a flag;

In the main loop, test this flag

void main()
{
    while(1)
    {
        if(RI)
        {
            GetData();
        }

        if( time_to_display_data )
        {
            // The flag is set - it's time to display the data
            DisplayData();

            // Clear the flag ready for next time...
            time_to_display_data = 0;
        }
    }
}


Simples!

Read-Only
Author
Andrew Neil
Posted
30-Sep-2011 08:18 GMT
Toolset
None
New! RE: How to call a function after a specific clock period

Note that the above is not specific to the 8051 - the approach would work for any processor with a timer that can generate an interrupt.

Next Thread | Thread List | Previous Thread Start a Thread | Settings

  Arm logo
Important information

This site uses cookies to store information on your computer. By continuing to use our site, you consent to our cookies.

Change Settings

Privacy Policy Update

Arm’s Privacy Policy has been updated. By continuing to use our site, you consent to Arm’s Privacy Policy. Please review our Privacy Policy to learn more about our collection, use and transfers
of your data.