Codingan Pengendali Panel Surya Berbasis Atmega328

Berikut adalah codingan dari pengendali panel surya berbasis Atmega328:

#include <mega8.h>
#include <delay.h>
#asm.equ __lcd_port=0x12 ;PORTD // Alphanumeric LCD Module functions
#endasm
#include <lcd.h>
#define ADC_VREF_TYPE 0x40
{
ADMUX=adc_input | (ADC_VREF_TYPE & 0xff); // Read the ADC conversion result unsigned int read ADC (unsigned char adc_input)
delay_us(10); // Delay needed for the stabilization of the ADC input voltage
ADCSRA|=0x40; // Start the AD conversion
while ((ADCSRA & 0x10)==0); // Wait for the AD conversion to complete
ADCSRA|=0x10;
return ADCW;
}
ldrPin 0; // Declare your global variables here
#define ls PINB.0; //limit switch
//kaki motor steper = arduino
//#define ms1 PORTB.4;  // kuning
//#define ms2 PORTB.5;  // merah
//#define ms3 PORTB.6;  // biru
//#define ms4 PORTB.7;  // hijau
//variable
int langkah=1;  //variable driver stepper motor
int posisi;     //variable posisi panel
float ldr;      //variable ldr
float ldrMax=1023;   //menyimpan data hasil pembacaan ldr terbesar
unsigned int posisiMax;  //menyimpan posisi panel dari hasil pembacaan ldr terbesar
bit saklar;
float derajat;
float tahanan;
float tegangan;
//fungsi eksekusi steper
void eksekusi(unsigned char x){
switch (x) {
case 1:
PORTB.4=0;
PORTB.5=1;
PORTB.6=1;
PORTB.7=1; break;
case 2:
PORTB.4=1;
PORTB.5=0;
PORTB.6=1;
PORTB.7=1; break;
case 3:
PORTB.4=1;
PORTB.5=1;
PORTB.6=0;
PORTB.7=1; break;
case 4:
PORTB.4=1;
PORTB.5=1;
PORTB.6=1;
PORTB.7=0; break;
default:       break;
}; }

//fungsi langkah steper
void stepper (char cw)
{
if(cw==0){if(langkah<4){langkah=langkah+1;}else{langkah=1;};}
else       {if(langkah>1){langkah=langkah-1;}else{langkah=4;};};
eksekusi(langkah);
delay_ms(100);
//fungsi baca ldr
void bacaLdr(){
ldr = read_adc(0);
delay_ms(100);
if(ldr<ldrMax){ldrMax=ldr;posisiMax=posisi;};
}
//fungsi scan######################################
void scan (){
int i;
unsigned int j;
for(i=1;i<=80;i++){
bacaLdr();
if(posisi<=80){posisi++;stepper(0);};
derajat= 18 + posisi*1.8;
tegangan=(5*ldr)/1024;
tahanan=tegangan*100/(5-tegangan);
lcd_gotoxy(2, 0);
lcd_putchar(tahanan);
lcd_gotoxy(2, 1);
lcd_putchar(derajat);
delay_ms(1000);
};
for(j=1; j<=80-posisiMax;j++){
stepper(1);
posisi=posisi-1;
derajat= 18 + posisi*1.8;
tegangan=(5*ldrMax)/1024;
tahanan=tegangan*100/(5-tegangan);
lcd_gotoxy(2, 0);
lcd_putchar(tahanan);
lcd_gotoxy(2, 1);
lcd_putchar(derajat);
delay_ms(100);
}
}
//fungsi putar mentok
void putarMentok()
{
saklar=ls;
while(saklar){
saklar=ls;
stepper(1);
delay_ms(50);
}
posisi=1;
}
void main(void)

{ //Tempat Mendeklarasikan variabel
// Reset Source checking
if (MCUCSR & 1)
{
// Power-on Reset
MCUCSR=0;
// Masukkan kode
}
else if (MCUCSR & 2)
{
// External Reset
MCUCSR=0;
// Masukkan kode
 }
else if (MCUCSR & 4)
{
// Brown-Out Reset
MCUCSR=0;
// Masukkan kode
}
else
{
// Watchdog Reset
MCUCSR=0;
// Masukkan kode
};
 // Input/Output Ports initialization
// Port B initialization
// Func7=Out Func6=Out Func5=Out Func4=Out Func3=In Func2=In Func1=In Func0=In
// State7=0 State6=0 State5=0 State4=0 State3=T State2=T State1=T State0=T
PORTB=0x00;
DDRB=0xF0;

// Port C initialization
// Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTC=0x00;
DDRC=0x00;
 // Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTD=0x00;
DDRD=0x00;
 // Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
TCCR0=0x00;
TCNT0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// 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=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
 // Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
OCR2=0x00; 
// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
MCUCR=0x00; 
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
ADMUX=ADC_VREF_TYPE & 0xff;
ADCSRA=0x81;
 // LCD module initialization
lcd_init(16);
while (1)
{
// Masukkan kode
lcd_gotoxy(0, 0);        //set crusor posisi baris 0 colom 0
lcd_putsf(“Mencari”);
lcd_gotoxy(0, 1);
lcd_putsf(“posisi 1”);
saklar=ls;delay_ms(10); //baca limit switch (ls) dan tunggu 10 mS
if(saklar==1){putarMentok();}    //jika ls terbuka putar ms mentok posisi awal 
//jika sudah mencapai posisi awal
lcd_clear();
lcd_gotoxy(0, 0);
lcd_putsf(“Horree..”);
lcd_gotoxy(0, 1);
lcd_putsf(“Berhasil”);
delay_ms(3000);
lcd_clear();

lcd_gotoxy(0, 0);
lcd_putsf(“R=”);
lcd_gotoxy(0, 1);
lcd_putsf(“P=”);
 scan();  //scan posisi matahari
while(posisi<=80){
delay_ms(7.2*60*1000);
stepper(0);
posisi=posisi+1;
bacaLdr();

derajat= 18 + posisi*1.8;
tegangan=(5*ldr)/1024;
tahanan=tegangan*100/(5-tegangan);

lcd_gotoxy(2, 0);
lcd_putchar(tahanan);
lcd_gotoxy(2, 1);
lcd_putchar(derajat);
};
putarMentok();
//tunggu pagi
while(ldr<880){delay_ms(60000);bacaLdr();};
};
}

Komentar

Postingan populer dari blog ini

Flowchart Pengendali Panel Surya berbasis Atmega328

Contoh Root Locus dan penyelesaiannya

Contoh Kasus IRR dan Kasus NPV