Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Prepared by
A.V.Microtech
Noida (India)
Rev A-1001110
Software packages
You will need functional installation of the following packages.
1) AVR studio
Source Atmel Website :- Installs as ICON
Sreenshot of AVR studio. It comes with a good set of documentation found under Help
2) WinAVR
Source sourceforge.net
Type “avr-gcc” at command prompt and you get (avr-gcc: no input files)
Type “avrdude” at command prompt and you get a list of avrdude flags.
Bootloader:- Microcontrollers like Atmega328p have a portion of flash that can be programmed
with bootloader code to auto download hex program into the main flash (0000 onwards). The
bootloader behave as though you have an in-circuit programmer between your PC and the
atmega328 board.
Upon hardware reset program counter jumps to the start of bootloader. The bootloader waits for
approx 2 sec for the PC (infact avrdude) to establish communication and if no sync character
received in 2 sec, it jumps to 0000 to start executing the code. The bootloader that Arduino board
comes programmed with can only communicate with avrdude.
Fuses :- From the Atmega328 datasheet you can see that it has configurable fuses which decide on
various runtime configuration. For example you can run it from internal clock or various types of
external clocks. Please do not change fuses when operating from bootloader.
We will use USB atmega328 board as shown in picture above. This board works with Arduino and
with a simple customization, seamlessly with AVR studio.
The Arduino environment (IDE) controls DTR signal and with boards having the DTR tied to reset
pin of atmega328, it is easy to bring the device into bootloader when required.
AVR Studio. AVRstudio by default uses STK500. Hence to download data we must come out
from AVR studio and activate avrdude to help us download data into atmega328. For this we will
write a batchfile in the default sub folder of the project directory. The reason being that AVRstudio
C project, keeps its hex file in a subfolder named default. The batchfile content are as below
Further we will use a simple hardware configuration to lock the bootloader permanently so that we
do not have the limitation of 2 sec. To enter into locked bootloader mode we will jumper the
Arduino pin12 with RESET as shown in figure below. Upon activating the reset button the
atmega328 will enter into bootloader indefinitely till either code is downloaded or the jumper is
removed.
AVRdude Programmer
The Atmega328 board can also be converted into programmer. Arduino 0018 comes with a sketch
called “Arduino ISP” that converts the board into avrdude USB programmer identified as avrisp
by avrdude. The default baud rate is 19600.
After downloading the Arduino ISP sketch please remove “AUTO” jumper and change the
adjacent jumper from AR to P as shown in picture below. Removing AUTO jumper will prevent
the board into entering bootloader unnecessarily.
Please note this is a complete programmer and can do any modification to Fuse and lockbits. You
can again create a batch file to execute your commands repeatedly.
#include <inttypes.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <avr/eeprom.h>
#include <math.h>
#include <stdio.h>
#include <util/delay.h>
#include <string.h>
#include <stdlib.h>
//function declaration
void PortInit(void);
d = 5000;
while(d>0){d = d-1;}
PORTB = ~b;
}
}
void PortInit(void)
{
DDRB = 7; //Set port B
}
****************************************************************************************************
int main(void) is the main entry point and is a must for every WinAVR C program. While(1) is a non
terminating loop to keep running untill the reset is pressed.
Port registers are addressed direct (sfr_defs.h). The AVR registers from R1 to R32 are however not
available. In similar way some of the useful instructions like SETB and CLRB also seems to have been lost
but are offered in more usefull way through a mechanism like
PORTB |= _BV(PB1) for sbi (sfr,bit)
PORTB &= ~(_BV(PB1)) for cbi(sfr,bit)
#include <inttypes.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <avr/eeprom.h>
#include <math.h>
#include <stdio.h>
#include <util/delay.h>
#include <string.h>
#include <stdlib.h>
void PortInit(void);
void TimerInit(void);
int main(void)
{
PortInit();
TimerInit();
uint8_t b;//(8 bit variable just like int a;)
//double d;
b = 0b01010101;
while(1)
{
if ((PINB & 1) == 1){
b = ~b;
//b = b & 0b11111110;
PORTB = b & 0b11111110;
}
}
}
void PortInit(void)
{
DDRB = 0b00001111;
ISR(TIMER1_OVF_vect)
{
TCNT1H = 0xF0;
TCNT1L = 0x00;
PORTB |= _BV(0);
void TimerInit(void)
{
//TCCR1A
TCCR1B = 0b00000101;
TIMSK1 |= _BV(0);
sei();
}
Attiny2313 based Character LCD controller
Index
Introduction page 1
SPI LCD controller page 2
Serial LCD controller page 3
WinAVR C code (atmega328) page 4
Arduino code page 9
Connecting LCD display is one of the complex interconnection in
a microcontroller project and taking up valuable I/O for small
devices like Atmega168/328. This controller works in two modes
as shown in the pictures below. The Serial mode can be
connected using the Tx out of microcontroller only.
Second command
Serial.write(CR);
Serial.write(L2);
Any data without hex 250 will be sent to data display register.
The display data (data register) can be send as continuous string.
sprintf(c,"CPU Temp %u%cC ",analogRead(8)/2,0xdf);
Serial.print(c);
This way you can also re-initialize the LCD display from your
code as shown in the Arduino sketch below. The controller has
been tested with many 16x2 LCD display from different
manufacturer.
Important :- The configuration change must always be done in power down mode.
The controller cannot change configuration in power up mode.
Notes
#define CR 250
#define CD 1
#include <inttypes.h>
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#include <avr/eeprom.h>
#include <math.h>
#include <stdio.h>
#include <util/delay.h>
#include <string.h>
#include <stdlib.h>
#include <avr/wdt.h>
//#include <uart.h>
int main(void)
{
USART_Init();
SPI_MasterInit();
int d, b;
char c[30] ;
//************************
//b = 0 for serial
//b = 1 for SPI
b = 1;
//******************************
//Clear Display
USART_Transmit( CR );
_delay_us(50);
USART_Transmit( CD );
_delay_us(50);
d = 0;
while(1){
d++;
Comm_Transmit( CR,b );
_delay_us(50);
Comm_Transmit( L1,b );
_delay_us(50);
sprintf(c,"Good welldone!!");
Comm_write( c,b );
Comm_Transmit( CR,b );
_delay_us(50);
Comm_Transmit( L2,b );
_delay_us(50);
sprintf(c,"The Value - %u%cC. ",d, 0xdf);
Comm_write( c,b );
if (d>9){
d = 0;
}
_delay_ms(500);
void USART_Init( )
{
/* Set baud rate */
UBRR0H = 0;
UBRR0L = 103;
UCSR0A &= !(1<<U2X0);//U2X0 = 0
/* Enable Receiver and Transmitter */
UCSR0B = (1<<RXEN0)|(1<<TXEN0);
//UCSR0B = _BV(TXEN0) | _BV(RXEN0);
/* Set frame format: 8data, 1stop bit */
UCSR0C = (3<<UCSZ00);
//OSCCAL = 0XB0;
void SPI_MasterInit(void)
{
Arduino Sketch
Requirement:-
HEspi Library to setup the SPI communication. Please note that the library does not assert any
chip_select . Hence the same library can control many SPI connected device by asserting the pins using
Digital I/O.
//***************************************************
#include <avr/io.h>
#include <stdlib.h>
#include <HEspi.h>
/*LCD CONTROL
CONTROL CHARACTER DECIDED BY INTERNAL OPERATION OF
ATTINY2313 SPI LCD / RS232 CONTROLLER
T0 SEND CONTROL CHARACTERS.
Serial.write(CR);
Serial.write(CD);
OR
Comm (CD,0) (0 for SPI and 1 for RS232)
// 0 0 0 0 1 D C B
#define CBY 0X0F //CURSOR ON BLINK ON 00001DCB
#define CBN 0X0C //CURSOR 0FF BLINK 0FF
int RCByte;
int TCByte = 0xff;
int mv = 0;
char c[30];
HEspi myspi;
void setup()
{
Serial.begin(9600);
pinMode(6,OUTPUT); //To SPI LCD
pinMode(7,OUTPUT); //To SPI I/O Board.
digitalWrite(7,HIGH);
digitalWrite(6,LOW);
LCD_Reset(0);
LCD_Reset(1);
digitalWrite(7,HIGH);
digitalWrite(6,LOW);
myspi.write(CR); // | Alternate |
myspi.write(CD); // | Comm(CD) |
Serial.write(CR);
Serial.write(L1);
myspi.print("HobbyElectronic");
sprintf(c,"Trimmer Val %u%% ",analogRead(0)/10);
Serial.print(c);
myspi.write(CR);
myspi.write(L2);
Serial.write(CR);
Serial.write(L2);
delay(500);
myspi.write(CR);
myspi.write(CBN);
//digitalWrite(6,HIGH);
//digitalWrite(7,LOW);
////BLINK LED CONNECTED TO ATTINY2313 SPI OPTOISOLATED I/O BOARD
//for (int i=0; i <= 5; i++){
//TCByte = 0;
//RCByte = myspi.Transmit(TCByte);
//delay(500);
//TCByte = 0XFF;
//RCByte = myspi.Transmit(TCByte);
//delay(500);
//}
void LCD_Reset(uint8_t inter) //inter is for interface: 0 for SPI: 1 for RS232
{
Comm(0b00110000,inter);
delay(5);
Comm(0b00110000,inter);
delayMicroseconds(100);
Comm(0b00110000,inter);
Comm(0b00111000,inter);
Comm(0b00001100,inter);
Comm(0b00000001,inter);
Comm(0b00000110,inter);
//****************************************************************
Line follower Infrared sensor
Arduino Sketch
#include <LiquidCrystal.h>
//The test was done with 33K and 10K resistor. The 33K gives longer range
//but 10k is more immune to reflected sunlight i.e in a well lit room.
// but still can give reasonable 4cm distance sensing.
void setup()
{
// Print a message to the LCD.
lcd.print(str1[st]);
lcd.print(analogRead(0));
pinMode(12,OUTPUT);
pinMode(13,OUTPUT);
pinMode(11,OUTPUT);
pinMode(7,INPUT);
digitalWrite(12,LOW);
}
void loop()
{
st = 0;
aval1 = 0;
aval2 = 0;
digitalWrite(11,HIGH);
while (st < 10)
{
st++;
digitalWrite(11,HIGH);
delayMicroseconds(10);
aval1 = aval1+analogRead(0);
digitalWrite(11,LOW);
delayMicroseconds(10);
aval2 = aval2+analogRead(0);
}
refval = analogRead(1);
if (abs(aval2-aval1)>refval)
{
digitalWrite(13,HIGH);
}
else
{
digitalWrite(13,LOW);
}
lcd.clear();
lcd.print(str1[0]);
lcd.setCursor(1,1);
lcd.print("Analog=");
lcd.print(aval2-aval1);
lcd.print(" : ");
lcd.print(refval);
delay(100);
}
Attiny2313 Twin DC Motor Driver
Robots like line follower require twin differential DC motor driver. This intelligent board does high speed
processing (16Mhz crystal) and reduces the number of I/O and processing overhead otherwise required by
the main CPU ( AVR or PIC ). Free from latchups and resets, this board offers the highest immunity to
electrical noise. This board can support low voltage motors from Tamiya. It can work as standalone fully
functional Robot controller. Like line tracing, obstacle avoidance or light following robot. This board can
also be run from 3.3V to be compatible with ARM devices.
Web Server
A simple web server that shows the value of the analog input pins, and set Digital
outputs on Attiny23213 Board. In the Demo sketch, only four outputs can be toggled,
however you can easily increase it 8.
This Sketch is currently configured to run on local network. You can plug a straight cable
directly to your PC LAN card. Cross cable is not required (most system can take care of
that).
Kindly ensure that your PC does not use the IP address 192.168.1.110 but another IP
address within the subnet mask 255.255.0.0 for example 192.168.1.120.
The power supply required for arduino board is 6V DC and power required by I/O board
(outputs) is 6V to 12V (DC Power jack). The Wiznet board is supplied with 3.3volts.
Soft Reset
1) Press first button on Add on Board (near 5V). This resets Wiznet board
Wait for a few seconds..............
2) Press Reset on I/O board. This resets Arduino 328 board and I/O board.
Interfacing Bootboard with SD Card
SD card pinout and corresponding Arduino and AVR pins are shown below. AVR
uses SPI interace to communicate with SD card.
A regulator like 78M33 can be used to produce 3.3V required for SD card. The
Mosfet used during trial was 2N7000. You also need 10K resistors as shown in
figure below. The DO pin can be directly connected to AVR MISO as 3.3V is logic 1
for AVR. However all the outputs of AVR (Clock, Chip select and MOSI) must use
level converter as shown below. Resistors based level converter are also
suggested by some, but not yet used by us.
Note :-
Please set CPOL and CPHA in SPI setting very carefully the default value is 0, 0 but
many times configuration 1, 1 is the acceptable one. Read the SD card technical
information carefully.