Académique Documents
Professionnel Documents
Culture Documents
ATmega8
Sunil S Pillai
Department of Electronics & Communications
Government Engineering College, Sector-26
Gandhinagar
October 2008
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.
2
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
12) Applications … … … … … … 15
13) Bibliography … … … … … … 16
3
INTRODUCTION
We can use a PC for connecting the homemade data acquisition hardware and
produce the GUI for user friendly graphical presentation easily.
4
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 be
recorded as the database.
5
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
.
CIRCUIT DIAGRAM
7
Temperature Logger hardware schematic
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 circuit
from 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
Capacitors
C1, C2, C3, C4 1 uF Ceramic Disc
C5 10 uF
C6,C7,C8 100 nF
C9,C10 22 pF
Miscellaneous
LED Red LED
L Inductor 10 uH
X Crystal, 8Mhz
S1 Push Button Switch
DB9 Connector
8
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:
in mpr,UDR
cpi mpr,'>'
brne _cmdr
rcall txchar
rjmp mainloop
_cmdr: cpi mpr,'r'
brne mainloop
9
_readadc0: ldi mpr,0b01000000 ;set ADMUX int ref ch0
out ADMUX,mpr
rcall readadcave
rcall sendvalue
sbi PORTD,4
rjmp mainloop
10
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 be
invoked to accept the incoming Data.
11
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
12
SCREENSHOT
13
Main screen of Data Acquisition program
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
APENDIX A
MICROCONTROLLER SOURCE CODE
;-------------------------------------------------------------------------
; Read ADC channel
;
;-------------------------------------------------------------------------
.nolist
.include "m8def.inc"
.list
.def rlpm = R0
;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
out UDR,rlpm ;Tx character read
adiw ZL,1 ;point to next char in memory
rjmp txstr
;-----------------------------------------------------------------------------------
;Transmit character
txchar: 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
16
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,0b00111000
out 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,0
brne delaystartup
17
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
18
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
sbi PORTD,4
rjmp mainloop
;--------------------------------------------------------------------------------------------
;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 txchar
pop 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
;------------------------------------------------------------------------------------------
;Delay 500ms @8mhz
delay500ms: push R23
push R24
push R25
ldi 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 R24
pop R23
ret
;--------------------------------------------------------------------------------------------
;Bin2ToBCD5
;converts a 16-bit-binary to a 5-digit-BCD
;in rbin1H:rbin1L
;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,mpr
rcall bin2todigit ;calculate digit
20
ldi mpr,low(100)
mov rbin2L,mpr
rcall bin2todigit ;calculate digit
rcall txchar
;-----------------------------------------------------------------------------------
;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
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
APPENDIX C
DATASHEETS
23
BIBILOGRAPHY
24