I suspect the problem is not with your C code but with your Makefile.
The following lines in your Makefile produce an example.o
object file.
main:
avr-gcc -g -Os -Wall -mmcu=atmega328 -c ../src/example.c
The created .o
file only contains the symbols and code from example.c
, not the additional source required to actually make it run on a target system such as interrupt vector jump tables and code to initialise the BSS RAM segment to zeros, and load your initialised data sections.
You'll need to add an additional line something like this to run the linker and produce an output object suitable for download to the AVR part. Alternatively, use avr-ld
, but you'll have to work out all the required linker options.
main.elf: example.o
avr-gcc example.o -o main.elf
You can use avr-objdump --disassemble-all <filename>
on both example.o
and main.elf
yourself to verify the different content of each file.
It's always a good idea to try to reduce your problem in steps to the most simple example possible. In this case, it would probably mean dropping into the AVR Studio software and creating a project running on the simulator using their managed build process. From there, you could them export the Makefile in use by their build process by using the 'Export Makefile' menu option. The generated makefile could then be compared with your version.
Actually, it's probably a good idea to use a Makefile similar to the one generated by AVR Studio because it has the correct rules already defined, you just have to set up some variables with regard to which objects need to be generated and the final target file name.
It's a broad question. I don't think the previous question was meant to imply that an Arduino isn't a 'real' or 'professional' development platform. It has its niche. Every microcontroller has its niche - you have to choose one that helps you do what you're trying to do. Does that sound vague? Yes. Because it all depends on what you're trying to do.
So let's back up - what IS the Arduino compared to everything else? I'll split it up into hardware and software.
As far as hardware goes the Arduino is a development board. There are lots of development boards out there - tons of them. They each are built around one or several microcontrollers. They are, to a certain extent, meant to be somewhat generic in function - you need to be able to use them to develop for just about any application. But they can also have a focus - you might see development boards with HDMI and Ethernet controllers - these are obviously meant to help people develop applications for networking or visual applications. The Arduino is a solid development board, but it has very few neat features. The approach behind Arduino is a wide-open sandbox - that's why there's so many shields available.
So development boards are built around microcontrollers. The Arduino is built around the AVR ATMega328. This is a specific microcontroller made by Atmel. It is one of Atmel's 8-bit microcontrollers. It is aimed at general purpose embedded applications that don't require a whole lot of bells and whistles or lots of I/O (it's rather limited in the number of pins it has). To put this in perspective, Atmel also offers 32-bit ARM-based offerings - this is a type of microcontroller you might find in a router that runs Linux. There are, of course, many different microcontroller manufacturers - all offering differnt microcontrollers focused on certain applications - automotive, networking, DSP, etc.
Now there's the question of the software - what does it mean to write an AVR program? Well, everything you write for an Arduino is an AVR program - it's just hard to tell. Arduino comes with a framework that basically hides a lot of the complexity of microcontroller programming from you. Typically, writing any microcontroller code (including AVR programs) involves lots of playing with bits in registers, keeping track of timing, etc. It's very low-level and there's a lot to keep track of.
In a broader sense, microcontrollers are typically programmed in a variant of C. I say a variant because every microcontroller needs a special C compiler with its own rules. So every different chip has its own variant of C. There aren't earth-shattering differences between each of them, but you can get tripped up on the differences if you're not careful.
How do you program them? There's lots of ways, but generally nowadays chips tend to program themselves. That's no joke - most programmers just implement a special kind of serial communication that lets them be programmed by one of several devices. This is called In-System Programming. You program microcontrollers with a variety of devices - they can be $5 USB dongles, parallel port devices or $1200 JTAG adapters. Typically you need a specific programmer for a specific line of chips. That is to say, you can buy a JTAG that will program most to all ARM chips, and you can find programmers that will program most if not all AVR chips.
So programming your own microcontroller basically means you 1) Select a microcontroller you want to use, 2) find a good development board for it, 3) find some sort of programmer - either an In-System Programmer, or JTAG or something else, 4) figure out what compiler you need, what code editor to use, etc. (Arduino of course, provides all of this, so it's easy). Then, you read the chip's data sheet and figure out where to start.
Best Answer
PORTB &= -(1<<PB0);
should bePORTB &= ~(1<<PB0);
.~(1<<PB0)
is0xFE
, which turns off bit 0.-(1<<PB0)
is0xFF
, which works out to a no-op, so the pin is never getting turned off.