Author Topic: Get started with C programming  (Read 16778 times)

0 Members and 1 Guest are viewing this topic.

Offline 97hilfelTopic starter

  • Regular Contributor
  • *
  • Posts: 66
  • Country: it
Get started with C programming
« on: March 06, 2016, 09:14:31 pm »
Hello everybody,
I'm currently in my final highschool year (in Italy so don't comare it directly to USA or somewhere else).
I wanted to learn how to program on microcontrolers last year in school we learned to program Assamby and a little bit Arduino,
but I want to learn real C.
Currently I'm working on a project and using a Atmel SAM D20 and I'm currently stuck with programming, because I can program but I don't know how to programm efficently on controllers.
I hope somebody can help me.
Felix
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11238
  • Country: us
    • Personal site
Re: Get started with C programming
« Reply #1 on: March 06, 2016, 09:35:31 pm »
but I don't know how to programm efficently on controllers.
That is something you will have to learn over time, there is not a single piece of advice that can help.

My recommendation is to not use manufacturer libraries (ASF in case of Atmel) and program MCU directly. It is more work upfront, but greater benefit in a long run.

I have a number of very simple starter projects (D20 included) here https://github.com/ataradov/mcu-starter-projects

You can take that and expand it as you need.
Alex
 

Offline 97hilfelTopic starter

  • Regular Contributor
  • *
  • Posts: 66
  • Country: it
Re: Get started with C programming
« Reply #2 on: March 06, 2016, 09:40:39 pm »
Well, I still need to get into C programing because Im used to C# on a pc where it doesn't matter how much memory you use (till you be under 2gb)


Gesendet von iPhone mit Tapatalk
 

Online ataradov

  • Super Contributor
  • ***
  • Posts: 11238
  • Country: us
    • Personal site
Re: Get started with C programming
« Reply #3 on: March 06, 2016, 09:46:01 pm »
Well, I still need to get into C programing because Im used to C# on a pc where it doesn't matter how much memory you use (till you be under 2gb)
That's easy. Just start with whatever you were doing in C# and you will be forced to come up with better solutions, since your stuff won't fit into the device.

But really, it is not that bad. On the MCU you are limited by the hardware you have, so it is reasonably easy to account for all the things that are consuming memory. In a typical project there will be very few big consumers (like buffers) and it is easy to  calculate memory requirements upfront. Most projects are not that critical to memory and the main constraint is a CPU performance.

There is not much you can do, but start doing things and optimizing as you go.
Alex
 

Offline MK14

  • Super Contributor
  • ***
  • Posts: 4527
  • Country: gb
Re: Get started with C programming
« Reply #4 on: March 06, 2016, 10:03:45 pm »
Hello everybody,
I'm currently in my final highschool year (in Italy so don't comare it directly to USA or somewhere else).
I wanted to learn how to program on microcontrolers last year in school we learned to program Assamby and a little bit Arduino,
but I want to learn real C.
Currently I'm working on a project and using a Atmel SAM D20 and I'm currently stuck with programming, because I can program but I don't know how to programm efficently on controllers.
I hope somebody can help me.
Felix

There are a lot of C program resources on the internet, including many free book(often PDF) downloads.

Some are even Embedded/Microcontroller C

Such as this one:

http://learn.mikroe.com/ebooks/piccprogramming/front-matter/introduction/

http://freecomputerbooks.com/langCBooks.html

Google can be good for finding them.
 

Offline RobertGogol

  • Supporter
  • ****
  • Posts: 22
  • Country: pl
    • my adventure with Electronics and Music
Re: Get started with C programming
« Reply #5 on: March 06, 2016, 10:19:18 pm »
I would recommend what I did:
Learn C++ specific to program and understand 8bit AVR MCU - most important thing, even more important than learning C++ is how to compile program and upload it to MCU using programmer like simple and cheap Fishl USB - I mean leaving proto board and use standalone MCU with your own projects. It is hard at the beginning (especially working with compiler and AVRdude from... command line) but everything is explained here, and it is cheap and easy:
http://hackaday.com/2010/10/23/avr-programming-introduction/

