NIOS的一小段程序

2019-03-25 09:41发布

在《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里的吗?
编程能力不好,求指教
此帖出自小平头技术问答
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
5条回答
kdy
1楼-- · 2019-03-25 11:22
< / volatile unsigned long int WORD;这句话的意思是:
volatile(非易失的)unsigned long int(无符号长整型)WORD(变量名),这里用了WORD具有迷惑性但WORD的确不是C语言的关键词。
这句话和int i;语法其实是一样的。volatile 告诉编译器对此变量不能做一些特殊优化,必须每次从地址取数。
UART->STATUS.WORD=0;是个简单的结构体和联合体嵌套的一种定义调用方式,仔细翻书,把结构体和联合层层分解,就一目了然了,C的基本用法。
白丁
2楼-- · 2019-03-25 15:08
 精彩回答 2  元偷偷看……
kdy
3楼-- · 2019-03-25 20:34
1、UART是一个结构体变量,UART.C中应该有UART_ST UART;这样的语句。定义一个结构体UART。 2、UART-&gt;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的位
白丁
4楼-- · 2019-03-25 21:19
明白啦,谢谢啦
kdy
5楼-- · 2019-03-25 21:31
不用客气。前几天看了坛子里的一些讨论,想做一个低成本实用的Altera Cyclone3的工程开发板包含NIOS 应用。
如果有什么好的建议欢迎到博客留言。

一周热门 更多>