用C访问AVR寄存器? [英] accessing AVR registers with C?

查看:86
本文介绍了用C访问AVR寄存器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近我一直在尝试学习有关微控制器的所有知识.由于这是自学的,所以花了我一段时间才能了解事物在裸机上的工作方式.长话短说,我不想在我的C代码中使用AVR库.我想使用C中的指针通过它们的地址专门访问寄存器.我已经在线搜索了无处不在,在AVR头文件中查找了内容,并读了一本书.如果有人可以帮助我,那就太好了.

I've been trying to learn everything I can about micro-controllers lately. Since this is self-study, it's taken me a while to learn how the things work at the bare metal. Long story short, I don't want to use the AVR libraries in my C code; I want to access the registers specifically through their addresses using pointers in C. I've searched everywhere online, looked inside the AVR header files, and read a book. If someone could help me out that would be wonderful.

推荐答案

您可以将整数转换为指针.这只是一个普通的强制转换表达式.

You can cast from an integer to a pointer. It's just a normal cast expression.

volatile char * const port_a = (volatile char *) 0x1B;

许多编译器提供了扩展,以指示链接程序将对象放置在特定地址:

Many compilers provide extensions to instruct the linker to place an object at a specific address:

volatile char port_a @ 0x1B; // Or something like this

优点是您不必引入全局变量来表示指针,但是对于硬件寄存器而言,它可能做的不正确.您需要仔细阅读针对特定平台的编译器手册.

The advantage is that you don't introduce a global variable to represent the pointer, but it might not do the right thing for a hardware register. You need to read carefully your compiler's manual for your specific platform.

AVR官方标头可能包含以下内容:

The official AVR headers probably contain something more like this:

#define PORTA (* (volatile char *) 0x1B)

这避免了全局变量和链接程序被黑客入侵,但是许多人也考虑使用预处理器也被黑客入侵.

This avoids the global variable and the linker hack, but many also consider using the preprocessor also to be hacking.

唯一可行的生产代码解决方案是使用官方标头.其他所有只是说明性的.

The only viable solution for production code is to use the official headers. Anything else is only instructional.

这篇关于用C访问AVR寄存器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