Uživatel:Ovesnpet

Z MAM wiki

Přejít na: navigace, hledání

[editovat] Semestrální práce, Voltmetr

[editovat] Kollárová (kollakve), Ovesný (ovesnpet), Macháček (machaj25)

 
Popis přípravku:
   Přípravek pomocí A/D převodníku porovnává referenční napětí (v našem případě 5V), s napětím neznámým (měřeným přes potenciometr) a do registru nám zapisuje hodnotu od 0 do 255. Tuto hodnotu posíláme formou jako 8 bitovou reprezentaci ASCI kodu konkrétního znaku a zobrazujeme na displeji.
Touto hodnotou je také ovládána svítivost diody pomocí PWM


Použité součástky:
ATMega8
display Sharp LM162551
potenciometr
LED dioda

Soubor:20130515_233811.jpg Soubor:schemaOvesnpet.png

 
.INCLUDE "m8def.inc"
.include "hd44780.inc"

.equ	LCD_PORT 	= PORTD
.equ	LCD_DDR		= DDRD

.equ	LCD_PORT_E 	= PORTB
.equ	LCD_DDR_E		= DDRB


.equ    LCD_PIN		= PIND

.equ	LCD_D4 		= 4
.equ	LCD_D5 		= 5
.equ 	LCD_D6 		= 6
.equ	LCD_D7 		= 7

.equ	LCD_RS		= 0 ;pb0
.equ    LCD_RW		= 3 ;pd3
.equ	LCD_EN		= 2; pd2


.def STO   = R17;
.def DESET = R18;
.def JEDNA = R19;



.ORG 0x0000 
rjmp Main ; Reset Handler


Main: 
;Stack pointer init:
	ldi r16,high(RAMEND); Main program start
	out SPH,r16 ; Set Stack Pointer to top of RAM
	ldi r16,low(RAMEND)
	out SPL,r16


LDI R16,255;
RCALL BinToDec2;

//LDI R16,0;
//RCALL BinToDec;
////NASTAVENI PWM

SBI DDRB,1		    // PB1 is now an output
SBI DDRB,2
;LDI R16, 0b10000101
LDI R16, 0b10100001

OUT TCCR1A, R16    //  Non-Inverted PWM, 8-Bit PWM



LDI R16,0;
OUT OCR1AH, R16

LDI R16,0
OUT OCR1AL, R16			// set PWM for 50% duty cycle A

LDI R16,0;
OUT OCR1BH, R16

LDI R16,0
OUT OCR1BL, R16			// set PWM for 50% duty cycle A



LDI R16, 0b00000001
OUT TCCR1B, R16

////KONEC NASTAVENI PWM


//NASTAVENI ADC

SBI ADMUX, REFS0 //referencni napeti je AREF (5V)
CBI ADMUX, REFS1 // -//-

SBI ADMUX, ADLAR //zarovnani vlevo

CBI ADMUX, MUX3 //nastaveni kanalu ADC
CBI ADMUX, MUX2 //ADC1
CBI ADMUX, MUX1
SBI ADMUX, MUX0


SBI ADCSRA, ADEN //povoleni ADC
SBI ADCSRA, ADIE //povoleni preruseni ADC



SBI ADCSRA,ADPS2 //nastaveni preddelicky na 128
SBI ADCSRA,ADPS1
SBI ADCSRA,ADPS0
//konec nastaveni ADC


//nastaveni tlacitek
sbi DDRD,0
cbi PORTD,0


cbi DDRC,5
cbi DDRC,4
SBI PORTC,5
SBI PORTC,4
//konec nastaveni tlacitek


//CLR FLAG1;
//SBR FLAG1,(1<<TL1)

//LDI R16,0;


RCALL NAPIS;


DO:
	SBI ADCSRA,ADSC //zapnuti AD prevodniku
	RCALL WAIT
	IN R16,ADCH;
	//LDI R16,255;
