Спойлер
Код: Выделить всё
.include "m8def.inc"
.list
#define F_CPU (4000000)
.DEF temp1= R16 ;ADCSRA 4 6
.def resL =R20
.def resH =R21
.cseg
.org 0 ;Aa?an ia?acaionea
;************************oaaeeoa i?a?uaaiee**********************************
rjmp Initial
.org ADCCaddr ;Aa?an aaeoi?a i?a?uaaiey ADC Complete
IN resL, ADCL ; Ecia?aiiia iai?y?aiea
IN resH, ADCH
reti
;***************************************************************************
Initial:
ldi Temp1, LOW(RamEnd)
out SPL, Temp1
ldi Temp1, HIGH(RamEnd)
out SPH, Temp1
cli
ldi temp1,0b11111111
out DDRB,temp1 ;Iai?aaeaiea aaiiuo ii?oa B ia auaia,(LCD-aenieae)
; out PORTB,temp1 ;Auaaou aaiiua ia ii?o A
SBI PORTD,6
; ldi temp2,0b01000000 ;Iiaoyaeaa?uaa nii?ioeaeaiea
; out PortD,temp2 ; aey ia?aiu?ee MW
rcall lcd_init;Eieoeaeecaoey aenieay.
;*****************************************************************************
Start:
ldi R17,0b10010000 ;SE=1 (?ac?aoaai "niyuee" ?a?ei), SM[2..0]=001
out MCUCR,R17 ; ("niyuee" ?a?ei = ooiiiiaaaeaiey AOI)
ldi R16, 0b10011011 ;сбросить флаг ADIF
; ldi Temp1, 0b10001011
out ADCSRA,R16
ldi Temp1, 0b01000000 ; подключить AVCC к AREF
out ADMUX, Temp1
SBI ADCSRA, ADSC ; Caione AOI (;sbi ADCSR, 6)
SEI
sleep ;ia?aoiaei a ?a?ei ooiiiiaaaeaiey AOI
CLI
LSR resH
ROR resL
rcall bin_to_dec ;i?aia?aciaaiea a aanyoe?iia e a ASKII
rcall LCD_Write ;Ia?aaa?a aaiiuo ia LCD
rjmp Start
;********************************************************************
;********************************************************************
bin_to_dec:
ser r18
bin_to_dec1:
inc r18
subi resL, 100
sbci resH, 0
brcc bin_to_dec1; au?eoaai aua ?ac (iao caaia)
subi resL,low(-100)
sbci resH, high(-100)
ser r17
bin_to_dec2: inc r17
subi resL, 10
brcc bin_to_dec2
subi resL, -10
MOV R22,resL
; ser r22
;bin_to_dec3:
; inc r22
; subi resL, 1
; brcc bin_to_dec3
; subi resL, -1
ret
;*************************************************************************
LCD_Write:
;i?aia?aciaaou aanyoe?iue eia a ASCII
SUBI R18,-$30
SUBI R17,-$30
SUBI R22,-$30
;*************************************************************************
ldi r16,0x45 ;;;;;;;;;;
rcall LCD_SetAddressDD ;eii?aeiaou a LCD ;;;;;;;;;;;
mov temp1,r18
rcall LCD_WriteData
LDI temp1,'.' ;Oi?ea
; mov temp1,point ;Oi?ea
rcall LCD_WriteData
mov temp1,r17
rcall LCD_WriteData
mov temp1,r22
rcall LCD_WriteData
reti
;----------
; HD44780 LCD Assembly driver
; http://avr-mcu.dxp.pl
; (c) Radoslaw Kwiecien
;----------
#define HD44780_CLEAR 0x01
#define HD44780_HOME 0x02
#define HD44780_ENTRY_MODE 0x04
#define HD44780_EM_SHIFT_CURSOR 0
#define HD44780_EM_SHIFT_DISPLAY 1
#define HD44780_EM_DECREMENT 0
#define HD44780_EM_INCREMENT 2
#define HD44780_DISPLAY_ONOFF 0x08
#define HD44780_DISPLAY_OFF 0
#define HD44780_DISPLAY_ON 4
#define HD44780_CURSOR_OFF 0
#define HD44780_CURSOR_ON 2
#define HD44780_CURSOR_NOBLINK 0
#define HD44780_CURSOR_BLINK 1
#define HD44780_DISPLAY_CURSOR_SHIFT 0x10
#define HD44780_SHIFT_CURSOR 0
#define HD44780_SHIFT_DISPLAY 8
#define HD44780_SHIFT_LEFT 0
#define HD44780_SHIFT_RIGHT 4
#define HD44780_FUNCTION_SET 0x20
#define HD44780_FONT5x7 0
#define HD44780_FONT5x10 4
#define HD44780_ONE_LINE 0
#define HD44780_TWO_LINE 8
#define HD44780_4_BIT 0
#define HD44780_8_BIT 16
#define HD44780_CGRAM_SET 0x40
#define HD44780_DDRAM_SET 0x80
.equ LCD_PORT = PORTB
.equ LCD_DDR = DDRB
.equ LCD_PIN = PINB
.equ LCD_D4 = 4
.equ LCD_D5 = 5
.equ LCD_D6 = 6
.equ LCD_D7 = 7
.equ LCD_RS = 3
.equ LCD_EN = 2
;----------
;
;----------
LCD_WriteNibble:
sbi LCD_PORT, LCD_EN
sbrs r16, 0
cbi LCD_PORT, LCD_D4
sbrc r16, 0
sbi LCD_PORT, LCD_D4
sbrs r16, 1
cbi LCD_PORT, LCD_D5
sbrc r16, 1
sbi LCD_PORT, LCD_D5
sbrs r16, 2
cbi LCD_PORT, LCD_D6
sbrc r16, 2
sbi LCD_PORT, LCD_D6
sbrs r16, 3
cbi LCD_PORT, LCD_D7
sbrc r16, 3
sbi LCD_PORT, LCD_D7
cbi LCD_PORT, LCD_EN
ret
;----------
;
;----------
LCD_WriteData:
sbi LCD_PORT, LCD_RS
push r16
swap r16
rcall LCD_WriteNibble
pop r16
rcall LCD_WriteNibble
clr XH
ldi XL,250
rcall Wait4xCycles
ret
;----------
;
;----------
LCD_WriteCommand:
cbi LCD_PORT, LCD_RS
push r16
swap r16
rcall LCD_WriteNibble
pop r16
rcall LCD_WriteNibble
ldi r16,2
rcall WaitMiliseconds
ret
;----------
;
;----------
LCD_WriteString:
lpm r16, Z+
cpi r16, 0
breq exit
rcall LCD_WriteData
rjmp LCD_WriteString
exit:
ret
;----------
;
;----------
LCD_WriteHexDigit:
cpi r16,10
brlo Num
ldi r17,'7'
add r16,r17
rcall LCD_WriteData
ret
Num:
ldi r17,'0'
add r16,r17
rcall LCD_WriteData
ret
;----------
;
;----------
LCD_WriteHex8:
push r16
swap r16
andi r16,0x0F
rcall LCD_WriteHexDigit
pop r16
andi r16,0x0F
rcall LCD_WriteHexDigit
ret
;----------
;
;----------
LCD_WriteDecimal:
clr r14
LCD_WriteDecimalLoop:
ldi r17,10
rcall div8u
inc r14
push r15
cpi r16,0
brne LCD_WriteDecimalLoop
LCD_WriteDecimalLoop2:
ldi r17,'0'
pop r16
add r16,r17
rcall LCD_WriteData
dec r14
brne LCD_WriteDecimalLoop2
ret
;----------
;
;----------
LCD_SetAddressDD:
ori r16, HD44780_DDRAM_SET
rcall LCD_WriteCommand
ret
;----------
;
;----------
LCD_SetAddressCG:
ori r16, HD44780_CGRAM_SET
rcall LCD_WriteCommand
ret
;----------
LCD_Init:
CLR R0
OUT LCD_PORT,R0
ldi r16, 100
rcall WaitMiliseconds
ldi r17, 3
InitLoop:
ldi r16, 0x03
rcall LCD_WriteNibble
ldi r16, 5
rcall WaitMiliseconds
dec r17
brne InitLoop
ldi r16, 0x02
rcall LCD_WriteNibble
ldi r16, 1
rcall WaitMiliseconds
ldi r16, HD44780_FUNCTION_SET | HD44780_FONT5x7 | HD44780_TWO_LINE | HD44780_4_BIT
rcall LCD_WriteCommand
ldi r16, HD44780_DISPLAY_ONOFF | HD44780_DISPLAY_OFF
rcall LCD_WriteCommand
ldi r16, HD44780_CLEAR
rcall LCD_WriteCommand
ldi r16, HD44780_ENTRY_MODE |HD44780_EM_SHIFT_CURSOR | HD44780_EM_INCREMENT
rcall LCD_WriteCommand
ldi r16, HD44780_DISPLAY_ONOFF | HD44780_DISPLAY_ON | HD44780_CURSOR_OFF | HD44780_CURSOR_NOBLINK
rcall LCD_WriteCommand
ret
;----------
;
;----------
;----------
; Busy-wait loops utilities module
; For F_CPU >= 4MHz
; http://avr-mcu.dxp.pl
; (c) Radoslaw Kwiecien, 2008
;----------
#ifndef F_CPU
#error "F_CPU must be defined!"
#endif
#if F_CPU < 4000000
#warning "F_CPU too low, possible wrong delay"
#endif
#define CYCLES_PER_US (F_CPU/1000000)
#define C4PUS (CYCLES_PER_US/4)
#define DVUS(x) (C4PUS*x)
;----------
; Input : XH:XL - number of CPU cycles to wait (divided by four)
;----------
Wait4xCycles:
sbiw XH:XL, 1
brne Wait4xCycles
ret
;----------
; Input : r16 - number of miliseconds to wait
;----------
WaitMiliseconds:
LDI XH,HIGH(1000*F_CPU/4/1000000)
LDI XL,LOW(1000*F_CPU/4/1000000)
WaitMsLoop:
SBIW XH:XL,1
BRNE WaitMsLoop
dec r16
brne WaitMiliseconds
ret
;***** Subroutine Register Variables
.def drem8u =r15 ;remainder
.def dres8u =r16 ;result
.def dd8u =r16 ;dividend
.def dv8u =r17 ;divisor
.def dcnt8u =r18 ;loop counter
;***** Code
div8u:
sub drem8u,drem8u ; clear remainder and carry
ldi dcnt8u,9 ; init loop counter
d8u_1:
rol dd8u ; shift left dividend
dec dcnt8u ; decrement counter
brne d8u_2 ; if done
ret ; return
d8u_2:
rol drem8u ; shift dividend into remainder
sub drem8u,dv8u ; remainder = remainder - divisor
brcc d8u_3 ; if result negative
add drem8u,dv8u ; restore remainder
clc ; clear carry to be shifted into result
rjmp d8u_1 ; else
d8u_3:
sec ; set carry to be shifted into result
rjmp d8u_1
.EXIT