Programming in C - Page 1
Forum section: electronics / beginners

author: Anson
author_rank: Regular Contributor
timestamp: April 09, 2013, 04:51:47 pm
content: Sometime soon I should be getting a programmer for Atmel AVR and using Atmel Studio 6 for programming. Thanks Jack! My question is which book should I get to teach me programming? Keep in mind I can get easily confused, I'm not stupid (yet) but between the Lyme disease and medications my cognitive functions are very slow. I am also on a very tight budget and cannot afford to buy several books. So I need a book that is complete with all the available functions and variables you can use yet is fairly stupid friendly.

author: Simon
author_rank: Unknown
timestamp: April 09, 2013, 05:44:41 pm
content: Get a copy of "programming in C" by Stephen G. KochanI do have to warn though that no book will teach you to program a microcontroller. ALL C books are aimed at PC programming and you will have to work out the missing pieces. I'm dyslexic and gave up and went with the arduino as it's made easier. I got fed up with people saying to me that I had to learn C before trying to program. Knowing C does not equate to knowing the nuances of mcu's which are a totally different environment from PC's

author: helloworld922
author_rank: Regular Contributor
timestamp: April 09, 2013, 07:08:45 pm
content: Any C book which focuses on the basics will do. Like simon said, MCU programming is quite unique from general PC programming, but that doesn't mean there aren't similarities. I actually didn't use a C book to learn C to begin with, but I did have experience with other programming before-hand and there are quite a few good free online resources.Things that are probably most important for MCU programming:1. Basic syntax.2. Get familiar with low-level operations, particularly bitwise integer operations. A lot of MCU programming deals with manipulating registers which control various hardware functions.3. Interrupts. This is a topic that really isn't covered in PC programming since it's generally too low-level, but is one of the basic essentials for MCUs.4. If you want to get more advanced, learning some assembly wouldn't hurt. Sometimes you'll find there are some operations you need to do specifically which you really can't define in C (for example, a NOP instruction).Things you can likely safely ignore:1. Any PC-specific API usage (especially if it's specific to a certain platform)2. Dynamic memory allocation3. Multi-threading or more complicated parallel programming topics. This is generally an advanced PC topic anyways, and most MCU's only have one processor anyways. A few concepts may apply to when you want to interface multiple MCUs or are using interrupts, but they're generally handled differently .4. Object oriented programming. Basic structures are useful, but I haven't found much use of more advanced object oriented topics such as inheritance, abstraction, and polymorphism.Hopefully I haven't missed anything important, if so I'm sure the other members will chime in

author: StevenB
author_rank: Contributor
timestamp: April 09, 2013, 07:24:39 pm
content: I learned C fromThe C Programming Languageby Kernighan and Ritchie. It's just the basics without PC centric stuff, and it's straight from the horse's mouth!Beyond that, you can probably get free documentation for the specific platform you are programming for to fill in the gaps.

author: Anson
author_rank: Regular Contributor
timestamp: April 09, 2013, 07:28:24 pm
content: Quote from: helloworld922 on April 09, 2013, 07:08:45 pmYes there are and I have been looking at many of them. I have even downloaded .pdf copies of some AVR specific books and these would be helpful if I could read them. I cannot focus on my monitor long enough to finish more than a few sentences at a time. Small monitor and bad eyes. I can however print out a few useful pages and read them. This has been how I have been trying to learn thus far. I was hoping though that there would be a good book. Something like "Programming AVR in C for Dummies" but no one has written that yet.

author: Simon
author_rank: Unknown
timestamp: April 09, 2013, 07:29:01 pm
content: Quote from: StevenB on April 09, 2013, 07:24:39 pmif your new in programming that book is as concise as the language itself and may not explain too clearly enough

author: MacAttak
author_rank: Unknown
timestamp: April 09, 2013, 09:46:51 pm
content: Best place to start is to skip the books at first and look at the example projects. Atmel Studio includes a number of samples if I recall.And even if you are directly programming Atmel microcontrollers and not just using an Arduino, you can still use the sample sketches that are included with the Arduino software as a learning tool - the language and semantics used by Arduino's programming language are very similar to what you would be writing in C using Atmel Studio. You can look at those samples and review them line for line until you think you understand what they are doing (start with the simple ones before moving to the complex ones). If you have questions, try to figure out the answers to those questions using the Arduino language reference:http://arduino.cc/en/Reference/HomePageAnother great resource are the "Full" datasheets from atmel.com for the microcontroller chip you are using. There are a lot of small code snippets in there that illustrate how to use key features of the chip.Other than that, most of the C language will eventually just start "making sense" to you after you have looked at it enough. The only part that might be a little difficult to grasp as a new programmer is the arithmetic operations. For the basics on that, I recommend starting with the Arduino documentation where it talks about direct port addressing:http://www.arduino.cc/en/Reference/PortManipulation.Using direct access to the port registers (PORTB, PORTC, PORTD) is much faster than the "easy" way of using digitalWrite() and digitalRead().... but it requires understanding how bitwise math works. Bitwise math is something most PC programmers don't have to deal with (they can get away with inefficient code)... but a microcontroller programmer is almost surely going to run into it sooner rather than later.

author: MasterOfNone
author_rank: Regular Contributor
timestamp: April 09, 2013, 10:51:07 pm
content: Quote from: Anson on April 09, 2013, 07:28:24 pmA strange suggestion but if you have space for another monitor next to your current one could try to get an old 4:3 1240x1024 model and connect it to your PC/Laptop if it supports two screens. But rather than mounting it landscape (like normal) try to find a stand so you can mount it portrait.If you do this PDF books and Datasheets displayed full screen would virtually fill the monitor. If you can read text books you may find that you can actually read a full page without having to zoom/scroll (providing you don't go for something like a tiny 15" model).Also having the examples/datasheet next to you so that don’t have to keep switching between the Development Environment and PDF reader is a real joy. Once you have worked like this you will not want to go back.

author: andersm
author_rank: Super Contributor
timestamp: April 09, 2013, 11:31:00 pm
content: Quote from: helloworld922 on April 09, 2013, 07:08:45 pmDisagree with these. Dynamic memory allocation is used in many embedded projects, multi-threading applies just as well to embedded RTOSes, and OO design techniques help improve overall software quality even if you're not using explicit object-oriented languages.Also, you will inevitably need custom tools running on your PC, so learning how to write GUI software isn't a wasted effort (though plain C isn't necessarily the best option for that).

author: Markybhoy
author_rank: Regular Contributor
timestamp: April 09, 2013, 11:53:00 pm
content: C Primer Plus by Stephen Prata is a good beginner book.

author: Anson
author_rank: Regular Contributor
timestamp: April 09, 2013, 11:53:28 pm
content: QuoteThat's a great idea! I don't have another monitor but hey I can flip this one over for temporary will see how well it works. I can work on getting another monitor.

author: Rick Law
author_rank: Super Contributor
timestamp: April 10, 2013, 03:49:27 am
content: Quote from: StevenB on April 09, 2013, 07:24:39 pmI assume you already know other programming languages? If so, I agree with Steve,The C programming Languageis really the best one to start. When you are done with this book, you have a good idea of how things work in C.If you don't know another programming language yet, start with BASIC to get an idea of programming first. C is a great language to handle hardware. Some call C "machine language done right" and I think with good justification. It is however not the easiest to learn as a first programming language.Rick

author: ddavidebor
author_rank: Super Contributor
timestamp: April 10, 2013, 05:14:01 am
content: Quote from: Anson on April 09, 2013, 04:51:47 pmOnly a suggestion.Buy a book about standard c, not c for avr.When you've learn WELL standard c, you can find all the other information online and program all things of the word.

author: cwalex
author_rank: Frequent Contributor
timestamp: April 10, 2013, 06:18:23 am
content: I used this book and kit a few years ago and I had no knowledge of programming, avrs or microcontrollers. I found it really easy to follow and learnt a lot including some of the things that you won't see much in general c programming books like bit shifting and masking to control IO. Lots of other stuff that you really only use on microcontrollers but hardly ever in PC programming. It is pretty old so it would be best to follow it using avr studio 4 from the CD provided.https://store-jwm4nhbo.mybigcommerce.com/c-programming-microcontrollers-book-and-projects-kit/https://www.smileymicros.com

author: MasterOfNone
author_rank: Regular Contributor
timestamp: April 10, 2013, 08:03:09 am
content: Quote from: Anson on April 09, 2013, 11:53:28 pmI don’t know what type of monitor you have to do the test, but if you have a 1280x800 (or 1440x900) 16:9 monitor it may not work as well as an old 4:3 1280x1024 monitor. Basically because it will have less horizontal pixels (when you rotate it) and the shape of a 16:9 might be too tall, so you won‘t use the whole screen. A 16:10 might be ok.

author: Simon
author_rank: Unknown
timestamp: April 10, 2013, 08:06:34 am
content: the A4 page is 1:1.41 so the 1:1.5 ratio of a 15:10 is ideal. Personally i hate 16:9 monitors except on a rare occasion I'm watching a wide screen film.

author: Rick Law
author_rank: Super Contributor
timestamp: April 10, 2013, 03:23:51 pm
content: Actually, having two monitor in whatever orientation would be a good thing. One view of the code, and the other view of the function specs. Such as, while you are looking at your code MyMoney = fgets("blah",bleep,bloom); on the other monitor, you can check the function protocol for fgets().With C and many other languages, you do want big screen to look at the code - not physically big, but good resolution to be able to display many lines/column. The more code you can fit on a screen, the easier it is to understand more of what is going on. It is very hard to find problems looking at just 6 lines at a time and then page-up page-down.

author: seb1982
author_rank: Regular Contributor
timestamp: April 10, 2013, 03:51:37 pm
content: Quote^ This!! *Looks accusingly at Arduino IDE designers*...

author: Rick Law
author_rank: Super Contributor
timestamp: April 10, 2013, 04:52:35 pm
content: Quote from: seb1982 on April 10, 2013, 03:51:37 pmI do not intend any accusation of poor design; I have not seen the Arduino IDE. If anyone took it that way … sorry…Given that Arduino is open source, you take what you get. Open source developers do what they can with what time/resource they have. So anything is better than nothing. I use open-source Java IDE, and thankful to the developers who made a tool I can use at no cost to me.That said; if Arduino IDE shows only 6 lines or less, one may consider this a suggestion for future improvement.

author: Simon
author_rank: Unknown
timestamp: April 10, 2013, 05:25:11 pm
content: Quote from: Rick Law on April 10, 2013, 04:52:35 pmCool off, no one said anything. The arduino IDe can actually be the less obstructive than any. Although it opens as a small window it can be maximized and you only have a slim status window at the bottom so you have as much room as a word processor

author: andersm
author_rank: Super Contributor
timestamp: April 10, 2013, 05:30:57 pm
content: Quote from: Rick Law on April 10, 2013, 03:23:51 pmThe ability to show several files side-by-side, and several views of the same file should be minimum feature requirements for any serious programming editor by now.

author: Anson
author_rank: Regular Contributor
timestamp: April 10, 2013, 05:52:13 pm
content: Anybody have an extra monitor laying around?

author: Simon
author_rank: Unknown
timestamp: April 10, 2013, 06:05:00 pm
content: When I bought a secnd hand monitor for work they thought I was being clever and copying the people in the engineering office. What I had to explain was that I had dual monitors over a year before the company did and it was natural for me.Now i am in the engineering office I appreciate having two of the same monitors..... one day

author: Anson
author_rank: Regular Contributor
timestamp: April 10, 2013, 06:43:49 pm
content: For my eyes i think I would need a pair of 21 inchers.I have in the past had a temporary dual monitor setup. It worked great for learning sketchup I could have the tutorial running in one window and the program in another. However my daughter stole my second monitor once I fixed her computer. Looking back maybe I should have told her I couldn't fix it.

author: ddavidebor
author_rank: Super Contributor
timestamp: April 10, 2013, 06:50:55 pm
content: 27" 2560x1440, work for three O0

author: hlavac
author_rank: Frequent Contributor
timestamp: April 10, 2013, 06:53:23 pm
content: Proper understanding of C programming comes from understanding machine code/assembly level.Only then will you know what does the CPU have to work with and what makes certain things faster than others.Then you will see what C really does for you and appreciate it fullySimple things like static type checking that without that deeper knowledge seem like unnecessary burden and compiler dumbness that should be removed suddenly start making sense...

author: seb1982
author_rank: Regular Contributor
timestamp: April 10, 2013, 07:08:57 pm
content: QuoteYeah, but that means I have to click a whole button!