Basic structure of single chip microcomputer and basic programming of C language

Publisher:RadiantBreezeLatest update time:2012-06-25 Source: 21ic Keywords:MCU Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

The external structure of the microcontroller:

1. DIP40 dual in-line socket;
2. P0, P1, P2, P3 four 8-bit quasi-bidirectional I/O pins; (When used as I/O input, high level must be output first)
3. Power supply VCC (PIN40) and ground GND (PIN20);
4. High level reset RESET (PIN9); (10uF capacitor connected to VCC and RESET can achieve power-on reset)
5. Built-in oscillation circuit, externally just connect crystal to X1 (PIN18) and X0 (PIN19); (the frequency is 12 times of the main frequency)
6. Program configuration EA (PIN31) connected to high level VCC; (run the program in the internal ROM of the microcontroller)
7. P3 supports the second function: RXD, TXD, INT0, INT1, T0, T1
MCU internal I/O components: (the so-called learning MCU is actually programming and controlling the following I/O components to complete the specified task)
1. 1. Four 8-bit general-purpose I/O ports, corresponding to pins P0, P1, P2 and P3;
2. Two 16-bit timer counters; (TMOD, TCON, TL0, TH0, TL1, TH1)
3. One serial communication interface; (SCON, SBUF)
4. One interrupt controller; (IE, IP)

For the AT89C52 microcontroller, the header file AT89x52.h gives the definitions of all ports of the SFR special function registers. Page 160 of the textbook gives the C language extended variable types for the MCS51 series microcontrollers.

Basics of MCU C language programming

1. Hexadecimal representation of byte 0x5a: binary is 01011010B; 0x6E is 01101110.
2. If a 16-bit binary number is assigned to an 8-bit byte variable, it is automatically truncated to the lower 8 bits and the upper 8 bits are discarded.
3. ++var means to increase the variable var by one first; var— means to decrease the variable by one later.
4. x |= 0x0f; is represented as x = x | 0x0f;
5. TMOD = ( TMOD & 0xf0 ) | 0x05; means to assign 0x5 to the lower four bits of the variable TMOD without changing the upper four bits of TMOD.
6. While( 1 ); means to execute the statement indefinitely, that is, an infinite loop. The semicolon after the statement indicates an empty loop body, that is, {;}

Programming method to output high level on a certain pin: (such as P1.3 (PIN4) pin)

#include //This header document contains symbolic definitions of the microcontroller's internal resources, including P1.3
void main( void ) //void means no input parameters and no function return value. This is the reset entry for the microcontroller to run
{
P1_3 = 1; //Assign 1 to P1_3, and pin P1.3 can output a high level VCC
While( 1 ); //Infinite loop, equivalent to LOOP: goto LOOP;
}

Note: When each pin of P0 is to output a high level, an external pull-up resistor (such as 4K7) must be connected to the VCC power supply.

Programming method to output low level on a certain pin: (such as P2.7 pin)

#include //This header document contains symbolic definitions of the microcontroller's internal resources, including P2.7
void main( void ) //void means no input parameters and no function return value. This is the reset entry for the microcontroller to run
{
P2_7 = 0; //Assign 0 to P2_7, and pin P2.7 can output a low level GND
While( 1 ); //Infinite loop, equivalent to LOOP: goto LOOP;
}

How to program a square wave output on a certain pin: (such as P3.1 pin)

#include //This header document contains symbolic definitions of the internal resources of the microcontroller, including P3.1
void main( void ) //void means no input parameters and no function return value. This is the reset entry for the microcontroller to run
{
While( 1 ) //Non-zero means true. If true, execute the following loop statement
{
P3_1 = 1; //Assign 1 to P3_1, and pin P3.1 can output a high level VCC
P3_1 = 0; //Assign 0 to P3_1, and pin P3.1 can output a low level GND
} //Since it is always true, it continuously outputs high, low, high, low..., thus forming a square wave
}

After inverting the input level of a pin, output it from another pin: (for example, P0.4 = NOT( P1.1) )

