                                                                 
                               
                       
                                                       
                                
                                                             
                                                                 

	list	p=16f877,r=hex	                       

	include	"pic16f877.inc"	                             
	include	"main.inc"		                  

                                                                 
                           
                                                                 
	global	Delay1ms
	global	Delay10ms
	global 	Delay200ms
	global	Delay5s
                                                                 
                             
                                                                 
	extern	AtaUpdate
	extern	AtaInit
#ifdef LCD
	extern	LcdUpdate
#endif

                                                                 
                   
                                                                 
_config code
	dw	FOSC_HS|WDTE_OFF|PWRTE_ON|CP_OFF|BODEN_OFF|LVP_OFF|CPD_OFF|WRT_ON|ICD_OFF|CP2_OFF	 		


                                                                 
                                   
 
                                                                 


                                                                 
             
                                                                 
_vector code
	goto	Start			                        

                                                                 
                 
                                            
               
                
                                                                 
_rom1 code
Start:		
	call	SysInit
	call	AtaInit
Mainloop:
	call	KeyUpdate
	call	AtaUpdate
#ifdef LCD
	call	LcdUpdate
#endif
	
	goto	Mainloop
                                                                 
                
                                    
               
                
                                                                 
SysInit:

                
	                      
	bcf		status,rp1		
	bsf		status,rp0

	                                   
	movlw	PORTA_CONFIG1	
	movwf	trisa			
	movlw	PORTB_CONFIG1	
	movwf	trisb
	movlw	PORTC_CONFIG1
	movwf	trisc
	movlw	PORTD_CONFIG1	
	movwf	trisd
	movlw	PORTE_CONFIG1	
	movwf	trise

	                  
	movlw	B'10001110'		                                                            
	movwf	adcon1

                
	                              
	bcf		status,rp1		
	bcf		status,rp0

	                  
	movlw	B'10000001'		                                                             
	movwf	adcon0

	                         
	bsf		porta,ATA_IOW
	bsf		porta,ATA_IOR

	bsf		portd,ATA_RST
	bcf		portd,LED_STATUS

	                 
	clrf	KeyEvent
	clrf	KeyState

	movlw	0x01
	movwf	TrackCurrentNr
	movwf	TrackStartNr

	return

                                                                 
                  
                                             
               
                    
                                                                 
KeyUpdate:
	clrf	KeyEvent			                 
	call	KeyScan			                 
	movf	KeyEventTemp1,w	                       
	xorlw	0x00
	btfsc	status,z			                
	goto	Kuend			                
	movf	KeyEventTemp1,w	         
	movwf	KeyEventTemp2		               
	call	Delay10ms	
	call	KeyScan			                   
	movf	KeyEventTemp1,w
	xorwf	KeyEventTemp2,w	                             
	btfss	status,z
	goto	Kuend			                              
	movf	KeyEventTemp2,w	               
	movwf	KeyEvent
Kuend:
	return

                                                                 
                
                                                    
               
                         
                                                                 
KeyScan:
	bsf		adcon0,godone		                 
Kuw:	btfsc	adcon0,godone		                              
	goto	Kuw

	movf	adresh,w		                          
	movwf	AdcMsb
	bcf		status,rp1	                       
	bsf		status,rp0
	movf	adresl,w
	bcf		status,rp1	                       
	bcf		status,rp0
	movwf	AdcLsb

	clrf	KeyEventTemp1		                           
	movf	AdcMsb,w
	sublw	0x03
	btfss	status,c
	goto	Kend
	movf	AdcMsb,w		                 
	sublw	0x03	
	btfss	status,z
	goto	Ku1			
	movf	AdcLsb,w		                         
	sublw	D'183'
	btfss	status,c
	goto 	Kend			                                         
	movf	AdcLsb,w
	sublw	D'36'
	btfsc	status,c
	goto	Ku0
	bsf		KeyEventTemp1	,KEY_EVENT_LCD_INFO
	goto	Kend
Ku0:	bsf		KeyEventTemp1	,KEY_EVENT_OPEN_CLOSE
	goto	Kend
Ku1:	movf	AdcMsb,w		              
	sublw	0x02
	btfss	status,z
	goto	Ku3			
	movf	AdcLsb,w 	                        
	sublw	D'145'
	btfsc	status,c
	goto	Ku2
	bsf		KeyEventTemp1	,KEY_EVENT_OPEN_CLOSE
	goto	Kend
Ku2:	bsf		KeyEventTemp1	,KEY_EVENT_NEXT
	goto	Kend
Ku3:	movf	AdcMsb,w		            
	sublw	0x01
	btfss	status,z		
	goto	Ku5
	movf	AdcLsb,w		                        
	sublw	D'110'
	btfsc	status,c
	goto	Ku4
	bsf		KeyEventTemp1	,KEY_EVENT_PLAY_STOP
	goto	Kend
Ku4:	bsf		KeyEventTemp1	,KEY_EVENT_PREVIOUS
	goto	Kend
Ku5:	movf	AdcLsb,w 	                        
	sublw	D'219'
	btfsc	status,c
	goto	Ku6			                           
	bsf		KeyEventTemp1	,KEY_EVENT_PREVIOUS
	goto	Kend
Ku6:	movf	AdcLsb,w
	sublw	D'74'
	btfsc	status,c
	goto	Kend			                     
	bsf		KeyEventTemp1	,KEY_EVENT_PAUSE_ONOFF
Kend:
	return

                                                                 
                                          
                                                                 
                                                                 
          
                                                                 
Delay1ms:	
	movlw	0x07
	movwf	timerb	
L11:	movlw	0xed	
	movwf	timera
L12:	decfsz	timera,f
	goto	L12
	decfsz	timerb,f
	goto	L11	
	return
                                                                 
           
                                                                 
Delay10ms:	
	movlw	0x41
	movwf	timerb	
L21:	movlw	0xff
	movwf	timera
L22:	decfsz	timera,f
	goto	L22
	decfsz	timerb,f
	goto	L21
	return
                                                                 
            
                                                                 
Delay200ms:
	movlw	0x06
	movwf	timerc
L31:movlw	0xd9	
	movwf	timerb	
L32:	movlw	0xff
	movwf	timera
L33:	decfsz	timera,f
	goto	L33
	decfsz	timerb,f
	goto	L32
	decfsz	timerc,f
	goto	L31
	return
                                                                 
         
                                                                 
Delay5s:
	movlw	0x7f
	movwf	timerc
L41:movlw	0xff	
	movwf	timerb	
L42:	movlw	0xff	
	movwf	timera
L43:	decfsz	timera,f
	goto	L43
	decfsz	timerb,f
	goto	L42
	decfsz	timerc,f
	goto	L41
	return


                                                                               
               
                                                                               

LedTest:
	bsf		portd,LED_STATUS
	call	Delay200ms
	call	Delay200ms
	bcf		portd,LED_STATUS	
	call	Delay200ms
	call	Delay200ms

	return

	END

