Vous êtes sur la page 1sur 16

AVR Programming

Preparations
• Build the hardware on a general-purpose PCB.
• Write the accompanying code and assemble it
on a PC.
• Program the microprocessor and plug it into
your PCB
Contoh Rangkaian

In general, although this varies by


the specific processor, the port
pins are capable of sinking
20 mA, but they can source much
less current.

The circuit operates at 4 MHz


using the external crystal.
Port I/O pada AVR
Register I/O
Using Digital Port
// File: led.c
// Date modified: 8 Sept 2012
// By: Yudi Gondokaryono

#include <avr/io.h> // avr header file for IO ports

int main(void)
{
DDRB = 0xFF; // set PORTB for output
PORTB = 0xFF; // turn OFF LED initially

while(1)
{
// Send output to PORTB.
// This port is connected to the LED
PORTB = 0xFE;
}
return 1;
}
Things to know
• Compiler needs to know CPU freq
– define F_CPU 4000000UL
• Use <util/delay.h>
– Using void _delay_ms ( double  __ms)
– The maximal possible delay is 262.14 ms / F_CPU in MHz
– When the user request delay which exceed the maximum
possible one, _delay_ms() provides a decreased resolution
functionality.
• In this mode _delay_ms() will work with a resolution of 1/10 ms,
providing delays up to 6.5535 seconds (independent from CPU
frequency).
• The user will not be informed about decreased resolution.
Blinking LED??
// File: blinkled.c
// Date modified: 8 Sept 2012
// By: Yudi Gondokaryono

#include <avr/io.h> // avr header file for IO ports


#include <util/delay.h> // avr header file for delay func.
#define F_CPU 4000000UL // CPU Freq 4 MHz

int main(void)
{
DDRB = 0xFF; // set PORTB for output
PORTB = 0xFF; // turn OFF LED initially

while(1)
{
PORTB ^= 0x01; // read from PORTB latch and invert
_delay_ms(50); // delay for 50 ms
}
return 1;
}
Contoh Rangkaian
Port I/O pada AVR
Things to know
• Set DDRx for input (=0)
• Does the switch have a pull-up?
– If not, then turn on internal pull-up
• Set PORTx to 1
• Read through PINx
• Needs to do debouncing (but not today)
Read switch and copy to LED
// File: sw2led.c
// Date modified: 8 Sept 2012
// By: Yudi Gondokaryono

#include <avr/io.h> // avr header file for IO ports

int main(void)
{
DDRB = 0xFF; // set PORTB for output
PORTB = 0xFF; // turn OFF LED initially
DDRD = 0x00; // set PORTD for input
PORTD = 0xFF; // turn on pull-up on PORTD

while(1)
{
PORTB = PIND; // read from PORTD and send it to PORTB
}
return 1;
}
PORT I/O ATmega8535
Special Function IO Register –
SFIOR

• Bit 2 – PUD: Pull-up disable


• When this bit is written to one, the pull-ups in
the I/O ports are disabled even if the DDxn
and PORTxn Registers are configured to enable
the pull-ups
Things to know
• Set DDRx for input (=0)
• Does the switch have a pull-up?
– If not, then turn on internal pull-up
• Set PORTx to 1
• Set SFIOR = 0 to enable all pull-up
Read switch and copy to LED
// File: sw2ledv2.c
// Date modified: 16 Sept 2012
// By: Yudi Gondokaryono

#include <avr/io.h> // avr header file for IO ports

int main(void)
{
DDRB = 0xFF; // set PORTB for output
PORTB = 0xFF; // turn OFF LED initially
DDRD = 0x00; // set PORTD for input
PORTD = 0xFF; // turn on pull-up on PORTD
SFIOR = 0<<PUD; // turn on all pull-up

while(1)
{
PORTB = PIND; // read from PORTD and send it to PORTB
}
return 1;
}

Vous aimerez peut-être aussi