40
www.avmicrotech.com Brief Introduction to programming AVR devices in C using AVR studio (Rev C) (www.avmicrotech.com) Prepared by A.V.Microtech Noida (India)

Brief Introduction to programming AVR devices in C … C programming with Arduino Re… · Brief Introduction to programming AVR devices in C using AVR studio ... Change the Hex file

  • Upload
    ledat

  • View
    244

  • Download
    2

Embed Size (px)

Citation preview

Page 1: Brief Introduction to programming AVR devices in C … C programming with Arduino Re… · Brief Introduction to programming AVR devices in C using AVR studio ... Change the Hex file

www.avmicrotech.com

Brief Introduction to programming AVR devices in C

using AVR studio (Rev C)

(www.avmicrotech.com)

Prepared by

A.V.Microtech

Noida (India)

Page 2: Brief Introduction to programming AVR devices in C … C programming with Arduino Re… · Brief Introduction to programming AVR devices in C using AVR studio ... Change the Hex file

www.avmicrotech.com

This document is to get you started with programming Atmel devices using WinAVR. From

authors experience, the supporting documents with AVRstudio, WinAVR and Atmel datasheets

are sufficient to achieving any level of expertise. The internet and online forums are also good

source of information.

Introduction

Atmel Atmega328 is versatile 28 Pin PDIP microcontroller. Loaded with many features it has

become a favorite microcontroller to learn embedded C programming. A bootloader can be burnt

in the Atmega328 to help us download the code (hex files) directly from PC without the need of

in-circuit programmer. Please note the difference, many Atmel microcontroller like Atmega32u4

and ARM microcontroller come with bootloader burnt during manufacturing process, however the

Atmega328 is blank when new.

We will introduce hardware and software to get started with Atmega328. As a prerequisite you

must have the following.

1) AVR Studio.

2) WinAVR.

3) Atmega328 USB board like Bootboard.

These requirements are explained in more details below:-

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

Page 3: Brief Introduction to programming AVR devices in C … C programming with Arduino Re… · Brief Introduction to programming AVR devices in C using AVR studio ... Change the Hex file

www.avmicrotech.com

2) WinAVR

Source sourceforge.net

The package includes:-

AVR-GCC free GNU c-compiler

AVRdude free AVR flash downloader

To check successful install

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.

The avrdude documents can be found at C:\winAVR-YYYYMMDD\doc\avrdude\avrdude.pdf

(Note: You may have to configure AVR Studio to compile C program)

Atmega328 Bootboard

Atmega328 bootboard is a versatile board that has Atmega328 loaded with bootloader. It has

jumper setting to convert it into and ArduinoISP programmer and burn bootloader into other

Atmega328 as with any Atmel programmer. The details are available at the end of this document.

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

Page 4: Brief Introduction to programming AVR devices in C … C programming with Arduino Re… · Brief Introduction to programming AVR devices in C using AVR studio ... Change the Hex file

www.avmicrotech.com

received in 2 sec, it jumps to 0000 to start executing the code. The bootloader that boot 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.

Programming Environment :-The bootboard works with Arduino IDE 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.

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 same directory that AVRstudio keeps the newly

created Hex file. The batchfile content are as below

Shown in the picture is default sub folder of our project “firstcblinky”

avrdude -P com30 -b 57600 -p m328p -c arduino -F -e -U flash:w:firstcblinky.hex

Page 5: Brief Introduction to programming AVR devices in C … C programming with Arduino Re… · Brief Introduction to programming AVR devices in C using AVR studio ... Change the Hex file

www.avmicrotech.com

Writing your first C program using AVRStudio

This chapter will walk you through the details of setting up and running your firstCblinky program

and later writing a batch file to download it to your Atmega328 board.

Please click AVR studio icon and click Create new project. Please select the location that will host

your projects.

After naming your files click Next. Click AVR simulator and select the correct Device

(Atmega328P). Click Next.

