Tuesday, May 31, 2011

A Simple Sample Code!

The code below can turn on a LED connected to pin4 of PORTB which means PB4. I found it very promising and encouraging as a starting point in both OS (Windows and Ubuntu).

#include

int main (void)
{
PORTB = 0x10; // Toggle the LED
DDRB = 0x10; // Set LED as output
return 0;
}

PORTB is an 8 bits register that represent the port B in ATmega128. to have 1 in fifth bit which stands for PB4 (start from 0), I put 0x10 in hex that is equal to 16 in decimal and 0b00010000 in binary. All of them give you same result. Have fun :)




MCU Programming in Ubuntu!

There are two separate commands in linux to program a micro-controller. In this case, I need avr-gcc for compiling a source code and avrdude to download the hex file to the chip. I am going to explain each command in more details below.

1- To compile a AVR code, you need to install three packages which are avr-binutils, avr-gcc, and avr-libc. They are simply available through Synaptic Pakage Manager.

To compile a C code for ATmega128, avr-gcc first creates .elf file using this command:

avr-gcc -mmcu=atmega128 filename.c -o filename.elf

elf extension is not the right one for flash memory programming. Then I used the following command to make .hex file which is suitable to download to the flash memory:

avr-objcopy -j .text -j .data -O ihex filename.elf filename.hex

In both command, there are few more options you need for different reason that I ignore at the moment since I don't need them now :)

2- Avrdude is the next command that I need to download the .hex file and it is also available in Synaptic Package Manager. It has also few useful options while I just used two of them that I need to erase the memory and write to the memory:

avrdude -c avrispv2 -p m128 -P usb -e

The above command says that programmer or connector (-c) is a AVR ISP MKII (avrispv2), the device or part number (-p) is ATmega128, connecting port (-P) is through usb, and -e erase the memory.

avrdude -c avrispv2 -p m128 -P usb -U flash:w:filename.hex

Up to usb is explained in last command. -U updates the memory and its syntax is :r|w|v:[:format] which in my case I am writing to ATmega128 (a flash memory), it may be an EEPROM or etc. w stands for write, r for read, v for verification, and declare your filename at the end.

MCU Programming in MS WINDOWS!

to write a code and download it to ATmega128, I've just managed to AVR Studio 4 only. To do that, the following steps have to be done:

1- Create a new project from Project menu.
2- On the left section of AVR Studio, AVR GCC window will appear that shows 4 different categories. The source code can be added or created in the Source Files category.
3- After the code was ready, press F7 or chose Build option from Build menu.
4- There are two icons, above the window that opens the codes, to connect and program the micro-controller. However, it is available through Tool -> Program AVR -> Connect... for making the connection and going to the Program section in the opened windows for downloading the hex file to the flash memory.

that's it :)

Saturday, May 14, 2011

First experience failed!

I have a set of tools including AVR Studio 4, WinAVR, and some code samples. I have connected my ATmega128 to my computer through a AVRISP MKII. So the connection is done. I could download a simple project (turning on a LED by PWM changes), but I don't get the LED on. It looks everything is right, I get ok on verification stage too, but no ON LED :(

Wednesday, May 11, 2011

A light start!

Ok after one whole day digging Internet about how to program ATmega128, I got nothing but errors. So here is the summary that I've learnt from the yesterday study, ATmega128 is a micro-controller chip can support many applications in terms of control and communication. It has an 128 Kbytes In-System Programming flash memory as well as 4 Kbytes EEPROM and 4 Kbytes internal SRAM. This micro-controller has 133 instructions and 32 registers which may be used for programming the chip to do specific operation. Well, I have to write a program and download it to the chip and it will be automatically start working after booting up. By the way, ATmega128 consists of many peripheral and special features which can be used for controlling other stuff like in my case it controls the Chipcon CC2420EM RF transmitter for wireless communication. Later :)

Tuesday, May 10, 2011

This is just the beginning!

I've started something new in my life, Micro-processor programming (or more precisely sensor programming). Initially, I have few sensor kits built on ATMega128 which are communicating to each other through Chipcon CC2420EM Zigbee-based RF transmitter. Looks nice, doesn't it? I have to end up with many thing in next few weeks and maybe years while I know almost nothing. So please cross your fingers :)