and first blinking LED on breadboard with atmega16 is so beautifull!!
I've looked at mine over hours ;)
Good luck!
« Last Edit: March 06, 2016, 10:22:09 pm by RobertGogol »
 

Offline MSO

  • Contributor
  • Posts: 42
  • Country: us
Re: Get started with C programming
« Reply #6 on: March 07, 2016, 01:58:36 am »
Learning C is one of the most beneficial things you can do for your career. Don't let anyone tell you that it's inefficient or that assembly will run rings around a C program; that's false, even when writing firmware.  A good C complier can produce much better code than even the best assembly language programmers. When it comes to highly optimized code and near universal availability, from the smallest MCU to the largest mainframe computers, no other language comes close. (With the possible exception of C++.)

Pickup a copy of "The C Programming Language" by Brian W. Kernighan & Dennis M. Ritchie and read it from cover to cover, twice. Pay special attention to the difference between the C language itself and its compilers and libraries. Don't, as many new to the language are wont to do, confuse the C language with its compilers and its libraries; the latter two can vary substantially in quality and efficiency but can easily be replaced with better implementation fro different vendors.

http://www.amazon.com/The-Programming-Language-Brian-Kernighan/dp/0131103628

Good luck with your new venture.

 

Offline dannyf

  • Super Contributor
  • ***
  • Posts: 8221
  • Country: 00
Re: Get started with C programming
« Reply #7 on: March 07, 2016, 02:20:15 am »
Quote
I don't know how to programm efficently on controllers.

The biggest lesson I have learned over the years is to code with a goal to never code it again: whenever you write a piece of code, always think about how you can write it so that it can be reused later for a different project.

To do that, you will need to write modular code; code with well defined inputs and outputs; code that's well debugged, etc. In the end, you learn a lot more than just coding.
================================
https://dannyelectronics.wordpress.com/
 

Online nctnico

  • Super Contributor
  • ***
  • Posts: 26896
  • Country: nl
    • NCT Developments
Re: Get started with C programming
« Reply #8 on: March 07, 2016, 07:58:29 am »
I'd start with programming C and C++ on a PC because it is much easier to test and debug code on a PC. A good excersize is to create a module which emulates the hardware (for example reading data from a file and the results to a file) and a module which is your microcontroller's firmware. This isn't sexy but it is a good step towards unit testing your firmware.
« Last Edit: March 07, 2016, 08:03:16 am by nctnico »
There are small lies, big lies and then there is what is on the screen of your oscilloscope.
 

Offline rx8pilot

  • Super Contributor
  • ***
  • Posts: 3634
  • Country: us
  • If you want more money, be more valuable.
Re: Get started with C programming
« Reply #9 on: March 07, 2016, 08:21:16 am »
I'd start with programming C and C++ on a PC because it is much easier to test and debug code on a PC. A good excersize is to create a module which emulates the hardware (for example reading data from a file and the results to a file) and a module which is your microcontroller's firmware. This isn't sexy but it is a good step towards unit testing your firmware.
+1

Sent from my horrible mobile....

Factory400 - the worlds smallest factory. https://www.youtube.com/c/Factory400
 

Offline Jeroen3

  • Super Contributor
  • ***
  • Posts: 4078
  • Country: nl
  • Embedded Engineer
    • jeroen3.nl
Re: Get started with C programming
« Reply #10 on: March 07, 2016, 08:29:33 am »
Quote
I don't know how to programm efficently on controllers.
The biggest lesson I have learned over the years is to code with a goal to never code it again: whenever you write a piece of code, always think about how you can write it so that it can be reused later for a different project.
This. The amount of people that blob some code into a single file, pray for it to work, and never look at it again. Is way too high.
The code becomes unmaintainable and only the original writer can make something of it for the next ~2 months.
Any time later it will be cheaper to just start over. Hours are not free.