Page 6: Brief Introduction to programming AVR devices in C … C programming with Arduino Re… · Brief Introduction to programming AVR devices in C using AVR studio ... Change the Hex file

www.avmicrotech.com

Expand the Source Files on left and click your C file. Copy paste your C code. Click Build and

select Rebuild All.

Downloading the code. Now we will write a batch file that will activate AVRdude and download

the newly created Hex file to the Atmega328.

Please locate your project folder that was created in the location selected. A default sub folder is

created by AVRstudio during the project creation step. Open default sub folder to see its contents.

Page 7: Brief Introduction to programming AVR devices in C … C programming with Arduino Re… · Brief Introduction to programming AVR devices in C using AVR studio ... Change the Hex file

www.avmicrotech.com

Create a batch file called Bootloader

Copy paste the following single line batch command.

(You can create a txt document, update it and rename it as “Bootloader.bat”. The comm Port and

file name has to be edited)

avrdude -P com30 -b 57600 -p m328p -c arduino -F -e -U flash:w:firstcblinky.hex pause

Page 8: Brief Introduction to programming AVR devices in C … C programming with Arduino Re… · Brief Introduction to programming AVR devices in C using AVR studio ... Change the Hex file

www.avmicrotech.com

Before clicking the Bootloader.bat to down load the code kindly check the following.

1) Edit batch file to update:-

A)Select the correct com port that your USB board shows up as

avrdude -P com1 -b 57600 -p m328p…………….

(go to Control Panel/System/Device manager and check under ports (com & LPT) to see the new

com port againstr USB serial Port)

B)Change the Hex file name to point to the correct one.

………….-F -e -U flash:w:firstcblinky.hex

Page 9: Brief Introduction to programming AVR devices in C … C programming with Arduino Re… · Brief Introduction to programming AVR devices in C using AVR studio ... Change the Hex file

www.avmicrotech.com

C programming Introduction:- AVR Libc user manual (avr-libc-user-manual.pdf) carries a great

deal of information on programming AVR devices using GNU GCC.

A typical C program is as shown below:- ****************************************************************************************************** #define F_CPU 16000000

#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);

int main(void) // Main entry into the program

{

PortInit();

uint8_t b; //(8 bit variable just like int a;)

double d;

b = 0b01010101;

while(1) // Loop to run continuously

{

d = 5000;

while(d>0){d = d-1;}

PORTB = b;

d = 5000;

while(d>0){d = d-1;}

PORTB = ~b;

}

}

void PortInit(void)

{

DDRB = 7; //Set port B

Page 10: Brief Introduction to programming AVR devices in C … C programming with Arduino Re… · Brief Introduction to programming AVR devices in C using AVR studio ... Change the Hex file

www.avmicrotech.com

}

****************************************************************************************************

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 useful way through a mechanism like

PORTB |= _BV(PB1) for sbi (sfr,bit)

PORTB &= ~(_BV(PB1)) for cbi(sfr,bit)

Another C program showing the usage of Interrupts.

#define F_CPU 16000000

#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>

//#define DDRD _SFR_IO8(0x11)

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)

Page 11: Brief Introduction to programming AVR devices in C … C programming with Arduino Re… · Brief Introduction to programming AVR devices in C using AVR studio ... Change the Hex file

www.avmicrotech.com

{

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();

}

Page 12: Brief Introduction to programming AVR devices in C … C programming with Arduino Re… · Brief Introduction to programming AVR devices in C using AVR studio ... Change the Hex file

www.avmicrotech.com

A direct control of 16x2 LCD display. The LCD controller is HD44780 from Hitachi

//RS--4 R/W--5 E--6

#include <avr/io.h>

#define F_CPU 16000000

#include <stdbool.h>

#include <stdint.h>

#include <stdio.h>

#include <math.h>

#include <avr/io.h>

#include <util/delay.h>

#define E PORTD4

#define RS PORTD5

#define RW PORTD6

void PortInit(void);

void e(int a);

void rs(int a);

void rw(int a);

void wrt(char a, int b);

