Study notes | LED lighting principle | Application of transistors in digital circuits | Tab settings in Keil | Alignment of two-way ports in C51 | STC32G microcontroller video development tutorial (Brother Chong) | Episode 4 - Part 1: Lighting the LED

1. LED lighting principle

Insert image description here
Why can the LED light up?
The concept is introduced: output voltage = VCC is high level, and output voltage
= GND (usually OV) is low level. They are represented by 1 and 0 respectively. These are ideal values.
Insert image description here

STC32G12K128LQFP64QFN64 pin diagram:

Insert image description here
Insert image description here
Insert image description here

Tips: USB-ISP download process

Now STC's MCU with hardware USB supports hardware USB download, because it uses the USB-HID communication protocol and does not require the installation of any driver. STC dog-beating stick, dragon-subduing stick, sky-opening axe, dragon-slaying knife core board, STC open source oscilloscope, and STC experimental box are connected to the PC-USB port when D-/P3.0.D+/P3.1 is well connected.
USB-ISP download program steps;
1. Press the P3.2/INTO button on the board, which means P3.2 is grounded.
2. Repower the target chip, regardless of whether it has been powered on before.
—==The electronic switch is pressed and then released to power off, or to power on and
wait for the "STC USB Writer (HID1)" to be automatically recognized in the STC-ISP download software. Once recognized, it has nothing to do with the P3.2 status. At this time You can loosen the P3.2 button
=== The traditional mechanical self-locking switch cuts off power when pressed up, and powers on when pressed down.
3. Click the "Download/Programming" button in the download software (Note: USB download and serial port download operations The order is different)
The download is successful!
—==In addition, soft reset from the user area to the system area is also waiting for the USB download.

GPIO (general purpose intput output)

The abbreviation of universal input and output port, its input level can be read through software, or its output high and low levels can be controlled.
P5.0 is a GPIO port.
PO is a group of GPIO ports
. PO.0 is one of the GPIO ports in a group. There are 8 in total from PO.0 to P0.7.
We can operate one IO port at a time
or a group at the same time.
How to light LED4? It is connected to the port of P6.0.
According to the schematic diagram, it is required that: P40 outputs low level, and P60 outputs low level to complete lighting.
Insert image description here

Tips: Application of transistors in digital circuits

Reference: Working principle and usage of triode

  • 1. Principle of transistor Transistor has three working states: cut-off, amplification and saturation. The amplified state is mainly used in analog circuits, and the usage and calculation methods are relatively complicated, so we will not use it for the time being. Digital circuits mainly use the switching characteristics of triodes, and only use two states: cutoff and saturation.
    NPN and PNP are mainly different in current direction and voltage sign.
    Here we take the NPN transistor as an example to explain its working principle. 
    Insert image description here
    It is a device that uses b (base) current Ib to drive the current Ic flowing through ce. Its working principle is much like a controllable valve.
    The small blue water flow in the thin pipe on the left drives the lever to open the valve of the large water pipe, allowing the larger red water flow to pass through the valve. When the blue water flow is larger, it also makes the red water flow in the large pipe larger. If the magnification is 100, then when the small blue water flow is 1 kg/hour, then the large pipe is allowed to flow 100 kg/hour of water. In the same way, when the amplification factor of the transistor is 100, when Ib (base current) is 1mA, a current of 100mA is allowed to pass through Ice.
    Insert image description here
    Insert image description here

