AVR MCU Learning (IV) Flow Light Verification in C Language

Publisher:VS821001Latest update time:2015-02-06 Source: 51heiKeywords:AVR Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Introduction to AVR IO ports and the concept of registers

1. What is a "register"

   Registers are special units in RAM that map the special functions of on-chip peripherals.

2. What is "IO port"

   A port that can convert "0" and "1" into voltage signals

   The most commonly used TTL level in microcontrollers: 0V represents "0", +5V represents "1"

   Mega16 has 4 IO ports: PA, PB, PC, PD

3. Performance indicators of IO ports

  Sink current capability: the maximum current that can flow into the IO port

  Current drawing capability: the maximum current that can flow out of the IO port

  Pull-up and pull-down resistors

  Maximum input voltage

Two states of IO port




As shown in the figure above (there is a selector switch that does not exist in the actual circuit, it is given for easy understanding) the output is either 5V or ground 0V  is the output 0  and 1

 

The ideal input IO port of the input model in the figure above  is equivalent to a suspended wire, that is, the equivalent impedance of the input to VCC and to ground must be infinite. The actual microcontroller now generally achieves more than 10M ohms.

IO port output performance indicators

1/AVR's IO port current sinking and sourcing capabilities are both up to 30mA

2/51 source current capability <100uA, sink current capability 10mA

Current sourcing capability:

The maximum current that can flow from the IO port

 The following figure only shows that the current flows out of the IO port, which is equivalent to the internal switch reaching VCC. As the external resistance value changes, the current changes. The maximum current is 30mA Imax


 

Sink current capability:

The maximum current that can flow into the IO port

The following figure is similar to the IO port input


IO port input performance indicators

1. The pull-up resistor of AVR IO port can be configured in the input state, and the value is in the order of 100K

2. Maximum input voltage range: -0.5V~VCC+0.5V


 

 AVR input IO can be configured with a pull-up resistor. It is a relatively weak pull-up of about 100K-200K. Why do we need to configure a pull-up resistor? For the input port, because when it is configured as an input, it is equivalent to a suspended point. If the input port is really suspended, what will be the level of such a suspended wire? Will the microcontroller think that it is inputting a 0 or a 1? The answer is uncertain. It may be affected by some external static electricity or some relatively strong electromagnetic fields, or 0 or 1. However, if we configure a pull-up resistor, when the IO port is suspended, the level is no longer uncertain and is determined to be pulled up to 1. The specific role of the pull-up resistor will be discussed in the next lecture. When designing this button program, everyone will have a deeper understanding. See the figure below to withstand the maximum input voltage: Input low voltage voltage -0.5  maximum 0.2VCC   input high voltage  small 0.7VCC maximum VCC+0.5V  , that is, minimum -0.5  maximum 5.5V [page]

 


Except for ATmega16, other AVR chips are similar to m16.

1. IO spoken IO register mapping (x represents A~D)\

Direction Register: DDRx

Data Register: PORTx

Input register: Pinx

2. Mapping of C language variables and registers

The io.h file maps all registers to variables with the same name. Reading and writing these variables is equivalent to reading and writing registers.

3. Output status IO register setting

When a certain DDRx position is set to 1, the corresponding IO port is set to output

A certain position of PORTx is 1 or 0, which corresponds to the level of the corresponding bit of the IO port

 

 

 

 

 

 

Here are some mapping relationships

 

First: C language is compiled into target code through AVR Studio/WinAVR, etc., and then downloaded to the hardware microcontroller through the download line programmer (now generally not used by individuals), JATG port

The second one is about the mapping of registers to registers through variables with the same name. Most microcontrollers are 8-bit timers, of course, there are also 16-bit counters, and then mapped to the data in the register 0 1. Although the RAM stores 0 or 1, it is equivalent to a form of internal high and low levels for storage. Finally, it is  converted to +5v  0v or LVTTL +3.3V 0V through the IO port UART SPI   IIC   .

Third, other variables are actually the data in the RAM, that is, the storage units in the RAM. Of course, this RAM cannot directly contact the outside world. [page]

Fourth, functions represent some operations and interactive relationships between various functions, which are manifested in the internal microcontroller as the interactive relationship between various registers and RAM, and finally converted into the relationship between hardware modules.

  • C language running light verification

loop statement,

8-bit running lights can be used to indicate 8-bit binary

By shifting the IO register, you can achieve the effect of running lights.

loop statement

 1. While statement

 2. for statement

Flow lamp verification of various operators

Assignment =  addition +  subtraction -  multiplication *  division / brackets ()  remainder%

AND&OR|Negation~  XOR^Left shift<<  Right shift>>

Logical AND && Logical OR ||  Logical NOT!

while(expression) //If the expression is not 0, it is true    (1) Infinite loop

