Anda di halaman 1dari 6

Latihan Program :

Kendali Kecepatan Kipas Menggunakan Mikrokontroler dan Sensor Suhu


LM35

Proyek kali ini yaitu kita akan mengatur kecepatan putar kipas berdasarkan sensor suhu LM35.
Ketika suhu yang diterima sensor suhu LM35 tinggi maka kecepatan putar kipas akan tinggi,
ketika suhu menurun maka kecepatan putar kipas juga akan menurun. Untuk skematiknya bisa
dilihat dibawah ini:

untuk programnya bisa dilihat berikut ini,

/*****************************************************
This program was produced by the
CodeWizardAVR V2.04.8b Evaluation
Automatic Program Generator
© Copyright 1998-2010 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com

Project : Pengukuran Suhu dan Pengntrollan Fan


Version : TA
Date : 03/07/2013
Author : Sarding Anwar
Company : Student
Comments:
Input Sensor LM35 (ADC)
Output Motor Fan (PWM Timer 1)
Tampilan Hasil (Lcd)

Chip type : ATmega8535


Program type : Application
AVR Core Clock frequency: 4,000000 MHz
Memory model : Small
External RAM size :0
Data Stack size : 128
*****************************************************/

#include <mega8535.h>
#include <stdio.h>
#include <stdlib.h>

// Alphanumeric LCD Module functions


#asm
.equ __lcd_port=0x18 ;PORTB
#endasm
#include <lcd.h>

#include <delay.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 temp[8];
unsigned int suhu1;
unsigned int suhu2;
unsigned int adc1;
unsigned int adc2;

//ADC Suhu
void baca_data_ADC()
{
adc1=read_adc(0); //Membaca Nilai ADC1
adc2=read_adc(1); //Membaca Nilai ADC2
}

void ubah_ADC_Celcius()
{
suhu1=(adc1*500)/1023; //Merubah Nilai ADC1 Ke Celcius
suhu2=(adc2*500)/1023; //Merubah Nilai ADC2 Ke Celcius
}

void tampilkan_Suhu1()
{
ftoa(suhu1,0,temp);//float to array
lcd_gotoxy(0,0);
lcd_puts(temp);
lcd_gotoxy(2,0);
lcd_putchar(0xdf);//menampilkan karakter derajat
lcd_putsf("C");
}

void tampilkan_Suhu2()
{
ftoa(suhu2,0,temp);//float to array
lcd_gotoxy(0,1);
lcd_puts(temp);
lcd_gotoxy(2,1);
lcd_putchar(0xdf);//menampilkan karakter derajat
lcd_putsf("C");
}

//PWM Kecepatan Motor

void kontrol_kipas1()
{
if (suhu1<=28)
{
OCR1A=0; //(0 * 1023) / 100 = 0
lcd_gotoxy(5,0);
lcd_putsf("OFF");
lcd_gotoxy(8,0);
lcd_putsf(" PWM 0%");
}
else if (suhu1>28 && suhu1<=32)
{
OCR1A=511; //(50 * 1023) / 100 = 511
lcd_gotoxy(5,0);
lcd_putsf("ON");
lcd_gotoxy(8,0);
lcd_putsf(" PWM 50%");
}
else if (suhu1>32 && suhu1<60)
{
OCR1A=1023; //(100 * 1023) / 100 = 1023
lcd_gotoxy(5,0);
lcd_putsf("ON");
lcd_gotoxy(8,0);
lcd_putsf(" PWM100%");
}
}

void kontrol_kipas2()
{
if (suhu2<=28)
{
OCR1B=0; //(0 * 1023) / 100 = 0
lcd_gotoxy(5,1);
lcd_putsf("OFF");
lcd_gotoxy(8,1);
lcd_putsf(" PWM 0%");
}
else if (suhu2>28 && suhu2<=32)
{
OCR1B=511; //(50 * 1023) / 100 = 511
lcd_gotoxy(5,1);
lcd_putsf("ON");
lcd_gotoxy(8,1);
lcd_putsf(" PWM 50%");
}
else if (suhu2>32 && suhu2<60)
{
OCR1B=1023; //(100 * 1023) / 100 = 1023
lcd_gotoxy(5,1);
lcd_putsf("ON");
lcd_gotoxy(8,1);
lcd_putsf(" PWM100%");
}
}

void main(void)
{
// Declare your local variables here

// Input/Output Ports initialization


// Port A initialization
PORTA=0x00;
DDRA=0x00;

// Port B initialization
PORTB=0x00;
DDRB=0x00;

// Port C initialization
PORTC=0x00;
DDRC=0x00;

// Port D initialization
PORTD=0x00;
DDRD=0x30;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 3.906 kHz
// Mode: Fast PWM top=03FFh
// OC1A output: Non-Inv.
// OC1B output: Non-Inv.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0xA3;
TCCR1B=0x0D;
TCNT1H=0xF0;
TCNT1L=0xBE;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x03;
OCR1AL=0xFF;
OCR1BH=0x03;
OCR1BL=0xFF;

// Analog Comparator initialization


// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

// ADC initialization
// ADC Clock frequency: 1000.000 kHz
// ADC Voltage Reference: AVCC pin
// ADC High Speed Mode: Off
// ADC Auto Trigger Source: None
ADMUX=ADC_VREF_TYPE & 0xff;
ADCSRA=0x82;
SFIOR&=0xEF;

// LCD module initialization


lcd_init(16);

lcd_clear();
lcd_gotoxy(1,0);
lcd_putsf("SARDING ANWAR");
lcd_gotoxy(0,1);
lcd_putsf("NIM 0100630066");
delay_ms(5000);
while (1)
{
// Place your code here
lcd_clear();
baca_data_ADC ();
ubah_ADC_Celcius();
tampilkan_Suhu1();
tampilkan_Suhu2();
kontrol_kipas1();
kontrol_kipas2();
delay_ms(500);
}
;
}

Anda mungkin juga menyukai