LISTING PROGRAM
#include <mega8535.h> #include <delay.h> #include <alcd.h> #define ADC_samp 25 #define kipas PORTC.0
float suhu_total,gas_total,suhu_hasil,gas_hasil,suhu_rata,gas_rata; unsigned int x,nilai_ADC;
char input,buflcd[16];
#define DATA_REGISTER_EMPTY (1<<UDRE) #define RX_COMPLETE (1<<RXC)
#define FRAMING_ERROR (1<<FE) #define PARITY_ERROR (1<<UPE) #define DATA_OVERRUN (1<<DOR) #define RX_BUFFER_SIZE 8
char rx_buffer[RX_BUFFER_SIZE]; #if RX_BUFFER_SIZE <= 256
unsigned char rx_wr_index=0,rx_rd_index=0; #else
unsigned int rx_wr_index=0,rx_rd_index=0; #endif
#if RX_BUFFER_SIZE < 256 unsigned char rx_counter=0; #else
unsigned int rx_counter=0; #endif
// This flag is set on USART Receiver buffer overflow bit rx_buffer_overflow;
// USART Receiver interrupt service routine interrupt [USART_RXC] void usart_rx_isr(void) {
char status,data; status=UCSRA; data=UDR;
if ((status & (FRAMING_ERROR | PARITY_ERROR | DATA_OVERRUN))==0)
{
rx_buffer[rx_wr_index++]=data; #if RX_BUFFER_SIZE == 256
#else
if (rx_wr_index == RX_BUFFER_SIZE) rx_wr_index=0; if (++rx_counter == RX_BUFFER_SIZE)
{
#ifndef _DEBUG_TERMINAL_IO_
// Get a character from the USART Receiver buffer #define _ALTERNATE_GETCHAR_
#pragma used+ char getchar(void) {
char data;
while (rx_counter==0);
data=rx_buffer[rx_rd_index++]; #if RX_BUFFER_SIZE != 256
if (rx_rd_index == RX_BUFFER_SIZE) rx_rd_index=0; #endif
#asm("cli") --rx_counter; #asm("sei") return data; }
#pragma used- #endif
// USART Transmitter buffer #define TX_BUFFER_SIZE 8 char tx_buffer[TX_BUFFER_SIZE]; #if TX_BUFFER_SIZE <= 256
unsigned char tx_wr_index=0,tx_rd_index=0; #else
unsigned int tx_wr_index=0,tx_rd_index=0; #endif
#if TX_BUFFER_SIZE < 256 unsigned char tx_counter=0; #else
unsigned int tx_counter=0; #endif
interrupt [USART_TXC] void usart_tx_isr(void) {
if (tx_counter) {
--tx_counter;
UDR=tx_buffer[tx_rd_index++]; #if TX_BUFFER_SIZE != 256
if (tx_rd_index == TX_BUFFER_SIZE) tx_rd_index=0; #endif
} }
#ifndef _DEBUG_TERMINAL_IO_
// Write a character to the USART Transmitter buffer #define _ALTERNATE_PUTCHAR_
#pragma used+ void putchar(char c) {
while (tx_counter == TX_BUFFER_SIZE); #asm("cli")
if (tx_counter || ((UCSRA & DATA_REGISTER_EMPTY)==0)) {
tx_buffer[tx_wr_index++]=c; #if TX_BUFFER_SIZE != 256
if (tx_wr_index == TX_BUFFER_SIZE) tx_wr_index=0; #endif
#pragma used- #endif
// Standard Input/Output functions #include <stdio.h>
#define ADC_VREF_TYPE ((0<<REFS1) | (1<<REFS0) | (0<<ADLAR)) unsigned int read_adc(unsigned char adc_input)
{
ADMUX=adc_input | ADC_VREF_TYPE;
// Delay needed for the stabilization of the ADC input voltage delay_us(10);
// Start the AD conversion ADCSRA|=(1<<ADSC);
ADCSRA|=(1<<ADIF); return ADCW;
}
void main(void) {
DDRA=(0<<DDA7) | (0<<DDA6) | (0<<DDA5) | (0<<DDA4) | (0<<DDA3) | (0<<DDA2) | (0<<DDA1) | (0<<DDA0);
PORTA=(0<<PORTA7) | (0<<PORTA6) | (0<<PORTA5) | (0<<PORTA4) | (0<<PORTA3) | (0<<PORTA2) | (0<<PORTA1) | (0<<PORTA0);
DDRB=(0<<DDB7) | (0<<DDB6) | (0<<DDB5) | (0<<DDB4) | (0<<DDB3) | (0<<DDB2) | (0<<DDB1) | (0<<DDB0);
PORTB=(0<<PORTB7) | (0<<PORTB6) | (0<<PORTB5) | (0<<PORTB4) | (0<<PORTB3) | (0<<PORTB2) | (0<<PORTB1) | (0<<PORTB0);
DDRC=(0<<DDC7) | (0<<DDC6) | (0<<DDC5) | (0<<DDC4) | (0<<DDC3) | (0<<DDC2) | (1<<DDC1) | (1<<DDC0);
PORTC=(0<<PORTC7) | (0<<PORTC6) | (0<<PORTC5) | (0<<PORTC4) | (0<<PORTC3) | (0<<PORTC2) | (0<<PORTC1) | (0<<PORTC0);
DDRD=(0<<DDD7) | (0<<DDD6) | (1<<DDD5) | (0<<DDD4) | (0<<DDD3) | (0<<DDD2) | (0<<DDD1) | (0<<DDD0);
PORTD=(0<<PORTD7) | (0<<PORTD6) | (0<<PORTD5) | (0<<PORTD4) | (0<<PORTD3) | (0<<PORTD2) | (0<<PORTD1) | (0<<PORTD0);
TCCR1A=(1<<COM1A1) | (0<<COM1A0) | (0<<COM1B1) | (0<<COM1B0) | (1<<WGM11) | (0<<WGM10);
TCCR1B=(0<<ICNC1) | (0<<ICES1) | (1<<WGM13) | (1<<WGM12) | (0<<CS12) | (1<<CS11) | (0<<CS10);
TCNT1H=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=(0<<OCIE2) | (0<<TOIE2) | (0<<TICIE1) | (0<<OCIE1A) | (0<<OCIE1B) | (0<<TOIE1) | (0<<OCIE0) | (0<<TOIE0);
UCSRA=(0<<RXC) | (0<<TXC) | (0<<UDRE) | (0<<FE) | (0<<DOR) | (0<<UPE) | (0<<U2X) | (0<<MPCM);
UCSRB=(1<<RXCIE) | (1<<TXCIE) | (0<<UDRIE) | (1<<RXEN) | (1<<TXEN) | (0<<UCSZ2) | (0<<RXB8) | (0<<TXB8);
UCSRC=(1<<URSEL) | (0<<UMSEL) | (0<<UPM1) | (0<<UPM0) | (0<<USBS) | (1<<UCSZ1) | (1<<UCSZ0) | (0<<UCPOL);
UBRRH=0x00; UBRRL=0x67;
ADCSRA=(1<<ADEN) | (0<<ADSC) | (1<<ADATE) | (0<<ADIF) | (0<<ADIE) | (1<<ADPS2) | (1<<ADPS1) | (0<<ADPS0);
lcd_init(16); #asm("sei") OCR1A=1600; lcd_gotoxy(0,0); lcd_putsf("NOVA"); while (1)
{
suhu_total = 0;gas_total=0; suhu_hasil = 0;gas_hasil=0; for (x=0; x<ADC_samp; x++) {
nilai_ADC = read_adc(1);
suhu_hasil=suhu_hasil + nilai_ADC; delay_ms(10);
nilai_ADC = read_adc(0); gas_hasil=gas_hasil + nilai_ADC; delay_ms(10);
}
suhu_rata = (float)suhu_hasil / ADC_samp; // nilai rata-rata gas_rata = (float)gas_hasil / ADC_samp; // nilai rata-rata suhu_total =(suhu_rata * 0.48825125)/10;
gas_total =(gas_rata * 0.0048825125)*100; lcd_clear();
sprintf(buflcd,"suhu:%2.2f C",suhu_total); lcd_gotoxy(0,0);lcd_puts(buflcd);
sprintf(buflcd,"gas:%2.2f %",gas_total); lcd_gotoxy(0,1);lcd_puts(buflcd);
printf("%dA%dB%dC%dD%dE",suhu_total,gas_total,0,0,0); input=getchar();
if(gas_total<500) OCR1A=1600; //tutup ventilasi else if(gas_total>=500) OCR1A=3250;//buka ventilasi if(input=='6') kipas=1;//hidup ventilasi
else if(input=='7') kipas=0;//tutup ventilasi }
• Pengujian Rangkaian Mikrokontroller ATMega8535 Programnya adalah sebagai berikut :
/***************************************************************** ***
Moving LED
CodeVisionAVR C Compiler Chip: ATMega8535
Memory Model: SMALL Data Stack Size: 128 bytes
8 LEDs are connected between the PORTC outputs and +5V using 1K current
limiting resistors .The LEDs anodes are connected to +5V
****************************************************************** **/
// I/O register definitions for ATMega8535 #include <mega8535.h>
// quartz crystal frquency [Hz] #define xtal 12000000
// moving LED frequency [Hz] #define fmove 2
// the LED on PORTC output 0 will be on unsigned char led_status=0xfe;
// TIMER1 overflow interrupt service routine // occurs every 0.5 seconds
interrupt [TIM1_OVF] void timer1_overflow(void) {
// preset again TIMER1
TCNT1=0x10000-(xtal/1024/fmove); // move the LED
if (led_status==0xff) led_status=0xfe; // turn on the LED
PORTC=led_status; }
void main(void) {
// set the I/O ports
// all PORTC pins are outputs DDRC=0xff;
// turn on the first LED PORTC=led_status; // init TIMER1
// TIMER1 is disconnected from pin OC1 // no PWM
TCCR1A=0;
// TIMER1 clock is xtal/1024 TCCR1B=5;
// preset TIMER1
TCNT1=0x10000-(xtal/1024/fmove); // clear TIMER1 interrupts flags TIFR=0;
// enable TIMER1 overflow interrupt TIMSK=0x80;
// all other interrupt sources are disabled GIMSK=0;
// global enable interrupts #asm
sei #endasm
// the rest is done by TIMER1 overflow interrupts while (1);
• Program yang diisikan ke mikrokontroller untuk menampilkan karakter pada display LCD adalah sebagai berikut:
#include <mega8535.h>
// Alphanumeric LCD Module functions
#include <alcd.h>
void main(void)
{
PORTA=0x00;
DDRA=0x00;
PORTB=0x00;
DDRB=0x00;
PORTC=0x00;
DDRC=0x00;
PORTD=0x00;
DDRD=0x00;
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
ASSR=0x00;
TCCR2=0x00;
OCR2=0x00;
MCUCR=0x00;
MCUCSR=0x00;
TIMSK=0x00;
ACSR=0x80;
SFIOR=0x00;
lcd_init(16);
lcd_clear();
while (1)
{
// Place your code here
lcd_gotoxy(6,0);
lcd_putsf("FISIKA"); delay_ms(500);
• Pengujian terhadap motor servo dilakukan dengan memberikan inputan pulsa melalui mikrokontroller yang telah dimasukkan program sebagai berikut :
#include <mega8535.h> #include <delay.h> #include <stdio.h> unsigned int i; void main(void) {
for (i=0;i<50;i++) {
PORTD.4=1; delay_ms(2); PORTD.4=0; delay_ms(20); }
delay_ms(1000);
for (i=0;i<50;i++) }