Chuong trinh dung AT mega 8535 do nhiet do ;Dung ATmega8535 do nhiet do bang LM35 tai cong ADC.0 - PA.0 ;Hien thi ket qua nhiet do bang 4 led 7 thanh ;cong PB (chon led) va PC (du lieu hien thi so) ; Chon thiet bi . .device AT90S8535 ; Dinh nghia cac cong va thanh ghi ; I/O Register Definitions .equ SREG =$3F .equ GIMSK =$3B .equ GIFR =$3A .equ TIMSK =$39 .equ TIFR =$38 .equ MCUCR =$35 .equ TCCR0 =$33 .equ TCNT0 =$32 .equ TCCR1 =$30 .equ TCNT1 =$2F .equ OCR1A =$2E .equ OCR1B =$2D .equ PORTA =$1B .equ DDRA =$1A .equ PINA =$19 .equ PORTB =$18 .equ DDRB =$17 .equ PORTC =$15 .equ DDRC =$14 .equ ACSR =$08 .equ ADMUX =$07 .equ ADCSR =$06 .equ ADCH =$05 .equ ADCL =$04 .equ SPDR =$0F ;Dinh nghia cac bien dung trong chuong trinh ; Variable Declarations .def temp = r16 .def isrsreg = r18 .def isrtemp1 = r19 .def isrtemp2 = r20 .def cseg = r21 .def seg0 = r22 .def seg1 = r23 .def seg2 = r24
.def seg3
= r25
;Dinh nghia dia chi ngat cua ADC .equ ADCCaddr=$00E .cseg
; CODE segment.
;Interrupt Vectors .org 0 rjmp init ;Reset .org ADCCaddr ;ADC Conversion Complete rjmp ADC_int ;----------------------------------------------;Han khoi tao ;Initialization init: ldi r16,$FF ;Cho phep 8 cong du lieu PB out DDRB,r16 ;Cong PB dung de giai ma dia chi led ldi r16,$00 ;Khoi tao cong PB out PORTB,r16 ;Dua 0x00 ra cong PB ldi r16,$FF ;Cho phep 8 cong du lieu PC out DDRC,r16 out PORTC, r16 ;Cong PC hien thi du lieu ldi r16,$01 ;Cong PA.0 dung de nhan tin hieu ADC out PINA,r16 sei ;Cho phep ngat ldi r18,$EF ;Khoi tao tin hieu giai ma dia chi led ;----------------------------------------------;Vong lap vo han loop: cli ;Cam ngat ldi r16,$FE ;Khoi tao ngat ADC.0 che do thu cong out ADCSR,r16 ldi r16,$28 ;Cho phep che do sleep cua CPU out MCUCR,r16 sei ;Cho phep ngat sleep ;CPU nghi cho den khi ngat ADC hoan thanh ;----------------------------------------------;Hien thi so lieu ra cac led 7 thanh ;Ma hien thi led hang tram: 0111.0111 = 0x77 ;Ma hien thi led hang chu : 0011.1011 = 0x3B ;Ma hien thi led hang chuc: 0001.1101 = 0x1D ;Ma hien thi chu C : 0000.1110 = 0x0E
; cpi r18,$0E ; So sanh ma hien thi voi 0x0E breq ganr18 ; Neu =0x0E thi gan tro lai 0xEF rjmp continous ; Neukhong bang 0x0E thi tiep tuc ganr18: ldi r18,$EF ; Gan tro lai gia tri r18 = 0xEF continous: lsr r18 ; Dich thanh ghi r18 sang phai 1 bit cpi r18,$77 ; Neu r18 = 0x77 hien thi led hang tram breq ganseg0 ; Gan du lieu hang tram cho led hang tram cpi r18,$3B ; Neu r18 = 0x3B hien thi led hang chuc breq ganseg1 ; Gan du lieu hang chuc cho led hang chuc cpi r18,$1D ; Neu r18 = 0x1D hien thi du lieu hang don vi breq ganseg2 ; Gan du lieu hang don vi cho led don vi cpi r18,$0E ; Neu r18 = 0x0E hien thi chu cai C breq ganseg3 ; Gan du lieu chu C cho led cuoi cung ganseg0: mov r16,seg0 rjmp loopsub ganseg1: mov r16,seg1 rjmp loopsub ganseg2: mov r16,seg2 rjmp loopsub ganseg3: mov r16,seg3 rjmp ganchuC
; Lay du lieu hang tram ; Lay du lieu hang chuc ; Lay du lieu hang don vi ; Lay du lieu cho chu C
loopsub: ldi r17, 1 ; Lay r17 = 1 de so sanh clc ; Xoa co nho sub r16, r17 brcs ganso0 ; neu r16<1 sub r16, r17 brcs ganso1 ; neu r16<2 sub r16, r17 brcs ganso2 ; neu r16<3 sub r16, r17 brcs ganso3 ; neu r16<4 sub r16, r17 brcs ganso4 ; neu r16<5 sub r16, r17 brcs ganso5 ; neu r16<6
sub r16, r17 brcs ganso6 ; neu r16<7 sub r16, r17 brcs ganso7 ; neu r16<8 sub r16, r17 brcs ganso8 ; neu r16<9 sub r16, r17 brcs ganso9 ; neu r16=9 rjmp ganchuC ganso0: cpi r18,$77 brne ganso00 ldi r19,$00 rjmp exit ganso00: ldi r19,$3F rjmp exit ganso1: ldi r19,$06 rjmp exit ganso2: ldi r19,$5B; rjmp exit ganso3: ldi r19,$4F; rjmp exit ganso4: ldi r19,$66; rjmp exit ganso5: ldi r19,$6D; rjmp exit ganso6: ldi r19,$7D; rjmp exit ganso7: ldi r19,$07 rjmp exit ganso8: ldi r19,$7F rjmp exit ganso9: ldi r19,$6F rjmp exit ganchuC:
;Khong hien thi so 0 ;So 0 ;So1 ;So 2 ;So 3 ;So 4 ;So 5 ;So 6 ;So 7 ;So 8 ;So 9
ldi r19,$39
;ChuC
exit: out PORTB,r18 out PORTC,r19
;Vi tri led can hien thi ;Du lieu can hien thi
;----------------------------------------------;Ham tao tre de duy tri thoi gian hien thi cho tung led delay: ldi temp, $30 d: dec temp ;Giam temp = temp -1 brne d ;Neu chua bang 0 thi quay lai nhan d: ;----------------------------------------------rjmp loop ;Tro lai vong lap vo han ;----------------------------------------------;Ham chuyen doi so ADC 10 bit thanh so 4 chu so he thap phan ;Ham nay thuc hien khi xay ra ngat ADC - cap nhat gia tri moi cua ADC ;Gia tri lon nhat cua 10 bit la 1024 ;Gia tri Vref = 2.048 V ;Qui doi gia tri 10bit x 2 = 1024 x 2 = 2048 ;Voi LM35 thi 10mv/1C ;Vay gia tri gan dung la 10 dec <-> 1C ADC_int: cli ;Xoa ngat he thong in r0,ADCL ;Doc gia tri byte thap cua ADC (2bit) in r1,ADCH ;Doc gia tri byte cao cua ADC (8bit) clc ;Xoa co nho add r0,r0 ;Nhan doi gia tri r0 adc r1,r1 ;Nhan doi gia tri r1 co nho (r1:r0 11 bit) ;Convert and display value for output ;Start value taken to be in r1:r0 clr r4 ;r4 chua gia tri hang ngan dc1a: mov r2,r0 ;Lay gia tri byte thap mov r3,r1 ;Lay gia tri byte cao n x 256 ldi r16,$e8 ;Lay gia tri 232 = 1000 - 3x256 sub r2,r16 ;Gia tri byte thap tru di 232 ldi r16,$3 ;lay gia tr 3 sbc r3,r16 ;Gia tri byte cao tru di 3 - C brcs dc1b ;Neu r1:r0 < 1000 nhay den dc1b inc r4 ;Neu r1:r0 > 1000 thi tang them r4 mov r0,r2 ;ket qua r1:r0 - 1000 luu vao r0 mov r1,r3 ;ket qua r1:r0 - 1000 luu vao r1 rjmp dc1a ;Tiep tuc lay r1:r0 - 1000
dc1b: mov seg0,r4
;Luu gia tri hang ngan vao seg0
clr r4 ;r4 chua gia tri hang tram dc2a: mov r2,r0 ;r1:r0 -100 mov r3,r1 ldi r16,100 ;Lay gia tri 100 sub r2,r16 ;r0 - 100 clr r16 ;Xoa r16 sbc r3,r16 ;r1 - C brcs dc2b ;Neu r1:r0 < 100 inc r4 ;Neu r1:r0 > 100 tang them r4 mov r0,r2 ;Luu gia tri r1:r0 - 100 mov r1,r3 rjmp dc2a ;Tiep tuc tru 100 dc2b: mov seg1,r4 ;Luu gia tri hang tram vao seg1 clr r4 ;r4 chua gia tri hang chuc dc3a: mov r2,r0 ;r1:r0 -10 ldi r16,10 ;Lay gia tri 10 sub r2,r16 ;r0 - 10 brcs dc3b ;Neu r0 < 10 inc r4 ;Neu r0 > 10 tang them r4 mov r0,r2 ;Luu r0- 10 rjmp dc3a ;tiep tuc tru 10 dc3b: mov seg2,r4 ;Luu gia tri hang chuc mov seg3,r0
;luu gia tri hang don vi
sei reti
;Cho phep ngat he thong ;Ket thuc ngat tro ve chuong trinh