Sunday, November 7, 2010

AVR Tutorial - C Language-



We use AVR Butterfly and AVR Studio


You might wonder why blinking an LED is the first project, when traditional C programming texts start with the classic “Hello, world” program. It certainly seems that since the Butterfly has an LCD that can show the words it would be easy. But the reality is that controlling the LCD is much more complex than linking an LED, so we’ll save the LCD for later when we’ve gotten a better handle on things.
Find Programmers Notepad that was installed as part of WinAVR (you should have an icon for it on your desktop) and open it. You will need to add a tool, which will let you use the AVR Studio simulator.

the Code :
//blinking.c

#include
#include

int main (void)
{
//set PortD for Output
DDRD =0xFF;
while(1)
{
for(int i=1;i<128;i=i*2)
{
PORTD=i;
_delay_loop_2(30000);
}
for(int i=128;i>1;i-=i/2)
{
PORTD=i;
_delay_loop_2(30000);
}
}
return 1;
}

Tuesday, October 14, 2008

Pulse Width Modulation



Description

Pulse Width Modulation allows microcontrollers to dim lights, control motor speeds, fan speeds and generate analog voltages. By changing the length of the pulse, the output can be controlled. The pulse occurs at a regular frequency, the modulation frequency. The length of the pulse ratio to period time is called the duty cycle.The larger the duty cycle the higher the output is.

The AVR microcontroller can be used to generate PWM signals. The PWM signals can be generated by hardware or by software. An microcontroller like the AT2313 has one hardware PWM on board of the chip. The output of the PWM signal is at the PortB.3(OC1) pin.

The hardware PWM can be programmed by setting the timer registers. The attiny2313 has three timer registers that you need to set to program the PWM:

  1. The Timer/Counter1 Control Register (TCCR1A) is to put the timer in PWM mode.
  2. The Timer/Counter1 registers (TCNT1H and TCNT1L) are used to set the modulation frequency.
  3. The Output Compare Register1 (OCR1A) is used to set the duty cycle.

PWM with the AVR-microcontroller is a matter of comparison. If the Timer/Counter1 is running and the value of the timer is matching the value that is put in the OCR1A register, the OC1 pin changes from high to low. By changing the value of the OCR1A register the lenght of the pulse can be changed.

Hardware

The PWM output can be used to fade a LED in and out. On the picture you can see the LED on a bread-board connected to the ISP-board with a flat-cable.

Software

The software is written with the AVR Studio 4 that uses assembler code. The software uses the hardware PWM of the AT2313 so first the registers of the PWM tiner needs to be set.


;***** PWM demonstration with LED ****
; Demonstrates how to set the PWM mde of the Timer/Counter1
; a LED @ the PWM output at PORTB.3 fades on and off.
; Author : www.laros-edu.net
; Target : AT2313
; Hardware : LED at PORTB.3
;*************************************
.include "2313def.inc"
.def Temp =r16 ; Temporary register
.def pw =r19 ;

;***** Initialization
INIT_SP:

ldi temp,RAMEND
out spl,temp

INIT_PORTB:
ser Temp
out DDRB,Temp ; Set PORTB to output
clr temp
out PORTB,temp ; Set PORTB to 0

INIT_TIMER:
ldi temp,0 ; Set Output Comp Reg H to 0
out OCR1AH,temp
ldi temp,0 ; Set pulse width
out OCR1AL,temp
ldi temp, 0b10000001 ; Set Timer/Counter1 as an 8-bit PWM
out TCCR1A, temp ;
ldi temp, 0b00001001 ; Start Timer/Counter1 , set PWM mode to clear OC1(PB3) at upcounting
out TCCR1B, temp


LOOP:

UP: out OCR1AL,pw ; Output to CompareRegister1 which will set the puls width
inc pw ; Increase the pulse width
rcall delay ; Delay of 0.01s
cpi pw,0xFF ; Check if pulse width is max
brne UP ;


DOWN: out OCR1AL,pw
dec pw
rcall delay
cpi pw,0x00
brne DOWN

rjmp delay05

rjmp LOOP


DELAY: ; delay of 0.01s @ 4Mhz
; =============================
; delay loop generator
ldi R17, $43
WGLOOP0: ldi R18, $C6
WGLOOP1: dec R18
brne WGLOOP1
dec R17
brne WGLOOP0
; -----------------------------
; delaying 1 cycle:
nop
; =============================
ret


LCD @ AVR




This page describes how to connect a Liquid Cristal Display to a AVR-microcontroller. The example is based on a LCD module with the Hitachi HD44780 LCD-controller.You can get these displays in various kinds, from 1 to 4 lines and from 8 to 40 characters per line. A display with 16 characters per line and 2 lines is used in this example. The display uses a power supply of 5V DC. Connecting of the display to the AVR-microcontroller is easy.

The display module has 8 data lines and 3 control lines. The 8 data lines are used to send data or instructions to the module. Instructions that can be send to the display are for example clearing the display or set the cursor. The control lines are of course used to control the display.

