Prayog #1 : Running hello world program

Each Prayog board is shipped with a demo program loaded onto it. Just open the box and plug in the power supply. You can use any DC power supply adapter with output range of 7-12V DC to provide power supply to Prayog board. As you plug in the DC power adapter to Prayog, RED Led named POWER should glow at full intensity. It indicates Prayog is getting proper 5V DC voltage for its operation.

The demo program loaded onto Prayog should blink the on board GREEN Led (LED1) at an interval of 1 seconds. This is your first check to see if everything is OK.

Creating your first Program, Sketch, Imagination and What not !

Now, you can write your first program using any AVR Programming environment like avr-gcc, AVR Studio, WinAVR, CodeVision AVR, IAR Workbench etc. We recommend use of WinAVR for windows users and avr-gcc for GNU/Linux users. A Tutorial on familiarizing with WinAVR is here, you can learn using AVR Programming on GNU/Linux machine using this tutorial.

Whatever IDE / Compiler / Programmer you are using, the underlying programming method remains almost same (avr-gcc programming method is the standard used by us and most of the people, CodeVision programming syntax is a little bit different still, you can start with any of them and this tutorial is beneficial for both of them)

As given earlier in the Getting started tutorial Prayog board has 2 On Board LEDs. They are connected to PORTD_0 (GREEN LED) and PORTD_1 (RED LED). First program can be to play with these two LEDs. I will show you, how ?

PORT Initialization

In 8 bit AVRs PORTS can be handled using a set of 3 Registers. The three Registers are PORTx , PINx, and DDRx where 'x' is PORT value something like A,B,C,D…

  • DDR ⇒ Data Direction register, Values written to this register decides the data direction of the corresponding bit of an 8 bit PORT, for instance if you write a value in Binary as 0b00001111. (0b in the beginning indicates its binary sequence).
  • PORTx ⇒ PORT Register, It is used to write digital value on each bit of an particular PORT. for example you want to write a value in binary say 0b11001100 onto PORTB, When you insert a statement PORTB = 0b11001100 in your C code, this will make the 8 bits of PORTB as 11001100 (MSB to LSB).
  • PINx ⇒ PORT Input Register, Though this register is a Read/Write Register, that means you can Read or Write the values at this register, However you are mostly going to read these registers as these register indicates the binary status of PORT bits physically. Say, we have declared PORTB as an digital input port by writing corresponding values in DDRB register. Now, when the user applies some kinda input from outside and changes the digital binary value of any of th ebits of PORTB, those values can be read from PINB. So basically PINx is read when you wanna know the current binary values at PORTx.

Code Listing

/*
Prayog #1 - Hello World Program
Blinking LEDs connected to PORTD_0 and PORTD_1
Compiler: WinAVR (avr-gcc)
*/
 
#include <avr/io.h>     // Defines the basic AVR Registers
#include <util/delay.h> // Including Library for precise delay generation
 
int main(void)
{
       DDRD = 0b00000011; // PORTD_0 and PORTD_1 as Output
       PORTD= 0b00000000; // Initially LEDs are Off
 
       while(1)
       {
            PORTD = 0b00000011; // LEDs are ON
            _delay_ms(1000);    // Delay of a second
            PORTD = 0b00000000; // LEDs are Off
            _delay_ms(1000);
       }
 
       return 0;
}

Listing

Save the Code file with some name say main.c, If you are using WinAVR its time to create a Makefile (WinAVR uses Makefile to perform a set of commands). Click here to see How to create a Makefile. Once you have saved Makefile into the same folder as your main.c code. Its time to compile the code and download the hex file onto the flash of Microcontroller on Prayog board. Check this page describing, How to use WinAVR to compile the C code and Program the flash.


Fatal error: Allowed memory size of 67108864 bytes exhausted (tried to allocate 72 bytes) in /home/electroo/public_html/wiki/inc/auth/plain.class.php on line 296