#include //This header document contains symbolic definitions of the microcontroller's internal resources, including P0.4 and P1.1
void main( void ) //void means there are no input parameters and no function return values. This is the reset entry for the microcontroller to run
{
P1_1 = 1; //Initialization. As an input, P1.1 must output a high level.
While( 1 ) //Non-zero indicates true. If true, execute the following loop statements
{
if( P1_1 == 1 ) //Read P1.1, which means P1.1 is considered as an input. If P1.1 inputs a high level VCC
{ P0_4 = 0; } //Assign 0 to P0_4, and pin P0.4 can output a low level GND
else //Otherwise, P1.1 input is a low level GND
//{ P0_4 = 0; } //Assign 0 to P0_4, and pin P0.4 can output a low level GND
{ P0_4 = 1; } //Assign 1 to P0_4, and pin P0.4 can output a high level VCC
} //Since it is always true, the output level of P0.4 is constantly changed according to the input of P1.1
}

The input level of 8 pins of a port is inverted, and the lower four bits are output from 8 pins of another port: (for example, P2 = NOT( P3 ) )

#include //This header document contains symbolic definitions of the microcontroller's internal resources, including P2 and P3
void main( void ) //void means no input parameters and no function return value. This is the reset entry for the microcontroller to run
{
P3 = 0xff; //Initialization. As an input, P3 must output a high level, and at the same time output a high level to the 8 pins of the P3 port
While( 1 ) //Non-zero means true. If true, execute the following loop statements
{ //The method of negation is XOR 1, and the method of non-negation is XOR 0
P2 = P3^0x0f //Read P3, that is, consider P3 as input, XOR the lower four bits to 1, that is, negate, and then output
} //Since it is always true, P3 is continuously negated and output to P2
}

Note: 8-bit D7, D6 to D0 of a byte are output to P3.7, P3.6 to P3.0 respectively. For example, if P3=0x0f, the four pins P3.7, P3.6, P3.5 and P3.4 will all output low level, while the four pins P3.3, P3.2, P3.1 and P3.0 will all output high level. Similarly, inputting a port P2 means reading P2.7, P2.6 to P2.0 into 8-bit D7, D6 to D0 of a byte.
1. Connect power supply: VCC (PIN40), GND (PIN20). Add decoupling capacitor 0.1uF
2. Connect crystal: X1 (PIN18), X2 (PIN19). Note that the crystal frequency (select 12MHz) and auxiliary capacitor 30pF are marked
3. Connect reset: RES (PIN9). Connect the power-on reset circuit and the manual reset circuit, and analyze the reset working principle
4. Connect configuration: EA (PIN31). Explain the reason.

LED control: MCU I/O output

Connect the positive pole (anode) of a light-emitting diode LED to P1.1, and the negative pole (cathode) of the LED to the ground GND. As long as P1.1 outputs a high level VCC, the LED will be forward-conducted (the voltage drop on the LED is greater than 1V when it is turned on), and current will flow through the LED until the LED lights up. In fact, since the high-level output resistance of P1.1 is 10K, it plays the role of output current limiting, so the current flowing through the LED is less than (5V-1V)/10K = 0.4mA. As long as P1.1 outputs a low level GND, which is actually less than 0.3V, the LED cannot be turned on, and the LED will not light up. [page]

Switch double key input: input first, output high

One button KEY_ON is connected between P1.6 and GND, and the other button KEY_OFF is connected between P1.7 and GND. When KEY_ON is pressed, the LED is on, and when KEY_OFF is pressed, the LED is off. When both buttons are pressed, the LED is half-lit, and when the buttons are released, the LED remains on, that is, ON is on and OFF is off.