The working principles of the two transistors are summarized as follows:
The emitter (e) of NPN is connected to ground, the collector © is connected to high level, the base (b) is connected to the control signal, and the current of be (Ib) is used to control the current (Ic) of ce. The e-electrode has the lowest potential, and the c-electrode has the highest potential during normal amplification, that is, Vc > Vb > Ve. The triode is turned on, and the current flows from the c pole to the e pole.
The emitter (e) of PNP is connected to high level, the collector © is connected to low level, the base (b) is connected to the control signal, the current (Ib) of eb is used to control the current (Ic) of ec, and the potential of e pole is the highest, and During normal amplification, the potential of c electrode is usually the lowest, that is, Vc < Vb < Ve. The triode is turned on, that is, the current flows from the e pole to the c pole.
Insert image description here

  • 2. How to use a triode
    . The key point of the usage characteristics of a triode is the voltage between the b pole (base) and the e level (emitter). For PNP, the e pole voltage only needs to be 0.7V higher than the b level. This Transistors E-level and C-level can be smoothly connected. In other words, the controlling end is between b and e, and the controlled end is between e and c. In the same way, the conduction voltage of the NPN transistor is that the b pole is 0.7V higher than the e pole. In short, if the starting point of the arrow is 0.7V higher than the end, the e pole and c pole of the triode can be turned on. This is the explanation of "the conduction voltage passes in the direction of the arrow, and the voltage conducts".
    The following takes a common LED control circuit as an example to illustrate the cut-off and saturation working states. As shown in the figure below, the base of the triode is connected to an IO port of the microcontroller through a 10K resistor, assuming it is P1, the emitter is directly connected to the 5V power supply, the collector is connected to an LED, and a 1K current limiter is connected in series The resistor is finally connected to the negative terminal of the power supply, GND. If P1 is given a high level of 1 by our program, then the base b and the emitter e are both 5V, which means that there will not be a 0.7V voltage drop from e to b. At this time, the emitter and collector will also There will be no conduction, so if you look at the circuit vertically, it will be disconnected at the transistor, and there will be no current flowing through it, so the LED will not light up. If the program gives P1 a low level of 0, then the e pole is still 5V, so a voltage difference occurs between e and b, and the transistor e and b are turned on. There is about 0.7V between the transistor e and b. voltage drop, then there will be a voltage of (5-0.7) V across resistor R47.
    [Note] The high level output of the P1 port here is 5V. The high level output voltage of the IO port of different microcontrollers is different. The IO output of some microcontrollers is 1.2V, which requires transistor amplification to drive LEDs, etc. Work.
    At this time, e and c will also be connected, so the LED itself has a voltage drop of 2V, and the transistor itself has a voltage drop of about 0.2V between e and c, which we ignore. Then there will be a voltage drop of about 3V on R41. It can be calculated that the current of this branch is about 3mA, which can successfully light up the LED.
    As mentioned before, the transistor has three states: cut-off, amplification, and saturation. It goes without saying that the cut-off state is as long as there is no conduction between e and b. We want this triode to be in a saturated state, which is what we call switching characteristics, and a condition must be met. Each transistor has an amplification factor β. To be in a saturated state, the b-electrode current must be greater than the current value between e and c divided by β. This β can be roughly considered to be 100 for commonly used transistors.
    Then we have to calculate the resistance of R47 above. Just now we calculated that the current between e and c is 3mA, then the minimum current of the b pole is 3mA divided by 100 equals 30uA, about 4.3V voltage will fall on the base resistance, then the maximum value of the base resistance is 4.3V/ 30uA = 143K. As long as the resistance value is smaller than this value, it is fine. Of course, it can’t be too small. Too small will cause the IO port current of the microcontroller to burn out the triode or the microcontroller. The maximum theoretical value of the input current of the IO port is 25mA. I recommend not to exceed 6mA. We Using the voltage and current calculations, you can calculate the minimum resistance value.
    +3 Summary: The arrow points inward to PNP, the conduction voltage passes along the arrow, the voltage is turned on, and the current is controlled.

2 New construction