void lcd_init(void);

void wrtb(char a, int c);

int main (void)

{

// set PORTD for output

uint8_t a, b, c;

char z;

PortInit();

a = 0b10101010;

z = 'A';

c = 'A';

b = 0b00000100;

wrt(b, 1);

b = 0b00001001;

wrt(b, 1);

_delay_ms(5);

Page 13: Brief Introduction to programming AVR devices in C … C programming with Arduino Re… · Brief Introduction to programming AVR devices in C using AVR studio ... Change the Hex file

www.avmicrotech.com

b = 0b00000100;

wrt(b, 1);

b = 0b00001001;

wrt(b, 1);

_delay_ms(5);

while(1) {

_delay_ms(100);

PORTC = a;

_delay_ms(100);

PORTC = ~a;

_delay_ms(100);

PORTC = a;

_delay_ms(100);

PORTC = ~a;

_delay_ms(100);

PORTC = a;

_delay_ms(100);

PORTC = ~a;

_delay_ms(1000);

}

}

void PortInit(void)

{

DDRC = 0xFF;

DDRB = 0xFF;

DDRD = 0xFF;

lcd_init();

}

void wrt(char a, int b)

{

rs(b);

rw(0);

e(1);

PORTB = a;

e(0);

}

void wrtb(char a, int c)

{

int b;

b = (a>>4);

Page 14: Brief Introduction to programming AVR devices in C … C programming with Arduino Re… · Brief Introduction to programming AVR devices in C using AVR studio ... Change the Hex file

www.avmicrotech.com

wrt( b, c);

b = (a);

wrt(b, c);

}

void e(int a)

{

if (a == 1){

PORTD |= (1<<E);

}

if (a == 0){

PORTD &= ~(1<<E);

}

}

void rs(int a)

{

if (a == 1){

PORTD |= (1<<RS);

}

if (a == 0){

PORTD &= ~(1<<RS);

}

}

void rw(int a)

{

if (a == 1){

PORTD |= (1<<RW);

}

if (a == 0){

PORTD &= ~(1<<RW);

}

}

void lcd_init(void)

{

uint8_t b;

_delay_ms(100);

b = 0b00000011;

wrt(b,0); //register

_delay_ms(50);

b = 0b00000011;

wrt(b,0); //register

Page 15: Brief Introduction to programming AVR devices in C … C programming with Arduino Re… · Brief Introduction to programming AVR devices in C using AVR studio ... Change the Hex file

www.avmicrotech.com

_delay_ms(200);

b = 0b00000011;

wrt(b,0); //register

_delay_ms(50);

b = 0b00000010;

wrt(b,0); //register

_delay_ms(50);

b = 0b00000010; //5

wrt(b,0); //register

b = 0b00001000; //N F

wrt(b,0); //register

_delay_ms(50);

b = 0b00000000;

wrt(b,0); //register

b = 0b00001110; //8

wrt(b,0); //register

_delay_ms(50);

b = 0b00000000;

wrt(b,0); //register

b = 0b00000001; //10

wrt(b,0); //register

_delay_ms(50);

b = 0b00000000;

wrt(b,0); //register

b = 0b00000110; //12

wrt(b,0); //register

_delay_ms(50);

_delay_ms(50);

b = 0b00001000;

wrt(b,0); //register

b = 0b00000001; //12

wrt(b,0); //register

_delay_ms(50);

}

Page 16: Brief Introduction to programming AVR devices in C … C programming with Arduino Re… · Brief Introduction to programming AVR devices in C using AVR studio ... Change the Hex file

www.avmicrotech.com

Arduino PLC board with DAQ

Atmega328 PLC Board with DAQ

1. Arduino hardware and software compatible.

2. Fully Optoisolated.

3. 8 Input / Output.

4. All I/O ports can be used as Input or Output.

5. Sinking output drive current 200mA.

6. 12V DC or 24V DC control voltage.

7. Arduino supply 9V or USB.

8. Arduino shield can be mounted.

9. User Manual provided

