MCU Universal Simulation Serial Port C Program

Publisher:Ziyu2022Latest update time:2013-03-15 Source: dzscKeywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
    //------------------------------------------------ -----------------------

  // UART.C

  // General simulation serial port program

  // Resource requirements: one hardware counter, two I/O ports

  // Set the hardware counter's count time to three times the baud rate

  // Define two I/O ports for sending and receiving, used in receive() and transmit(char) respectively

  // Set up a dedicated receive buffer to store received data

  // Note: The timer must be set according to the baud rate;

  // Each time communication starts, the initialization function init_uart() must be called

  //------------------------------------------------ -------------------------------------------------- ---------------

  //----------------------------------Pin definition, library function declaration-------------------------------------------------

  #include

  #include

  //Port definition

  sbit RXD3=P1^0;

  sbit TXD3=P1^1;

  //----------------------------------------------Variable declaration-----------------------------------------------------

  #define IN_BUF_SIZE 10 //Input buffer length definition

  #define TRUE 1

  #define FALSE 0

  static unsigned char inbuf[IN_BUF_SIZE]; //input buffer

  static unsigned char qin = 0;

  static unsigned char qout = 0;

  static char flag_rx_waiting_for_stop_bit;

  static char flag_rx_off;

  static char rx_mask;

  static char flag_rx_ready;

  static char flag_tx_ready;

  static char timer_rx_ctr;

  static char timer_tx_ctr;

  static char bits_left_in_rx;

  static char bits_left_in_tx;

  static char rx_num_of_bits;

  static char tx_num_of_bits;

  static char internal_rx_buffer;

  static char internal_tx_buffer;

  static char user_tx_buffer;

  //----------------------------------Hardware related function declarations------------------------------------------------

  bit get_rx_pin_status(); //1. Returns the value returned by the receive pin (0 or 1)

  void set_tx_pin_high(void); //2. Set the transmit pin high, output 1

  void set_tx_pin_low(); //3. Set the transmit pin to low, output 0

  void timer_set(); //4. Set the timer value to three times the baud rate

  void set_timer_interrupt(); //5. Enable timer interrupt

  void idle(); //6. Execute while waiting for port input, modify according to specific application

  //-----------------------------------Declare hardware-independent functions-------------------------------------------

  void flush_input_buffer( void ); // Clear the receive buffer

  char kbhit( void ); // Determine whether the data is received normally

  char get_char( void ); // Read a data from the receive buffer

  void turn_rx_on( void ); // Start receiving function

  void turn_rx_off( void ); // End receiving function

  extern void put_char( char ); // Send a data to the sender.

  void timer_isr(void); //interrupt processing function

  //------------------------------------------------ -------------------------------------------------- -----

  // Function name: set_tx_pin_high

  // Function description: Set the serial port sending port to high, which is related to the hardware

  //------------------------------------------------ -------------------------------------------------- ------

  void set_tx_pin_high( void )

  {

  TXD3=1;

  }

  //------------------------------------------------ -------------------------------------------------- -----

  // Function name: set_tx_pin_low

  // Function description: Set the serial port sending port to low, which is related to the hardware

  //------------------------------------------------ -------------------------------------------------- ------

  void set_tx_pin_low( void )

  {

  TXD3=0;

  }

  //------------------------------------------------ -------------------------------------------------- -----

  // Function name: get_rx_pin_status

  // Function description: Returns the status of the receiving port, high is 1, low is 0, related to the hardware

  //------------------------------------------------ -------------------------------------------------- ------

  bit get_rx_pin_status()

  {

  return(RXD3);

  }

  //------------------------------------------------ -------------------------------------------------- -----

  // Function name: timer_set

  // Function description: Set the timer working status, related to the hardware

  //------------------------------------------------ -------------------------------------------------- ------

  void timer_set()

  {

  TMOD=0x22; //Timer 1 is working mode 2 (8-bit automatic reload), 0 is mode 2 (8-bit automatic reload)

  PCON=0x00;

  TR0=0; //Start using when sending or receiving

  TF0=0;

  TH0=(256-96); //9600bps is 1000000/9600=104.167 microseconds. The timer to be executed is

  //104.167*11.0592/12= 96

  TL0=TH0;

  ET0=1;

  }

  //------------------------------------------------ -------------------------------------------------- -----

  // Function name: set_timer_interrupt

  // Function description: Enable timer interrupt, related to hardware

  //------------------------------------------------ -------------------------------------------------- ------

  void set_timer_interrupt()

  {

  EA=1;

  }

  //------------------------------------------------ -------------------------------------------------- -----

  // Function name: IntTimer0

  // Function description: Timer interrupt function, related to hardware, but no processing is done, directly call the general interrupt processing function

  //------------------------------------------------ -------------------------------------------------- ------

  void IntTimer0() interrupt 1

  {

  timer_isr(); //Call interrupt processing function

  }[page]

  //------------------------------------------------ -------------------------------------------------- -----

  // Function name: idle

  // Function description: The execution program when waiting for the timer interrupt is related to the specific application

  //------------------------------------------------ -------------------------------------------------- ------

  void idle()

  {

  }

  //------------------------------------------------ -------------------------------------------------- -----

  // Function name: timer_isr

  // Function description: Interrupt processing function, hardware-independent, general function

  //------------------------------------------------ -------------------------------------------------- ------

  void timer_isr(void) /*interrupt service routine*/

  {

  char mask, start_bit, flag_in;

  //Sending program

  if ( flag_tx_ready )

  {

  if ( --timer_tx_ctr<=0 )

  {

  mask = internal_tx_buffer&1;

  internal_tx_buffer >>= 1;

  if ( mask )

  {

  set_tx_pin_high();

  }

  else

  {

  set_tx_pin_low();

  }

  timer_tx_ctr = 3;

  if ( --bits_left_in_tx<=0 )

  {

  flag_tx_ready = FALSE;

  }

  }

  }

  // Receiving procedure

  if ( flag_rx_off == FALSE )

  {

  if (flag_rx_waiting_for_stop_bit)

  {

  if ( --timer_rx_ctr<=0 )

  {

  flag_rx_waiting_for_stop_bit = FALSE;

  flag_rx_ready = FALSE;

  internal_rx_buffer &= 0xFF;

  if (internal_rx_buffer!=0xC2)

  {

  inbuf[qin] = internal_rx_buffer;

  if ( ++qin>=IN_BUF_SIZE )

  {

  qin = 0;

  }

  }

  }

  }

  else // rx_test_busy receiving busy

  {

  if ( flag_rx_ready == FALSE )

  {

  start_bit = get_rx_pin_status();

  // Determine the starting position

  if ( start_bit == 0 )

  {

  flag_rx_ready = TRUE;

  internal_rx_buffer = 0;

  timer_rx_ctr = 4;

  bits_left_in_rx =

  rx_num_of_bits;

  rx_mask = 1;

  }

  }

  else

  {

  if ( --timer_rx_ctr<=0 )

  {

  timer_rx_ctr = 3;

  flag_in =

  get_rx_pin_status();

  if ( flag_in )

  {

  internal_rx_buffer |= rx_mask;

  }

  rx_mask <<= 1;

  if ( --bits_left_in_rx<=0 )

  {

  flag_rx_waiting_for_stop_bit = TRUE;

  }

  }

  }

  }

  }

  }[page]

  //------------------------------------------------ -------------------------------------------------- -----

  // Function name: init_uart

  // Function description: Initialize the asynchronous serial port, independent of hardware, general function

  //------------------------------------------------ -------------------------------------------------- ------

  void init_uart( void )

  {

  flag_tx_ready = FALSE;

  flag_rx_ready = FALSE;

  flag_rx_waiting_for_stop_bit = FALSE;

  flag_rx_off = FALSE;

  rx_num_of_bits = 10;

  tx_num_of_bits = 10;

  set_tx_pin_low();

  timer_set();

  set_timer_interrupt(); // Enable timer interrupt

  }

  //------------------------------------------------ -------------------------------------------------- -----

  // Function name: get_char

  // Input parameters: None

  // Output parameter: ch, receive data

  // Function description: Receive a byte, regardless of hardware, general function

  //------------------------------------------------ -------------------------------------------------- ------

  char get_char( void )

  {

  char ch;

  do

  {

  while ( qout==qin )

  {

  idle();

  }

  ch = inbuf[qout] & 0xFF;

  if ( ++qout>=IN_BUF_SIZE )

  {

  qout = 0;

  }

  }

  while ( ch==0x0A || ch==0xC2 );

  return( ch );

  }

  //------------------------------------------------ ----------------------------------------

  // Function name: put_char

  // Function description: Receive a byte, regardless of hardware, general function

  //------------------------------------------------ ----------------------------------------

  void put_char( char ch )

  {

  while ( flag_tx_ready );

  user_tx_buffer = ch;

  //Wake up the serial port

  timer_tx_ctr = 3;

  bits_left_in_tx = tx_num_of_bits;

  internal_tx_buffer = (user_tx_buffer<<1) | 0x200;

  flag_tx_ready = TRUE;

  }

  //------------------------------------------------ -------------------------------------------------- -----

  // Function name: flush_input_buffer

  // Function description: Initialize the buffer, independent of hardware, general function

  //------------------------------------------------ -------------------------------------------------- ------

  void flush_input_buffer(void)

  {

  qin = 0;

  qout = 0;

  }

  //------------------------------------------------ ----------------------------------------

  // Function name: kbhit

  // Function description: Determine whether the receiving pointer is equal to the sending pointer. It has nothing to do with the hardware. It is a general function.

  //------------------------------------------------ ----------------------------------------

  char kbhit( void )

  {

  return( qin!=qout );

  }

  //------------------------------------------------ ----------------------------------------

  // Function name: turn_rx_on

  // Function description: Start receiving, independent of hardware, general function

  //------------------------------------------------ ----------------------------------------

  void turn_rx_on( void )

  {

  flag_rx_off = FALSE;

  }

  //------------------------------------------------ ----------------------------------------

  // Function name: turn_rx_off

  // Function description: Stop receiving, not related to hardware, general function

  //------------------------------------------------ ----------------------------------------

  void turn_rx_off( void )

  {

  flag_rx_off = TRUE;

  }

  ;****************************************************** ***********

