C learning - Page 1
Forum section: electronics / beginners

author: SuperMiguel
author_rank: Regular Contributor
timestamp: September 16, 2010, 02:27:51 am
content: whats the best way to finish learning C? like i know how to make small programs, that perfrom small calculations and that make small decitions. I can how to scan, print, while loops... But still not to a point where i can program avr chips or feel really confortable with C..Like i can do arduino code with no problem (i know im not that great on it but i feel a bit confortable to a point that i built a small robot (http://forums.adafruit.com/viewtopic.php?f=25&t=14402))On AVR i can do realllllly basic stuff like turn a light on and off... but thats prob about it...So should i go back to making C programs? or should i keep going forward with AVRAnd with any of choices what should i do to get better? or to cross that basic line?

author: Mechatrommer
author_rank: Super Contributor
timestamp: September 16, 2010, 02:38:32 am
content: learn C in PC first then move to mcu programming when you comfortable with C.

author: SuperMiguel
author_rank: Regular Contributor
timestamp: September 16, 2010, 02:52:08 am
content: Quote from: shafri on September 16, 2010, 02:38:32 amwhat would the best way for me to do that? a book?

author: EEVblog
author_rank: Unknown
timestamp: September 16, 2010, 02:57:29 am
content: There are two issues at play here.The first is knowing C and it's syntax. That simply comes with experience, and it makes no real difference what your target platform is (PC, AVR, PIC etc)The second is knowing how to access the resources on the AVR chip. Things like setting up registers, using peripherals, reading/writing I/O etc. If you stick with Arduino then most of this is hidden from you.But if you really want to learn microcontrollers then you have to learn to start using the resources on the chip. Start with I/O and then work your up to things like timers, interrupts, UART's, I2C/SPI etc. You usually need to do this on physical hardware so you can see some form of physical result. C really has nothing to do with learning stuff like this, it's the same process for assembler, BASIC, C or whatever, it's just that the syntax is different.Dave.

author: SuperMiguel
author_rank: Regular Contributor
timestamp: September 16, 2010, 03:41:12 am
content: Start with I/O and then work your up to things like timers, interrupts, UART's, I2C/SPI etcumm is there a guide on this? or a book?

author: EEVblog
author_rank: Unknown
timestamp: September 16, 2010, 04:05:28 am
content: Quote from: SuperMiguel on September 16, 2010, 03:41:12 amThere are dozens of books on programming AVR chips.Sorry, can't recommend any off-hand.Dave.

author: Zad
author_rank: Super Contributor
timestamp: September 16, 2010, 04:06:30 am
content: The first thing to do is to get hold of the technical manual for the product. You will be able to download a PDF of it from the manufacturer's site. (Oh for the days of printed manuals - they were so much easier to read!) That will have all the details of the UARTs, timers/counters, I/O registers, interrupts etc. Most embedded micros operate in a very similar manner. In theory they should be identical, but each manufacturer has their own idea of how best to do things, and bugs always creep in, so don't forget to download any errata sheets too.There are books around on programming various micros. I can't really recommend any particular book, so have a browse around Amazon.

author: DJPhil
author_rank: Frequent Contributor
timestamp: September 16, 2010, 07:00:39 am
content: Quote from: EEVblog on September 16, 2010, 02:57:29 amI've been struggling with this myself recently. I've been hitting the books hard in the last year or so learning analog stuff, but I haven't programmed seriously since Commodore basic. Enter the MSP430 Launchpad kit . . .I'm still working my way through literature (MSP430 Microcontroller Basicshas my approval), but I'm starting to see what Dave means above. I thought of computer languages as more of a deep investment, something you take pains to learn and lean heavily on as you expand. It seemed like a big choice to pick one for micros, but really it isn't. If you've got a fair grasp of logic and control structures you'll probably do fine. I haven't formally learned a shred of C and it's not the syntax that's not making sense when I first read code, it's the detailed manipulation of the dark corners of the peripherals and things like that. I'm sort of picking up the syntax as I go, and I'm sure when I actually start working with the chip instead of reading about it (projects have backed up a bit) it'll be no big deal compared to remembering how to set up the timers!I hope that sets you at ease a bit.

author: joelby
author_rank: Frequent Contributor
timestamp: September 16, 2010, 07:06:22 am
content: The best way is to have some project in mind. I find it very difficult to play around with a programming language for the sheer joy of it. If you have a useful goal in mind, you'll be able to eventually achieve some sense of self-satisfaction!

author: Zero999
author_rank: Super Contributor
timestamp: September 16, 2010, 09:36:55 am
content: Quote from: EEVblog on September 16, 2010, 02:57:29 amI would've thought using assembler would be the best way to learn about the hardware because you have no choice but to learn the different instructions and registers.Quote from: joelby on September 16, 2010, 07:06:22 amYes, I've struggled with some PIC tutorials recently, not because I don't understand it but because it's boring so I thought of a simple project and it became easier.

author: mikeselectricstuff
author_rank: Super Contributor
timestamp: September 16, 2010, 09:44:54 am
content: QuoteNO! no! no! You will get into bad habits learning on a platform that has essentially unlimited resourcesQuote from: joelby on September 16, 2010, 07:06:22 amAbsolutely - By far the best way to learn pretty much anything is to have a definite project with fairly well-defined goals. It should be something that can be done in incremental steps.The reason is that when you come up against a problem, you have to work through it instead of moving on to something else.This is how you really learn stuff.

author: SuperMiguel
author_rank: Regular Contributor
timestamp: September 16, 2010, 12:13:14 pm
content: Quote from: mikeselectricstuff on September 16, 2010, 09:44:54 amQuotelol...

author: Mechatrommer
author_rank: Super Contributor
timestamp: September 16, 2010, 12:34:09 pm
content: Quote from: SuperMiguel on September 16, 2010, 02:52:08 amwith IDE such as Visual C++, and specifically console programming, you'll directly see the result on the console, such as:int i = 12;i =+ 13;printf(i);(not sure if thats correct, i've been playing more C for mcu lately (asm to be precise!) and not doing C for PC for a while) and you'll know your syntax and semantic is correct. my previous advice is if you are almost total zero experience in C, you'll get familiar and see the result quickly with C in PC. with mcu, you have to download the code, use IC debugging or simulated debugginng in order to debug your software, extra registers need to be learnt, silicon bugs etc. for the book, i suggest topic like... "C/C++ introduction". My best C bible is "C++ nuts and bolts for experienced programmers" by Larry Levitsky, Osborne McGraw Hill.

author: Mechatrommer
author_rank: Super Contributor
timestamp: September 16, 2010, 12:45:04 pm
content: Quote from: mikeselectricstuff on September 16, 2010, 09:44:54 amthat is correct if you have not program to the maximum limit of a particular system you are working on (a PC in my context).... or you "half learn" programming C in PC and algorithm.i have to admit that i've program in a very abundant of resources system (compared to 6 pin mcu), but can you believe i also hitted the limit and have to find the best programming style for it? have to design a specific assembly inline codes in PC? and when i moved to a very small mcu, i have to admit that i felt its very limiting, but can you believe that i made it working?i'm not into portability and easiness of varying mcu's brand, features etc yet. if i let my points out, sure lots of people will argue, so i'd rather not to talk about it... software and hardware... are two very different beasts.

author: SuperMiguel
author_rank: Regular Contributor
timestamp: September 16, 2010, 12:57:24 pm
content: Quote from: shafri on September 16, 2010, 12:45:04 pmbut you need a bit of software to control hardware.And thats where i get confused, on whether i should focus more on hardware or on software for now that im learning.

author: EEVblog
author_rank: Unknown
timestamp: September 16, 2010, 01:35:37 pm
content: Quote from: mikeselectricstuff on September 16, 2010, 09:44:54 amI'll third that.Keep it simple and try and get each peripheral feature working one at a time, don't try and do it all at once. Building your program up step by step is the best way.That's not just for beginners either, but is a common practice for most programmers.Dave.

author: migsantiago
author_rank: Frequent Contributor
timestamp: September 16, 2010, 02:31:45 pm
content: I learned a lot reading...The C programming languageBrian W. KernighanDennis M. RitchieMany people consider it as the C bible.

author: Zero999
author_rank: Super Contributor
timestamp: September 16, 2010, 03:27:10 pm
content: Quote from: mikeselectricstuff on September 16, 2010, 09:44:54 amAgreed, especially if you're going to learn assembly.The PC has hundreds of powerful instructions, most MCUs have under 100 and baseline PICs have 33 simple instructions.The PC has many general purpose registers, baseline PICs only have one working register and the rest are part of the RAM.

author: SuperMiguel
author_rank: Regular Contributor
timestamp: September 16, 2010, 03:49:06 pm
content: Quote from: Hero999 on September 16, 2010, 03:27:10 pmhow important is to learn assembly?

author: lhc
author_rank: Contributor
timestamp: September 16, 2010, 07:24:16 pm
content: http://www.avrfreaks.netGo to the forums and read tutorials it will show you how to address different parts of AVR uC in C. Start with basic I/O tutorials and go from there.

author: migsantiago
author_rank: Frequent Contributor
timestamp: September 16, 2010, 07:45:23 pm
content: Quote from: SuperMiguel on September 16, 2010, 03:49:06 pmFor optimization.If you know how a CPU works, then you can take advantage of the C instructions knowing how they translate into ASM code.For example, an integer division by 32 in C code would be done by a novice like this:Code:int a, b, c;a=450;b=32;c= a/b;But a person who has used ASM would definitely know that it can be optimized...Code:int a, c;a=450;c= a>>5;

author: Zero999
author_rank: Super Contributor
timestamp: September 16, 2010, 08:33:16 pm
content: I don't know C.I know some BASIC (VB and QBaisc) and a PC assembly (16-bit and 32-bit).Now I know some baseline PIC assembly PIC12F509 and have completed my first program today but I'll start a new thread for that.I tried C awhile ago but never got on with it and actually find assembler easier.I'll probably try C again in the future it will be for an MCU. I can see the advantages: once learnt it's probably easier and it's easier to port to a new device.I'll stick with PIC assembly for now though.

author: djsb
author_rank: Super Contributor
timestamp: September 16, 2010, 09:22:57 pm
content: I learnt C from this book (C Programming- a modern approach)http://knking.com/books/c2/index.htmlFor microcontroller C programming (PIC24) I use a Microchip Explorer 16 board that complements the following book which uses the board.http://www.amazon.co.uk/exec/obidos/ASIN/0750682922/embeddedsys08-21Books are a very personal thing and I find writing style and presentation very important. I found them very easy to learn from.David.

author: Zero999
author_rank: Super Contributor
timestamp: September 16, 2010, 09:29:25 pm
content: I've found, what appears to be a good C tutorial here:http://www.howstuffworks.com/c.htmMaybe I'll give it a go one day but I'll get the hand of PIC assembler first.

author: mikeselectricstuff
author_rank: Super Contributor
timestamp: September 16, 2010, 11:04:42 pm
content: QuoteYou can't beat assembly for learning the intimite details of the hardware of lower-end processors like PICs, however I'd say that as a minimum you should at least be able to read it and have a reasonable idea of what it's doing, so you can see what a C compiler is doing, and in particular if it's doing something stupid, or taking you too literally when you do something stupid.

author: andri
author_rank: Contributor
timestamp: September 17, 2010, 01:45:32 am
content: QuoteThis is not really about knowing assembly, rather about general base-2 calculations.In fact, any decent C compiler will happily do it for you. Example: I compiled with gcc 4.1.2 (with flags -m32 -march=i386 for simplicity) the following function:Code:int divide (unsigned int a) {unsigned int b = 32;return a / b;}(I had to move it into a function, otherwise the compiler optimized the whole division away.)With -O0 optimizations (everything turned off) it indeed compiles into rather dumb division:Code:divide:pushl %ebpmovl %esp, %ebpsubl $16, %espmovl $32, -4(%ebp)movl 8(%ebp), %eaxmovl $0, %edxdivl -4(%ebp)leaveretHowever, even with -O1 it does the optimization for us:Code:divide:pushl %ebpmovl %esp, %ebpmovl 8(%ebp), %eaxshrl $5, %eaxleaveret(Quick guide to i386 assembly: "shrl" is logical shift right, "divl" is unsigned division).

author: migsantiago
author_rank: Frequent Contributor
timestamp: September 17, 2010, 01:54:10 am
content: Quote from: andri on September 17, 2010, 01:45:32 amYeah, I was just looking for a simple example. Sorry.

author: joelby
author_rank: Frequent Contributor
timestamp: September 17, 2010, 02:08:22 am
content: Quote from: migsantiago on September 17, 2010, 01:54:10 amWell, one that applies to the MSP430 - the processor has instructions that handle BCD numbers, but there's no standard way to represent these in C so the compiler isn't able to recognise that these instructions could be used. The solution is pretty easy - use the inline assembly code examples that TI helpfully provide.