Rancangan Pengolah Tampilan Dan Penyimpan Hasil Alat Timbang Berbasis Serat Optik Menggunakan Mikrokontroler ATMEGA32

  

LAMPIRAN A

Program

  #include <mega32.h> #include <stdio.h> #include <delay.h> #include "mmc.c" #if TINY_FAT #include "tff.c" #else #include "ff.c" #endif #include <delay.h> // DS1307 Real Time Clock functions #include <ds1307.h> // Alphanumeric LCD functions //#include <alcd.h> #asm .equ __lcd_port=0x15 // untuk port c #endasm /* now you can include the LCD Functions */ #include <lcd4x40.h> //variabel global #define RTC_ADDR 0xD0 char *day[7]={"Senin","Selasa","Rabu","Kamis","Jum'at","Sabtu","Minggu"}; char

  • month[12]={"Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","N ov","Dec"}; unsigned char hari, tanggal,bulan,tahun,jam,menit,detik; char text[16]; unsigned int j=1; float sum=0; bit start,stop=0; // External Interrupt 0 service routine interrupt [EXT_INT0] void ext_int0_isr(void) {

  // Place your code here start=1; stop=0; j=1; } // External Interrupt 1 service routine interrupt [EXT_INT1] void ext_int1_isr(void) { // Place your code here start=0; stop=1; sum=0; lcd_gotoxy(0,2); lcd_clear(); lcd_gotoxy(0,0); lcd_puts("SENSOR BEBAN BERBASIS FIBER OPTIK"); } #define ADC_VREF_TYPE 0x40 // 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; } char filename[15]="data/tes.csv"; //char filename[15]; char i,FBuffer[100]; char *file;; unsigned char data; char foldername[10]="data"; unsigned char dta[25],loop; unsigned int cobi; FRESULT report; unsigned char command,a,b,flag,sreg,bcd,cek; //char FBuffer[100]; //UINT bw; unsigned int bw; //unsigned long bw; FATFS fs; // Work area (file system object) for logical drive FIL ftest; // file objects FILINFO finfo; #ifndef RXB8 #define RXB8 1 #endif #ifndef TXB8 #define TXB8 0 #endif #ifndef UPE #define UPE 2 #endif #ifndef DOR #define DOR 3 #endif #ifndef FE #define FE 4 #endif #ifndef UDRE #define UDRE 5 #endif #ifndef RXC #define RXC 7 #endif #define FRAMING_ERROR (1<<FE) #define PARITY_ERROR (1<<UPE) #define DATA_OVERRUN (1<<DOR) #define DATA_REGISTER_EMPTY (1<<UDRE) #define RX_COMPLETE (1<<RXC) // USART Receiver buffer #define RX_BUFFER_SIZE 24 char rx_buffer[RX_BUFFER_SIZE]; #if RX_BUFFER_SIZE <= 256 unsigned char rx_wr_index,rx_rd_index,rx_counter; #else

