Data Acquisition System Using ATmega8

Embed Size (px)

Citation preview

  • 8/9/2019 Data Acquisition System Using ATmega8

    1/24

    Data Acquisition System using

    ATmega8

    Under the Guidance of

    Mrs. Neeta Mehta Mam

    &

    Ms.

    Sunil S Pillai

    Department of Electronics & Communications

    Government Engineering College, Sector-26Gandhinagar

    October 2008

  • 8/9/2019 Data Acquisition System Using ATmega8

    2/24

    ACKNOWLEDGEMENT

    Before we get into thick of the thing, I would like to add a few heartfelt words

    for the people who have been behind this document.

    First of all, few words for the Almighty God for showering his choicest

    blessings on me and enabling me to complete this Project report well in time and with

    desired quality.

    I would like to voice my sincere thanksgiving to Mrs. Neeta Maam Head of

    Electronics and Communication Engineering Department and to Ms., Subject In

    charge, for providing us with such a high potential opportunity. I hope I have utilized

    it to the best of your expectation.

    In continuance, I would like to mention about my parents. Thank you Mom

    and Dad for your continued encouragement and guidance.

    Finally I would like to express my gratitude to all my classmates for their

    constant feedback and support in improving this document and making it more reader

    friendly. Lastly I do not intend to forget all the people who painstakingly read the

    entire manuscript and provided valuable inputs to make it free from errors.

    Thanking You All.

    2

  • 8/9/2019 Data Acquisition System Using ATmega8

    3/24

    INDEX

    1) Introduction 4

    2) Project Description

    3) Hardware Section

    4) Circuit Diagram 5

    5) Parts List

    6) Microcontroller Program 7

    7) Software Section

    8) PC-Board HandShake

    9) Appendix A: AVR Source Code

    10) Appendix B: AVR HEX FILE

    11) Appendix C: Datasheet 8

    12) Applications 15

    13) Bibliography 16

    3

  • 8/9/2019 Data Acquisition System Using ATmega8

    4/24

    INTRODUCTION

    We can use a PC for connecting the homemade data acquisition hardware and

    produce the GUI for user friendly graphical presentation easily.

    4

  • 8/9/2019 Data Acquisition System Using ATmega8

    5/24

    PROJECT DESCRIPTION

    In this project we use a PC for GUI display and the homemade data converter

    board using the AVR microcontroller. The circuit is built with the ATmega8. The

    chip has the on chip 10-bit resolution A/D Converter. Temperature sensor are

    connected to the 6 channels ADC input. You can connect up to 8 sensors with the

    ATmega8 TQFP package. The analog inputs are captured by sending a command

    from PC. The readings will display graphically or text data. The data can also berecorded as the database.

    5

  • 8/9/2019 Data Acquisition System Using ATmega8

    6/24

    HARDWARE

    The complete hardware schematic for the ADC board is shown below. The

    board consists of the ATmega8, the temperature sensor (LM335), and the serial

    interface. The on chip firmware controls reading the ADC converter, receives

    commands from PC and sends the reading to PC. The microcontroller is running with

    8MHz crystal oscillator, if we use the internal RC oscillator, the external crystal may

    be omitted. The ATmega8 DIP version has 6 channels analog input. If you need up to

    8 ADC input, you can use ATmega8 in TQFP package, or you can use another AVR

    microcontroller with the on-chip ADC.

    We use LM335 temperature sensor. It can measure the temperature using two

    wires remotely. The chip produces an output signal that is proportional to the Celsius

    temperature scale. The trimmer POT is used to calibrate the correct output voltage at

    known temperature, for example 2.98V @ 25 C.

    6

  • 8/9/2019 Data Acquisition System Using ATmega8

    7/24

    CIRCUIT DIAGRAM

    .

    7

    Temperatu

    reLoggerhardwaresc

    hematic

  • 8/9/2019 Data Acquisition System Using ATmega8

    8/24

    PARTS LIST

    The circuit is built around Atmega8 and a max232 serial Interface.

    It works off a 12V DC, 500mA power supply. You can also power the circuitfrom mains by using a 12V DC, 500mA adaptor in place of the 12V DC power supply.

    Semiconductors

    U1 Atmega8 Microcontrroller U2 MAX232 Serial Controller

    T1 LM335A Temperature Sensor

    Resistors(All - Watt +- 5% Carbon unless stated otherwise )

    R1, 1 Kilo Ohm

    R2, 10 Kilo Ohm

    Capacitors

    C1, C2, C3, C4 1 uF Ceramic Disc

    C5 10 uF

    C6,C7,C8 100 nF

    C9,C10 22 pF

    Miscellaneous

    LED Red LEDL Inductor 10 uH

    X Crystal, 8Mhz

    S1 Push Button Switch

    DB9 Connector

    8

  • 8/9/2019 Data Acquisition System Using ATmega8

    9/24

    MICROCONTROLLER PROGRAM

    The AVR program was written in assembly language. It can be fed into AVR

    Studio to generate the Hex File. The Detailed Source Code has been Listed in

    Appendix-A. The Generated Hex File is given in Appendix-B. In case of non

    availability of Cross Compilers, the Hex file can directly be loaded into Atmega8.

    The main code running waits the serial command from PC. The speed is set

    to19200 8n1 format. If the board receives character '>', the AVR must echo the

    character '>' to the PC. This step is used to check the hardware is connected or not.

    Another command is 'r' (read), the microcontroller reads 8 channel ADC and sends

    them to PC serially.

    If you have only 6 channel, for last channel (ADC6 and ADC7), you can

    modify the source code so the microcontroller will send a dummy value. These values

    will be ignored by PC software. Or if you want to ignore another channel from PC

    logging, just fill ADC values with dummy into their field. The descriptions have been

    commented in the source code. You may modify the source code easily.

    The main loop has been given below for a close look at logic:

    mainloop: sbis USR,RXC ;test RXC bit

    rjmp mainloop ;none available, wait

    in mpr,UDR

    cpi mpr,'>'brne _cmdr

    rcall txchar

    rjmp mainloop

    _cmdr: cpi mpr,'r'

    brne mainloop

    readadc: cbi PORTD,4

    ldi mpr,13

    rcall txchar

    ldi mpr,'D'

    rcall txchar

    9

  • 8/9/2019 Data Acquisition System Using ATmega8

    10/24

    _readadc0: ldi mpr,0b01000000 ;set ADMUX int ref ch0

    out ADMUX,mpr

    rcall readadcave

    rcall sendvalue

    _readadc1: ldi mpr,0b01000001 ;set ADMUX int ref ch1

    out ADMUX,mpr

    rcall readadcave

    rcall sendvalue

    . . . . . . .

    . . . . . . .

    . . . . . . .

    . . . . . . .

    _readadc5: ldi mpr,0b01000101 ;set ADMUX int ref ch5out ADMUX,mpr

    rcall readadcave

    rcall sendvalue

    _readadc6: ldi mpr,0b01000110 ;set ADMUX int ref ch6

    out ADMUX,mpr

    rcall readadcave

    ldi mpr,0xFF ;these 4 lines to disable ADC6

    mov rbin1H,mpr ;(in DIP version)

    ldi mpr,0xFF ;ADC6 = 0xFFFF

    mov rbin1L,mpr ;dummy value

    rcall sendvalue

    _readadc7: ldi mpr,0b01000111 ;set ADMUX int ref ch7

    out ADMUX,mpr

    rcall readadcave

    ldi mpr,0xFF ;these 4 lines to disable ADC7

    mov rbin1H,mpr ;(in DIP version)

    ldi mpr,0xFF ;ADC7 = 0xFFFF

    mov rbin1L,mpr ;dummy value

    rcall sendvalue

    sbi PORTD,4

    rjmp mainloop

    adcparstr: .db "ADC parameter :",13,10,0

    10

  • 8/9/2019 Data Acquisition System Using ATmega8

    11/24

    SOFTWARE

    A Graphical User Interface (GUI) Application has been build to operate the

    Board from PC. After Connecting the Hardware to Serial Port, the application can beinvoked to accept the incoming Data.

    Features of the PC Logger software are as follows.

    Adjustable sampling interval,

    Real time view in single graph or multiple graph,

    Record the data, save, and open the recorded database,

    Get statistically reading, print the record and graph (or export it as the

    metafile, JPG),

    Serial control window for monitoring and manually data transfer.

    The full version PC software is designed for capturing up to 16 channels. In this

    version, the PC software will read 8 channels. For the ATmega8 in DIP version, it can

    display only 6 channels.

    11

  • 8/9/2019 Data Acquisition System Using ATmega8

    12/24

    The PC Logger GUI has been build with Borland C++ Builder 4.

    dll for BDE, Borland Database Engine

    - bantam.dll

    - idapi32.dll

    - idbat32.dll

    - idpdx32.dll

    - idr20009.dll

    - idsql32.dll

    Core BDE Files

    --------------

    IDASCI32.DLL For accessing ASCII files

    IDBAT32.DLL For batch movement of data

    IDDA3532.DLL For accessing Microsoft Access databases

    IDDAO32.DLL For accessing Microsoft Access databases

    IDDBAS32.DLL For accessing dBASE databases

    IDDR32.DLL For Data Repository (Paradox only)

    IDODBC32.DLL For BDE access to ODBC drivers

    IDPDX32.DLL For accessing Paradox databases

    IDQBE32.DLL QBE query engine

    IDR20009.DLL BDE resources

    IDAPI32.DLL Main BDE system DLL

    IDSQL32.DLL SQL query engine (including local SQL)

    IDPROV32.DLL For MIDAS providers

    Language Driver Files

    ---------------------

    BANTAM.DLL Internationalization engine

    *.CVB Character set conversion files

    *.BTL Locales

    BLW32.DLL Expression engine

    12

  • 8/9/2019 Data Acquisition System Using ATmega8

    13/24

    SCREENSHOT

    13

    Mainscre

    enofDataAcquisitionp

    rogr

    am

  • 8/9/2019 Data Acquisition System Using ATmega8

    14/24

    PC-BOARD HANDSHAKE

    Data transferring between ADC board and PC uses the following protocol .

    For the first time connecting, after opening serial port, PC tries to connect the

    ADC board. The PC send a character '>' (0x3E) and the board will echo it

    back.

    After the connection has been established, we can send the command to the

    ADC board. For command read, PC sends character 'r' (0x72), and the board

    responses with ADC reading as: '\rD' + 2byte(ADC0) + 2byte(ADC1) + +

    2byte(ADC7), total 18 bytes length. The reading is 16-bit data, (10-bit

    resolution) with high byte send first followed with low byte.

    Note :

    For real time recording using fast sampling, for example 50ms or 100ms, better view

    in a single graph. The display performance depends on the PC speed.

    I tested running with 700MHz CPU speed, 128MB RAM, it gives smooth view but

    for multiple view, it could not display fast enough.

    14

  • 8/9/2019 Data Acquisition System Using ATmega8

    15/24

    APENDIX A

    MICROCONTROLLER SOURCE CODE

    ;-------------------------------------------------------------------------; Read ADC channel

    ;

    ;-------------------------------------------------------------------------

    .nolist

    .include "m8def.inc"

    .list

    .def rlpm = R0

    .def rbin1L = R10

    .def rbin1H= R11

    .def rbin2L = R12

    .def rbin2H= R13

    .def mpr = R16

    .def var1 = R17

    .def var2 = R18

    .def var3 = R19

    ;constants

    .equ fq = 8000000 ;xtal frequency, CKSEL3..0 V_VVV

    .equ baud = 19200 ; baud rate

    .equ baudgen = (fq/ (16*baud))-1 ; baud divider

    ; Reset-/Interrupt Vector

    .CSEG

    .org 0x0000

    rjmp start

    ;------------------------------------------------------------------------------------

    ; Subroutine for string transfer

    txstr: sbis USR,UDRE ;wait Tx buff empty

    rjmp txstr

    lpm ;read next char from prog mem

    and rlpm,rlpm ;null = end of string

    brne txsend

    ret

    txsend: lpm ;read the same char again

    15

  • 8/9/2019 Data Acquisition System Using ATmega8

    16/24

    out UDR,rlpm ;Tx character read

    adiw ZL,1 ;point to next char in memory

    rjmp txstr

    ;-----------------------------------------------------------------------------------

    ;Transmit charactertxchar: sbis USR,UDRE

    rjmp txchar

    out UDR,mpr

    ret

    ;----------------------------------------------------------------------------------

    ;Receive character

    rxchar: sbis USR,RXC

    rjmp rxchar

    in mpr,UDR

    ret

    ;-------------------------------------------------------------------------------------

    ;Read ADC (var2 + 1) times and get average

    readadcave: sbi ADCSR,ADSC

    ldi var2,3

    _adcave1: sbis ADCSR,ADIF

    rjmp _adcave1

    sbi ADCSR,ADIF ;clear flag by setting bit

    in mpr,ADCL

    mov rbin2L,mpr

    in mpr,ADCH

    mov rbin2H,mpr

    _adcave2: sbi ADCSR,ADSC

    _adcave3: sbis ADCSR,ADIF

    rjmp _adcave3

    sbi ADCSR,ADIF ;clear flag by setting bit

    in mpr,ADCL

    mov rbin1L,mpr

    in mpr,ADCHmov rbin1H,mpr

    add rbin2L,rbin1L ;add adc(n)+adc(n-1)

    adc rbin2H,rbin1H

    dec var2

    brne _adcave2

    lsr rbin2H

    ror rbin2L

    lsr rbin2H

    ror rbin2L

    _adcend: mov rbin1H,rbin2H

    16

  • 8/9/2019 Data Acquisition System Using ATmega8

    17/24

    mov rbin1L,rbin2L

    ret

    ;-------------------------------------------------------------------------

    ;Start of program

    start: ldi mpr,low(RAMEND)out SPL,mpr

    ldi mpr,high(RAMEND)

    out SPH,mpr

    ldi mpr,baudgen ;baud generator

    out UBRRL,mpr ;set divider

    ldi mpr,0b00011000 ;enable TX and RX

    out UCR,mpr ;to UART Control Register

    ;PORTB IIOOOIII

    ldi mpr,0b00111000out DDRB,mpr

    ldi mpr,0b11000111 ;pull-up input

    out PORTB,mpr

    ;PORTC --IIIIII

    ldi mpr,0b00000000

    out DDRC,mpr

    ldi mpr,0b00000000 ;cut pull-up input

    out PORTC,mpr

    ;PORTD IIIOOOIO

    ldi mpr,0b00011101

    out DDRD,mpr

    ldi mpr,0b11100010 ;pull-up input

    out PORTD,mpr

    sbi PORTD,4

    ldi var1,2

    delaystartup: rcall delay500ms

    dec var1

    cpi var1,0brne delaystartup

    intro: ldi mpr,12

    rcall txchar

    ldi ZH,high(adcparstr*2)

    ldi ZL,low(adcparstr*2)

    rcall txstr

    ldi mpr,0b10000010 ;set ADC enable

    out ADCSR,mpr

    17

  • 8/9/2019 Data Acquisition System Using ATmega8

    18/24

    mainloop: sbis USR,RXC ;test RXC bit

    rjmp mainloop ;none available, wait

    in mpr,UDR

    cpi mpr,'>'

    brne _cmdrrcall txchar

    rjmp mainloop

    _cmdr: cpi mpr,'r'

    brne mainloop

    readadc: cbi PORTD,4

    ldi mpr,13

    rcall txchar

    ldi mpr,'D'

    rcall txchar

    _readadc0: ldi mpr,0b01000000 ;set ADMUX int ref ch0

    out ADMUX,mpr

    rcall readadcave

    rcall sendvalue

    _readadc1: ldi mpr,0b01000001 ;set ADMUX int ref ch1

    out ADMUX,mpr

    rcall readadcave

    rcall sendvalue

    _readadc2: ldi mpr,0b01000010 ;set ADMUX int ref ch2

    out ADMUX,mpr

    rcall readadcave

    rcall sendvalue

    _readadc3: ldi mpr,0b01000011 ;set ADMUX int ref ch3

    out ADMUX,mpr

    rcall readadcave

    rcall sendvalue

    _readadc4: ldi mpr,0b01000100 ;set ADMUX int ref ch4

    out ADMUX,mpr

    rcall readadcave

    rcall sendvalue

    _readadc5: ldi mpr,0b01000101 ;set ADMUX int ref ch5

    out ADMUX,mpr

    rcall readadcave

    rcall sendvalue

    _readadc6: ldi mpr,0b01000110 ;set ADMUX int ref ch6

    18

  • 8/9/2019 Data Acquisition System Using ATmega8

    19/24

    out ADMUX,mpr

    rcall readadcave

    ldi mpr,0xFF ;these 4 lines to disable ADC6

    mov rbin1H,mpr ;(in DIP version)

    ldi mpr,0xFF ;ADC6 = 0xFFFF

    mov rbin1L,mpr ;dummy valuercall sendvalue

    _readadc7: ldi mpr,0b01000111 ;set ADMUX int ref ch7

    out ADMUX,mpr

    rcall readadcave

    ldi mpr,0xFF ;these 4 lines to disable ADC7

    mov rbin1H,mpr ;(in DIP version)

    ldi mpr,0xFF ;ADC7 = 0xFFFF

    mov rbin1L,mpr ;dummy value

    rcall sendvalue

    sbi PORTD,4

    rjmp mainloop

    adcparstr: .db "ADC parameter :",13,10,0

    ;--------------------------------------------------------------------------------------------

    ;ASCII to 2HEX

    ascii2hex: push mpr

    push mpr

    swap mpr

    andi mpr,0x0F

    cpi mpr,10 ;nibble > 9?

    brcs _ascii2hex1 ;no

    ldi var1,7 ;add 7 to get hex A to F

    add mpr,var1

    _ascii2hex1: ldi var1,'0'

    add mpr,var1

    rcall txcharpop mpr

    andi mpr,0x0F

    cpi mpr,10 ;nibble > 9?

    brcs _ascii2hex2 ;no

    ldi var1,7 ;add 7 to get hex A to F

    add mpr,var1

    _ascii2hex2: ldi var1,'0'

    add mpr,var1

    rcall txchar

    pop mpr

    ret

    19

  • 8/9/2019 Data Acquisition System Using ATmega8

    20/24

    ;------------------------------------------------------------------------------------------

    ;Delay 500ms @8mhz

    delay500ms: push R23

    push R24

    push R25ldi R23,0x6B

    _wg500loop0: ldi R24,0x46

    _wg500loop1: ldi R25,0xB1

    _wg500loop2: dec R25

    brne _wg500loop2

    dec R24

    brne _wg500loop1

    dec R23

    brne _wg500loop0

    pop R25

    pop R24pop R23

    ret

    ;--------------------------------------------------------------------------------------------

    ;Bin2ToBCD5

    ;converts a 16-bit-binary to a 5-digit-BCD

    ;in rbin1H:rbin1L

    sendvalue: mov mpr,rbin1H

    rcall txchar

    mov mpr,rbin1L

    rcall txchar

    ret

    ;sendvalue:

    bin2tobcd5: push rbin1H ;save number

    push rbin1L

    ldi mpr,high(10000) ;start with 10.000

    mov rbin2H,mpr

    ldi mpr,low(10000)

    mov rbin2L,mprrcall bin2todigit ;calculate digit

    ldi mpr,high(1000) ;next with 1.000

    mov rbin2H,mpr

    ldi mpr,low(1000)

    mov rbin2L,mpr

    rcall bin2todigit ;calculate digit

    rcall txchar

    ldi mpr,high(100) ;next with 100

    mov rbin2H,mpr

    20

  • 8/9/2019 Data Acquisition System Using ATmega8

    21/24

    ldi mpr,low(100)

    mov rbin2L,mpr

    rcall bin2todigit ;calculate digit

    rcall txchar

    ldi mpr,high(10) ;next with 10mov rbin2H,mpr

    ldi mpr,low(10)

    mov rbin2L,mpr

    rcall bin2todigit ;calculate digit

    rcall txchar

    mov mpr,rbin1L ;remainder are ones

    ori mpr,0x30

    rcall txchar

    pop rbin1L

    pop rbin1H

    ret

    ;-----------------------------------------------------------------------------------

    ;Bin2ToDigit

    ;converts one decimal digit by continued subtraction of a BCD

    bin2todigit: clr mpr ;digit count is zero

    _bin2todigita: cp rbin1H,rbin2H ;number bigger than decimal?

    brcs _bin2todigitc ;MSB smaller than decimal

    brne _bin2todigitb ;MSB bigger than decimal

    cp rbin1L,rbin2L ;LSB bigger or equal decimal

    brcs _bin2todigitc ;LSB smaller than decimal

    _bin2todigitb: sub rbin1L,rbin2L ;subtract LSB decimal

    sbc rbin1H,rbin2H ;subtract MSB decimal

    inc mpr ;increment digit count

    rjmp _bin2todigita ;next loop

    _bin2todigitc: ori mpr,0x30

    ret

    ;end

    21

  • 8/9/2019 Data Acquisition System Using ATmega8

    22/24

    APPENDIX B

    HEX FILE

    This is the Assembled Hex File. In case of non availability of Cross Compilers, this

    Hex file can be directly loaded into Microcontroller.

    :020000020000FC

    :100000002EC05D9BFECFC895002009F40895C895C9

    :100010000CB83196F6CF5D9BFECF0CB908955F9B6F

    :10002000FECF0CB10895369A23E0349BFECF349A6C

    :1000300004B1C02E05B1D02E369A349BFECF349A2F

    :1000400004B1A02E05B1B02ECA0CDB1C2A95A1F775

    :10005000D694C794D694C794BD2CAC2C08950FE5C4:100060000DBF04E00EBF09E109B908E10AB908E3D0

    :1000700007BB07EC08BB00E004BB00E005BB0DE1DB

    :1000800001BB02EE02BB949A12E061D01A951030C7

    :10009000E1F70CE0C0DFF1E0E0E1B3DF02E806B930

    :1000A0005F9BFECF0CB10E3311F4B5DFF9CF0237F1

    :1000B000B9F794980DE0AFDF04E4ADDF00E407B9D1

    :1000C000B2DF55D001E407B9AEDF51D002E407B981

    :1000D000AADF4DD003E407B9A6DF49D004E407B98D

    :1000E000A2DF45D005E407B99EDF41D006E407B999

    :1000F0009ADF0FEFB02E0FEFA02E39D007E407B92B

    :1001000092DF0FEFB02E0FEFA02E31D0949AC8CF10:1001100041444320706172616D65746572203A0DCF

    :100120000A000F930F9302950F700A3010F017E03A

    :10013000010F10E3010F6FDF0F910F700A3010F005

    :1001400017E0010F10E3010F66DF0F9108957F9311

    :100150008F939F937BE686E491EB9A95F1F78A95CE

    :10016000D9F77A95C1F79F918F917F9108950B2DC3

    :1001700052DF0A2D50DF0895BF92AF9207E2D02ED2

    :1001800000E1C02E18D003E0D02E08EEC02E13D010

    :1001900042DF00E0D02E04E6C02E0DD03CDF00E0B0

    :1001A000D02E0AE0C02E07D036DF0A2D006333DFE1

    :1001B000AF90BF9008950027BD1438F011F4AC142F

    :0E01C00020F0AC18BD080395F7CF006308953A

    :00000001FF

    22

  • 8/9/2019 Data Acquisition System Using ATmega8

    23/24

    APPENDIX C

    DATASHEETS

    23

  • 8/9/2019 Data Acquisition System Using ATmega8

    24/24

    BIBILOGRAPHY