0% found this document useful (0 votes)
52 views6 pages

Program Avr Atmega 16: Lampiran

Download as docx, pdf, or txt
Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1/ 6

LAMPIRAN

Program AVR ATMega 16


/*****************************************************
This program was produced by the
CodeWizardAVR V2.05.0 Professional
Automatic Program Generator
Copyright 1998-2010 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com
Project :
Version :
Date : 18/05/2015
Author : NeVaDa
Company :
Comments:
Chip type
: ATmega16
Program type
: Application
AVR Core Clock frequency: 16,000000 MHz
Memory model
: Small
External RAM size
:0
Data Stack size
: 256
*****************************************************/
#include <mega16.h>
#include <stdio.h>
#include <delay.h>
// Alphanumeric LCD Module functions
#include <alcd.h>
#define ADC_VREF_TYPE 0x40
// 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
char cetak1[16],cetak2[16],cetak3[16],cetak4[16],cetak5[16],tampilc[16];

void main(void)
{
int i,j,mode;
Func0=In
PORTA=0x00;
DDRA=0x00;
Func0=In
PORTB=0x00;
DDRB=0x04;
Func0=In
PORTC=0x00;
DDRC=0x00;
Func0=In
PORTD=0x00;
DDRD=0x30;
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
TCCR1A=0xA2;
TCCR1B=0x11;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0xC8;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00;
MCUCR=0x00;
MCUCSR=0x00;
TIMSK=0x00;
UCSRB=0x00;
ACSR=0x80;
SFIOR=0x00;
ADMUX=ADC_VREF_TYPE & 0xff;
ADCSRA=0x84;
SPCR=0x00;
TWCR=0x00;
// Alphanumeric LCD initialization
// Connections specified in the
// Project|Configure|C Compiler|Libraries|Alphanumeric LCD menu:
// RS - PORTC Bit 0

// RD - PORTC Bit 1
// EN - PORTC Bit 2
// D4 - PORTC Bit 4
// D5 - PORTC Bit 5
// D6 - PORTC Bit 6
// D7 - PORTC Bit 7
// Characters/line: 20
lcd_init(16);
lcd_gotoxy(0,0);
lcd_putsf(" Bismillah ");
lcd_gotoxy(0,1);
lcd_putsf(" Battery Charger ");
delay_ms(2000);
lcd_clear();
lcd_gotoxy(0,0);
lcd_putsf(" Aulia Rizky Z ");
lcd_gotoxy(0,1);
lcd_putsf(" 1303121011 ");
delay_ms(2000);
lcd_clear();
sp=28.8;
while (1)
{
if(PINB.0==0)
{
//Sensor Tegangan Output BB
for(i=0;i<3000;i++)
{
a=read_adc(1);
teg=(float)(a/1023)*30;
c=(float)c+teg;
}
xbar=(float)c/500;
sprintf(cetak1,"V=%.2fV",teg);
lcd_gotoxy(0,0);
lcd_puts(cetak1);
// Program PWM 40 khz
d=read_adc(0);
duty=d*0.09778;
pwm=duty*2;
if(duty>70)
{
duty=70;
}
if(pwm>140)
{

pwm=140;
}
OCR1AL=pwm;
lcd_gotoxy(0,1);
sprintf(cetak2,"D=%2.0f%% ",duty);
lcd_puts(cetak2);
}
else if(PINB.0==1)
{
for(i=0;i<3000;i++)
{
a=read_adc(1);
teg=(float)(a/1023)*30;
c=(float)c+teg;
}
xbar=(float)c/500;
sprintf(cetak1,"V=%.2fV",teg);
lcd_gotoxy(0,0);
lcd_puts(cetak1);
//Increment Decrement
if(teg<sp)
{
OCR1AL=OCR1AL+1;
}
if(teg>sp)
{
OCR1AL=OCR1AL-1;
}
if(OCR1AL>=140)
{
OCR1AL=140;
}
if(OCR1AL<=60)
{
OCR1AL=60;
}
duty=OCR1AL/2;
lcd_gotoxy(0,1);
sprintf(cetak2,"D=%2.0f%% ",duty);
lcd_puts(cetak2);
}
//Program Pembacaan Arus Output BB
for(j=0,b=0;j<500;j++)
{
e=read_adc(2);

arus=(float)(e-512)/140;
b=(float)b+arus;
}
I=(float)b/1500;
if(I<0)I=0;
sprintf(cetak3,"I=%1.2fA",I);
lcd_gotoxy(8,0);
lcd_puts(cetak3);
if(I<=0.17)
{
mode=1;
PORTB.2=0;
Vb=((float)read_adc(3)*30/1023);
sprintf(tampilc,"V=%.2fV",Vb);
lcd_gotoxy(0,0);
lcd_puts(tampilc);
lcd_gotoxy(7,0);
lcd_puts("Relay ON ");
if (23,02>Vb<=23,32)
{
lcd_gotoxy(0,1);
lcd_puts("Baterai 20%");
delay_ms(5000);
lcd_clear();
}
if (23.32>Vb<=23.62)
{
lcd_gotoxy(0,1);
lcd_puts("Baterai 30%");
delay_ms(5000);
lcd_clear();
}
if (23.62>Vb<=23.92)
{
lcd_gotoxy(0,1);
lcd_puts("Baterai 40%");
delay_ms(5000);
lcd_clear();
}
if (23.92>Vb<=24.2)
{
lcd_gotoxy(0,1);
lcd_puts("Baterai 50%");
delay_ms(5000);
lcd_clear();
}

//Sensor Tegangan Baterai

if (24.2>Vb<=24.48)
{
lcd_gotoxy(0,1);
lcd_puts("Baterai 60%");
delay_ms(5000);
lcd_clear();
}
if (24.48>Vb<=24.74)
{
lcd_gotoxy(0,1);
lcd_puts("Baterai 70%");
delay_ms(5000);
lcd_clear();
}
if (24.74>Vb<=25)
{
lcd_gotoxy(0,1);
lcd_puts("Baterai 80%");
delay_ms(5000);
lcd_clear();
}
if (25>Vb<=25.24)
{
lcd_gotoxy(0,1);
lcd_puts("Baterai 90%");
delay_ms(5000);
lcd_clear();
}
if (25.24>Vb<=25.46)
{
lcd_gotoxy(0,1);
lcd_puts("Baterai 100%");
delay_ms(5000);
lcd_clear();
}
}
if(I<=0.0&&Vb<=23.92)
{
PORTB.2=1;
lcd_gotoxy(7,1);
lcd_puts("Relay OFF");
delay_ms(5000);
lcd_clear();
}
}}

You might also like