4026 views|1 replies

90

Posts

0

Resources
The OP

Bit Operation in AVR Microcontroller [Copy link]

In the standard C language textbook, the operation of bit operation is basically not involved, but in the program of the single-chip computer system, it is necessary to frequently operate various registers in bytes, and these registers are usually data combinations with bits in binary as the control unit. Often each bit in an 8-bit register has its own control object, such as the direction register DDRB of port B. As shown in the figure below, it actually controls the direction of the 8 ports PB0-PB7 of the PB port, that is, each bit of it controls the direction of a port. If we want to set ports PB0-PB3 as output ports and PB4-PB7 as input ports, without using bit operators, we can directly use the assignment statement DDRB=0x0f to achieve it, which is completely feasible. However, if the following situation occurs: In the program, the status of the 8-bit port of the PB port is originally 1, 3, 5, and 7 as input. 0, 2, 4, and 6 are outputs (that is, DDRB=0x55), then the first bit of the PB port is set to output, and the status of other ports remains unchanged, and then the second bit is set to input, and the status of other ports remains unchanged. How to achieve it? Maybe we can still use assignment statements to implement it, such as DDRB=0x55; then set DDRB=0x57; then set DDRB=0x53; First of all, we must be sure that this approach is absolutely correct. But have you noticed that when changing the value of one bit, we also have to consider the status of the other 7 bits, and be careful to avoid accidentally changing the values of other bits. So is there a way to simply modify the status of a certain bit without changing the status of other bits? This brings up the concept of bit operations in microcontroller C language programming. Let's look at this statement: DDRE |= (1 << PE5); The function of this statement is to set the 5th bit of the PE port as an output port, and the status of the other ports remains unchanged. How is it implemented? PE0 0 English: First, let's look at the expression 1 << PE5. We have introduced before that the macro definitions of each AVR register are defined in the header file io.h, and we can call them directly. Now let's take a look at how PE5 is defined in io.h (search for iom64.h in the installation directory of WINAVR). We can see that the 8 bits of the PE port are defined as follows: /* Port E Data Register - PORTE */ #define PE7 7 #define PE6 6 #define PE5 5 #define PE4 4 #define PE3 3 #define PE2 2 #define PE1 1 #define PE0 0 It can be seen that, in fact, PE5=5; then 1 << PE5 is very easy to understand. Its function is to shift 1 left by 5 bits. The final result is 0b00100000 in binary. DDRE|= (1 << PE5); is actually DDRE= DDRE | (1 << PE5); First, "|" indicates an "OR" operation. DDRE is the direction register of port E, which is defined in iom64.h as: #define DDRE _SFR_IO8(0x02); It actually defines an identifier, which corresponds to an address in the data storage area RAM. We will not go into this for now. Let's look back at the function implemented by DDRE= DDRE | (1 << PE5); This sentence actually performs an OR operation on the content (data) in register DDRE and the binary number 0b00100000. We know that the result of the OR operation of two numbers is: as long as one is 1, the result is 1. So if the original value in DDRE is 0b10001010 (0x8a), the result of the "OR" operation between it and 0b00100000 becomes 0b10101010 (0xaa). We can see that after the phase or, the values of all bits except the 5th bit in DDRE have not changed, and the 5th bit has become 1. Our goal is to set the 5th bit as the output port (that is, to set the 5th bit to 1). Now let's take a look at the bitwise operators in the language: Shift operator: left shift <<, right shift >> AND operator: & OR operator: | Negation operator: ~ XOR operator: ^ That's it, there are only 6 bitwise operators in total. Now let's take a look at what these operators do; Left shift operator: The expression is x<=8以后,x的值就一直是0了,这是因为x是一个只有8位的整形变量,它的最大二进制长度是8位,当n超过8以后,移位操作的结果已经超出8位的范围了,产生了溢出现象。
        右移的原理跟左移相似,只是它的运算结果跟左移刚好相反。
       在单片机C语言编程中,经常使用移位操作来实现将数据乘以(左移)或除以(右移)2的n次方的乘除运算,利用移位操作实现乘除运算可以显著提高单片机的运算速度和效率。其详细原理我们可以翻阅相关的C语言书籍来进行更深了解。
      “取反”、“与”、“或”、“非”运算经常用于对寄存器的某一位进行操作,
例如,使端口B的第二位输出高电平,同时不改变其余端口的状态,我们可以采用如下方法:
PORTB |= (1<
Bit operations can be directly supported in IAR

Bit Operation in AVR Microcontroller


This post is from Microchip MCU

Latest reply

Bit operations can be directly supported in IAR  Details Published on 2013-12-30 18:09

48

Posts

0

Resources
2
Bit operations can be directly supported in IAR
This post is from Microchip MCU

Guess Your Favourite
Find a datasheet?

EEWorld Datasheet Technical Support

Related articles more>>

    EEWorld
    subscription
    account

    EEWorld
    service
    account

    Automotive
    development
    circle

    Robot
    development
    community

    About Us Customer Service Contact Information Datasheet Sitemap LatestNews

    Room 1530, Zhongguancun MOOC Times Building, Block B, 18 Zhongguancun Street, Haidian District, Beijing 100190, China Tel:(010)82350740 Postcode:100190

    Copyright © 2005-2025 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
    快速回复 返回顶部 Return list