Various Problems in C51

Publisher:Yuexiang888Latest update time:2016-11-22 Source: eefocusKeywords:C51 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

question:

     How do I write a C program to echo characters from an 8051 serial interface?

answer:

     The functions _getkey and putchar use the on-chip serial port to perform serial I/O. These subroutines are included in the C51 library, and the source files for each function are located in the \C51\LIB directory. See your C51 user manual for more information about these subroutines.

    The following example program sets up the on-chip serial port, sets the baud rate, and waits for a character (using the _getkey function). When a character is received, it outputs it using the putchar function.

#i nclude "reg51.h"
#i nclude "stdio.h"

void main (void)
{
     SCON = 0x50; /* SCON: Mode 1, 8-bit UART, enable rcvr */
     TMOD |= 0x20; /* TMOD: Timer 1, Mode 2, 8-bit reload */
     TH1 = 0xf3; /* TH1: reload value for 2400 baud */
     TR1 = 1; /* TR1: Timer 1 starts timing */
     TI = 1; /* TI: Set TI to send the first character of UART */

while (1)
     {
     unsigned char aaa;
     aaa = _getkey();
     putchar(aaa);
     }
}

question:

     I want to call a function written in C from my assembly program. How can I do this?

answer:

     The easiest way is to let the C compiler generate the correct assembly code for you.

     Suppose you have a C function called "foo" that takes a single parameter of type unsigned char and returns an unsigned char. In a new C file, write a dummy function, also called "foo". Here is an example:

#pragma src

extern unsigned char foo(unsigned char);

void dummy(void)
{
     unsigned char x, y;
     x = 1;
     y = foo(x);
}

When the file is compiled, #pragma SRC instructs the C compiler to generate assembly code. The extension of the assembly file is "src".

     If you look at the src file, you will find out how to call the function "foo" from assembly. The file shows the registers and memory addresses used to pass function variables, and the registers and memory addresses used to return values. In addition, it also tells you the correct function naming conventions, which is necessary for interfacing assembly with C.

     Then, you can use the src file as a template to write your own assembly call code. Note that you must include the EXTRN directive in the function, that is:

   EXTRN CODE (_foo)      

question:

     In the C51 compiler manual, there is an example of an assembly function calling a C function. So is there an example of a C program calling an assembly subroutine?

answer:

     It's not in the manual, but it's easy to make one yourself. An ASM subroutine must know how parameters are passed, what the return value is, and the naming conventions for segments. Follow the steps below and you can make one yourself.

  1. Write a simple function in C that passes parameters and returns values ​​the same way you would write it in assembly.

  2. Use the SRC directive (#PRAGMA SRC at the top of the file) to have the C compiler generate a .SRC file instead of an .OBJ file.

  3. Compile the C file. Because the SRC directive is used, a .SRC file is generated. The .SRC file contains the assembly code generated from the C code you wrote.

  4. Rename the .SRC file to .A51 file.

  5. Edit the .A51 file and insert the assembly code you want to execute in the assembly function body.

For example, the following code


#pragma SRC
unsigned char my_assembly_func {
         unsigned int argument)
{
    return (argument + 1); // Insert dummy lines to access all variables and intevals.
}


The SRC file is generated when compiling.

question:

     Do you have any examples of mixed C and assembly programming?

answer:

     The following example shows how to mix C and assembly in an 8051 program.

     This example starts with a MAIN C function, which calls an assembly subroutine and then calls a C function.

     The MAIN C module is as follows:


extern void a_func (void);

void main (void)
{
a_func ();
)


Function a_func is an assembly subroutine:


NAME A_FUNC

?PR?a_func?A_FUNC SEGMENT CODE
                  EXTRN CODE (c_func)
                  PUBLIC a_func

                  RSEG ?PR?a_func?A_FUNC
a_func:
                  USING 0
                  LCALL c_func
                  RET

                  END


     Note that the assembler calls the C function c_func:


void c_func (void)
{
}


     The actual code for the assembly module is generated using the SRC pragma directive and the following C source file:


extern void c_func (void);

void a_func (void)
{
c_func ();
}


You can download C2ASM2C.ZIP from the Keil website.


Keywords:C51 Reference address:Various Problems in C51

Previous article:Calendar clock program written in C513
Next article:How to locate a subroutine segment at a fixed address in C51

Recommended ReadingLatest update time:2024-11-16 15:39

Taxi meter C51 program based on 51 single chip microcomputer
Passed the simulation test #include reg52.h unsigned char cir_num,pwm_mach,temp,distance,wait_time,price,distance; unsigned int a,t; sbit pwm_mach=P1^0; sbit key_clean=P3^0; //define key position sbit key_stop=P3^1; sbit key_cheak=P3^2; sbit key_oneway=P3^3; sbit key_doubleway=P3^4; unsigned char code led_buf ={0x3F
[Microcontroller]
Mixed Programming of C51 and Assembly Language 1
1. Mixed programming within functions If you want to use assembly language inside a C language function, you should use the following Cx51 compiler control commands: #pragma asm ;;; Assembly code #pragma endasm function: The asm and endasm commands are used to merge the assembler program marked by them into
[Microcontroller]
C51 MCU expands 64K RAM
This article will briefly explain how to expand the RAM of the 51 microcontroller. In order to avoid expanding the RAM in the future, I will expand the RAM to 64KB in one step. 1. Schematic diagram illustrate: 1. The power supply is not provided in the figure, and is powered by an external pin header 2. The
[Microcontroller]
C51 MCU expands 64K RAM
C51 microcontroller pointer example
#pragma src #include reg51.H f(){} f1(){} f2(){} main() { { int x; int *px; //The following representations are annoying, but the generated code But it is extremely concise: //Assign xdata type pointer 0x4000 to px px=(int xdata *)0x4000; //It means taking a char from xdata 0x4000 and giving it to x x=*((char xdata *
[Microcontroller]
51 MCU PS2 keyboard decoding experiment --C51 source code
//Address: http://www.jdgcs.org/wiki/Downloads #include at89x51.h #include "KBCODE.H" #define LCM_RS    P2_0 #define LCM_RW P2_1 //define LCD pin #define LCM_E     P2_2 #define LCM_Data  P0 #define Busy 0x80 //Used to detect the Busy flag in the LCM status word #define Key_Data P3_2 //Define Keyboard pin #define Key_
[Microcontroller]
MCU--Power-off Protection 24C02(c51)
51 single-chip microcomputer drives 24C02 chip program, function: to achieve power-off protection.   #include"reg52.h" #define uchar unsigned char #define uint  unsigned int sbit sda=P3^4; sbit   scl=P3^6; void delay_ee() //about 1us {    ;; }   void start_ee() //Start signal When scl is a high l
[Microcontroller]
c51 ultra long time delay program 1 hour to 1000... hours
;;////////////////////////////////////////////////////////////////////;; ;; c51 ultra-long time delay program from 1 hour to 1000... hours. ;;19:44 2007-3-10;; ;;6MHZ crystal oscillator is 100ms;if the crystal oscillator is 12mhz, it is 50ms;enter a timer interrupt;; ;;You use timer interrupt 0 mode 1, enter an interr
[Microcontroller]
Single chip computer two machine communication (C51 program)
/* Send program Connection: Two MCUs are connected with 3 wires, they should share the same ground, rxd and txd should be cross-connected Program effect: Send through the host, receive from the slave In the host, the number of times the button is pressed is recorded, and the host displays the last
[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号