

;********************************************************************** 
; Filename:	    TLC548_binary.asm 
; Date: Nov. 12, 2013  
; Author: Lewis Loflin  
; 16 mHz crystal   
; Will read 8-bit serial code from TLC548 and display on 8 LEDs on PORTB
; This uses 16 mHz external crystal.
; Pins on the TLC548 8-bit ADC:
; 1 -> Ref + -> connect to Vcc
; 2 -> analog in
; 3 -> Ref - -> connect to GND
; 4 -> GND
; 5 -> CS-NOT -> chip select active LOW
; 6 -> DATA OUT
; 7 -> CLK
; 8 -> Vcc
;************************************************************************

	list      p=16f628A           ; list directive to define processor
	#include <p16F628A.inc>       ; processor specific variable definitions
	errorlevel  -302              ; suppress message 302 from list file

__CONFIG   _CP_OFF & _LVP_OFF & _BOREN_OFF & _MCLRE_OFF & _WDT_OFF & _PWRTE_ON & _HS_OSC 
	
; Use  _INTOSC_OSC_NOCLKOUT  for 
;internal 4 mHz osc and no ext reset, use pin Ra5 as an input
; Use _HS_OSC for a 16 mHz ext crystal. 
;Use _XT_OSC for 4 mHz ext crystal. Page 95 in spec sheet. 

;------------------------------------------------------------
	cblock 0x20	; Begin General Purpose-Register
;-------------------------- counters	
	count1
	count2
	count3
	count4
	count5
	val
	i
	endc
	
	
;--------------------------------------
CS  equ 2  ; select TLC548 pin 5
DATABIT equ 0  ; DATA bit TLC548 pin 6
CLK     equ 1  ; CLK TLC548 pin 7
;---------------------------------------

	ORG     0x000             ; processor reset vector
	goto    setup              ; go to beginning of program


setup ; init PIC16F628A

	movlw	0x07		
	; Turn comparators off and enable pins for I/O 
	movwf	CMCON
	banksel TRISA    
	; BSF	STATUS, RP0 Jump to bank 1 use BANKSEL instead
	movlw b'00000001' 
	; RA0 is input
	movwf	TRISA
	clrf    TRISB
	banksel INTCON ; back to bank 0
	clrf	PORTB
	movlw b'00000100'
	movwf	PORTA
	goto main


main
	call read_adc 
	; value returned in val
	movf val, w
	movwf PORTB
	call delay_100ms
goto main

read_adc ; value returned in val

	clrf val
	bcf PORTA, CS ; LOW CS
	movlw 0x08
	movwf i
zz	bsf PORTA, CLK  ; HIGH CLK
	call delay_1ms
	btfss PORTA, DATABIT
	goto $+3  
   	movlw 0x01
	addwf val, F 
	bcf PORTA, CLK  ; LOW CLK
	decfsz i, F
	goto $+3
	goto $+4
	nop
	rlf val
	goto zz
 	bsf PORTA, CS  ; HIGH CS
	return


delay_1ms
	; 16 mHz crystal
	movlw 0x07
	movwf count1
aa	movlw 0xBC
	movwf count2
	decfsz count2, F
	goto $-1
	decfsz count1, F
	goto aa 
	return


delay_100ms
	; 16  mHz crystal
	movlw d'99'
	movwf count4
bb	call delay_1ms
	decfsz count4, F
	goto bb
	return

	END  ; directive 'end of program'
