Avr LED Blinking

This program explains hello world LED blinking program.In this program an LED is connected through a resistor to PINB0.

#include <avr/io.h>
#include <avr/delay.h>

int main()
{
	DDRB |= 1<<PINB0;
	while(1)
	{
		PORTB |= 1<<PINB0;
		_delay_ms(100);
		PORTB &= ~(1<<PINB0);
		_delay_ms(100);
	}
}

The above program makes PINB0 blink with 100ms delay.
Compile and upload the hex file.You can use the blow make file for the whole process

CC = avr-gcc
OBJCOPY = avr-objcopy
OBJDUMP = avr-objdump
AVRDUDE = avrdude
REMOVE = rm -f

MCU =atmega8

TARGET = blink
SRC = $(TARGET).c

AVRDUDE_PROGRAMMER = dapa
AVRDUDE_PORT = /dev/parport0 
AVRDUDE_WRITE_FLASH = -U flash:w:$(TARGET).hex

elf:
	$(CC) -mmcu=$(MCU) -Os -o $(TARGET).elf $(SRC)
hex:
	$(OBJCOPY) -j .text -j .data -O ihex  $(TARGET).elf $(TARGET).hex
flash:
	$(AVRDUDE) -p m8 -P $(AVRDUDE_PORT) -c $(AVRDUDE_PROGRAMMER) $(AVRDUDE_WRITE_FLASH)
clean:
	$(REMOVE) $(TARGET).hex $(TARGET).elf $(TARGET).c~

When your program name changes change name of the TARGET variable in Makefile.
Use the commands in sequence

make elf

make hex

make flash

HelloWorld in action

Happy Hacking 🙂

Advertisements

Getting started with STM32F4Discovery and ChibiOS

ChibiOS is a portable,open source RTOS…ChibiOS can be downloaded from source forge

http://sourceforge.net/projects/chibios/

Extract the downloaded file..Inside the folder you can see a folder named Demos inside that our board will be listed ARMCM4-STM32F407-DISCOVERY inside that folder there will be a main.c….Edit that main.c file

#include "ch.h"
#include "hal.h"

int main(void) {

halInit();
chSysInit();

palSetPadMode(GPIOD, GPIOD_LED4, PAL_MODE_OUTPUT_PUSHPULL); /* Green. */

while (TRUE) {
palSetPad(GPIOD, GPIOD_LED4);
chThdSleepMilliseconds(500);
palClearPad(GPIOD, GPIOD_LED4);
chThdSleepMilliseconds(500);
}
}

palSetPadMode configure the I/O as input or output….palSetPad and  palClearPad is similar to High and Low…halinit is the intialization of hardware abstraction layer and chsysinit is the intialization of kernel…

Open a terminal and change directory to that folder

cd ChibiOS_2.4.1/demos/ARMCM4-STM32F407-DISCOVERY/

make

st-flash write build/ch.bin 0x08000000

Happy Hacking 🙂