But to learn code there is only 1 thing you can do: Read code. Other peoples code.
Find reputable C libraries and project, and read the code. See what they are doing, recognize the architecture.
« Last Edit: March 07, 2016, 10:22:40 am by Jeroen3 »
 

Offline Brutte

  • Frequent Contributor
  • **
  • Posts: 614
Re: Get started with C programming
« Reply #11 on: March 07, 2016, 10:18:19 am »
I'd start with programming C and C++ on a PC
+1

Sent from my horrible mobile....
+2
Sent from my bathroom....
 

Offline Karel

  • Super Contributor
  • ***
  • Posts: 2217
  • Country: 00
Re: Get started with C programming
« Reply #12 on: March 07, 2016, 11:03:02 am »
My advice: Forget the crap they call C++ and stick with plain C only.

And buy this book: http://shop.oreilly.com/product/0636920033844.do

And no, I'm not going to let me drag into a C vs. C++ flamewar so I'm not going to explain why C is so much better than C++ (for MCU's).



 

Offline Philfreeze

  • Regular Contributor
  • *
  • Posts: 123
  • Country: ch
Re: Get started with C programming
« Reply #13 on: March 07, 2016, 11:44:05 am »
Well, to learn the C langugage there is only one "true" guide written by one of the creators of C (Ritchie) and prof. Kernighan.
http://www.ime.usp.br/~pf/Kernighan-Ritchie/C-Programming-Ebook.pdf
 

Offline legacy

  • Super Contributor
  • ***
  • !
  • Posts: 4415
  • Country: ch
Re: Get started with C programming
« Reply #14 on: March 07, 2016, 11:57:11 am »
I sugget to be focused on "design" instead of C coding
 

Offline ElektroQuark

  • Supporter
  • ****
  • Posts: 1244
  • Country: es
    • ElektroQuark
Re: Get started with C programming
« Reply #15 on: March 07, 2016, 12:21:12 pm »
About original work of K&R, don't do C evolve a bit?
Is the K&R book up to date?

 :popcorn:

Offline 97hilfelTopic starter

  • Regular Contributor
  • *
  • Posts: 66
  • Country: it
Re: Get started with C programming
« Reply #16 on: March 07, 2016, 12:52:12 pm »

A good C complier can produce much better code than even the best assembly language programmers. When it comes to highly optimized code and near universal availability, from the smallest MCU to the largest mainframe computers, no other language comes close. (With the possible exception of C++.)

Good luck with your new venture.
Well I can program assambly Inlearned it on a AW32 Freescale controller and I hated it... Because you can't implement much in assamby code and can do a lot of errors... I will read trough the rest of the awnsers later after school


Gesendet von iPhone mit Tapatalk
 

Offline Philfreeze

  • Regular Contributor
  • *
  • Posts: 123
  • Country: ch
Re: Get started with C programming
« Reply #17 on: March 07, 2016, 01:49:40 pm »
About original work of K&R, don't do C evolve a bit?
Is the K&R book up to date?

It evolved a bit but the the "big" new thing you will have to learn on top of K&R is the whole embedded stuff (register access, interrupts etc.)
 

Online nctnico

  • Super Contributor
  • ***
  • Posts: 26896
  • Country: nl
    • NCT Developments
Re: Get started with C programming
« Reply #18 on: March 07, 2016, 02:46:35 pm »
My advice: Forget the crap they call C++ and stick with plain C only.
Noooooo! C++ is the next 'big thing' for microcontrollers because C++ allows to circumvent the parts of C which are so error prone: pointers and you can put an entire module in it's own class so there is much less worry about name clashes with other modules. Also the ability to have fine grained control over which parts of the code can touch which variables is worth a lot because it makes code more predictable. Forget whatever negative is being said about C++ because it is either not true or not a problem in the real world. After all C++ works just fine.
« Last Edit: March 07, 2016, 02:52:10 pm by nctnico »
There are small lies, big lies and then there is what is on the screen of your oscilloscope.
 

Offline 97hilfelTopic starter

  • Regular Contributor
  • *
  • Posts: 66
  • Country: it
Re: Get started with C programming
« Reply #19 on: March 07, 2016, 03:31:05 pm »
Well guys, I don't want to learn Cpp now, I don't even like that language, I got a 1500 page thick book in my bookshelf which I bought for 50 bucks because I taught that Cpp would be the "only way" to write goog PC programms, bit C# does the job... Cpp is way to high for me and currently the market for C programmers (I don't want to programm to earn my money I want ro design PCB) is bigger and C is a bit more universal on small controllers so I do like to start with it. Let the flamwar c vs cpp beginn!
Thanks to everybody who helped me!


Gesendet von iPhone mit Tapatalk
 

Offline 97hilfelTopic starter

  • Regular Contributor
  • *
  • Posts: 66
  • Country: it
Re: Get started with C programming
« Reply #20 on: March 07, 2016, 03:34:39 pm »
And what Im going to do these days:
Finish the asf program and grab my atmega 328PU and code on it a bit (got some of them lying around for my Arduino uno)


Gesendet von iPhone mit Tapatalk
 

Offline Dielectric

  • Regular Contributor
  • *
  • Posts: 127
  • Country: 00
Re: Get started with C programming
« Reply #21 on: March 07, 2016, 03:58:51 pm »
I've been writing C for a couple of decades now, and still found this to be helpful when I found it last year:
http://c.learncodethehardway.org/book/

It's a pretty badass way to learn C, and he pulls no punches about how C can really screw you over.

I'm also implementing many of the ideas from Patterns in C:

https://leanpub.com/patternsinc

Having a structured way to attack a problem set is awesome, because IF you're diligent about the implementation, you can come back to it later and understand what you were thinking at the time.  Very important!  I find that a lot of the ideas pulled from OO languages are really useful in C, like function pointers and structures.  There are some pretty good arguments for using C++ for embedded programming (great interview on embedded.fm's podcast, episode 137 with Dan Saks):

