在《NIOS那些事》上看的程序,串行通信一节,有点小疑问 SOP.H中的 typedef struct { //接收寄存器 union{ struct{ volatile unsigned long int RECEIVE_DATA :8; volatile unsigned long int NC :24; }BITS; volatile unsigned long int WORD; }RXDATA; //发送寄存器 union{ struct{ volatile unsigned long int TRANSMIT_DATA :8; volatile unsigned long int NC :24; }BITS; volatile unsigned long int WORD; }TXDATA; //状态寄存器 union{ struct{ volatile unsigned long int PE :1; volatile unsigned long int FE :1; volatile unsigned long int BRK :1; volatile unsigned long int ROE :1; volatile unsigned long int TOE :1; volatile unsigned long int TMT :1; volatile unsigned long int TRDY :1; //准备信号,准备好为1,否则为0 volatile unsigned long int RRDY :1; volatile unsigned long int E :1; volatile unsigned long int NC :1; volatile unsigned long int DCTS :1; volatile unsigned long int CTS :1; volatile unsigned long int EOP :1; volatile unsigned long int NC1 :19; } BITS; volatile unsigned long int WORD; }STATUS; //控制寄存器 union{ struct{ volatile unsigned long int IPE :1; volatile unsigned long int IFE :1; volatile unsigned long int IBRK :1; volatile unsigned long int IROE :1; volatile unsigned long int ITOE :1; volatile unsigned long int ITMT :1; volatile unsigned long int ITRDY :1; volatile unsigned long int IRRDY :1; volatile unsigned long int IE :1; volatile unsigned long int TRBK :1; volatile unsigned long int IDCTS :1; volatile unsigned long int RTS :1; volatile unsigned long int IEOP :1; volatile unsigned long int NC :19; }BITS; volatile unsigned long int WORD; }CONTROL; //波特率寄存器 union{ struct{ volatile unsigned long int BAUD_RATE_DIVISOR :16; volatile unsigned long int NC :16; }BITS; volatile unsigned int WORD; }DIVISOR;
}UART_ST; 在UART.C中有个语句 UART->STATUS.WORD=0; 说是这个语句对状态寄存器清零了,这是为什么呢?volatile unsigned long int WORD;这个状态寄存器中的WORD定义的是什么意思呢? 还有->是什么用法?C里的吗? 编程能力不好,求指教
volatile unsigned long int WORD;这句话的意思是:
volatile(非易失的)unsigned long int(无符号长整型)WORD(变量名),这里用了WORD具有迷惑性但WORD的确不是C语言的关键词。
这句话和int i;语法其实是一样的。volatile 告诉编译器对此变量不能做一些特殊优化,必须每次从地址取数。
UART->STATUS.WORD=0;是个简单的结构体和联合体嵌套的一种定义调用方式,仔细翻书,把结构体和联合层层分解,就一目了然了,C的基本用法。
关键是我找不到UART这个结构体,还有为什么UART->STATUS.WORD=0这样就是对状态寄存器清零了呢,他跟这些是什么关系呢
struct{
volatile unsigned long int PE :1;
volatile unsigned long int FE :1;
volatile unsigned long int BRK :1;
volatile unsigned long int ROE :1;
volatile unsigned long int TOE :1;
volatile unsigned long int TMT :1;
volatile unsigned long int TRDY :1; //准备信号,准备好为1,否则为0
volatile unsigned long int RRDY :1;
volatile unsigned long int E :1;
volatile unsigned long int NC :1;
volatile unsigned long int DCTS :1;
volatile unsigned long int CTS :1;
volatile unsigned long int EOP :1;
volatile unsigned long int NC1 :19;
} BITS;
1、UART是一个结构体变量,UART.C中应该有UART_ST UART;这样的语句。定义一个结构体UART。 2、UART->STATUS.WORD=0这样就是对状态寄存器清零是因为 union{ struct{ volatile unsigned long int PE :1; volatile unsigned long int FE :1; volatile unsigned long int BRK :1; volatile unsigned long int ROE :1; volatile unsigned long int TOE :1; volatile unsigned long int TMT :1; volatile unsigned long int TRDY :1; //准备信号,准备好为1,否则为0 volatile unsigned long int RRDY :1; volatile unsigned long int E :1; volatile unsigned long int NC :1; volatile unsigned long int DCTS :1; volatile unsigned long int CTS :1; volatile unsigned long int EOP :1; volatile unsigned long int NC1 :19; } BITS; volatile unsigned long int WORD; 这一段联合Union的定义表示WORD和以上的Struct共享同一段内存(请仔细看C的联合、结构体章节)所以可以互相赋值。 BITS就是WORD的位