5. Percobaan pengiriman
data ADC
Pengambilan data ADC dan data hasil pembacaan dikirimkan ke Hyperterminal
via Port Serial dalam bentuk Desimal: 0000 sd 1024 dan bentuk Tegangan
:0.0000 sd 5.00000 volt
#include <mega8535.h> // Alphanumeric LCD Module functions #asm .equ __lcd_port=0x15 ;PORTC #endasm #include <lcd.h> // Standard Input/Output
functions
#include <stdio.h>
#include <delay.h>
#include <stdlib.h>
#define ADC_VREF_TYPE 0xC0
unsigned int data_adc;
unsigned char temp1[6],temp2[6];
float vin;
// 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 main(void)
{
PORTC=0x00;
DDRC=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;
// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: Off
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud Rate: 2400
UCSRA=0x00;
UCSRB=0x08;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x19;
SFIOR=0x00;
// ADC initialization
// ADC Clock frequency: 500.000 kHz
// ADC Voltage Reference: Int., cap. on AREF
// ADC High Speed Mode: Off
// ADC Auto Trigger Source: Free Running
ADMUX=ADC_VREF_TYPE & 0xff;
ADCSRA=0xA1;
SFIOR&=0x0F;
// LCD module initialization
lcd_init(16);
while (1)
{
// Place your code here
data_adc=read_adc(0);
vin=((float)data_adc*5/1023);
itoa(data_adc,temp1);
printf("Data ADC: %s", temp1);
//
ftoa(vin,10,temp2);//variabel 10=jumlah digit float
printf(" Data Vin: %s",temp2);
putchar(13);
delay_ms(1000);
}
}
6. Pengaturan LED melalui komunikasi serial
Mengendalikan nyala LED secara komunikasi serial, Perintah pengendalian
LED menggunakan tombol keyboard di PC.
#include <mega8535.h> // Standard Input/Output functions #include <stdio.h>
// Declare your global variables here
unsigned char data_serial;
void main(void)
{
PORTC=0x00;
DDRC=0xFF;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;
// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: Off
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud Rate: 2400
UCSRA=0x00;
UCSRB=0x08;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x19;
SFIOR=0x00;
while (1)
{
// Place your code here
data_serial=getchar();
PORTC=a-0x30;//dikurangi 30h mengembalikan ke desimal
};
}
|