Embedded register Operation-----The learning of C language bit operation

Source: Internet
Author: User
Tags volatile


April 1, 2015 summary.

A |= 1<<x         //x bit write 1
a &=~ (1<<x)        //X position 0
(A & (1<<x)) = =//       judgment 1, The equal-left bracket cannot be omitted
(a& (1<<x)) ==0        //judgment 0
a>>n              //Division a/2^n
a<<n              //multiplication a*2^n


-----------------------------------------

Learning embedded in the process of discovering. C's bit operation is more, according to the data sorted as follows. Consult later.


According to the chip manual configuration registers the process, often encounter to set a certain or a few bits of data, according to the situation of 0 or 1. And for the other bits of the register, we don't care and should not care (who knows if other bits will have other uses).

At this time, bit operation is very convenient.


The bit operation of a register is mainly to place a specific position of 0 or 1. The general situation is to erase the corresponding bit first (that is, set 0), and then reset 1.

1, if only one of them is 1

A &= ~ (1<<x)//place x position 0

B | = (1<<x)//place X position 1


2, if you are placing multiple bits, the following method can be used

A &=~0001 1100//第2-4 0

B | =0001 1100//第2-4位 1

3, but this multi-digit usage makes the above mentioned problem, we care about the other bit, therefore, the recommended usage should be as follows.

A &=~ ((1<<2) | (1<<3) | (1<<4)) Set 2-4 to 0

a|= (1<<2) | (1<<3) | (1<<4)//Will be 2-4 to 1

This code initially looks like it's complicated and inconvenient to read, but that's not the case, because we usually define (1<<2) This code as a macro operation. You can look at the following code I posted.


4, how to determine whether a register is 0 or 1. Register at the bottom of the operation, often used to determine the state of a register, that is, read the value of a register.

A & (1<<X)//and operation, if the corresponding bit is 0, then the result is 0, if the corresponding bit is 1, then the result is 1.

B | (0<<x)//or operation, if the corresponding bit is 0, then the result is 0, if the corresponding bit is 1, then the result is 1.

There should be no difference between the two.

The following is my TQ2440 rewrite the key led program, bit operation using macro definition.


----------------------------------------------------

/* Button control LED light////* Observation schematic diagram, gpb5,6,7,8 respectively connected led1,2,3,4, low level light///button 1,2,3,4 respectively corresponding to eint1,4,2,0 external interrupt, Low level press * * Here we do not interrupt,		eint0-4 corresponding gpf0-4*//* thinking: Constantly scan gpf0-4 (PIN input to enable), the corresponding output gpb5-8, the corresponding relationship is as follows * */*GPB LED KEY eini gpf*//*5 1 1 1 1*//*6 2 2 4 4*//*7 3 3 2 2*//*8 4 4 0 0*//* Here is the main attention to the learning bit operation * * #include <stdio.h>/* Configuration Register Address/*/*gpbcon address (unsigned long*) 0 x56000010*//* So Gpbcon data is (* (unsigned long*) 0x56000010) */#define GPBCON (* (volatile unsigned) long*) #define   Gpbdat (* (volatile unsigned long*) 0x56000014) #define GPFCON (* (volatile unsigned) long*) #define 0X56000050 (* (volatile unsigned long*) 0x56000054)/* Register erase a 0*//* This is a 16-bit register, each setting has 2 binary digits, so erase 2 bits at a time, 3=0b11*/#define GPB5_MSK (3 << (5*2)/bit left 10 bit, [10.9] bit 1, the rest 0 #define GPB6_MSK (3<< (6*2)) #define GPB7_MSK (3<< (7*2)) #define GPB8 _msk (3<< (8*2)) #define GPF0_MSK (3<< (0*2)) #define GPF1_MSK (3<< (1*2)) #define GPF2_MSK (3<<) (2* 2) #define GPF4_MSK (3<< (4*2))/* Set PIN for input function, corresponding position 00*/#define GPF0_IN (0<< (0*2)) #define GPF1_IN (0<< (1*2)) #define GPF2_IN (0<< (2*2)) #define GP  F4_in (0<< (4*2))/* Set PIN to output function, corresponding position 01*/#define GPB5_OUT (1<< (5*2)) #define Gpb6_out (1<< (6*2)) #define Gpb7_out (1<< (7*2)) #define Gpb8_out (1<< (8*2)) int main (void) {/* Configuration pin function, gpb[5,8] for output, gpf[0,3] for input/GPB CON &=~ (Gpb5_msk | Gpb6_msk | Gpb7_msk |
	GPB8_MSK); Gpbcon |=gpb5_out | Gpb6_out | Gpb7_out |
	Gpb8_out; Gpfcon &=~ (Gpf0_msk | Gpf1_msk | Gpf2_msk |
	GPF4_MSK); Gpfcon |=gpf0_in | gpf1_in | gpf2_in |
	
	gpf4_in;

	unsigned long key_date;

		/* Low level means press/while (1) {key_date=gpfdat;		if (Key_date & (1<<0))//judge whether the No. 0 digit is 1, the corresponding key 4,gpb8 Gpbdat |= (1<<8);		is 1, the key is not pressed, the corresponding LED set 1, not light else gpbdat &=~ (1<<8);      
	    is 0, press the key, the corresponding led low level, light if (Key_date & (1<<1))//gpf1,key1,led1,gpb5 Gpbdat |= (1<<5);
			
		else Gpbdat &=~ (1<<5); if (Key_date & (1<<2))//gpf2,key3,led3,gpb57 Gpbdat |= (1<<7);
		
		else Gpbdat &=~ (1<<7);        
		if (Key_date & (1<<4))//gpf4,ke21,led2,gpb6 Gpbdat |= (1<<6);
	else Gpbdat &=~ (1<<6); }
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.