Follow the manual: "STC32G12K128 Experimental Box-V9.4 Instruction Manual" to create a new project, and realize the lighting step by step.

  • 1. Create a new Keil project
    (since there are many versions of Keil, this manual will only use Keil’s uVersion5 as an example for introduction. Other versions of Keil are similar> First
    open the Keil software and open “New uVersion” in the “Project” menu Project..." project.

The STC chip library has been added in the previous section, here select in the drop-down menu: STC MCU Database
Insert image description here
Create a new file Demo.c (the extension is recommended to be lowercase):

Write code:

sfr P0 = 0x80; //关键字sfr以红色显示
sfr P0M1  = 0x93;
sfr P0M0  = 0x94;

void main()
{
    
    
    P0M0 = 0x00;
    P0M1 = 0x00;

    while(1)
    {
    
    
        P0++;
    }
}

Compiled and passed. ".\Objects\Demo" - 0 Error(s), 0 Warning(s).
But no files were prompted to be generated.

Tips:Tab settings

GB2312+4 spaces:
Insert image description here

  • 2 option to set output, select HEX386:

Recompile:
Insert image description here
Prompt: creating hex file from “.\Objects\Demo”…

  • 3 Download
    Follow the tutorial method and download manually. The prompt is successful, but the light is not on.
    Cause analysis: The pins are wrong.
    Here we need to introduce a sbit function.
sbit P00 = P0^0;    //选择P0.0引脚

TIPS: Bit variables of special function registers in sbit c language

sbit is a bit variable that defines a special function register. Both bit and sbit are variable types extended by C51. Typical applications are: sbit P0_0=P0^0;//That is, define P0_0 as the first bit of the P0 port for bit operations. Bit is similar to int char, except that char=8 bits and bit=1 bit. They are all variables, and the compiler assigns addresses during the compilation process.
Manual 9.2.12 Bit-addressable data memory in the STC32G series microcontroller: The bit-
addressable data memory inside the STC32G series microcontroller consists of two parts: the address range of the first part is 20H 7FH in the DATA area, and the address range of the second part is Special function register SFR:8OH FFH.
Special function register (SFR) area
Insert image description here
Insert image description here

80H~-FFH of all SFR areas. 128 bytes in total. Each byte is bit addressable. Specify a name, corresponding to a path. The single-chip microcomputer realizes a function we want according to the value in the address.

11.2 Configure IO port

The configuration of each IO needs to be set using two registers.
Taking the P0 port as an example, configuring the P0 port requires the use of two registers, P0M0 and P0M1, as shown in the figure below: The
0th bit of P0M0 and the 0th bit of P0M1 are combined to configure the mode of the P0.0 port,
which is the 0th bit of P0M0. The combination of bit 1 and the first bit of P0M1 configures the mode of P0.1 port, and the configuration of all other IOs is similar to this.
Insert image description here

Insert image description here

Notice:

Although each IO port can withstand a sink current of 20mA in weak pull-up (quasi-bidirectional port)/strong push-pull output/open drain mode (you still need to add a current limiting resistor, such as 1K, 560Q2, 472Q2, etc.), in strong The push-pull output can output a pull current of 20mA (a current limiting resistor must also be added), but the operating current of the entire chip is not recommended to exceed 90mA, that is, the current flowing in from VCC is not recommended to exceed 90mA, and the current flowing out from GND is not recommended to exceed 90mA. The overall inflow/outflow current is recommended not to exceed 90mA.
Insert image description here
Insert image description here

Here it is configured as a quasi-bidirectional port, which can output high and low levels externally, and can also read an external high and low level.

Tips: Explanation of aligning two-way ports in C51

Source: Baidu Encyclopedia
Quasi-bidirectional port: There is a fixed internal pull-up resistor.
The quasi-bidirectional port means that P1, 2, and 3 have fixed internal pull-up resistors. When used as inputs, they are pulled high. When externally pulled low (low level), the current is sourced (source current). The source current here is the source of the current. The microcontroller goes out), and P0 is a true bidirectional port, because it is suspended when used as input.
The Chinese name of the quasi-bidirectional port has an internal pull-up, so the high level is given internally and is not a real external signal. The
manual
of C51 says: "Because Ports 1, 2, and 3 have fixed internal pullups, they are sometimes called quasi -bidirectional ports. When configured as inputs, they pull high and source current (IIL) when externally pulled low. Port 0, on the other hand, is considered truly bidirectional, because it floats when configured as an input. " "Accurate"
is The "basic meaning" is that the "quasi-two-way port" is not a true two-way port.
In fact, the focus is on the P0 port.
The P0 port is bidirectional, which means that when it is used as an address/data port, only at this time is the P0 port in the push-pull state of the two switch tubes. When both switch tubes are closed, a high-impedance state will occur. When the P0 port is used as a general I/O port, the switch tube connected to Vcc internally is disconnected from the pin (port). At this time, only the switch tube pulling the ground works, and the P0 port is used as an output. An external pull-up resistor is required, otherwise it will not be able to output a high level;
If the P0 port is used as an input, you must first write 1 to the port to disconnect the ground switch. At this time, if the pull-up resistor is not connected, it will be in a high-impedance state, which is a bidirectional port. If a pull-up resistor is connected, Then it outputs a high level and has no impact on the logic of the input signal (note that it has no impact on the logic. I am not sure whether it has any impact on the actual parameters, but I think it does).
Bidirectional and quasi-bidirectional, the basic principle is that bidirectional includes the state of high resistance, not whether you need to write 1 first or not. The P1 and P3 ports have internal pull-up resistors, so they are not bidirectional anyway; there is no internal pull-up in the P0 port When the resistor is in the data/address function, it automatically completes the conversion of the 3 states. It is bidirectional. When it is in the general I/O port, if it is not connected to an external pull-up and writes 1 to the port first, it will be in a high-impedance state. At this time, it is also an artificial bidirectional port, which is different from the automatic bidirectional port when it is in the address/data function, and it is different from the output latch on the I2C bus when the output latch is 1 when P1 and P3 are in the input. The open-pole or open-collector structure is similar. A level drive is provided through a pull-up resistor (or pull-down resistor).
When used as an input, the switch is turned off, so that only the pull-up (or pull-down) is left Resistor, so the impedance is relatively high, and
the IO port can be driven by other devices. When the quasi-bidirectional port is used as an input, it is actually an output state.
It’s just that the internal resistance of the output state is relatively large. The real bidirectional IO port has a direction control register, and the
output part is disconnected when used as an input.
The difference between the bidirectional port and the quasi-bidirectional port is that the bidirectional port has a high-impedance state, and the input is a real external signal, and the quasi-bidirectional port has an internal pull-up, so the high level is given internally and is not a real external signal! When the software does processing You have to write "1" to your mouth first!
The P0 port is a true two-way port, and the rest are quasi-bidirectional ports! P0 has no internal pull-up (open-drain output), and the external NMOS circuit needs to be connected to a pull-up! The input is a high-impedance floating state! The driving capacity of P0 is twice that of a single other port!

