Jumat, 21 Oktober 2016

Contoh Program Aktifkan Led

Tulis program berikut dengan Code Vision AVR :

/Program 1: Menghidupkan LED di Port B
//OK
#include <mega8535.h>
void main()
{
 DDRB=0b11111111;
 PORTB=0b01010101;
 while(1);
}


//Program 2: LED Blink
#include <mega8535.h>
#include <delay.h>
void main()
{
 DDRB=0b11111111;
 while(1)
 {
  PORTB=0b11111111;
  delay_ms(1000);
  PORTB=0b00000000;
  delay_ms(1000);
 }

}


//Program 3: LED Berjalan
//OK
#include <mega8535.h>
#include <delay.h>
void main()
{
 DDRB=0b11111111;
 while(1)
 {
  PORTB=0b00000001;
  delay_ms(1000);
  PORTB=0b00000010;
  delay_ms(1000);
  PORTB=0b00000100;
  delay_ms(1000);
  PORTB=0b00001000;
  delay_ms(1000);
  PORTB=0b00010000;
  delay_ms(1000);
  PORTB=0b00100000;
  delay_ms(1000);
  PORTB=0b01000000;
  delay_ms(1000);
  PORTB=0b10000000;
  delay_ms(1000);
 }

}

Program Menyalakan Lampu dengan tombol (Tulis dengan Bascom AVR)

$regfile = "m16.dat"
$crystal = 16000000
Config Porta = Input
Porta = &HFF
Config Portc = Output
Portc = &HFF
Do
If Pina.0 = 0 Then
Portc = &B11111110
Elseif Pina.1 = 0 Then
Portc = &B11111101
Elseif Pina.2 = 0 Then
Portc = &B11111011
Elseif Pina.3 = 0 Then
Portc = &B11110111
Elseif Pina.4 = 0 Then
Portc = &B11101111
Elseif Pina.5 = 0 Then
Portc = &B11011111
Elseif Pina.6 = 0 Then
Portc = &B10111111
Elseif Pina.7 = 0 Then
Portc = &B01111111
End If
Loop
End