C++ 对于8051微控制器?

发布于 2024-07-21 18:27:45 字数 567 浏览 8 评论 0原文

有人可以告诉我是否可以用 C++ 程序烧录 8051 微控制器吗? 我尝试在网上搜索它,但似乎无法确定是否可能。 Keil 使用 C,但我需要编写的程序是字符串密集型的,与我习惯使用的 C# 相比,C 对字符串非常不友好。 目前,我正在尝试用 C 编写代码,但它变得非常混乱,所以如果我可以用 C++ 编写它,我会非常放心。

我需要一个 C++ 编译器来创建一个十六进制输出文件,然后可以将其烧录到微控制器上。 有人听说过我可以使用的东西吗? 而且,C 使用一个头文件来引用端口,但是当我试图找出这个头文件是否也在 C++ 中使用时,我找不到任何有关它的信息。

添加: 我使用的微控制器是 Atmel AT89C51,具有 4K 字节可重编程闪存和 128 x 8 位内部 RAM。 这实际上是针对大学项目的机器人,并且编码实际上不需要 OOP。 它只是有很多二维字符串数组格式的查找表。 我想考虑 C++ 的唯一原因是因为操作字符串似乎变得很混乱(由于我缺乏 C 方面的专业知识)。

有谁知道头文件吗? C 使用 #include reg51.h 但我试图找出这是否适用于 C++,但找不到任何内容。

Could someone please tell me if it's possible to burn an 8051 microcontroller with a C++ program?
I've tried searching about it online but can't seem to find out for sure if it's possible or not.
Keil uses C, but the program I need to write is very string-intensive and C is quite string-unfriendly as compared to C# which is what I'm used to using. At the moment, I'm trying my hand at writing the code in C but it's getting really messy, so I'd be extremely relieved if I could write it in C++ instead.

I would need a C++ compiler that creates a Hex output file that can then be burnt onto the microcontroller. Anyone heard of something I could use?
And also, C uses a header file that lets you refer to ports, but when I tried to find out if this header file is used in C++ as well I couldn't find any information on it.

Addition:
The microcontroller I'm using is an Atmel AT89C51 with 4K Bytes of Reprogrammable Flash Memory, and 128 x 8-bit Internal RAM.
This is actually for a Robot for a project at university and the coding does not actually require OOP. It just has a lot of look up tables that are in 2D string array format.
The only reason I wanted to consider C++ was because of how messy manipulating strings seemed to be getting (due to MY lack of expertise in C).

And does anyone know about the header file? C uses #include reg51.h but I tried to find out if this works for C++ and couldn't find anything on it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(11

多彩岁月 2024-07-28 18:27:45

有几种可用的商业编译器。 业界排名第一的是Keil Software

There are several commercial compilers available. The number 1 in the industry is from Keil Software.

韵柒 2024-07-28 18:27:45

其他人提到 8051 有 C++ 编译器。我猜你的主要问题是成本。 许多公司会让你免费编写汇编,但对 C 或 C++ 编译器收费。 我们这里讨论的可能是几百美元。

我的主要问题是你的代码中什么变得“混乱”? 您想在 C++ 中使用哪些在 C 中变得混乱的功能? C++ 中的一些功能不能很好地转换到这样一个最小的嵌入式环境(流、构造函数、析构函数等)。 C 可以使用结构执行许多面向对象类型的函数。 应该避免其他函数(任何具有动态内存管理的函数)。

在可能花费大量金钱并获得庞大、缓慢且难以处理的代码之前,我会努力使其在 C 中运行。

Others have mentioned that there are C++ compilers for the 8051. I'd guess your main issue with those is going to be cost. Lots of companies will let you write assembly for free but charge for the C or C++ compiler. We're probably talking a few hundred dollars here.

My main question is what is getting 'messy' in your code? What features are you trying to use in C++ that is getting messy in C? Some of the features in C++ don't translate well to such a minimal embedded environment (streams, constructors, destructors, etc). C can do many of the object-oriented type functions with structs. Other functions should just be avoided (anything with dynamic memory management).

I'd make an effort to make it work in C before potentially spending lots of money and getting code that is large, slow and unwieldy.

何以心动 2024-07-28 18:27:45

为什么不使用 C 字符串库? 像 bstrlib 或类似的? C++ 并不是该微控制器所需要的。

Why not use a C string library? Like bstrlib or similar? C++ just isn't what you need for this microcontroller.

终止放荡 2024-07-28 18:27:45