Summary of learning content:

  • 1. Think about why the LED does not light up?
    The pins are wrong.
  • 2. How to control a single pin?
    sbit P00 = P0^0; //Select P0.0 pin
  • 3. How to control the output level of this pin?
    P00 = 1; //Output low level
  • 4. Writing standards
    : one line per line, indent each brace (needs to be set), indent four spaces, and have complete comments.
  • 5. Comment symbols
    // single-line comments
    /* */ multi-line comments
  • 6. Learn to search
    CTRL+F

3. Light up the first LED (code)

//sfr P0    = 0x80; 	//原始例程,关键字sfr以红色显示,第一次下载不亮,需修改
//sfr P0M1  = 0x93;
//sfr P0M0  = 0x94;

sfr P2    = 0xA0;	//P2端口操作完成(屠龙刀三板载led)
sfr P2M1  = 0x95;
sfr P2M0  = 0x96;

//sfr P4    = 0xC0;	//P4端口操作完成(STC32G12K128实验箱-V9.6)
//sfr P4M1  = 0xB3;
//sfr P4M0  = 0xB4;

//sfr P6    = 0xE8;	//P6端口操作完成(STC32G12K128实验箱-V9.6)
//sfr P6M1  = 0xCB;
//sfr P6M0  = 0xCC;

//sbit P40 = P4^0;    //选择P4.0引脚(三极管控制)
//sbit P60 = P6^0;    //选择P6.0引脚(LED控制)

sbit P21 = P2^1;    //选择P2.1引脚(LED控制)(屠龙刀三板载led)

void main()
{
//    P0M0 = 0x00; //原始例程,
//    P0M1 = 0x00; //原始例程,

//    P4M0 = 0x00; //配置P4为准双向口
//    P4M1 = 0x00;
//    P6M0 = 0x00; //配置P6为准双向口
//    P6M1 = 0x00;
	P2M0 = 0x00; //配置P2为准双向口(屠龙刀三板载led)
	P2M1 = 0x00;
//
    while(1) //死循环
    {
        //P0++; //原始例程,
//		 P40 = 0; //三极管引脚输出低电平(STC32G12K128实验箱-V9.6)
//		 P60 = 0; //LED引脚输出低电平(STC32G12K128实验箱-V9.6)
		 P21 = 0; //设置低电平	(屠龙刀三板载led)
    }
}

4. Implement automatic download projects

You can refer to: Study notes | LED lighting principle | Automatically download the minimum project implementation and bug troubleshooting ideas | USB-HID | USB-CDC implementation | STC32G microcontroller video development tutorial (Brother Chong) | Episode 4-Part 2: Lighting the LED

Tips: Explanation of EA = 1

EA is obviously a register, recompile, jump to open the header file definition and take a look at "stc.h" --> "stc32g.h":
Insert image description here
Search to find EA, defined as: "sbit EA = IE^7;" (IE A bit in the register), open the manual, there is also a corresponding definition:

Insert image description here
IE^7 only appears in this place in the manual. There is too little information. You need to continue searching for IE. There is IE in the special register:

Insert image description here
Insert image description here
There is still a certain gap between the detailed introduction we need. Then search sequentially to find the interrupt enable register (interrupt enable bit): Insert image description here
EA: Total interrupt enable control bit. The function of EA is to enable interrupts to form multi-level control. That is, each interrupt source is first controlled by EA; secondly, it is also
controlled by each interrupt source's own interrupt enable control bit.
0: CPU blocks all interrupt requests
1: CPU opens interrupts

5. Summary

1.了解新工程的一个基本流程
2.了解IO口的高低电平的含义
3.务必牢记端口的几个寄存器 PxM0,PxM1,Px
4.务必牢记 sbit 的 作用,牢记#include的作用
5.了解USB_cDc不停电下载的用法USB_HID不停电下载

After-class exercise: Light up all the LED lights.

Guess you like

Origin blog.csdn.net/Medlar_CN/article/details/132167619