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

  • Home
  • SEARCH
  • 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 8554399
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T14:52:23+00:00 2026-06-11T14:52:23+00:00

My platform is a c8051F120 microcontroller. I would like to send (=tx) bytes via

  • 0

My platform is a c8051F120 microcontroller. I would like to send (=tx) bytes via UART0 using interrupts. My design so far is the following:

#define UART0_TX_SIZE 16    
char UART0_tx[UART0_TX_SIZE];
short UART0_tx_uart = 0;
short UART0_tx_main = 0;
short UART0_tx_available = 0;

void UART0_putChar(char value) {
  char SAVE_SFRPAGE;
  bit EA_SAVE = EA;
  // potentially blocking code
  while (UART0_tx_available == UART0_TX_SIZE)
    ;
  // disable interrupts
  EA = 0;
  EA = 0;
  if (UART0_tx_available) {
    UART0_tx[UART0_tx_main] = value;
    ++UART0_tx_main;
    if (UART0_tx_main == UART0_TX_SIZE)
      UART0_tx_main = 0;
    ++UART0_tx_available;
  } else {
    SAVE_SFRPAGE = SFRPAGE;
    SFRPAGE = UART0_PAGE;
    SBUF0 = value;
    SFRPAGE = SAVE_SFRPAGE;
  }
  // reenable if necessary
  EA = EA_SAVE;
}

// (return void works for other interrupts)
void UART0_Interrupt() interrupt (4) {
  if (RI0 == 1) {
    RI0 = 0;
  }
  if (TI0 == 1) { // cause of interrupt: previous tx is finished
    TI0 = 0; // Q: Should this clear tx interrupt flag be further down?
    if (SSTA0 & 0x20) { // Errors tx collision
      SSTA0 &= 0xDF;
    }
    if (UART0_tx_available) { // If buffer not empty
      --UART0_tx_available; // Decrease array size
      SBUF0 = UART0_tx[UART0_tx_uart]; //Transmit
      ++UART0_tx_uart; //Update counter
      if (UART0_tx_uart == UART0_TX_SIZE)
        UART0_tx_uart = 0;
    }    
  }
}

I am pretty sure that the initialization regarding UART0 registers and timing via Timer2 (not part of the above code) is correct, because I am able to use the blocking function:

char putchar_Blocking(char value) {
  char SFRPAGE_SAVE = SFRPAGE;
  SFRPAGE = UART0_PAGE;
  while (!TI0) // while TI0 == 1 wait for transmit complete
    ;
  TI0 = 0;
  SBUF0 = value;
  SFRPAGE = SFRPAGE_SAVE;
  return value;
}

When I want to switch to the interrupt design, of course, I also set

ES0 = 1;

Does anybody find a flaw in my design that attempts to use the interupt? Or, does anybody have sample code for this? Thank you! And a big shout-out to jszakmeister, who answered my question regarding reading the TCNT register.

  • 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-11T14:52:24+00:00Added an answer on June 11, 2026 at 2:52 pm

    My colleague Guo Xiong found the mistake: The variable UART0_tx_available was not incremented and decremented at the right place. Below is the corrected and tested version:

    #define UART0_TX_SIZE 16    
    char UART0_tx[UART0_TX_SIZE];
    short UART0_tx_uart = 0;
    short UART0_tx_main = 0;
    short UART0_tx_available = 0;
    
    void UART0_putChar(char value) {
      char SAVE_SFRPAGE;
      bit EA_SAVE = EA;
      // potentially blocking code
      while (UART0_tx_available == UART0_TX_SIZE)
        ;
      // disable interrupts
      EA = 0;
      EA = 0;
      if (UART0_tx_available) {
        UART0_tx[UART0_tx_main] = value;
        ++UART0_tx_main;
        if (UART0_tx_main == UART0_TX_SIZE)
          UART0_tx_main = 0;
      } else {
        SAVE_SFRPAGE = SFRPAGE;
        SFRPAGE = UART0_PAGE;
        SBUF0 = value;
        SFRPAGE = SAVE_SFRPAGE;
      }
      ++UART0_tx_available;
      // reenable if necessary
      EA = EA_SAVE;
    }
    
    // (return void works for other interrupts)
    void UART0_Interrupt() interrupt (4) {
      if (RI0 == 1) {
        RI0 = 0;
      }
      if (TI0 == 1) { // cause of interrupt: previous tx is finished
        TI0 = 0; // Q: Should this clear tx interrupt flag be further down?
        if (SSTA0 & 0x20) { // Errors tx collision
          SSTA0 &= 0xDF;
        }
        --UART0_tx_available; // Decrease array size
        if (UART0_tx_available) { // If buffer not empty
          SBUF0 = UART0_tx[UART0_tx_uart]; //Transmit
          ++UART0_tx_uart; //Update counter
          if (UART0_tx_uart == UART0_TX_SIZE)
            UART0_tx_uart = 0;
        }    
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Platform is windows only. From Java I'd like to send input (key strokes and
I am programming cross-platform apps via PhoneGap and I learned that using template engines
What is the best platform for developing Java EE apps using Eclipse 3.7.1 (Indigo)
Each platform has its own way of opening installed apps via URL. Android has
I have a NetBeans Platform based application using NetBeans platform 7.1.1. I have various
Platform: Linux, GTK+ Tools: Python, PyGTK and Glade. Problem: I'd like to write a
Using platform 1.4.9 with Android 3.2 on a Motorola xyboard (Xoom 2), the connectionStateChange
Coding Platform: ASP.NET 4.0 C# Consider the following scenario. I am uploading a file
On a Windows platform with Node running the following command lessc style.less > style.css
Platform: Windows XP Development Platform: VB6 When trying to set an application title via

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.