位操作:
1 DDRB3=1 DDRB|=(1<<3) DDRB|=BIT(3) DDRB|=BIT(DDB3) 是等同的
2 PORTA&=0x7f; PORTA&=0b0111 1111
if ((PIND&0x80)==0) if((PIND&(1<<7))==0)
if ((PIND&0x80)==0x80) if((PIND&(1<<7))==(1<<7))
3 const int table[7]={1,2,3);//说明table 是按照表格样分配在程序存储器的数组的
const char *ptr1; //数据在数据存储器,而指向数据的指针在程序存储器。(地址ptr1在rom)
char * const ptr2;//数据在程序存储器,而指向数据的指针在数据存储器。
const char * const ptr3;//都在程序存储器。
const char hello[]="hello world";//为了节约空间,用常量字符形数组将字符串放在rom中。
unsigned char c =*(volatile unsigned char *)0x5f;//在数据内存中一个直接地址可以通过加指针类型符号直接访问。sreg的地址是5f,这样可以访问sreg。
*(volatile unsigned char *)0x5f|=0x80; //同上 访问0x20--0x5f地址
4 EECR|=(1<<EEMWE);
while (EECR&(1<<EEWR))
WDTCR=(1<<WDCE)|(1<<WDE); 置位WDCE,WDE
PORTB=(1<<PB7)|(1<<PB5)|(1<<PB4)|(1<<PB2);
DDRB=(1<<DDB7)|(1<<DDB5)|(1<<DDB4)|(1<<DDB2);
PORTA|=BIT(PA7); PORTA|=0X80;
PORTA^=0X80;//翻转位7
5 置位数据寄存器的第7位,
char a;
a|=(1<<7);
或者 a|=BIT(7);
6 在macros.h中。定义了常用的位操作宏定义。
#define WDR() asm("wdr")
#define SEI() asm("sei")
#define CLI() asm("cli")
#define NOP() asm("nop")
#define _WDR() asm("wdr")
#define _SEI() asm("sei")
#define _CLI() asm("cli")
#define _NOP() asm("nop")
6
#define pi 3.1415926
#define LEAP_YEAR year%4==0\
& year%100!=0| year%400==0 //一行写不下用\
#define S(a,b) a*b // area=s(3,2)
#define S(r) pi*(r)*(r) //area=S(a+b); =pi*(a+b)(a+b)
7
条件编译。
#ifdef KEY
.....;
#else
....;
#endif
用0b<1|0>指定二进制。
#define U8 unsigned char
#define U16 unsigned int
#define U32 unsigned long
#define I8 signed char
#define I16 signed int
#define I32 signed long
#define F32 float
1 DDRB3=1 DDRB|=(1<<3) DDRB|=BIT(3) DDRB|=BIT(DDB3) 是等同的
2 PORTA&=0x7f; PORTA&=0b0111 1111
if ((PIND&0x80)==0) if((PIND&(1<<7))==0)
if ((PIND&0x80)==0x80) if((PIND&(1<<7))==(1<<7))
3 const int table[7]={1,2,3);//说明table 是按照表格样分配在程序存储器的数组的
const char *ptr1; //数据在数据存储器,而指向数据的指针在程序存储器。(地址ptr1在rom)
char * const ptr2;//数据在程序存储器,而指向数据的指针在数据存储器。
const char * const ptr3;//都在程序存储器。
const char hello[]="hello world";//为了节约空间,用常量字符形数组将字符串放在rom中。
unsigned char c =*(volatile unsigned char *)0x5f;//在数据内存中一个直接地址可以通过加指针类型符号直接访问。sreg的地址是5f,这样可以访问sreg。
*(volatile unsigned char *)0x5f|=0x80; //同上 访问0x20--0x5f地址
4 EECR|=(1<<EEMWE);
while (EECR&(1<<EEWR))
WDTCR=(1<<WDCE)|(1<<WDE); 置位WDCE,WDE
PORTB=(1<<PB7)|(1<<PB5)|(1<<PB4)|(1<<PB2);
DDRB=(1<<DDB7)|(1<<DDB5)|(1<<DDB4)|(1<<DDB2);
PORTA|=BIT(PA7); PORTA|=0X80;
PORTA^=0X80;//翻转位7
5 置位数据寄存器的第7位,
char a;
a|=(1<<7);
或者 a|=BIT(7);
6 在macros.h中。定义了常用的位操作宏定义。
#define WDR() asm("wdr")
#define SEI() asm("sei")
#define CLI() asm("cli")
#define NOP() asm("nop")
#define _WDR() asm("wdr")
#define _SEI() asm("sei")
#define _CLI() asm("cli")
#define _NOP() asm("nop")
6
#define pi 3.1415926
#define LEAP_YEAR year%4==0\
& year%100!=0| year%400==0 //一行写不下用\
#define S(a,b) a*b // area=s(3,2)
#define S(r) pi*(r)*(r) //area=S(a+b); =pi*(a+b)(a+b)
7
条件编译。
#ifdef KEY
.....;
#else
....;
#endif
用0b<1|0>指定二进制。
#define U8 unsigned char
#define U16 unsigned int
#define U32 unsigned long
#define I8 signed char
#define I16 signed int
#define I32 signed long
#define F32 float
本文介绍了嵌入式系统中的位操作方法,包括位设置、位翻转及使用宏进行位操作等内容。同时展示了如何通过直接内存访问对特殊寄存器进行控制,并讨论了不同类型的指针及其存储位置。
1425

被折叠的 条评论
为什么被折叠?