//	RCALL BinToDec2;
	RCALL BinToDec2;
	RCALL ZAPIS;
	INC R21;
	MOV R16,R21;
	IN R16,ADCH
	OUT OCR1AL, R16  //set pwm duty cycle
	OUT OCR1BL, R16  //set pwm duty cycle	

RJMP DO;

ZAPIS:
	PUSH R20;
	PUSH R16;
	LDI R20, 48;
	ADD R17,R20;
	ADD R18,R20;
	ADD R19,R20;

	LDI R16,0x45
	RCALL LCD_SetAddressDD 

	MOV R16, R17
	RCALL LCD_WriteData	; zapis jednotek

	LDI R16, 46
	RCALL LCD_WriteData 	; zapis .

	MOV R16, R18
	RCALL LCD_WriteData	; zapis desetin

	MOV R16, R19
	RCALL LCD_WriteData	;zapis setin
	
	LDI R16, 86		
	RCALL LCD_WriteData	;zapis V
	POP R16;
	POP R20;
	ret;



NAPIS:
	RCALL LCD_Init
	LDI R16,0x00
	RCALL LCD_SetAddressDD
	LDI R16,78
	RCALL LCD_WriteData	; NAPETI:

	LDI R16,97
	RCALL LCD_WriteData

	LDI R16,112
	RCALL LCD_WriteData

	LDI R16,101
	RCALL LCD_WriteData

	LDI R16,116
	RCALL LCD_WriteData

	LDI R16,105
	RCALL LCD_WriteData

	LDI R16,58
	RCALL LCD_WriteData
	ret;

BinToDec:
	//push R17
	//push R18
	//push R19
	push R20;
	clr STO;
	clr DESET;
	clr JEDNA;

P_STO:
	INC STO
	SUBI R16,100;
	BRCC P_STO;

	DEC STO;
	LDI R20,100;
	ADD R16,R20;
P_DESET:
	INC DESET;
	SUBI R16,10;
	BRCC P_DESET;

	DEC DESET;
	LDI R20,10;
	ADD R16,R20
	MOV JEDNA, R16;
	
	//pop R19
	//pop R18
	//pop R17
	pop R20;
	ret;

BinToDec2:
	//push R17
	//push R18
	//push R19
	push R20;
	clr STO;
	clr DESET;
	clr JEDNA;

P_STO2:
	INC STO
	SUBI R16,51;
	BRCC P_STO2;

	DEC STO;
	LDI R20,51;
	ADD R16,R20;
P_DESET2:
	INC DESET;
	SUBI R16,5;
	BRCC P_DESET2;

	DEC DESET;
	LDI R20,5;
	ADD R16,R20
	MOV JEDNA, R16;
	
	//pop R19
	//pop R18
	//pop R17
	pop R20;
	ret;


WAIT:
	PUSH R16
	CLR R16
	PUSH R17
	CLR R17
	LDI R17, 244;
	WAIT1:
		INC	R16
		BRBS 1,WAIT2; test zero bitu kvuli preteceni
		RJMP WAIT1
	WAIT2:
		INC R17
		BRBS 1,WAIT_RET; test zero bitu kvuli preteceni
		RJMP WAIT1
	WAIT_RET:
		POP R17
		POP R16
		NOP
		RET

WAIT_250:
	PUSH R16
	CLR R16
	LDI R16,13
	WAIT1S1:
		RCALL WAIT
		INC	R16
		BRBS 1,WAIT1S2; test zero bitu kvuli preteceni
		RJMP WAIT1S1
	WAIT1S2:
		POP R16
		RET

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_E, LCD_RS
	push	r16
	swap	r16
	rcall	LCD_WriteNibble
	pop		r16
	rcall	LCD_WriteNibble

	clr		XH
	ldi		XL,250
	;RCALL WAIT_250
	;RCALL WAIT_250
	;RCALL WAIT_250
	RCALL WAIT
	ret
