;******************************************************************************
;Software License Agreement                                         
;                                                                    
;The software supplied herewith by Microchip Technology             
;Incorporated (the "Company") is intended and supplied to you, the  
;Company’s customer, for use solely and exclusively on Microchip    
;products. The software is owned by the Company and/or its supplier,
;and is protected under applicable copyright laws. All rights are   
;reserved. Any use in violation of the foregoing restrictions may   
;subject the user to criminal sanctions under applicable laws, as   
;well as to civil liability for the breach of the terms and         
;conditions of this license.                                        
;                                                                    
;THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION. NO WARRANTIES,  
;WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED  
;TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A       
;PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT,  
;IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR         
;CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER.       
; *******************************************************************
; PICkit 2 Lesson 4 - "A2D"
;
; This shows how to read the A2D converter and display the
; High order parts on the 4 bit LED display.
; The pot on the Low Pin Count Demo board varies the voltage 
; coming in on in A0.
;
; The A2D is referenced to the same Vdd as the device, which 
; is provided by the USB cable and nominally is 5V.  The A2D
; returns the ratio of the voltage on Pin RA0 to 5V.  The A2D
; has a resolution of 10 bits, with 1023 representing 5V and
; 0 representing 0V.
;
; *******************************************************************
; *    See 44-pin Demo Board User's Guide for Lesson Information    *
; *******************************************************************
	LIST	p=16F887
#include <p16F887.inc>
	ERRORLEVEL	0, -302
	__CONFIG    _CONFIG1, _LVP_OFF & _FCMEN_OFF & _IESO_OFF & _BOR_OFF & _CPD_OFF & _CP_OFF & _MCLRE_OFF & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT
	__CONFIG    _CONFIG2, _WRT_OFF & _BOR21V

     cblock 0x20
Delay1           ; Assign an address to label Delay1
Delay2
Display          ; define a variable to hold the diplay
temp0
temp1
     endc
          
     org 0
Start:
     bsf       STATUS,RP0     ; select Register Bank 1
     movlw     0xFF
     movwf     TRISA          ; Make PortA all input
     clrf      TRISD          ; Make PortD all output
     ;**************************************************************
	 bsf	   OSCCON,6	    ;          8Mhz re állítás   
	 bsf	   OSCCON,5	
	 bsf	   OSCCON,4       
	 ;**************************************************************		
     movlw     0x00           ; Left Justified, Vdd-Vss referenced
     movwf     ADCON1
     bsf       STATUS,RP1     ; select Register Bank 3
     movlw     0xFF           ; we want all Port A pins Analog
     movwf     ANSEL
     bcf       STATUS,RP0     ; back to Register Bank 0
     bcf       STATUS,RP1
     
     movlw     0x41
     movwf     ADCON0         ; configure A2D for Fosc/8, Channel 0 (RA0), and turn on the A2D module

    ; *****************************************************************
	; *                        PWM inicializálása                     *
	; *****************************************************************
	
	banksel		TRISC
	movlw		0xFF
	movwf		TRISC
	movlw		0xFF
	movwf		PR2
	banksel		CCP2CON
	movlw		0x0C
	movwf		CCP2CON
	movlw		0x80             ;50% os kitöltési tényező
	movwf		CCPR2L
	banksel		PIR1
	bcf			PIR1,TMR2IF
	movlw		0x04
	movwf		T2CON          ;postscale 1:1,prescale;1:1,Timer2 bekapcsolása pwm frekvencia 15625Hz
	btfss		PIR1,TMR2IF
	goto		$-1
	banksel		TRISC
	clrf		TRISC

    ; *****************************************************************

	banksel		ADCON0

MainLoop:
     nop                      ; wait 5uS for A2D amp to settle and capacitor to charge.
     nop                      ; wait 1uS
     nop                      ; wait 1uS
     nop                      ; wait 1uS 
     nop                      ; wait 1uS
     bsf       ADCON0,GO_DONE ; start conversion
	;**************************************************************************************
     btfsc    ADCON0,GO_DONE ; this bit will change to zero when the conversion is complete
	;***********(btfss-t átírtam brfscre mert szerintem igy logikus)***************************
     goto      $-1

     movf      ADRESH,w       ; Copy the display to the LEDs
     movwf     PORTD
	; ************************************************************************
	
	movwf		temp0
	movwf		temp1
	incf		temp1
	btfsc		STATUS,0
	decf		temp1
	bcf			STATUS,0
	rrf			temp1,0				;w	ami a CCPR2 be kell a tempben a másolat w éről
	bcf			PIR1,TMR2IF
	btfss		PIR1,TMR2IF
	goto		$-1

	movwf		CCPR2L
	bcf			PIR1,TMR2IF
	btfss		PIR1,TMR2IF
	goto		$-1

	btfsc		STATUS,0
	bsf	  		CCP2CON,5
	btfss		STATUS,0
	bcf			CCP2CON,5
	movf		temp0,0
	banksel		PR2
	movwf		PR2
	
	banksel		ADCON0

	; ***********************************************************************
     
	goto      MainLoop

     end
     
