简体   繁体   English

用于8051微控制器的C ++?

[英]C++ for 8051 microcontroller?

Could someone please tell me if it's possible to burn an 8051 microcontroller with a C++ program? 有人可以告诉我是否可以使用C ++程序刻录8051微控制器? 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. Keil使用C,但是我需要编写的程序是字符串密集型的,与C#相比,C是非常不符合字符串的,这是我以前习惯使用的。 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. 目前,我正在尝试用C语言编写代码,但它变得非常混乱,所以如果我能用C ++编写它,我会非常放心。

I would need a C++ compiler that creates a Hex output file that can then be burnt onto the microcontroller. 我需要一个C ++编译器来创建一个Hex输出文件,然后可以将其烧录到微控制器上。 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. 而且,C使用一个头文件,让你引用端口,但当我试图找出这个头文件是否在C ++中使用时,我找不到任何关于它的信息。

Addition: The microcontroller I'm using is an Atmel AT89C51 with 4K Bytes of Reprogrammable Flash Memory, and 128 x 8-bit Internal RAM. 另外:我正在使用的微控制器是Atmel AT89C51,具有4K字节的可重编程闪存和128 x 8位内部RAM。 This is actually for a Robot for a project at university and the coding does not actually require OOP. 这实际上是用于大学项目的机器人,编码实际上并不需要OOP。 It just has a lot of look up tables that are in 2D string array format. 它只有很多以2D字符串数组格式查找的表。 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). 我想考虑C ++的唯一原因是因为操作字符串似乎有些混乱(由于我缺乏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. C使用#include reg51.h但我试图找出它是否适用于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. 我理解在一般情况下想要在C语言上使用c ++的原因,但在8位哈佛架构微控制器的情况下,我会警告这一点。

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. 8位机器上OOP的运行时开销。 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. 此外,如果您真的要进行一些严格的字符串处理,我建议使用C标准库而不是字符串对象库,因为您可以更好地控制就地替换,因此字符串副本在代码中变得非常明显。

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. 请发布一些您计划使用的微控制器(数据存储器,程序存储器),以及是否有任何性能要求必须满足,以便我们可以更具体地帮助您。

IAR Systems有一个8051编译器 ,它可以本地编译C ++(不能转换为C),源代码级调试也不应该是一个问题。

There is a commercial compiler from ceibo . ceibo有一个商业编译器。

However if you can use c++ (especially STL string) depends on how much resources (both ROM and RAM you will have. 但是,如果你可以使用c ++(特别是STL字符串)取决于你将拥有多少资源(ROM和RAM)。

There's a 8051 site with forums, tutorials and downloads where you may get some more resources for programming the 8051. 有一个8051网站,有论坛,教程和下载,你可以获得更多资源来编程8051。

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? 您在发布时提到了C ++和C#,这两者肯定不是理想用于微控制器上的重字符串处理,更不用说您可能正在考虑大量使用STL,这将进一步增加STL的大小。可执行文件?

So what exactly are your primary constraints (RAM, CPU, ROM etc)? 那么你的主要限制是什么(RAM,CPU,ROM等)?

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 (eg lua or nasal would both seem like suitable candidates). 如果您真的认为需要以OO方式执行此字符串处理,您可能需要考虑在控制器上运行轻量级嵌入式脚本解释器,以便您可以使用脚本语言提供字符串处理例程,而解释器本身将ANSI C编译为HEX文件(例如luanasal似乎都是合适的候选者)。

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. 但是,考虑到诸如lua之类的脚本语言通常会在空间中施加大约100kb +的开销,Nasal稍微更轻,如果禁用某些扩展,可以编译为50-70 kb。

Also, there are other scripting interpreters available that are specifically meant to be used on embedded platforms. 此外,还有其他脚本解释器可用于嵌入式平台。

IAR appears to offer a C/C++ compiler for 8051's a C/C++ compiler for 8051's . IAR似乎为8051的8051的 C / C ++编译器提供了一个C / C ++编译器 -- But in full disclosure, I have only used Keil's C compilers for 8051 development. - 但在完全披露中,我只使用Keil的C编译器进行8051开发。

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. 至于头文件问题:头文件通常由IDE供应商或硬件制造商分发,并且通常提供寄存器映射的符号表示。 A modest amount of massaging may be required to incorporate a C-based header file into a C++ project. 可能需要适度的按摩量才能将基于C的头文件合并到C ++项目中。 -- If you're about to switch IDE's / compilers you can often expect some massaging of your source code to accommodate the new compiler. - 如果您要切换IDE /编译器,您通常可以期望对源代码进行一些按摩以适应新的编译器。 (Read: accessing C code from a C++ code-base often causes me to stop for a day to do it right.) (阅读:从C ++代码库访问C代码通常会让我停下来做一天正确。)

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. 是的,您可以使用C ++程序刻录8051微控制器的内存,还有几个可用于创建Hex文件然后将文件发送到微控制器的免费编译器。 You could find any information about the process of programming microcontroller in a comprehensive article with tutorials, compilers, simulators, etc 您可以在包含教程,编译器,模拟器等综合文章中找到有关编程微控制器过程的任何信息

There are several commercial compilers available. 有几种商业编译器可供使用。 The number 1 in the industry is from Keil Software . 业界第一名来自Keil Software

You could try to convert the C++ code to C code and then compile it with your existing C compiler. 您可以尝试将C ++代码转换为C代码,然后使用现有的C编译器进行编译。

You should be able to create a Makefile which calls the C++ compiler and then runs the C compiler afterwards. 您应该能够创建一个调用C ++编译器的Makefile,然后运行C编译器。

It's not the most elegant solution but it really is pretty unusual to use C++ on small devices such as the 8051. 它不是最优雅的解决方案,但在8051等小型设备上使用C ++确实很不寻常。

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. 如果是我,我会坚持使用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. 其他人提到8051有C ++编译器。我猜你的主要问题是成本。 Lots of companies will let you write assembly for free but charge for the C or C++ compiler. 许多公司将允许您免费编写程序集,但对C或C ++编译器收费。 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? 您试图在C ++中使用哪些功能在C中变得混乱? Some of the features in C++ don't translate well to such a minimal embedded environment (streams, constructors, destructors, etc). C ++中的某些功能无法很好地转换为这样的最小嵌入式环境(流,构造函数,析构函数等)。 C can do many of the object-oriented type functions with structs. C可以使用结构来执行许多面向对象的类型函数。 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. 我会努力让它在C中运行,然后才可能花费大量资金并获得大而缓慢且难以处理的代码。

It sounds like you want a C++ compiler so that you can use std::string . 听起来你想要一个C ++编译器,以便你可以使用std::string std::string requires a heap. std::string需要一个堆。 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. 你不会有一个只有128x8位内部RAM的可用堆,特别是对于std::string对象不会考虑如果你从串口读取一个80个字符的字符串,那么它占用了60%以上的可用内存。 Are you also going to use external RAM? 你还要使用外置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. 如果是这样,您应该尽可能地将字符串处理与其余代码隔离开来,并在别处使用标记(枚举类型或#defined积分常量)。 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. 同样,128字节不是用于字符串处理的大量空间。

Why not use a C string library? 为什么不使用C字符串库? Like bstrlib or similar? bstrlib或类似? C++ just isn't what you need for this microcontroller. C ++并不是你需要的微控制器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM