Malam ini adalah malam yang special buatku karena baru kali ini saya menggunakan program CV AVR, ternyata mudah-mudah sulit, ya namanya belajar.. hehehee..(^_^). disini saya contohkan mengenai bagaimana cara membuat sebuah program LCD, led, push button, stepper dan lainnya. sebelum melakukan percobaan alangkah bagus persiapkan dulu alat yang dibutuhkan. untuk lebih jelasnya berikut adalah contoh program dan skemanya.
a. Minimum System
b. Program Code Vision AVR
/*****************************************************
This program was produced by the
CodeWizardAVR V2.05.3 Standard
Automatic Program Generator
© Copyright 1998-2011 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com
Project :
Version :
Date : 3/30/2016
Author : asus
Company :
Comments:
Chip type : ATmega16
Program type : Application
AVR Core Clock frequency: 12.000000 MHz
Memory model : Small
External RAM size : 0
Data Stack size : 256
*****************************************************/
#include <mega16.h>
#include <delay.h>
// Alphanumeric LCD functions
#include <alcd.h>
#include <stdlib.h>
#define ADC_VREF_TYPE 0x40
int i;
int Dataadc;
char temp[10];
// Read the AD conversion result
unsigned int read_adc(unsigned char adc_input)
{
ADMUX=adc_input | (ADC_VREF_TYPE & 0xff);
// Delay needed for the stabilization of the ADC input voltage
delay_us(10);
// Start the AD conversion
ADCSRA|=0x40;
// Wait for the AD conversion to complete
while ((ADCSRA & 0x10)==0);
ADCSRA|=0x10;
return ADCW;
}
// Declare your global variables here
void shake250()
{
Dataadc = read_adc(7);
itoa(Dataadc,temp);
lcd_gotoxy (0,0);
lcd_puts(temp);
PORTD = 0b00000001;
delay_ms(10);
PORTD = 0b00000010;
delay_ms(10);
PORTD = 0b00000100;
delay_ms(10);
PORTD = 0b00001000;
delay_ms(10);
if (Dataadc >= 400 && Dataadc <= 415 )
{
for(i=0;i<10;i++){
PORTD = 0b10000000;
delay_ms(10);
PORTD = 0b01000000;
delay_ms(10);
PORTD = 0b00100000;
delay_ms(10);
PORTD = 0b00010000;
delay_ms(10);
}
i = 0;
while(1) {
PORTC.6 = 0;
}
}
else
{
shake250();
}
}
void shake350()
{
Dataadc = read_adc(7);
itoa(Dataadc,temp);
lcd_gotoxy (0,0);
lcd_puts(temp);
PORTD = 0b00000001;
delay_ms(5);
PORTD = 0b00000010;
delay_ms(5);
PORTD = 0b00000100;
delay_ms(5);
PORTD = 0b00001000;
delay_ms(5);
if (Dataadc >= 380 && Dataadc <= 395 )
{
for(i=0;i<10;i++){
PORTD = 0b10000000;
delay_ms(10);
PORTD = 0b01000000;
delay_ms(10);
PORTD = 0b00100000;
delay_ms(10);
PORTD = 0b00010000;
delay_ms(10);
}
i = 0;
while(1) {
PORTC.6 = 0;
}
}
else
{
shake350();
}
}
void shake450()
{
Dataadc = read_adc(7);
itoa(Dataadc,temp);
lcd_gotoxy (0,0);
lcd_puts(temp);
PORTD = 0b00000001;
delay_ms(5);
PORTD = 0b00000010;
delay_ms(5);
PORTD = 0b00000100;
delay_ms(5);
PORTD = 0b00001000;
delay_ms(5);
if (Dataadc >= 370 && Dataadc <= 380 )
{
for(i=0;i<10;i++){
PORTD = 0b10000000;
delay_ms(10);
PORTD = 0b01000000;
delay_ms(10);
PORTD = 0b00100000;
delay_ms(10);
PORTD = 0b00010000;
delay_ms(10);
}
i = 0;
while(1) {
PORTC.6 = 0;
}
}
else
{
shake450();
}
}
void main(void)
{
// Declare your local variables here
// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTA=0x00;
DDRA=0x00;
// Port B initialization
// Func7=In Func6=In Func5=Out Func4=In Func3=In Func2=Out Func1=Out Func0=Out
// State7=T State6=T State5=0 State4=P State3=P State2=0 State1=0 State0=0
PORTB=0x18;
DDRB=0x27;
// Port C initialization
// Func7=In Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
// State7=P State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
PORTC=0x80;
DDRC=0x7F;
// Port D initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
PORTD=0x00;
DDRD=0xFF;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=0xFF
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer1 Stopped
// Mode: Normal top=0xFFFF
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer2 Stopped
// Mode: Normal top=0xFF
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;
// USART initialization
// USART disabled
UCSRB=0x00;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
// ADC initialization
// ADC Clock frequency: 750.000 kHz
// ADC Voltage Reference: AVCC pin
// ADC Auto Trigger Source: Free Running
ADMUX=ADC_VREF_TYPE & 0xff;
ADCSRA=0xA4;
SFIOR&=0x1F;
// SPI initialization
// SPI disabled
SPCR=0x00;
// TWI initialization
// TWI disabled
TWCR=0x00;
// Alphanumeric LCD initialization
// Connections are specified in the
// Project|Configure|C Compiler|Libraries|Alphanumeric LCD menu:
// RS - PORTC Bit 0
// RD - PORTB Bit 6
// EN - PORTC Bit 1
// D4 - PORTC Bit 2
// D5 - PORTC Bit 3
// D6 - PORTC Bit 4
// D7 - PORTC Bit 5
// Characters/line: 8
lcd_init(16);
for(i=0;i<10;i++){
PORTD = 0b00010000;
delay_ms(10);
PORTD = 0b00100000;
delay_ms(10);
PORTD = 0b01000000;
delay_ms(10);
PORTD = 0b10000000;
delay_ms(10);
}
i = 0;
while (1)
{
PORTB.5 = 0;
PORTC.6 = 1;
PORTB.0 = 1;
PORTB.1 = 1;
PORTB.2 = 1;
lcd_gotoxy (0,0);
lcd_putsf("PILIH MENU");
if (PINB.3 == 0)
{
PORTB.0 = 0;
PORTB.1 = 1;
PORTB.2 = 1;
lcd_clear();
lcd_gotoxy (0,0);
lcd_putsf("Memilih Kantung");
lcd_gotoxy (0,1);
lcd_putsf("Shake 250ml");
delay_ms(3000);
lcd_clear();
PORTB.5 = 1;
shake250();
}
else if (PINB.4 == 0)
{
PORTB.0 = 1;
PORTB.1 = 0;
PORTB.2 = 1;
lcd_clear();
lcd_gotoxy (0,0);
lcd_putsf("Memilih Kantung");
lcd_gotoxy (0,1);
lcd_putsf("Shake 350ml");
delay_ms(3000);
lcd_clear();
PORTB.5 = 1;
shake350();
}
else if (PINC.7 == 0)
{
PORTB.0 = 1;
PORTB.1 = 1;
PORTB.2 = 0;
lcd_clear();
lcd_gotoxy (0,0);
lcd_putsf("Memilih Kantung");
lcd_gotoxy (0,1);
lcd_putsf("Shake 450ml");
delay_ms(3000);
lcd_clear();
PORTB.5 = 1;
shake450();
}
}
}
This program was produced by the
CodeWizardAVR V2.05.3 Standard
Automatic Program Generator
© Copyright 1998-2011 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com
Project :
Version :
Date : 3/30/2016
Author : asus
Company :
Comments:
Chip type : ATmega16
Program type : Application
AVR Core Clock frequency: 12.000000 MHz
Memory model : Small
External RAM size : 0
Data Stack size : 256
*****************************************************/
#include <mega16.h>
#include <delay.h>
// Alphanumeric LCD functions
#include <alcd.h>
#include <stdlib.h>
#define ADC_VREF_TYPE 0x40
int i;
int Dataadc;
char temp[10];
// Read the AD conversion result
unsigned int read_adc(unsigned char adc_input)
{
ADMUX=adc_input | (ADC_VREF_TYPE & 0xff);
// Delay needed for the stabilization of the ADC input voltage
delay_us(10);
// Start the AD conversion
ADCSRA|=0x40;
// Wait for the AD conversion to complete
while ((ADCSRA & 0x10)==0);
ADCSRA|=0x10;
return ADCW;
}
// Declare your global variables here
void shake250()
{
Dataadc = read_adc(7);
itoa(Dataadc,temp);
lcd_gotoxy (0,0);
lcd_puts(temp);
PORTD = 0b00000001;
delay_ms(10);
PORTD = 0b00000010;
delay_ms(10);
PORTD = 0b00000100;
delay_ms(10);
PORTD = 0b00001000;
delay_ms(10);
if (Dataadc >= 400 && Dataadc <= 415 )
{
for(i=0;i<10;i++){
PORTD = 0b10000000;
delay_ms(10);
PORTD = 0b01000000;
delay_ms(10);
PORTD = 0b00100000;
delay_ms(10);
PORTD = 0b00010000;
delay_ms(10);
}
i = 0;
while(1) {
PORTC.6 = 0;
}
}
else
{
shake250();
}
}
void shake350()
{
Dataadc = read_adc(7);
itoa(Dataadc,temp);
lcd_gotoxy (0,0);
lcd_puts(temp);
PORTD = 0b00000001;
delay_ms(5);
PORTD = 0b00000010;
delay_ms(5);
PORTD = 0b00000100;
delay_ms(5);
PORTD = 0b00001000;
delay_ms(5);
if (Dataadc >= 380 && Dataadc <= 395 )
{
for(i=0;i<10;i++){
PORTD = 0b10000000;
delay_ms(10);
PORTD = 0b01000000;
delay_ms(10);
PORTD = 0b00100000;
delay_ms(10);
PORTD = 0b00010000;
delay_ms(10);
}
i = 0;
while(1) {
PORTC.6 = 0;
}
}
else
{
shake350();
}
}
void shake450()
{
Dataadc = read_adc(7);
itoa(Dataadc,temp);
lcd_gotoxy (0,0);
lcd_puts(temp);
PORTD = 0b00000001;
delay_ms(5);
PORTD = 0b00000010;
delay_ms(5);
PORTD = 0b00000100;
delay_ms(5);
PORTD = 0b00001000;
delay_ms(5);
if (Dataadc >= 370 && Dataadc <= 380 )
{
for(i=0;i<10;i++){
PORTD = 0b10000000;
delay_ms(10);
PORTD = 0b01000000;
delay_ms(10);
PORTD = 0b00100000;
delay_ms(10);
PORTD = 0b00010000;
delay_ms(10);
}
i = 0;
while(1) {
PORTC.6 = 0;
}
}
else
{
shake450();
}
}
void main(void)
{
// Declare your local variables here
// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTA=0x00;
DDRA=0x00;
// Port B initialization
// Func7=In Func6=In Func5=Out Func4=In Func3=In Func2=Out Func1=Out Func0=Out
// State7=T State6=T State5=0 State4=P State3=P State2=0 State1=0 State0=0
PORTB=0x18;
DDRB=0x27;
// Port C initialization
// Func7=In Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
// State7=P State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
PORTC=0x80;
DDRC=0x7F;
// Port D initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=Out Func2=Out Func1=Out Func0=Out
// State7=0 State6=0 State5=0 State4=0 State3=0 State2=0 State1=0 State0=0
PORTD=0x00;
DDRD=0xFF;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=0xFF
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer1 Stopped
// Mode: Normal top=0xFFFF
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer2 Stopped
// Mode: Normal top=0xFF
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;
// USART initialization
// USART disabled
UCSRB=0x00;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
// ADC initialization
// ADC Clock frequency: 750.000 kHz
// ADC Voltage Reference: AVCC pin
// ADC Auto Trigger Source: Free Running
ADMUX=ADC_VREF_TYPE & 0xff;
ADCSRA=0xA4;
SFIOR&=0x1F;
// SPI initialization
// SPI disabled
SPCR=0x00;
// TWI initialization
// TWI disabled
TWCR=0x00;
// Alphanumeric LCD initialization
// Connections are specified in the
// Project|Configure|C Compiler|Libraries|Alphanumeric LCD menu:
// RS - PORTC Bit 0
// RD - PORTB Bit 6
// EN - PORTC Bit 1
// D4 - PORTC Bit 2
// D5 - PORTC Bit 3
// D6 - PORTC Bit 4
// D7 - PORTC Bit 5
// Characters/line: 8
lcd_init(16);
for(i=0;i<10;i++){
PORTD = 0b00010000;
delay_ms(10);
PORTD = 0b00100000;
delay_ms(10);
PORTD = 0b01000000;
delay_ms(10);
PORTD = 0b10000000;
delay_ms(10);
}
i = 0;
while (1)
{
PORTB.5 = 0;
PORTC.6 = 1;
PORTB.0 = 1;
PORTB.1 = 1;
PORTB.2 = 1;
lcd_gotoxy (0,0);
lcd_putsf("PILIH MENU");
if (PINB.3 == 0)
{
PORTB.0 = 0;
PORTB.1 = 1;
PORTB.2 = 1;
lcd_clear();
lcd_gotoxy (0,0);
lcd_putsf("Memilih Kantung");
lcd_gotoxy (0,1);
lcd_putsf("Shake 250ml");
delay_ms(3000);
lcd_clear();
PORTB.5 = 1;
shake250();
}
else if (PINB.4 == 0)
{
PORTB.0 = 1;
PORTB.1 = 0;
PORTB.2 = 1;
lcd_clear();
lcd_gotoxy (0,0);
lcd_putsf("Memilih Kantung");
lcd_gotoxy (0,1);
lcd_putsf("Shake 350ml");
delay_ms(3000);
lcd_clear();
PORTB.5 = 1;
shake350();
}
else if (PINC.7 == 0)
{
PORTB.0 = 1;
PORTB.1 = 1;
PORTB.2 = 0;
lcd_clear();
lcd_gotoxy (0,0);
lcd_putsf("Memilih Kantung");
lcd_gotoxy (0,1);
lcd_putsf("Shake 450ml");
delay_ms(3000);
lcd_clear();
PORTB.5 = 1;
shake450();
}
}
}
c. Cara Upload Program
Untuk memasukkan program ke mikrokontroller, langkah pertama yaitu COMPILE dulu program yang dibuat, kemudian BUILT, setelah hasilnya NO ERROR maka buka program uploader seperti AVR DUDE atau PROG ISP. jika berhasil maka program akan bekerja sesuai keinginan.
No comments:
Post a Comment