10. Two voltage divider to read high voltage analog signal.

11. I2C connector with pull up resistors

Page 17: Brief Introduction to programming AVR devices in C … C programming with Arduino Re… · Brief Introduction to programming AVR devices in C using AVR studio ... Change the Hex file

www.avmicrotech.com

Attiny2313 based Character LCD controller

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.

This controller works by using hex code 250 to differentiate between writing instruction register

and data register. Using two consecutive bytes, hex 250 followed by instruction data byte will write to instruction register of the LCD. Every data to be sent to Instruction register must be

constructed this way using two bytes.

For Example:-

First command

Page 18: Brief Introduction to programming AVR devices in C … C programming with Arduino Re… · Brief Introduction to programming AVR devices in C using AVR studio ... Change the Hex file

www.avmicrotech.com

Serial.write(CR); //where CR is defined as 250

Serial.write(L1);

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.

Page 19: Brief Introduction to programming AVR devices in C … C programming with Arduino Re… · Brief Introduction to programming AVR devices in C using AVR studio ... Change the Hex file

www.avmicrotech.com

Important :- The configuration change must always be done in power down mode.

The controller cannot change configuration in power up mode.

Notes

1) Move the three jumper to short D0, D1,D2

2) Use D3 as Chip select signal. If you do not have any other SPI device than this can be

pulled low permanently.

3) The backlight resistor is 500 ohm and it works perfectly with new LCD with high

efficiency LED backlight. However this can be sorted also. Infact this was designed for

automatic backlight control using BC547 transistor. Hence you can replace the resistor

with BC547 and connect the base to AVR/PIC PWM output through 2K resistor.

4) The reset button will also reset the AVR device.

Page 20: Brief Introduction to programming AVR devices in C … C programming with Arduino Re… · Brief Introduction to programming AVR devices in C using AVR studio ... Change the Hex file

www.avmicrotech.com

Notes

1) Move the three jumper to short B7, B6,B5

2) Pull D2 to ground as it is polled at the start to configure the controller to either serial or SPI

interface.

3) The backlight resistor is 500 ohm and it works perfectly with new LCD with high

efficiency LED backlight. However this can be sorted also . Infact this was designed for

automatic backlight control using BC547 transistor. Hence you can replace the resistor

with BC547 and connect the base to AVR/PIC PWM output through 2K resistor.

4) Connect Tx out of microcontroller to D0.

Page 21: Brief Introduction to programming AVR devices in C … C programming with Arduino Re… · Brief Introduction to programming AVR devices in C using AVR studio ... Change the Hex file

www.avmicrotech.com

WinAVR C Code

#define F_CPU 16000000UL

#define CR 250

#define CD 1

#define L1 0X80 //ROW 0 COL 0

#define L2 0XC0 //ROW 1 COL 0

#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>

void ADC_InitA (void);

void PWM_Init (void);

void WDT_off(void);

void WDT_Prescaler_Change(void);

int ADC_Meas(void);

void Comm_write( const char* myval, int b);

void Comm_Transmit( char myval, int b);

void SPI_MasterInit(void);

void SPI_MasterTransmit(char cData);

void USART_Init( );

void USART_Transmit( unsigned char data);

int main(void)

{

USART_Init();

SPI_MasterInit();

int d, b;

char c[30] ;

//************************

//b = 0 for serial

//b = 1 for SPI

b = 1;

//******************************

Page 22: Brief Introduction to programming AVR devices in C … C programming with Arduino Re… · Brief Introduction to programming AVR devices in C using AVR studio ... Change the Hex file

www.avmicrotech.com

//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;

}

Page 23: Brief Introduction to programming AVR devices in C … C programming with Arduino Re… · Brief Introduction to programming AVR devices in C using AVR studio ... Change the Hex file

www.avmicrotech.com

void USART_Transmit( unsigned char data )

{

/* Wait for empty transmit buffer */

//! Not. And USCRA with the bit position UDR0E

while ( !( UCSR0A & (1<<UDRE0)) );

/* Put data into buffer, sends the data */

UDR0 = data;

}