unsigned int rx_wr_index,rx_rd_index,rx_counter; #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 // special case for receiver buffer size=256 if (++rx_counter == 0) rx_buffer_overflow=1; #else if (rx_wr_index == RX_BUFFER_SIZE) rx_wr_index=0; if (++rx_counter == RX_BUFFER_SIZE) { rx_counter=0; rx_buffer_overflow=1; } #endif } } #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 24 char tx_buffer[TX_BUFFER_SIZE]; #if TX_BUFFER_SIZE <= 256 unsigned char tx_wr_index,tx_rd_index,tx_counter; #else unsigned int tx_wr_index,tx_rd_index,tx_counter; #endif // USART Transmitter interrupt service routine 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

  • tx_counter; } else UDR=c; #asm("sei") } #pragma used-

  #endif void clear_buffer() { for (data=0;data<100;data++) { FBuffer[data]=0; } } /*---------------------------------------------------------*/ /* 100Hz timer interrupt generated by OC2 */ /*---------------------------------------------------------*/ interrupt [TIM2_COMP] void timer2_comp_isr(void) { disk_timerproc(); /* Drive timer procedure of low level disk I/O module */ } void remove() { report=0; do { report=f_unlink(filename); } while(report!=FR_OK); } void take_data(unsigned int len_data) { unsigned int len_data2; //FBuffer[0]=0; //for (len_data2=0;len_data2<len_data;len_data2++) //while (FBuffer[0]!='E' ) //while(1) //{ f_read(&ftest, FBuffer, strlen(FBuffer), &bw); //if (FBuffer[0]=='E') break; for (data=0;data<30;data++) { //delay_ms(100); putchar(FBuffer[data]);

  } putchar(0x0d); //} } void reading() { f_mount(0,&fs); report=f_open(&ftest,filename, FA_OPEN_EXISTING | FA_READ); if (report==FR_NO_PATH) { printf("NO PATH"); } else if (report==FR_NO_FILE) { printf("NO FILE"); } else { take_data(200); } f_close(&ftest); f_mount(0, NULL); } void mount_on() { f_mount(0,&fs); } void display_status(char *file_name) { if ((report=f_stat(file_name,&finfo))==FR_OK) { } } void mk_dir() { f_mkdir(foldername); } void mk_file() { report=0; do { report=f_open(&ftest,filename, FA_CREATE_ALWAYS | FA_WRITE); } while(report!=FR_OK); } //========================================================= === void isi_file(void) { unsigned char ulang; unsigned char status_tulis; status_tulis=0; //reset_filename(); //file_data(); //mk_dir(); //mk_file(); display_status(filename); for(ulang=0;ulang<10;ulang++) { //timer_mmc(); if ((report=f_open(&ftest,filename,FA_WRITE))==FR_OK)status_tulis=1; else status_tulis=0; if(status_tulis==1) { if ((report=f_lseek(&ftest,finfo.fsize))==FR_OK)status_tulis=1; else status_tulis=0; } if(status_tulis==1) { if ((report=f_write(&ftest,FBuffer,strlen(FBuffer),&bw))==FR_OK)status_tulis=1; else status_tulis=0; } if(status_tulis==1)

  { if ((report=f_close(&ftest))==FR_OK)status_tulis=1; else status_tulis=0; } if(status_tulis==1)break; delay_ms(500); } //stop_timer_mmc(); take_data(30); } //void write_data() //{ // report=0; // do // { // report=f_write(&ftest, FBuffer, strlen(FBuffer), &bw); // } // while(report!=FR_OK); //} void mount_off() { f_close(&ftest); f_mount(0, NULL); } DWORD get_fattime () { /* struct tm *tmr; time_t t; cli(); t = rtc; sei(); tmr = gmtime(&t); return ((DWORD)(tmr->tm_year - 80) << 25) | ((DWORD)(tmr->tm_mon + 1) << 21) | ((DWORD)tmr->tm_mday << 16) | (WORD)(tmr->tm_hour << 11) | (WORD)(tmr->tm_min << 5) | (WORD)(tmr->tm_sec >> 1);

  • / return ((2007UL-1980) << 25) // Year = 2006 | (7UL << 21) // Month = juli | (29UL << 16) // Day = 29 | (22U << 11) // Hour = 22

  | (30U << 5) // Min = 30 | (0U >> 1) // Sec = 0 ; } void main(void) { // Declare your local variables here unsigned int dt_adc; float rata_rata, v, BEBAN; char buf [33]; unsigned char second_akhir=0,time_transmit=0; // 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; DDRB=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 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; // 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 ADMUX=ADC_VREF_TYPE & 0xff; ADCSRA=0x83; // SPI initialization // SPI disabled SPCR=0x00; // TWI initialization // TWI disabled TWCR=0x00; OCR2 = 77-1; // Timer2: 100Hz interval (OC2) TCCR2 = 0b00001111; //CTC mode, TIMSK=0x80; DDRB=1; #asm("sei") PORTD=0x00; DDRD=0x04; UCSRA=0x00; UCSRB=0xD8; UCSRC=0x86; UBRRH=0x00; UBRRL=0x19; // I2C Bus initialization // I2C Port: PORTA // I2C SDA bit: 1 // I2C SCL bit: 0 // 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: On // Square wave frequency: 1Hz rtc_init(0,1,0); rtc_set_time(10,00,00); // jam 6:00:00 rtc_set_date(22,26,05,12); printf("serial init\n\r"); if(disk_initialize(0)==STA_NOINIT) { printf("MMC not init\n\r"); printf("PleaseInsertSDCard!!\r"); printf("RestartY'rInstrument\r"); while(1); // stop! if card was not present or was not detected currectly } //filename="data/awal.csv"; mount_on(); mk_dir(); mk_file(); //file_baru(); sprintf(FBuffer,"Tanggal, Waktu, Data\n"); //write_data(); isi_file(); //mount_off(); printf("done\n\r"); printf("Coba SD Card\r"); // Alphanumeric LCD initialization // Connections are specified in the // Project|Configure|C Compiler|Libraries|Alphanumeric LCD menu: // RS - PORTC Bit 0 // RD - PORTC Bit 1 // EN - PORTC Bit 2 // D4 - PORTC Bit 4 // D5 - PORTC Bit 5 // D6 - PORTC Bit 6 // D7 - PORTC Bit 7 // Characters/line: 40 lcd_init(); lcd_gotoxy(0,0); lcd_puts("SENSOR BEBAN BERBASIS FIBER OPTIK"); // Global enable interrupts #asm("sei") while(1) { // Place your code here rtc_get_date(&hari,&tanggal, &bulan, &tahun); rtc_get_time(&jam, &menit, &detik); lcd_gotoxy(0,1); sprintf(text,"%s/%02d/%s/%02d",day[hari],tanggal,month[bulan],tahun); lcd_puts(text); lcd_gotoxy(20,1); sprintf(text,"Jam: %02d/%02d/%02d",jam,menit,detik); lcd_puts(text); delay_ms(500); //lcd_clear(); if (start==1) { while (stop==0) { rtc_get_date(&hari,&tanggal, &bulan, &tahun); rtc_get_time(&jam, &menit, &detik); lcd_gotoxy(0,1); sprintf(text,"%s/%02d/%s/%02d",day[hari],tanggal,month[bulan],tahun); lcd_puts(text); lcd_gotoxy(20,1); sprintf(text,"Jam: %02d/%02d/%02d",jam,menit,detik); lcd_puts(text); delay_ms(500); //lcd_clear(); dt_adc=read_adc(0); v=0.0024265*(float)dt_adc; //PORTB=~dt_adc; lcd_gotoxy(0,2); sum = sum+v; rata_rata = (sum/j); sprintf(buf, "%i",j); lcd_puts(buf); lcd_gotoxy(4,2); sprintf(buf, "V = %.2f volt",v); lcd_puts(buf); lcd_gotoxy(20,2);

sprintf(buf, "R = %.2f volt", rata_rata); lcd_puts(buf); j++; delay_ms(500); } } lcd_gotoxy(0,3); BEBAN = (21.94*rata_rata*rata_rata*rata_rata)- 176.3*rata_rata*rata_rata)+

  (476.3*rata_rata )-328 ; sprintf(buf, "BEBAN = %.2f kg",BEBAN); lcd_puts(buf); rtc_get_time(&jam,&menit,&detik); if(second_akhir!=detik)time_transmit+=1; second_akhir=detik; if (time_transmit==60) { for (j=0;j<100;j++) { rtc_get_time(&jam,&menit,&detik); rtc_get_date(&hari,&tanggal, &bulan, &tahun); sprintf(FBuffer,"%02d/%02d/%02d/%02d, %02d:%02d:%02d, %d\n",hari,tanggal,bulan,tahun,jam,menit,detik,j); isi_file(); take_data(30); delay_ms(1000); } printf("selesai\n"); j=0; second_akhir=0; rtc_get_time(&jam,&menit,&detik); time_transmit=detik; } }; }