#include
#define LED P1^1 //Replace P1_1 with the symbol LED
#define KEY_ON P1^6 //Replace P1_6 with the symbol KEY_ON
#define KEY_OFF P1^7 //Replace P1_7 with the symbol KEY_OFF
void main( void ) //The execution entry after the MCU is reset, void means empty, no input parameters, no return value
{
KEY_ON = 1; //As input, first output high, then KEY_ON, P1.6 is grounded to 0, otherwise the input is 1
KEY_OFF = 1; //As input, first output high, then KEY_OFF, P1.7 is grounded to 0, otherwise the input is 1
While( 1 ) //Always true, so always loop and execute all the statements in the following brackets
{
if( KEY_ON==0 ) LED=1; //Yes, KEY_ON is connected, as shown, P1.1 outputs high and LED is
onif( KEY_OFF==0 ) LED=0; //When KEY_OFF is pressed, P1.1 output is low and LED is off
} //After releasing the key, no value is assigned to the LED, so the LED maintains the last key state.
//When pressed at the same time, the LED is constantly on and off, each taking half the time, and the alternation frequency is very fast. Due to the inertia of the human eye, it looks half-lit
}

Digital tube connection and driving principle and single chip microcomputer programming

A seven-segment digital tube is actually composed of 8 LEDs, 7 of which form the seven strokes of the number 8, so it is called a seven-segment digital tube, and the remaining LED is used as a decimal point. As a habit, the 8 LEDs are marked with symbols: a, b, c, d, e, f, g, h. Corresponding to the top stroke of 8, arranged in a clockwise direction, the middle stroke is g, and the decimal point is h.

We usually correspond each diode to the 8 bits of a byte, a(D0), b(D1), c(D2), d(D3), e(D4), f(D5), g(D6), h(D7), and the corresponding 8 light-emitting diodes are connected to the 8 pins of a port Pn of the microcontroller, so that the microcontroller can control the brightness of the 8 light-emitting diodes through the high and low levels output by the pins, thereby displaying various numbers and symbols; corresponding to the byte, the pin connection method is: a(Pn.0), b(Pn.1), c(Pn.2), d(Pn.3), e(Pn.4), f(Pn.5), g(Pn.6), h(Pn.7).
If the negative poles (cathodes) of the 8 light-emitting diodes are connected together as a pin of the digital tube, this digital tube is called a common cathode digital tube, the common pin is called a common cathode, and the 8 positive poles are segment poles. Otherwise, if the positive pole (anode) is connected together and led out, it is called a common anode digital tube, the common pin is called a common anode, and the 8 negative poles are segment poles.

Taking a single common cathode digital tube as an example, the segment pole can be connected to a port Pn, and the common cathode can be connected to GND, then the seven-segment code table byte data corresponding to the hexadecimal code can be written as shown on the right:

16-key code display single chip program

We connect a common cathode digital tube SLED to the P1 port, and connect 16 keys to the P2 and P3 ports, numbered KEY_0, KEY_1 to KEY_F respectively. Only one key can be pressed during operation, and after the key is pressed, the SLED displays the corresponding key number.

#include
#define SLED P1
#define KEY_0 P2^0
#define KEY_1 P2^1 #define KEY_2 P2^2 #define KEY_3 P2^3
#define KEY_4 P2^4 #define KEY_5 P2^5 #define KEY_6 P2^6 #define KEY_7 P2^7 #define KEY_8 P3^0 #define KEY_9 P3^ 1 #define KEY_A P3^2 #define KEY_B P3^3 #define KEY_C P3^4 #define KEY_D P3^5 #define KEY_E P3^6 #define KEY_F P3 ^7













Code unsigned char Seg7Code[16]= //Use hexadecimal as array subscript to directly obtain the corresponding seven-segment code byte
// 0 1 2 3 4 5 6 7 8 9 A b C d EF
{0x3f, 0x06, 0x5b, 0x4f, 0x66, 0x6d, 0x7d, 0x07, 0x7f, 0x6f, 0x77, 0x7c, 0x39, 0x5e, 0x79, 0x71};
void main( void )
{
unsigned char i=0; //As array subscript
P2 = 0xff; //P2 is used as input, initialize output high
P3 = 0xff; //P3 is used as input, initialize output high
While( 1 )
{
if( KEY_0 == 0 ) i=0; if( KEY_1 == 0 ) i=1;
if( KEY_2 == 0 ) i=2; if( KEY_3 == 0 ) i=3;
if( KEY_4 == 0 ) i=4; if( KEY_5 == 0 ) i=5;
if( KEY_6 == 0 ) i=6; if( KEY_7 == 0 ) i=7;
if( KEY_8 == 0 ) i=8; if( KEY_9 == 0 ) i= 9;
if( KEY_A == 0 ) i=0xA; if( KEY_B == 0 ) i=0xB;
if( KEY_C == 0 ) i=0xC; if( KEY_D == 0 ) i=0xD;
if( KEY_E == 0 ) i=0xE; if( KEY_F == 0 ) i=0xF;
SLED = Seg7Code[ i ]; // Display 0 at the beginning, and get the seven-segment code according to i
}
}