听起来您想要一个 C++ 编译器,以便可以使用 std::stringstd::string 需要一个堆。 您不会有一个只有 128x8 位内部 RAM 的可用堆,特别是对于 std::string 对象来说,考虑一下,如果您从串行端口读取 80 个字符的字符串,则会消耗超过 60 个字符可用 RAM 的百分比。 您还要使用外部 RAM 吗? 多少?

您的固件真的需要在运行时处理字符串吗? 例如,它是否通过串行端口或其他接口以字符串形式发送/接收命令? 如果是这样,您应该尽可能将字符串处理与代码的其余部分隔离,并在其他地方使用标记(枚举类型或#define 整型常量)。 如果没有,通过使用标记而不是字符串,将逻辑装入处理器的受限内存中时会遇到很多麻烦。

另外,如果您确实需要进行字符串解析,那么最好编写一个一次处理一个字符的状态机,这样您就不必处理完整的字符串。 同样,128 字节对于字符串处理来说并不是很大的空间。

It sounds like you want a C++ compiler so that you can use std::string. std::string requires a heap. You won't have a usable heap with only 128x8 bits of internal RAM, especially not for std::string objects Consider that if you read an 80-character string from a serial port, that consumes over 60% of your available RAM. Are you also going to use external RAM? How much?

Does your firmware really need to handle string processing at runtime? For example, does it send/receive commands as strings via a serial port or some other interface? If so, you should isolate the string handling from the rest of your code as much as possible, and use tokens (enumerated types or #defined integral constants) elsewhere. If not, you will have a lot less trouble fitting your logic into your processor's constrained memory by using tokens instead of strings.

Also, if you do need to do string parsing, you may be better off writing a state machine that processes characters one at a time, so that you don't have to deal with full strings. Again, 128 bytes is not a lot of space for string processing.

甜心 2024-07-28 18:27:45

是的,您可以使用 C++ 程序烧录 8051 微控制器的内存,并且还有几个免费的编译器可用于创建 Hex 文件,然后将文件发送到微控制器。 您可以在 包含教程、编译器、模拟器等的综合文章

yes, you can burn the memory of 8051 microcontroller with a C++ program and also there are several free compilers that can be used to create Hex files and then send the files to the microcontroller. You could find any information about the process of programming microcontroller in a comprehensive article with tutorials, compilers, simulators, etc

梦里°也失望 2024-07-28 18:27:45

您可以尝试将 C++ 代码转换为 C 代码,然后使用现有的 C 编译器进行编译。

您应该能够创建一个调用 C++ 编译器的 Makefile,然后运行 ​​C 编译器。

这不是最优雅的解决方案,但在 8051 等小型设备上使用 C++ 确实很不寻常。

免责声明:我实际上还没有尝试过,祝你好运! 如果是我,我会坚持使用 C 并编写一些强大的字符串处理函数。

You could try to convert the C++ code to C code and then compile it with your existing C compiler.

You should be able to create a Makefile which calls the C++ compiler and then runs the C compiler afterwards.

It's not the most elegant solution but it really is pretty unusual to use C++ on small devices such as the 8051.

Disclaimer: I have not actually tried this so good luck! If it were me, I'd stick to C and write some robust string-handling functions.

遮云壑 2024-07-28 18:27:45

我首先会质疑这是否真的是一个好主意。 我理解在一般情况下想要使用 C++ 而不是 C 背后的原因,但在 8 位哈佛架构微控制器的情况下,我会警告不要这样做。

需要记住的事情包括:

  • 源代码级调试支持将介于较差和不可能之间。
  • 8 位机器上 OOP 的运行时开销。 我强烈建议在使用工具之前进行一些认真的基准测试。
  • 嵌入式系统中的内存并不便宜,毫无疑问,您会受到一些地址空间的限制。

另外,如果您确实要进行一些严肃的字符串处理,我建议您使用 C 标准库而不是字符串对象库,因为您可以更好地控制就地替换,因此字符串副本在代码中变得非常明显。

请发布一些有关您计划使用的微控制器(数据存储器、程序存储器)以及是否必须满足任何性能要求的信息,以便我们可以提供更具体的帮助。

I would question whether this is really a good idea in the first place. I understand the reasoning behind wanting to use c++ over C in the general case but in the case of an 8-bit Harvard architecture microcontroller I would warn against this.

Things to bear in mind include:

  • Source-level debugging support will be somewhere between poor and impossible.
  • Runtime overhead of OOP on an 8-bit machine. I would strongly recommend doing some serious benchmarking before committing to a tool.
  • Memory isn't cheap in embedded systems and you will no doubt have some address space limitations.

Also, if you really are going to be doing some serious string handling I would recommend using the C standard library rather than a string object library simply because you have better control over in-place substitution and so string copies become glaringly obvious in the code.

Please post a little about the microcontroller you plan to use (Data Memory, Program Memory) and whether there are any performance requirements that must be met so we can help a little more concretely.

九公里浅绿 2024-07-28 18:27:45

IAR Systems 有一个 8051 编译器,可以本地编译 C++(无需翻译为C),并且源代码级调试也不应该成为问题。

IAR Systems have a 8051 compiler which can compile C++ natively (no translation to C), and source level debugging shouldn't be a problem either.

硬不硬你别怂 2024-07-28 18:27:45

ceibo 有一个商业编译器。

但是,是否可以使用 C++(尤其是 STL 字符串)取决于您将拥有多少资源(ROM 和 RAM)。

有一个 8051 网站,其中包含论坛、教程和下载,您可以在其中获得更多有关 8051 编程的资源。

There is a commercial compiler from ceibo.

However if you can use c++ (especially STL string) depends on how much resources (both ROM and RAM you will have.

There's a 8051 site with forums, tutorials and downloads where you may get some more resources for programming the 8051.

怪我入戏太深 2024-07-28 18:27:45

您可能需要考虑提供有关您打算在该微控制器上运行的程序类型的更多详细信息:

您在帖子中提到了 C++ 以及 C#,这两种语言肯定不适合用于繁重的字符串处理 < em>在微控制器上,更不用说您可能正在考虑大量使用 STL,这会进一步增加可执行文件的大小?

那么您的主要限制到底是什么(RAM、CPU、ROM 等)?

如果您确实认为需要以 OO 方式进行字符串处理,您可能需要考虑在控制器上运行一个轻量级嵌入式脚本解释器,以便您可以使用脚本语言提供字符串处理例程,而解释器本身将是 ANSI C 编译为 HEX 文件(例如 luanasal 似乎都是合适的候选人)。

然而,考虑到像 lua 这样的脚本语言通常会在空间中施加大约 100kb 以上的开销,Nasal 更轻量级,如果禁用某些扩展,编译后的大小可能会减少到 50-70 kb。

此外,还有其他专门用于嵌入式平台的脚本解释器。

You might want to consider providing additional detail about the sort of program that you intend to run on that microcontroller:

You mentioned C++, as well as C# in your posting, both of which are surely not ideally used for heavy string processing on a microcontroller, not to mention that you are probably considering heavy use of the STL, which would furthermore increase the size of the executable?

So what exactly are your primary constraints (RAM, CPU, ROM etc)?

If you really think that you need to do this string processing in an OO fashion, you might want to consider running a lightweight embedded scripting interpreter on the controller, so that you can then provide your string processing routines using the scripting language, while the interpreter itself would be ANSI C compiled to a HEX file (e.g. lua or nasal would both seem like suitable candidates).

However, take into account that a scripting language such as lua will usually impose approximately 100kb+ of overhead in space, Nasal is somewhat more lightweight and may compile down to 50-70 kb if you disable certain extensions.

Also, there are other scripting interpreters available that are specifically meant to be used on embedded platforms.

×纯※雪 2024-07-28 18:27:45

IAR 似乎提供8051 的 C/C++ 编译器8051 的 C/C++ 编译器。 -- 但坦白说,我只使用 Keil 的 C 编译器进行 8051 开发。

至于头文件问题:头文件通常由 IDE 供应商或硬件制造商分发,并且通常提供寄存器映射的符号表示。 可能需要进行适量的修改才能将基于 C 的头文件合并到 C++ 项目中。 -- 如果您要切换 IDE/编译器,您通常会期望对源代码进行一些调整以适应新的编译器。 (阅读:从 C++ 代码库访问 C 代码通常会让我停下来一天才能正确执行。)

IAR appears to offer a C/C++ compiler for 8051'sa C/C++ compiler for 8051's. -- But in full disclosure, I have only used Keil's C compilers for 8051 development.

As for your header-file concerns: The header files are often distributed by either the IDE vendor or the hardware manufacturer and often provide a symbolic representation of your register mappings. A modest amount of massaging may be required to incorporate a C-based header file into a C++ project. -- If you're about to switch IDE's / compilers you can often expect some massaging of your source code to accommodate the new compiler. (Read: accessing C code from a C++ code-base often causes me to stop for a day to do it right.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文