Contoh Code C Microcontroller AVR Membaca String Barcode Scanner

/*****************************************************
Date : 7/27/2015
Chip type : ATmega16
Clock frequency : 11.059200 MHz
*****************************************************/

#include <mega16.h>
#include <delay.h>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <stdlib.h>
#asm
//.equ __lcd_port=0x15 ;PORTC
//.equ __lcd_port=0x15 ;PORTC
.equ __lcd_port=0x1B ;PORTA
#endasm
#include <lcd.h>
#define PANJANGBARCODE 10

char barislcd=0;
unsigned int REGISTERS[50];
unsigned char data_in[15];
unsigned char databaca[15];
unsigned char data_count;
unsigned char ada_data;
unsigned int QtyOutput;
char lcd[25];
unsigned char indexarray=0;
char temdataArray[10][11];
//char balasan[]=”C=250″;
// Variables to hold current settings

void simpandiarray( char data[],unsigned char indek);
void bacaarray(char data[], char indek);
void process_command();
void simpankeregister(char strData[], unsigned char tempAddr) ;
// USART Receiver interrupt service routine
interrupt [USART_RXC] void usart_rx_isr(void)
{
data_in[data_count] = UDR;

// End of line!
if (data_in[data_count] == ‘\r’) // end of frame: ‘\n’ ‘\r’
{
data_in[data_count-1]=0x00; //data_in[] dijadikan string tambah 0x00 di akhir

if(data_count==12)
{
process_command() ;
}
else {
lcd_gotoxy(0,2);
lcd_putsf(“data eror “);
}
//clear_data_in();
data_count = 0;
return;
}
else
{
data_count++;
}
//============

}
// USART Transmitter buffer
#define TX_BUFFER_SIZE 8
char tx_buffer[TX_BUFFER_SIZE];
unsigned char tx_index,tx_counter;
// USART Transmitter interrupt service routine
interrupt [USART_TXC] void usart_tx_isr(void)
{
if (tx_counter)
{
–tx_counter;
UDR=tx_buffer[tx_index];
if (++tx_index ==8) tx_index=0;
};
}
void process_command()
{
char i,j,k,m;
//a b c d e f g h i j
//0 1 2 3 4 5 6 7 8 9
QtyOutput++ ;
delay_ms(50);
lcd_gotoxy(13,3);
itoa(QtyOutput,lcd);
lcd_puts(lcd);

simpandiarray(data_in,indexarray) ;

//display
bacaarray(databaca,indexarray);

lcd_gotoxy(0,barislcd);
lcd_puts(databaca);
delay_ms(50);
if(barislcd == 0){
lcd_gotoxy(11,barislcd+3);
lcd_putchar(0x20);
}
//=======================
if(barislcd > 0){
lcd_gotoxy(11,barislcd-1);
lcd_putchar(0x20);
}
lcd_gotoxy(11,barislcd);
lcd_putsf(“*”);
delay_ms(50);
//=======================

barislcd++;
if(barislcd==4) {
barislcd=0;
}

delay_ms(50);
lcd_gotoxy(13,2);
itoa(indexarray*5,lcd);
lcd_puts(lcd);

if(indexarray==9) {
indexarray=0;
lcd_gotoxy(13,2);
lcd_putchar(0x20);
lcd_gotoxy(14,2);
lcd_putchar(0x20);
lcd_gotoxy(13,2);
itoa(indexarray*5,lcd);
lcd_puts(lcd);
lcd_puts(lcd);
}
indexarray++;

}

void simpandiarray( char data[], unsigned char indek)
{
char i;
{
for(i=0;i<PANJANGBARCODE;i++)
temdataArray[indek,i]= data[i];
}
data[10]=0x00;
simpankeregister(data,(PANJANGBARCODE/2)*indek); // alamat 0,5,10,15,20

}

void bacaarray(char data[], char indek)
{
char i;
for(i=0;i<PANJANGBARCODE;i++){
data[i] =temdataArray[indek,i];
}
data[10]=0x00;
}
void main(void)
{

PORTA=0x00;
DDRA=0x00;

PORTB=0x00;
DDRB=0xff;

PORTC=0x00;
DDRC=0x00;

PORTD=0x37; //0011 0111
DDRD=0xc8; //1100 1000

// USART initialization
// Communication Parameters: 8 Data, 1 Stop, No Parity
// USART Receiver: On
// USART Transmitter: On
// USART Mode: Asynchronous
// USART Baud rate: 9600
UCSRA=0x00;
UCSRB=0xD8;
UCSRC=0x86;
UBRRH=0x00;
UBRRL=0x47; //9600
//UBRRL=0x0B; //57600
//UBRRL=0x05; // 115200
PORTD.3=0;
// LCD module initialization
lcd_init(16);

lcd_gotoxy(0,0);
lcd_putsf(“barcode buffer”);
delay_ms(1000);
lcd_clear();
// Global enable interrupts
#asm(“sei”)

while (1)
{

}
}
//=============function declaration ==========================

//simpan 1 string barcode ke 5 register ab cd ef gh ij
void simpankeregister(char strData[],unsigned char tempAddr)
{
char i;
unsigned int tempData=0;

for(i=0;i<PANJANGBARCODE/2;i++,tempAddr++)
{
tempData = strData[(i*2)];
tempData = tempData <<8;
tempData = tempData | strData[i*2+1]; //
REGISTERS[tempAddr] = tempData;
}
}

Advertisements

About pccontrol

Berisi Tutorial Menggunakan PC untuk mengontrol Peralatan dengan cara mudah & praktis.

Posted on 22/12/2015, in AVR, Contoh-contoh Aplikasi, Daftar isi. Bookmark the permalink. Leave a comment.

Komentar ,Saran atau Pertanyaan

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: