Académique Documents
Professionnel Documents
Culture Documents
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
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
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
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
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;
}