;------------------------------------------------------------------------------
;
;------------------------------------------------------------------------------
LCD_WriteCommand:
	cbi		LCD_PORT_E, LCD_RS
	push	r16
	swap	r16
	rcall	LCD_WriteNibble
	pop		r16
	rcall	LCD_WriteNibble
	;ldi		r16,2
	;rcall	WaitMiliseconds
	RCALL WAIT
	RCALL WAIT
	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	
	NOP
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:


	SBI LCD_DDR, LCD_RW
	CBI LCD_PORT,LCD_RW
	sbi		LCD_DDR, LCD_D4
	sbi		LCD_DDR, LCD_D5
	sbi		LCD_DDR, LCD_D6
	sbi		LCD_DDR, LCD_D7
	
	sbi		LCD_DDR_E, LCD_RS
	sbi		LCD_DDR, LCD_EN

	cbi		LCD_PORT_E, LCD_RS
	cbi		LCD_PORT, LCD_EN

;	ldi		r16, 100
;	rcall	WaitMiliseconds
	RCALL WAIT_250

	ldi		r17, 3
InitLoop:
	ldi		r16, 0x03
	rcall	LCD_WriteNibble

	RCALL WAIT
	RCALL WAIT
	RCALL WAIT
	RCALL WAIT
	RCALL WAIT
 	;ldi	r16, 5
	;rcall	WaitMiliseconds

	dec		r17
	brne	InitLoop

	ldi		r16, 0x02
	rcall	LCD_WriteNibble

	;ldi		r16, 1
	;rcall	WaitMiliseconds
	RCALL 	WAIT

	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


[editovat] Regulace ventilatoru PWN + klavesnice

 
.INCLUDE "m168def.inc"

.EQU PWM_PULSES = 36    ; value for 27.8 kHz PWM
;.EQU PWM_PULSES = 45    ; value for 22.2 kHz PWM


.ORG 0x0000 
rjmp Main ; Reset Handler (jump)

.ORG 0x0008 
rjmp isr1 ; PCINT1 Handler (jump)



; Main program start

Main: 

; Stack pointer init:
ldi r16,high(RAMEND); Main program start
out SPH,r16 ; Set Stack Pointer to top of RAM
ldi r16,low(RAMEND)
out SPL,r16


; Hardware initialization

; Keyboard init:
cbi DDRC, 2 ; set inputs for keyboard (COL1-3)
cbi DDRC, 3
cbi DDRC, 4

sbi PORTC, 2 ; set internal Pull-Ups for keyboard
sbi PORTC, 3
sbi PORTC, 4

sbi DDRD, 0 ; set driving outputs for keyboard
sbi DDRD, 1 ; (ROW1-ROW4)
sbi DDRD, 2
sbi DDRD, 3

cbi PORTD, 0    ; log. 0 on all rows
cbi PORTD, 1
cbi PORTD, 2
cbi PORTD, 3


; Timer controlled PWM init:
;
; There are registers TCCR0A and TCCR0B for config, DDRD for output bit
; enabling, OCR0A for counter lenght and OCR0A for pulse "high" lenght.
; Register TIMSK0 controlls interrupts - not used, 0x00 by default 

PWM_INIT:
ldi R17, 0b00100011    ; Fast PWM Mode, out on OC0B, non Inverting
out TCCR0A, R17    ; 
ldi R17, 0b00001001    ; WGM2:1 (Fast PWM), CS2..0:001 (internal clock, prescale=1)
out TCCR0B, R17
ldi R17, PWM_PULSES    ; load number of clock pulses for 1 PWM period
out OCR0A, R17   

sbi DDRD, 5    ; set pin 11 as PWM output OC0B (PD5 pin)


; Keyboard interrupt setup
;
; Activation of pin change interrupt - PROBLEM!!! PCICR and PCIMSK are 
; extended I/O registers (0x68 and 0x6C), and must be handled as 
; a memory location

ldi r26, PCICR ; load address of PCICR in Y low
clr r27 ; load high byte with 0
ldi r16,0b00000010 ; activate PCINT1
st X, r16 ; store new PCINT1

