control7 blog

~Electronics & Programming~

Skip to: Content | Sidebar | Footer

Pages

AVR tutorial – Basic I/O Operation. Input signal from push button and drive LED

30 March, 2011 (23:40) | AVR Microcontroller, Electronics | By: xenon ark

This small tutorial will show how to read signal from a push button and on/off two led by the input signal. I have used Atmega48 to write and test the program but you can use any AVR which supports AVR GCC.

Before pressing the push button it is like:

After pressing the push button:

Sketch of the schematic:

Here is the program:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <avr/io.h>
#include <stdio.h>
 
//Method to read pin status
uint8_t GetKeyStatus(uint8_t key)
{
	return (!(PINB & (1<<key)));
}
 
void main()
{
	DDRB = 0b00000111; //set B0, B1 and B2 as output
	while(1)
	{
		if(GetKeyStatus(0)) //reading B0 status
		{
			PORTB = 0b00000010; //set B1 as HIGH
		}
		else
		{
			PORTB = 0b00000100;	//set B2 as HIGH
		}
	}
}

We can read PB0. When the button is not pressed, PB1 is high otherwise PB2 is high.

AVR PWM

29 March, 2011 (13:11) | AVR Microcontroller, Electronics | By: xenon ark

This is a program to show a PWM output. Attach an LED with B1 and run the program. I have used Atmega48 while testing.

Here is the Circuit:

This is the program

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <avr/io.h>
#include <util/delay.h>
 
main(void)
{
	OCR1AH = 0;
	DDRB |= (1<<PB1); // PortB 1 as output
	TCCR1A = (1<<COM1A1)|(1<<1);  // OC1A to zero, 8-Bit PWM
	TCCR1B = 1; // Start PWM
	while (1) // Start the Unlimited Loop
	{
		int i;
		for (i=0; i<255; i++) //Pulse Increasing Loop
		{
			OCR1AL = i;  //Change lowbyte of OCR1A
			_delay_ms(10); // Delay of each pulse while Increasing 
		}
 
		for (i=255; i>0; i--) //Pulse Decreasing Loop
		{
			OCR1AL = i;
			_delay_ms(10); // Delay of each pulse while Decreasing
		}
	}
}

Single cell (1.5 Volt) LED flasher circuit

27 January, 2011 (12:05) | Electronics | By: xenon ark

LED is the most interesting part of amateur electronics hobbyist. The main barer of LED is, it needs minimum 3 vlots to illuminate. I’ve found many circuits to drive LED by a single cell alkaline or manganese cell.

This is a complete circuit

This is the coil of my first circuit.

I collected copper wire from a relay and use a plastic-coated paper clip as core.

For Experiment: I used a transformers 12-0-12 part as coil and it worked!

This is the first circuit I made

After making the circuit I wanted to check how long it will work. I found it can illuminate more than one day by a single cell!

The Circuit Diagram

Complete Circuit with ferrite core

Circuit

The Coil

The coil is the important part of the circuit. you can use 20-20 turn to any maximum turns. it will works better with ferrite core but you can use any core. At the first time i used a paper clip as core with 70-70 turns and it worked nicely.


More circuit picture