5
/**************************************************** Project : sensor Temp LM35 Version : V_1 Date : 6/7/2011 Author : yogi Company : CERC Comments: Chip type : ATmega8535 Program type : Application Clock frequency : 4.000000 MHz Memory model : Small External SRAM size : 0 Data Stack size : 128 *****************************************************/ #include <mega8535.h> #include <delay.h> #include <stdio.h> #include <stdlib.h> int Temp, dataADC; // sensor menggunakan LM35 Pin output terhubung dengan PINA.0 (pada mikrokontroller) // Alphanumeric LCD Module functions #asm .equ __lcd_port=0x15 ;PORTC #endasm

Adc

Embed Size (px)

Citation preview

Page 1: Adc

/****************************************************

Project : sensor Temp LM35

Version : V_1

Date : 6/7/2011

Author : yogi

Company : CERC

Comments:

Chip type : ATmega8535

Program type : Application

Clock frequency : 4.000000 MHz

Memory model : Small

External SRAM size : 0

Data Stack size : 128

*****************************************************/

#include <mega8535.h>

#include <delay.h>

#include <stdio.h>

#include <stdlib.h>

int Temp, dataADC;

// sensor menggunakan LM35 Pin output terhubung dengan PINA.0 (pada mikrokontroller)

// Alphanumeric LCD Module functions

#asm

.equ __lcd_port=0x15 ;PORTC

#endasm

#include <lcd.h>

#define ADC_VREF_TYPE 0x40

Page 2: Adc

// Read the AD conversion result

unsigned int read_adc(unsigned char adc_input)

{

ADMUX=adc_input | (ADC_VREF_TYPE & 0xff);

// Start the AD conversion

ADCSRA|=0x40;

// Wait for the AD conversion to complete

while ((ADCSRA & 0x10)==0);

ADCSRA|=0x10;

return ADCW;

}

void tampil (unsigned int dat) // rutin untuk menampilkan di LCD

{

unsigned int data;

//lcd_gotoxy(0,0);

data = dat / 1000;

data+=0x30;

lcd_putchar(data);

dat%=1000;

data = dat / 100;

data+=0x30;

lcd_putchar(data);

dat%=100;

data = dat / 10;

data+=0x30;

lcd_putchar(data);

dat%=10;

data = dat + 0x30;

Page 3: Adc

lcd_putchar(data);

}

// Declare your global variables here

void main(void)

{

// Declare your local variables here

PORTA=0x00;

DDRA=0x00;

PORTB=0x00;

DDRB=0x00;

PORTC=0x00;

DDRC=0x00;

PORTD=0x00;

DDRD=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: 1000.000 kHz

// ADC Voltage Reference: AVCC pin

// ADC High Speed Mode: Off

// ADC Auto Trigger Source: None

ADMUX=ADC_VREF_TYPE & 0xff;

Page 4: Adc

ADCSRA=0x82;

SFIOR&=0xEF;

// LCD module initialization

lcd_init(16);

while (1)

{

// Place your code here

dataADC=read_adc(0); //pembacaan ADC pada PINA.0 (no1)

lcd_gotoxy(0,0); //menentukan kordinat huruf pada LCD 2x16 (x,y) = (0,0) //artinya kolom ke 0 dan baris ke0 (no2)

lcd_putsf("view data sensor"); //menampilkan tulisan string "view data sensor" (jangan //lebih dari 16 character) (no3)

lcd_gotoxy(0,1); //menentukan kordinat huruf pada LCD 2x16 (x,y) = (0,1) //artinya kolom ke 0 dan baris ke1 (no4)

tampil(dataADC); //panggil program rutin penampil LCD yg diatas, untuk //menampilkan "dataADC" dari no 1 (no5)

// dataADC yg ditampilkan belum berupa nilai dalam //suhu/temp (derajat celcius) (no6)

Temp=(dataADC*4.9/10); //conversi nilai ADC ke nilai Temperatur (no7)

lcd_gotoxy(7,1); //menentukan kordinat huruf pada LCD 2x16 (x,y) = (7,1) //artinya kolom ke 7 dan baris ke1 (no8)

tampil(Temp); //menampilkan nilai temp hasil dari no7 (no9)

};

}