Keywords:MCU Reference address:MCU Universal Simulation Serial Port C Program

Previous article:Brushless DC motor controller based on dedicated single chip microcomputer
Next article:Delayed shutdown circuit for small single-chip computer system

Recommended ReadingLatest update time:2024-11-16 23:48

MCU Programming Tutorial - Introduction to MCU
The so-called single-chip microcomputer , in layman's terms, is a microcomputer that integrates some of the main functional components of a computer, such as the central processing unit (CPU), memory, timer, I/O (Input/Output) interface circuit, on an integrated circuit chip. Single-chip microcomputers are also called
[Microcontroller]
MCU timer (timer0 working mode 2)
; Let the LED light change every R1ms COUNT EQU 92; for a crystal oscillator of 11.0592, 92 is equivalent to 100us LED EQU P1.1   ORG 0000H   MOV R0,#00H   DJNZ R0,$     MOV SP,#60H   MOV R1,#00H ; Use R1 as the overall count and use   MOV R2,#00H   //MOV A,TMOD   //ANL A,#11110000B    //CLR ACC.3   //CL
[Microcontroller]
Design of digital clock based on stc89c52rc microcontroller (digital tube display)
Things always look simple, but they are not so easy to do. I wrote this program according to the tutorial at the beginning. I skipped some parts that I didn't understand... It turned out that this was not advisable... After writing a hundred lines, I compiled it and got an error! I couldn't solve it. O
[Microcontroller]
The function of startup.a51 program module of 80C51 microcontroller
The first program module executed by 80C51 after power on reset is not the user's main program main(), but a program module called startup.a51 hidden in the KEIL-C51 standard link library. . The main job of startup.a51 is to clear the memory blocks including idata, xdata, and pdata to 0 and initialize the recursive
[Microcontroller]
Advantages and disadvantages of seven mainstream microcontrollers
Various manufacturers have also been up and down in speed, memory, and functions, and they are uneven. At the same time, a large number of manufacturers with representative microcontrollers have emerged: Atmel, TI , ST, MicroChip, ARM... The domestic Hongjing STC microcontroller is also remarkable... The following is
[Microcontroller]
DS18b20 and PIC microcontroller communication source program
     ORG     PIC54      GOTO    MAIN      ORG     0 ;---------------------- ;---------------------------- DELAY22                     MOVLW   D'200'               ; DELAY 2*250=500mS         MOVWF    COUNT1 DE32    MOVLW   D'250'                ; 8*250=2mS     MOVWF    COUNT2 DE42    NOP                          ; 1+2+
[Microcontroller]
51 microcontroller general assembly delay subroutine,
: General delay subroutine : 7FEBH - FFEBH : Delay : The delay constant is placed in the R2 register. The delay time corresponding to the time constant N (hexadecimal) (for 6MZH crystal oscillator) is shown in the following table: : R2=0 : R2 : None Example: Delay 1 second subroutine MOV R2,#18H LCALL 7FEBH RET
[Microcontroller]
Learning experience of single chip microcomputer beginners
The single-chip microcomputer is a very practical subject. Without practice, everything is just a mirage. It is very necessary to spend some money to buy a development board. ("The development board is like the seeds that farmers plant. As long as they can be used reasonably, they will take root and sprout, and finally
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号