LAMPIRAN A
( Lanjutan )Pada gambar B di bawah ini merupakan suatu form monitoring output perancangan solar tracker dual axis dapat di lihat pada form di bawah ini sebagai berikut :
LAMPIRAN B
Pada lampiran B ini terdapat 2 program,yaitu program yang ada di dalam mikrokontroller dengan menggunakan bahasa C sebagai pembaca data dari sensor LDR dan sensor Arus dan program VB yang di gunakan pada PC sebagai output data terakhir selanjutnya dapat di perhatikan program sebagai berikut :
Program Bahasa C ( Mikrokontroller )
/*****************************************************
This program was produced by the CodeWizardAVR V2.05.3 Standard Automatic Program Generator
© Copyright 1998-2011 Pavel Haiduc, HP InfoTech s.r.l. http://www.hpinfotech.com
Project : Version : Date : Author : iqbal
Company : ekstensi fisika USU Comments:
Chip type : ATmega8535 Program type : Application
AVR Core Clock frequency: 11,059200 MHz Memory model : Small
External RAM size : 0 Data Stack size : 128
*****************************************************/
#include <mega8535.h> #include <delay.h>
// I2C Bus functions #include <i2c.h>
// DS1307 Real Time Clock functions #include <ds1307.h>
// Alphanumeric LCD functions #include <alcd.h>
// Standard Input/Output functions #include <stdio.h>
unsigned int adc_data;
#define ADC_VREF_TYPE 0x40 #define ADC_samp 20
/* //komentar, tidak di eksekusi // ADC interrupt service routine
interrupt [ADC_INT] void adc_isr(void) {
// Read the AD conversion result adc_data=ADCW;
}
// Read the AD conversion result // with noise canceling
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); #asm in r30,mcucr cbr r30,__sm_mask sbr r30,__se_bit | __sm_adc_noise_red out mcucr,r30 sleep cbr r30,__se_bit out mcucr,r30 #endasm return adc_data; } */
// Read the AD conversion result //rutin membaca adc
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 unsigned char x,y,buflcd[33];
unsigned char alamatI2C,temp1,temp2,temp3,temp4;
unsigned int intTOTAL, intADC, posx1, posx2, posy1, posy2; //float flHASIL, flMEAN;
signed int flHASIL, flMEAN,pulse; int a,pulsa;
//fungsi membaca nilai adc LDR
unsigned LDR(char flash *namaLDR,unsigned int numadc,unsigned int posx,unsigned int posy) { intTOTAL = 0; flHASIL = 0; for (x=0; x<ADC_samp; x++) { intADC = read_adc(numadc); intTOTAL = intTOTAL + intADC;
delay_ms(10); // 50 x 10 mS = 500 mS = 0.5 detik }
flMEAN = intTOTAL / ADC_samp; // nilai rata-rata flHASIL = (flMEAN * 0.48825125);
// if (numadc==5) flHASIL=flHASIL-100; if (flHASIL<=0) flHASIL=0;
printf("=%d \r \n",flHASIL); return flHASIL;
delay_ms(10); }
fungsi membaca nilai adc arus dan tegangan pada solar cell
unsigned solar_cell(unsigned int numadc) { intTOTAL = 0; flHASIL = 0; for (x=0; x<ADC_samp; x++) { intADC = read_adc(numadc); intTOTAL = intTOTAL + intADC;
delay_ms(10); // 50 x 10 mS = 500 mS = 0.5 detik }
flMEAN = intTOTAL / ADC_samp; // nilai rata-rata flHASIL = (flMEAN * 0.48825125); if (flHASIL<=0) flHASIL=0; //--- printf("=%d \n \r",flHASIL); return flHASIL; delay_ms(10); }
void servo(unsigned char servoNum, unsigned int pulse, unsigned char rate) {
i2c_start(); // Start Condition
i2c_write(alamatI2C); // Tulis ke modul SPC SERVO CONTROLLER i2c_write(0x02); // Perintah “Run Servo Motor”
i2c_write(servoNum); // Nomor Servo
i2c_write(pulse/256); // Lebar pulsa kontrol (MSB) i2c_write(pulse%256); // Lebar pulsa kontrol (LSB) i2c_write(rate); // Rate
i2c_stop(); // Stop Condition
delay_us(15); // delay 4 ms
i2c_start(); // Start Condition
temp1 = i2c_read(0);// Data Acknowledgement i2c_stop(); // Stop Condition
}
unsigned int read(unsigned char servoNum) {i2c_start(); // Start Condition
i2c_write(0xE0); // Tulis ke modul SPC SERVO CONTROLLER i2c_write(0x07); // Perintah “Read Servo Position”
i2c_write(servoNum); // Nomor Motor Servo i2c_stop(); // Stop Condition
delay_us(25); // delay 25 us i2c_start(); // Start Condition
i2c_write(0xE1); // Baca ke modul SPC SERVO CONTROLLER temp1 = i2c_read(1);// Data Acknowledgement
temp2 = i2c_read(1);// Enable/Disable temp3 = i2c_read(1);// Pulsa Kontrol (MSB) temp4 = i2c_read(0);// Pulsa Kontrol (LSB) i2c_stop(); // Stop Condition
pulse =(temp3*256)+temp4; return pulse; printf("=%d \n \r",pulse); } void main(void) {
// Declare your local variables here
// Input/Output Ports initialization // Port A 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 PORTA=0x00;
DDRA=0x00;
// Port B 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 PORTB=0x00;
// Port C 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 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 // Mode: Normal top=0xFF // OC0 output: Disconnected TCCR0=0x00;
TCNT0=0x00; OCR0=0x00;
// Timer/Counter 1 initialization // Clock source: System Clock // Clock value: Timer1 Stopped // Mode: Normal top=0xFFFF // OC1A output: Discon. // OC1B output: Discon. // Noise Canceler: Off
// Input Capture on Falling Edge // Timer1 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: Timer2 Stopped // Mode: Normal top=0xFF // OC2 output: Disconnected ASSR=0x00;
TCCR2=0x00; TCNT2=0x00; OCR2=0x00;
// External Interrupt(s) initialization // INT0: Off
// INT1: Off // INT2: Off MCUCR=0x00; MCUCSR=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: 9600 UCSRA=0x00;
UCSRB=0x08; UCSRC=0x86; UBRRH=0x00; UBRRL=0x47;
// Analog Comparator initialization // Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off ACSR=0x80;
/*// ADC initialization
// ADC Clock frequency: 172,800 kHz // ADC Voltage Reference: AVCC pin // ADC High Speed Mode: Off
// ADC Auto Trigger Source: ADC Stopped ADMUX=ADC_VREF_TYPE & 0xff; ADCSRA=0x8E;
SFIOR&=0xEF;*/
// ADC initialization
// ADC Clock frequency: 86,400 kHz // ADC Voltage Reference: AVCC pin // ADC High Speed Mode: Off
// ADC Auto Trigger Source: Free Running ADMUX=ADC_VREF_TYPE & 0xff; ADCSRA=0xA7; SFIOR&=0x0F; // SPI initialization // SPI disabled SPCR=0x00; // TWI initialization // TWI disabled TWCR=0x00;
// I2C Bus initialization // I2C Port: PORTC // I2C SDA bit: 0 // I2C SCL bit: 1 // Bit Rate: 100 kHz
// Note: I2C settings are specified in the
// Project|Configure|C Compiler|Libraries|I2C menu. i2c_init();
// DS1307 Real Time Clock initialization // Square wave output on pin SQW/OUT: Off // SQW/OUT pin state: 0
// Alphanumeric LCD initialization // Connections are specified in the
// Project|Configure|C Compiler|Libraries|Alphanumeric LCD menu: // RS - PORTB Bit 0 // RD - PORTB Bit 1 // EN - PORTB Bit 2 // D4 - PORTB Bit 3 // D5 - PORTB Bit 4 // D6 - PORTB Bit 5 // D7 - PORTB Bit 6 // Characters/line: 16 lcd_init(16); alamatI2C = 0xE0; delay_ms(500); lcd_gotoxy(0,0); lcd_putsf("iqbal system"); lcd_gotoxy(0,1); lcd_putsf("solar_tracker"); lcd_clear();
a=b=600; //posisi awal motor servo servo(1,a,50);
delay_ms(1000); while (1)
{
solar_cell(0); //fungsi untk mendeteksi arus solar cell solar_cell(1); //fungsi untuk mendeteksi tegangan solar cell posx1=LDR("x1",4,0,0); //
posy1=LDR("y1",5,9,0); posy2=LDR("y2",6,9,1); posx2=LDR("x2",7,0,1);
//fungsi dibawah membandingkan nilai ldr x1-Vs-x2 dan ldr y1-vs-y2, jika posisi ldr sama, maka
//motor tidak akan bergerak
while((posy1==(posy2-10)|posy2==(posy1-10)& (posx1==(posx2-10)|posx2==(posx1-10)))){#asm("nop")};
//jika nilai ldr y1-vs-y2 tidak sama, maka motor akan bergerak if (posy1<posy2)
{ a=a+5;
servo(1,a,10); pulsa=read(1); if (pulsa >=2499) {a=700;servo(1,a,50);delay_ms(1500);} } if (posy2<posy1) { a=a-5; servo(1,a,10);*/ if (a<=600) {a=2449;servo(1,a,50);delay_ms(500);} }
//jika nilai ldr x1-vs-x2 tidak sama, maka motor akan bergerak if (posx1<posx2) { b=b+5; servo(2,b,10); pulsa=read(2); if (pulsa >=2499) {b=700;servo(2,b,50);delay_ms(1500);} } if (posx2<posx1) { b=b-5; servo(1,b,10); if (b<=600) {b=2449;servo(1,b,50);delay_ms(500);} } delay_ms(50); } //end while } //end main
Program Visual Basic (VB 6,0)
Program ini untuk menampilan tegangan, arus, daya dan posisi solar cell dalam text box yang terpisah
Private Sub cmdConnect_Click() Dim port As Integer
On Error GoTo errcode
If MSComm1.PortOpen = False Then MSComm1.CommPort = 9 MSComm1.RThreshold = 1 MSComm1.InputLen = 100 MSComm1.Settings = 9600 & ",N,8,1" MSComm1.PortOpen = True cmdConnect.Enabled = False cmdDisconnect.Enabled = True End If Exit Sub errcode:
MsgBox "Port Salah !", vbOKOnly, "Peringatan" End Sub
Private Sub cmdDisconnect_Click() If MSComm1.PortOpen = True Then MSComm1.PortOpen = False End If
cmdConnect.Enabled = True cmdDisconnect.Enabled = False End Sub
Private Sub Form_Load() Timer1.Enabled = False cmdConnect.Enabled = True cmdDisconnect.Enabled = False End Sub
Private Sub MSComm1_OnComm() Dim buffer As String
Dim temp As String Dim pisah() As String Dim i As Integer
If MSComm1.CommEvent = comEvReceive Then buffer = MSComm1.Input
pisah = Split(buffer, Chr$(13)) On Error Resume Next
If buffer <> " " Then With Text1 .SelStart = Len(.Text) .SelText = buffer End With For i = 0 To 2 If i < 3 Then Text2(i).Text = CStr(pisah(i)) End If Next i Else buffer = " " End If End If
Text2(3).Text = Val(Text2(0).Text) * Val(Text2(1).Text) „menampilkan daya End Sub
Private Sub Timer1_Timer() MSComm1_OnComm End Sub