The 8 data lines can be used as a 4-bit bus or as a 8-bit bus. In the 4-bit bus mode the data and instructions are send by 2 groups of 4 bits after each other so the program code will be more then when the 8-bits bus is used. In this example the 4-bits bus is used, this saves 4 connections and because of that the display can be controlled by only one 8-bit port of the AVR microcontroller.

The line Enable controls the display. If it is high the data on data bus is put in the register of the display. If the line is low all the data lines are tri-state, what means that they are not connected to the microcontroller. You can see this as a switch that is open.

The line Read/Write enables reading from or writing to the display. If the R/W signal is low data can be written to the display, if it is high data can be read from the display. This is useful if you want to know if the module is ready to recieve new data.

The line Register Select selects if the data send to the display are instructions for the display or characters that have to be shown on the display.

In the table below you can see the pin assignment of the LCD module

Hardware

In the table below you can see how the LCD module is connected to the AVR-microcontroller.Because the display is used in the 4-bit mode, 4 lines of the data bus are not connected.The signal R/W has to be connected to GND so the display is always in the write mode. The pins 15 and 16 are used to connect the supply for the LED-backlight of the display, if it is present at the module. If you use 5V DC you need to place a resistor to limit the current. With a trimpotmeter of 10K connected at pin 3 (VO) of the display the contrast can be set.

Below is the schematic that shows you how to connect the LCD to the AVR microcontroller via a 10 pole flatcable with a 10 pin connector. The 10 pin connector can be plugged into the 10 pin header of the STK500 Board or the ATTiny2313 ISP Board.


Software

To control the display you can use BASCOM or Assembler. BASCOM has special commands for configuring and driving a LCD Display. In the table below you see the LCD commands and their functions.

To control the LCD display you need to implement the instructions in your program code. First you have to configure the display, which type of display you use (number of lines and number of characters on a line), to which pins of the microcontroller the lines of the LCD module are connected and which mode you use, 4-bit mode or 8-bit mode (default is 4-bit mode). Below is an example program of how to write text to the LCD display.

'--------------------------------------------------------------
' Title : LCD-test.bas
' Author : www.laros-edu.net
' Target : ATTiny2313
' program code : BASCOM AVR
' Description : Write text to a LCD module
'--------------------------------------------------------------

'main program

'config LCD pins for portB
Config Lcdpin = Pin , Db4 = Portb.0 , Db5 = Portb.1 , Db6 = Portb.2 ,
Db7 = Portb.3 , E = Portb.6 , Rs = Portb.7
'config LCD module for 2 lines and 16 characters.
Config Lcd = 16 * 2
Cls
Cursor Off
Lcd "Welcome to"
Lowerline
Lcd "avrprojects.net"

End

Saturday, October 11, 2008

AVR Projects - Output Board with 4 Relais




Description

This is a peripheral board with 4 relais, rated at 5A/250V each. The board has a ML10 output connector for connection with the AT2313 Project board. It has also 4 LED's for indication which LED is on.

Hardware

The circuit is simple, it consists of no more then an ML10 connector, a ULN2803A, which contains eight open collector darlington transistors, four of them amplifies the signals that come from the microcontroller. The IC has build in protection diodes, so no external diodes for driving the relais are needed. There are also foeur LED's with current limiting resistors to indicate if a relais is energized, each output of the relais goes to a 2-pole screw connector.


AVR Learning - Blink a LED

For beginners the first thing to try on a new system or in a new programming language is to print out "hello world". The equivalent for microcontrollers and other embedded systems is to blink a LED. When even the target circuit and the programmer are freshly put together it is wise to start even lower, with a blink-a-LED program written by someone else that is known to be working.

On this page you will find a 1 Hz blink-a-LED test programs for the Tiny12 and the AT2313 targets chips and circuits. You can build the circuits on a breadboard or use the AT2313 ISP board. The programs are made in assembler with AVR Studio 4 for the ATTiny12 and with the BASCOM complilor for the AT2313. Look on the AVR Programming Hardware pages for how to make a programming cable and on the AVR Programming Software pages for the software to get the hex file into your device.

Because the ports of the AVR chips can draw 20mA current, only a resistor to limit the current is neccesary to connect a LED to one of the ports. In both the circuits the LED is connected to PortB.0. In the circuit for the ATTiny12 the internal 1.2 MHz oscillator is used, because of that you don't need an external oscillator, but it can be configured also for an external crystal.

here the source code:

Config Portb = Outputout       'set port B as output

Do ' eternal loop
Portb.0 = 1 ' make portB.0 high
Waitms 500 ' wait 500ms
Portb.0 = 0 ' make Portb.0 Low
Waitms 500 'wait 500ms
Loop

The HEX file:

:020000020000FC
:100000000FEF07BBC09802D0C09AFCCF1FE027E3D8
:1000100039EC3A95F1F72A95D9F71A95C1F711E01D
:0A0020001A95F1F7000000000895A2

:00000001FF

AVR Programming Hardware - Serial Port Programmer



This is the schematic of a Serial Port Programmer for the popular programmer software PonyProg and avrdude.

    Features:
  • Connects to PC via RS232 port (note: It will not work with USB to RS232 converters!)
  • No need for external power supply as it takes the power supply from target board.
  • It uses Atmel's 2x5 pin ICSP connector layout