void Comm_write(const char* myval, int b)

{

int a = 0;

while (a<strlen(myval)){

if (b == 0){

USART_Transmit(myval[a]);

a++;}

if (b > 0){

SPI_MasterTransmit(myval[a]);

a++;}

}

}

void Comm_Transmit( char myval, int b)

{

if (b == 0){

USART_Transmit(myval);

}

if (b > 0){

SPI_MasterTransmit(myval);

}

}

void SPI_MasterInit(void)

{

/* Set MOSI and SCK output, all others input */

DDRB |=(1<<PORTB2)|(1<<PORTB3)|(1<<PORTB5);//Keep portb2 as output

PORTB = (1<<PORTB4);

/* Enable SPI, Master, set clock rate fck/16 - SPR0 = 1, fck/64- SPR1 = 1*/

SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0);

}

void SPI_MasterTransmit(char cData)

{

/* Start transmission */

SPDR = cData;

/* Wait for transmission complete */

while(!(SPSR & (1<<SPIF)))

;

_delay_ms(1);

}

Note:- You may receive new Arduino sketch without the need of HEspi

library(no library will be sent). This is just for the convenience of the user

Page 24: Brief Introduction to programming AVR devices in C … C programming with Arduino Re… · Brief Introduction to programming AVR devices in C using AVR studio ... Change the Hex file

www.avmicrotech.com

as otherwise you have to ensure that the library is installed on all the

machines that you are working on. We have retained the code below just

to show the library usage.

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)

SEND ASCII 250 AND THEN ANY CONTROL CHARACTER */

#define CR 250 //TO ACCESS CONTROL REGISTER

/*SEE PAGE 191,192,193 OF HITACHI LCD CONTROLLER HD44780 DATSHEET

0 0 0 0 0 0 0 1 */

#define CD 1 //CLEAR DISPLAY

//1 ADD ADD ADD ADD ADD ADD ADD

#define L1 0X80 //ROW 0 COL 0

#define L2 0XC0 //ROW 1 COL 0

// 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;

Page 25: Brief Introduction to programming AVR devices in C … C programming with Arduino Re… · Brief Introduction to programming AVR devices in C using AVR studio ... Change the Hex file

www.avmicrotech.com

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);

}

void loop() // run over and over again

{

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);

myspi.print("support LCD 1");

//Serial.print("support LCD 2");

//

sprintf(c,"CPU Temp %u%cC ",analogRead(8)/2,0xdf);

Serial.print(c);

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++){

Page 26: Brief Introduction to programming AVR devices in C … C programming with Arduino Re… · Brief Introduction to programming AVR devices in C using AVR studio ... Change the Hex file

www.avmicrotech.com

//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);

}

void Comm(uint8_t mval, uint8_t inter){

if (inter == 0){

myspi.write(CR);

myspi.write(mval); }

if (inter == 1){

Serial.write(CR);

Serial.write(mval);

}

}

//****************************************************************

Page 27: Brief Introduction to programming AVR devices in C … C programming with Arduino Re… · Brief Introduction to programming AVR devices in C using AVR studio ... Change the Hex file

www.avmicrotech.com

Line follower Infrared sensor

Page 28: Brief Introduction to programming AVR devices in C … C programming with Arduino Re… · Brief Introduction to programming AVR devices in C using AVR studio ... Change the Hex file

www.avmicrotech.com

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.

// LiquidCrystal display with:

// rs on pin 12

// rw on pin 11

// enable on pin 10

// d0, d1, d2, d3 on pins 5, 4, 3, 2

LiquidCrystal lcd(10, 9, 8, 5, 4, 3, 2);

int aval1 =0;

int aval2 = 0;

int refval = 0;

int r=0;

int s=0;

int t=0;

char* str1[] = {"CHANNEL A", "CHANNEL B", "CHANNEL C"};

int lf = 0;

int rg = 0;