ldi r26, PCMSK1 ; load address of PCMSK1 in Y low
clr r27 ; load high byte with 0
ldi r16,0b00011100 ; allow pin change interrupt on portC bits 2,3,4
st X, r16 ; store new PCMSK1

sei ; Enable interrupts


; Set sleep mode of the CPU
; SMCR: 0,0,0,0,SM2,SM1,SM0,SE   SM2..0: Sleep Mode Select Bits, SE: Sleep enabled)

ldi r16,0b00000001 ; Idle mode 
out SMCR,r16 ; Sleep mode set


; Sleep loop, wake up by the Interrupt, return with RETI back to sleep loop

loop:
sleep ; now AVR sleeps
nop ; return from Interrupt to this instruction
rjmp loop ; and sleep again


; PCINT0 Service Routine:
;
; To do: *** rpm reading, regulation, etc.


isr1:
rcall KEYPRESS

mov R17, R16
lsl R17
lsl R17    ; multiply key number by 4 for 10 steps in 40 clock pulses of PWM
out OCR0B, R17  ; output pulse "high" lenght to PWM compare unit 

reti ; return from Interrupt


; Keyboard decoding:

KEYPRESS:
KEY0:
cbi DDRD, 0
cbi DDRD, 1
cbi DDRD, 2
sbi DDRD, 3

sbi PORTD, 0
sbi PORTD, 1
sbi PORTD, 2
cbi PORTD, 3    ;log. 0 to the row with *, 0, #, D    

sbic    PINC, 3 ; přeskočí pokud bit na nule
rjmp    KEY1
ldi    R16, 0    ;pressed key 0
cbi DDRD, 5    ; vypne vystup PWM na PORTD 5
rjmp    KEYRET

KEY1:
sbi DDRD, 5    ; zapne vystup PWM na PORTD 5

sbi DDRD, 0 
cbi DDRD, 1
cbi DDRD, 2
cbi DDRD, 3



cbi PORTD, 0    ;log. 0 to the row with 1, 2, 3, A
sbi PORTD, 1    ;log. 1 on another three
sbi PORTD, 2
sbi PORTD, 3    
sbic    PINC, 2
rjmp    KEY2
ldi    R16, 1    ;pressed key 1
rjmp    KEYRET

KEY2:
sbic    PINC, 3
rjmp    KEY3
ldi    R16, 2
rjmp    KEYRET

KEY3:
sbic    PINC, 4
rjmp    KEY4
ldi    R16, 3
rjmp    KEYRET

KEY4:

cbi DDRD, 0 
sbi DDRD, 1
cbi DDRD, 2
cbi DDRD, 3



sbi PORTD, 0
cbi PORTD, 1    ;log. 0 to the row with 4, 5, 6, B
sbi PORTD, 2
sbi PORTD, 3    
sbic    PINC, 2
rjmp    KEY5
ldi    R16, 4    ;pressed key 4
rjmp    KEYRET

KEY5:
sbic    PINC, 3
rjmp    KEY6
ldi    R16, 5
rjmp    KEYRET

KEY6:
sbic    PINC, 4
rjmp    KEY7
ldi    R16, 6
rjmp    KEYRET

KEY7:

cbi DDRD, 0 
cbi DDRD, 1
sbi DDRD, 2
cbi DDRD, 3


sbi PORTD, 0
sbi PORTD, 1
cbi PORTD, 2    ;log. 0 to the row with 7, 8, 9, C
sbi PORTD, 3    
sbic    PINC, 2
rjmp    KEY8
ldi    R16, 7    ;pressed key 7
rjmp    KEYRET

KEY8:
sbic    PINC, 3
rjmp    KEY9
ldi    R16, 8
rjmp    KEYRET

KEY9:
sbic    PINC, 4
rjmp    KEYRET
ldi    R16, 9

KEYRET:

sbi DDRD, 0 
sbi DDRD, 1
sbi DDRD, 2
sbi DDRD, 3

cbi PORTD, 0    ; log. 0 on all rows for next key press catch
cbi PORTD, 1
cbi PORTD, 2
cbi PORTD, 3
ret
Osobní nástroje