{

   

 



How to set a breakpoint  toggle breakpoint F9

The breakpoint is when running at full speed and stops when it reaches the breakpoint position as shown below

Run  F5



To remove a breakpoint, place the cursor at the breakpoint and click the little hand or F9 key again. The breakpoint will be removed.

Press full speed again and the result is as shown below: All lights are on

The reason is that the running loop speed is too fast and a delay should be added. The AVR CPU main frequency can run at 16MHZ for 1 to 2 microseconds. 1 million cycles per second, but the human eye cannot distinguish the delay.

for (assign the initial value only once; condition (true to enter the content); execute this statement after executing the content){}; judge the second and third statements back and forth

The for loop does not have curly braces, which means only the second and third judgments are executed.


Bitwise Operations

  1. Set a position to 1, 0, or invert
  2. Set 1,

   The corresponding bit is bitwise ORed with 1.

 3. Set to 0

   The corresponding bit is bitwise ANDed with 0.

 4. Negate

   The corresponding bit is bitwise XORed with 1. If    the same bit is 0  , if the different bit is 1

 5. Shift operator: the left side is the highest bit, the right side is the lowest bit

   Shift Left<<:

 Shifting left by n bits is equivalent to multiplying by 2^n. The n bits on the left are discarded, and the n bits on the right are 0.

 6. Shift right

 Shifting right by n bits is equivalent to multiplying by 2^n. The n bits on the right are discarded and n 0s are added to the left.

Note: In 32-bit operating systems, it is the complement sign bit.  I have tried it in an embedded 32-bit operating system.

If the homemade simulator has a disadvantage and the filtering is not done well, the signal may fly away.

 

(Quote from Baidu: Program runaway

Program runaway means that after the system is disturbed by some kind of interference, the value of the program counter PC deviates from the given unique change history, causing the program to deviate from the normal running path. The factors and consequences of program runaway are often unpredictable. In many cases, after the program runaway, the system will enter an infinite loop and cause a crash.

 

Any number XORed with 1 will be inverted.

PORTB = PORTB ^ 0b11111111; // Use XOR to achieve flashing lights

 

IO port is set to output state

  • buzzer

1. Active buzzer

When the power supply voltage is added, a beeping sound will be emitted, and the current consumption is about 20mA

Traditional buzzer driving circuit

Transistor drive (as shown below)

AVR buzzer driver circuit

IO port direct drive (as shown in the figure)

 




Write a program to verify (buzzer)

 





You can see that PORTA changes and  the buzzer sounds.

Okay, let’s summarize.

Step 1: Set IO as output

DDRB = 0XFF;

DDRB |= 0b00100000 //The fifth bit outputs high level

Step 2: Output data for corresponding bits

Output 1: PORTB |= 0b00100000; //The fifth bit outputs high level

Output 0: PORTB &= ~0b00100000; //The fifth bit outputs low level

Output 1: PORTB |= (1<<5); //The fifth bit outputs high level

Output 0: PORTB &= ~ (1<<5); //The fifth bit outputs low level

Corresponding bit inversion: PORTB^ = (1<<5); //The fifth bit outputs high level

Keywords:AVR Reference address:AVR MCU Learning (IV) Flow Light Verification in C Language

Previous article:AVR MCU Learning (II) Hardware Circuit Design Tutorial
Next article:AVR MCU Learning (VI) Interrupts and Timers

Recommended ReadingLatest update time:2024-11-16 13:40

Denon AVR-1507 amplifier internal disassembly
A friend of mine has a broken Denon AVR-1507 amplifier. He said that this amplifier has been in the basement for a long time. He took it for repair but it didn't work, so he gave up repairing it. I said I would try to repair it. Someone else had repaired it before. After checking, they found that the main channel wa
[Embedded]
Denon AVR-1507 amplifier internal disassembly
Entering AVR
sequence:              I used to use STC51 + Freescale xs128 combination, 51 for simple programs and xs128 for complex programs, I only heard of AVR, PIC, STM series but never saw them. Recently, I started to use AVR for the first time because I wanted to make a Freescale electromagnetic field generator, and I felt th
[Microcontroller]
Entering AVR
Design of serial-to-FSK communication module based on AVR microcontroller
Introduction: This article uses ATmega48 chip and CMX865 chip to implement FSK communication module. Based on this module, FSK information interaction is carried out between users and business platform, which is a simple serial port communication for terminals. With the rapid development of information technology an
[Microcontroller]
Design of serial-to-FSK communication module based on AVR microcontroller
AVR AD conversion interrupt
System functions Most AVRs have internal AD. This section uses the internal AD of ATMEGA16 as an example to give the AD conversion interrupt program.    hardware design AVR main control circuit schematic diagram     AD conversion value low, LED control circuit schematic AD conversion value high, LED control c
[Microcontroller]
AVR AD conversion interrupt
AVR+DS1302 clock chip test program
#include iom16v.h #include macros.h #include "delay.h" #include "lcd.h" #define uchar unsigned char #define uint  unsigned int #define DS1302_RST_SET    PORTA |=  ( 1 0 ) #define DS1302_RST_CLR    PORTA &=~ ( 1 0 ) #define DS1302_SCLK_SET   PORTA |=  ( 1 1 ) #define DS1302_SCLK_CLR   PORTA &=~ ( 1 1 ) #define DS13
[Microcontroller]
AVR EEPROM Experiment
System functions   Write data to the EEPROM inside the AVR, then read the data from the EEPROM, use LED to indicate, and observe whether the read data is consistent with the written data.   hardware design     AVR main control circuit schematic diagram   LED control circuit schematic diagram  
[Microcontroller]
AVR EEPROM Experiment
How to learn AVR microcontroller well
  As IC manufacturers launch various high-performance microcontrollers, 51 microcontrollers are far from meeting everyone's demand for high-performance microcontrollers. At present, many companies and schools have begun to turn to AVR microcontrollers. I believe that in the next few years, AVR will be very prosperou
[Microcontroller]
AVR MCU Hardware/Software Co-simulation Using VMLAB
Preface In the process of single-chip microcomputer application development, after the source file is compiled successfully, simulation debugging work must be carried out. Simulation debugging can be divided into two categories-chip-level simulation and code-level simulation. Chip-level simulation refers to the
[Microcontroller]
AVR MCU Hardware/Software Co-simulation Using VMLAB
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号