int st = 0;

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)

Page 29: Brief Introduction to programming AVR devices in C … C programming with Arduino Re… · Brief Introduction to programming AVR devices in C using AVR studio ... Change the Hex file

www.avmicrotech.com

{

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);

}

Page 30: Brief Introduction to programming AVR devices in C … C programming with Arduino Re… · Brief Introduction to programming AVR devices in C using AVR studio ... Change the Hex file

www.avmicrotech.com

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

Page 31: Brief Introduction to programming AVR devices in C … C programming with Arduino Re… · Brief Introduction to programming AVR devices in C using AVR studio ... Change the Hex file

www.avmicrotech.com

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. Procedure for a proper reset Hard Reset -> Power off and on. 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.

Page 32: Brief Introduction to programming AVR devices in C … C programming with Arduino Re… · Brief Introduction to programming AVR devices in C using AVR studio ... Change the Hex file

www.avmicrotech.com

Page 33: Brief Introduction to programming AVR devices in C … C programming with Arduino Re… · Brief Introduction to programming AVR devices in C using AVR studio ... Change the Hex file

www.avmicrotech.com

Page 34: Brief Introduction to programming AVR devices in C … C programming with Arduino Re… · Brief Introduction to programming AVR devices in C using AVR studio ... Change the Hex file

www.avmicrotech.com

Interfacing Bootboard with SD Card

This is a functional hardware to connect Bootboard to an SD card. The card tested was 128MB

Panasonic SD card.

SD card pinout and corresponding Arduino and AVR pins are shown below. AVR uses SPI

interace to communicate with SD card.

Page 35: Brief Introduction to programming AVR devices in C … C programming with Arduino Re… · Brief Introduction to programming AVR devices in C using AVR studio ... Change the Hex file

www.avmicrotech.com

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.

Page 36: Brief Introduction to programming AVR devices in C … C programming with Arduino Re… · Brief Introduction to programming AVR devices in C using AVR studio ... Change the Hex file

www.avmicrotech.com

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.

Please contact your seller if you need any further clarification.

Page 37: Brief Introduction to programming AVR devices in C … C programming with Arduino Re… · Brief Introduction to programming AVR devices in C using AVR studio ... Change the Hex file

www.avmicrotech.com

Arduino as a Programmer

With Arduino release-0018 it is possible to down load programming sketch in a AVR and use it to

burn programs or bootloader code in other devices. We have tested the sketch with Atmega8-16

and the size fits in.You will need a standard 6 pin (2x3) ISP cable.

The step by step instruction is as below.

Page 38: Brief Introduction to programming AVR devices in C … C programming with Arduino Re… · Brief Introduction to programming AVR devices in C using AVR studio ... Change the Hex file

www.avmicrotech.com

Step 1) Prepare your Arduino as programmer i.e burn a programming sketch. Connect your

programming board to your PC as you would do normally to burn sketches. Go to examples and

select ArduinoISP (new in Arduino 0018). Click upload as you do normally to upload the sketch to

your board. After completion your Programmer is ready.

Page 39: Brief Introduction to programming AVR devices in C … C programming with Arduino Re… · Brief Introduction to programming AVR devices in C using AVR studio ... Change the Hex file

www.avmicrotech.com

With your programmer ready, you can now download program to your board as below.

Step 2) Connect the Target Board ( blank AVR )as in picture below.

Page 40: Brief Introduction to programming AVR devices in C … C programming with Arduino Re… · Brief Introduction to programming AVR devices in C using AVR studio ... Change the Hex file

www.avmicrotech.com

Please maintain the direction of programming cable. Further the pin1 of programmer’s ISP header

must match with Pin1 of target board’s ISP header. Move jumper as in picture above. Auto Jumper

can be removed.

Create a batch file in the default subfolder of your project.

Upon clicking this batchfile your hexfile will be downloaded to your project board.

avrdude -P com1 -b 19600 -p m328p -c AVRISP -F -e -U flash:w:firstcblinky.hex