Program an AVR manually
DDRX = DATA DIRECTION REGISTER X [1=output/0=input] where X is the letter of the register
eg. DDRC |= _BV(7); set pin 7 of register C as output
PORTX = 1;
eg. PORTC ^= (1<<1); set pin 1 of register C to HIGH (5v) until otherwise set
DDRB = 0b00000110;
PORTB = 0b00000100;
Set a bit :
_BV() is a macro, BV = bit value
#define _BV(bit) (1 << (bit))
_BV(7) = (1<<7)
a |= (1<<position) or
a |=_BV(position)
example :
a = 00000000
a |= (1<<3) => a = a|(1<<3) => a=(0000000)|(00000100) => a=00000100
a |= _BV(3)
Set a bit :
a |= (1<<2);
a |= _BV(2);
Clear a bit :
a &= ~(1<<2);
a &= ~_BV(2);
uint8_t (same as: unsigned char)
int8_t (same as: signed char)
uint16_t (same as: unsigned int)
int16_t (same as: int)
Hello world program in microcontroller world = Blink program :
#define F_CPU 1000000UL // set proc speed to 1MHz----
#include <avr/io.h>
#include <util/delay.h>
int main (void)
{
DDRD |= _BV(6); // pin D6 set as output
while(1)
{
PORTC |=_BV(6);
_delay_ms(1000);
PORTC &= ~_BV(6);
_delay_ms(1000);
}
}
itoa() - convert integer to string.
Software :
For uploading hex data to avr :
eXtreme Burner - AVR => GUI alternative to avrdude - for uploading hex data to AVR
http://extremeelectronics.co.in/
For compiling code to hex :
WinAVR - Gnu C compiler and AVRLibC - http://sourceforge.net/projects/winavr/
AVR Studio - IDE - aStudio4b589.exe
Resurse :
Using LCD module with AVR - http://extremeelectronics.co.in/avr-tutorials/using-lcd-module-with-avrs/
AVR basic programming - SPI PROGRAMMING BASICS
Interfacing ps/2 - http://codelake9.wordpress.com/2011/12/10/interfacing-ps2-keyboard-to-atmega128atmega64/
ADC - analog to digital convertor
//InitADC
ADMUX=(1<<REFS0); // For Aref=AVcc;
ADCSRA=(1<<ADEN)|(7<<ADPS0);
uint16_t ReadADC(uint8_t ch)
{
ch = ch & 0b00000111;
// clear previous channel selection
ADMUX &= 0xF8;
ADMUX |= ch; // add in the new selection bits (REFS0 untouched)
//Start Single conversion
ADCSRA|=(1<<ADSC);
//Wait for conversion to complete
while((ADCSRA & (1<<ADSC)));
return(ADC);
}
//ReadADC( channel )
//ADC Channel ch must be 0-7 or what you micro has
adc_value=ReadADC(7);
Avr - SPI (Serial Peripheral Interface)
Using USBASP :
CONECTIONS :
MISO, MOSI, SCK, SS
CODE :
#define F_CPU 4000000UL
char spiData = 0;
SPCR = (1<<SPE); // Enable SPI
while(!(SPSR & (1 << SPIF))); // Wait for transmission end
spiData = SPDR; // Read SPI Data Register - 1 byte
------------------------------------------------------------------------------------------------
No comments:
Post a Comment