Keywords:MCU Reference address:Basic structure of single chip microcomputer and basic programming of C language

Previous article:The software and hardware design of the minimum system of single chip microcomputer
Next article:MCU LCD display code

Recommended ReadingLatest update time:2024-11-16 17:52

Teach you how to learn 51 single chip microcomputer: learn the basic knowledge of hardware
1. Electromagnetic Interference Use decoupling capacitors to achieve electromagnetic compatibility. 2. Application of Decoupling Capacitors By analogy with electric current and water flow, we can get the role of decoupling capacitor: (1) Buffering effect: Preventing impact on electronic components when current is
[Microcontroller]
Teach you how to learn 51 single chip microcomputer: learn the basic knowledge of hardware
Encrypted data transmission - implementing TEA encryption and decryption algorithm on MCU
When transmitting data, have you ever considered encrypting the data for transmission? If the data to be transmitted is encrypted in the serial port or wireless, wouldn't it increase the security of communication? Commonly used encryption and decryption algorithms such as DES and RSA are difficult to implement due to
[Microcontroller]
Design and implementation of network switch based on single chip microcomputer control
introduction 随着计算机性能的提高及通信量的聚增,传统局域网已经越来越超出了自身负荷,交换式以太网技术应运而生,大大提高了局域网的性能。网络交换机能显著的增加带宽,可以建立地理位置相对分散的网络。局域网交换机的每个端口可并行、安全、实时传输信息,而且性能稳定、结构灵活、易于安装、便于管理,能很好地满足企业网和电信运营商宽带接入的需求。 1 Hardware Design of Network Switch As people demand more security and high bandwidth in network applications, network
[Microcontroller]
Design and implementation of network switch based on single chip microcomputer control
Introduction to MCU IO port configuration mode
1. Quasi-bidirectional port 2. Bidirectional port The main difference between a bidirectional port and a quasi-bidirectional port is that the quasi-bidirectional port I/O port needs to be set to 1 when inputting data, otherwise if the previous bit is low level and the next bit input level is high, the MOS tube canno
[Microcontroller]
51 MCU bit addressing instructions and programming
The 80C51 microcontroller has a bit processing function and can operate on data bits, so there is a corresponding bit addressing mode. The so-called bit addressing is to directly set a bit in the internal RAM or the bit-addressable special function register SFR to 1 or reset it to 0. The scope of bit addressing, tha
[Microcontroller]
PIC OTP microcontroller program burning method
MCU minimum system and programming pins Programming pin connection: Here we should pay special attention to the processing of Vpp pin; The burning tool used PIC K150 programmer; Here the author initially purchased Kit 3.5 because he saw that MPLAB X IDE v5.50 supported the chip he used. When he bought it, he fo
[Microcontroller]
PIC OTP microcontroller program burning method
51 MCU Study Notes (IV)_Interrupt System
Related concepts Interrupt source: The event that caused the interrupt Interrupt request: The interrupt source makes a request to the CPU for processing Breakpoint: The pause point of the program when an interrupt occurs Interrupt response: The process by which the CPU pauses the current program and responds
[Microcontroller]
PIC microcontroller multi-way switch program
Program introduction: Use the same IO port for setting, the high byte is the button, the low byte is the light, and the relationship changes are embedded in the process:  FCV_COUNT = FCV_COUNT 4; FCV_COUNT = FCV_COUNT & 0x0f; //**  Date:          Wednesday, November 21, 2012 20:50:00 //本程序版权所有: 51hei.com #define
[Microcontroller]
PIC microcontroller multi-way switch program
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号