NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » Switching from C to Python tips?

September 10, 2012
by wadaltmon
wadaltmon's Avatar

I have just finished learning the basics of the Python language, and I think I am ready to start utilizing it with my Nerdkits! That is, if it is possible... But, what are the basic changes when switching to Python from C?

•Is it possible to transfer Python programs to my nerdkit ATMEGA 168 or 328, then run those programs off the MCU?

•What kind of changes do I need to make to my loaders paired with each source file to run Python programs on the MCU?

•What changes do I need to make to my physical setup, if any, to run Python programs?

•What are the code changes I need to make, i.e. variables to check for input on a pin or set output on a pin; are they the same as they are in C?

•Anything else helpful you might want to add about switching from putting C programs on the MCUs to running Python programs off of them?

I might add that I am quite a bit better with Python than I am with C.

Thanks,

Dalton

September 11, 2012
by Rick_S
Rick_S's Avatar

To my knowledge, you cannot run python programs on the MCU. The NK guys and several others here often use Python computer side to communicate with the MCU. The MCU can be programmed in C, C++, BASIC, Assembler, and some other lesser common languages. There may be some other compilers out there I'm not aware of, but a quick web search didn't return a whole lot about Python. I saw where someone said they were trying to develop a Python compiler, but their webpage was gone.

I think as far as code size goes, your best bet would be to stick with C, Assembler, or Basic (Bascom produces some fairly small code at times).

Rick

Rick

September 12, 2012
by BobaMosfet
BobaMosfet's Avatar

Python, being a scripted, interpreted language, requires a fat slow object on the target platform called an 'interpreter' that converts the text commands to native code and back (for responses), on the fly.

The 168 simply does not have enough RAM to hold both an interpreter, and your code. The Python interpreter alone could be many megabytes in size.

This is why C is so great. It's almost bare-metal with little between your mind and the chip core, (with the exception of a chip debugger or 'monitor' for the rest of us old-school folks who recognize the term).

Python is more portable, because of the sacrifice in size and performance. A volvo. Whereas C is considerably less portable (defines help blur that line a lot), and can do the quarter mile faster than almost anything else, while still providing human understandability.

If you still need more speed, nothing substitutes like doing your own assembly language. Truly bare-metal. If your 'C' compiler supports inline assembly, you get the best of both worlds. Easy coding for most, and significant gains for a little tough coding.

BM

September 12, 2012
by wadaltmon
wadaltmon's Avatar

Hmm, so I guess the 328 doesn't have enough RAM either for both of them? I know it has more, but it isn't enough? I know some Assembly, so I can see where I'll go with that for a bit.

September 12, 2012
by pcbolt
pcbolt's Avatar

Dalton -

You may want to look into a Python to C translator. Basically it takes source code Python (in text form) and translates it into C (text files i.e. "not compiled" code). I've never used one myself but it may be what you are looking for. I agree with BobaMosfet, C is a good middle ground between bloated high-level code and agonizingly detailed but efficient assembly.

September 13, 2012
by wadaltmon
wadaltmon's Avatar

Interesting idea, pcbolt. However, the only ones I can find are just Python with C extensions; basically Python integrating C. There is one called Shedskin, but that is C++ conversion (perhaps it will do others? I dunno) and it is, as I hear, still highly experimental. Think that Shedskin is something worth looking into, or should I just keep looking for one?

Post a Reply

Please log in to post a reply.

Did you know that you can follow NerdKits on Facebook, YouTube, and Twitter? Learn more...