http://embedded.fm/episodes/137

Having said that, I actually don't know C++ and have only done a tiny bit of Java and Objective C so I could be totally snowed over and wrong.
 

Offline rsjsouza

  • Super Contributor
  • ***
  • Posts: 5985
  • Country: us
  • Eternally curious
    • Vbe - vídeo blog eletrônico
Re: Get started with C programming
« Reply #22 on: March 07, 2016, 04:18:05 pm »
I am usually a learner by example, therefore the approach I would take is:

Start with something step-by-step such as:
https://github.com/sagarsp/arduino-blink-c
https://balau82.wordpress.com/arduino-in-c/

Once you have something running you can start understand its functionality, compare its structure with one of the books you bought, exploring and experimenting with it.

BTW, if you don't have a Linux machine and have the bandwidth, you can download VirtualBox and one of the pre-configured images from here. Or download VirtualBox and create your own.
Vbe - vídeo blog eletrônico http://videos.vbeletronico.com

Oh, the "whys" of the datasheets... The information is there not to be an axiomatic truth, but instead each speck of data must be slowly inhaled while carefully performing a deep search inside oneself to find the true metaphysical sense...
 

Offline legacy

  • Super Contributor
  • ***
  • !
  • Posts: 4415
  • Country: ch
Re: Get started with C programming
« Reply #23 on: March 07, 2016, 04:59:11 pm »
highschool year (in Italy

"ITIS"?
if so, { electronics, telecom, computer science } ?
 

Offline 97hilfelTopic starter

  • Regular Contributor
  • *
  • Posts: 66
  • Country: it
Re: Get started with C programming
« Reply #24 on: March 08, 2016, 05:57:50 am »


"ITIS"?
if so, { electronics, telecom, computer science } ?

Electronics...


Gesendet von iPhone mit Tapatalk
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf