LIST p=16F88
ERRORLEVEL -302
__CONFIG H'2007', B'11111100110000' ; 3F30H the _CONFIG 1 directive.
__CONFIG H'2008', B'00000000000011' ; 0003H
; Storage
CBLOCK 20h ; bank 0 storage
tick ; delay counter
char ; char to transmit
bitctr ; bit counter
ENDC
CBLOCK 0A0h ; bank 1 storage
GenCount ; generic counter/temp register
Mem_Loc_high ; EEPROM high memory address
Mem_Loc_low ; EEPROM low memory address
Data_Buf ; EEPROM data buffer
OutputByte ; EEPROM output byte
flags ; flag bit register
inx ; message index
ENDC
; Defines
#define SDA 86h,2 ; SDA = data = RB2
#define SCL 86h,1 ; SCL = clock = RB1
#define BANK0 BCF 3,5 ; Select bank 0
#define BANK1 BSF 3,5 ; Select bank 1
;Equates
status EQU 03h ; status register (duplicated all banks)
PORTA EQU 05h ; port A register
PORTB EQU 06h ; port B register
PIR1 EQU 0Ch ; flags
sspcon EQU 14h ;*I2C control
RCSTA EQU 18h ; in bank0
TXREG EQU 19h ; Transmit data
RCREG EQU 1AH ; Receive data
adcon0 EQU 1Fh ;*A/D channel control
TRISA EQU 85h ; data direction A
TRISB EQU 86h ; data direction B
INTCON EQU 0Bh ;*Interrupt control
OSCON EQU 8Fh ; OSCCON controls clock speed
OSCTUNE EQU 90h ; Frequency control
TXSTA EQU 98h ; in bank1
SPBRG EQU 99h ; in bank1
ANSEL EQU 9Bh ; a/d line property
adcon1 EQU 9Fh ;*A/D format control
c EQU 0 ; carry bit
z EQU 2 ; zero bit
rp0 EQU 5 ; 0,1 bank select bit
rp1 EQU 6 ; 2,3 bank select bit
pcl EQU 2 ; program counter low byte
WRITE EQU 0A0h ; EEPROM write command
READ EQU 0A1h ; EEPROM read command
xmit EQU 0 ; RA0 is COM1 transmit pin
NULL EQU 0 ; null byte, message terminator
EOF EQU 0 ; also using 0 as end of file here
CR EQU d'13' ; carriage return
LF EQU d'10' ; line feed
;*******
; Main *
;*******
ORG 0000h
Start BCF status,rp1 ; banks 0 & 1 only
BANK0
kilint bcf INTCON,7 ;*turn gie off
btfsc INTCON,7 ;*MicroChip recommends this check!
goto kilint ;*!!! GOTCHA !!! without this check
;*you are not sure gie is cleared!
clrf sspcon ;*I2C control
clrf 0Ch ;*pir1 clear peripheral flags
clrf 0Dh ;*pir2 all of them
clrf PORTA ;*clear all i/o registers...
clrf PORTB
movlw 00h ;*set all i/o as digital. no analog.
movwf adcon0
BANK1
clrf 8Ch ;*pie1 disable peripheral interrupts
clrf 8Dh ;*pie2 all of them
movlw b'10001111' ;*set right justified
movwf adcon1
MOVLW 24h ;set portb direction for i/o pins
MOVWF TRISB ;*0=output 1=input
;* rB5 output _uart_TX
;*!!! rB5 output _uart_tx. Actually an output,
;*!!! BUT for UART use rB5 MUST be programmed
;*!!! as an INPUT! (How wonderfully counter-intuitive!)
MOVLW 0x43
MOVWF 81h ;*Option register p18
CLRF 8Bh ;*intcon no interrupts used.
MOVLW 0x26 ;was 04 set BRGH clear SYNC
MOVWF TXSTA
MOVLW 0x19 ;Set SPBRG to 25d=19h d'25' or .25
MOVWF SPBRG
MOVLW 60h ; speed up clock 0110 0000=60h
MOVWF OSCON ; OSCCON= H'8F' makes 4MHz
MOVLW 01h ; speed up clock 00 0001 =0.00375
MOVWF OSCTUNE ; OSCTUNE=90h
BANK0
MOVLW 0x43 ; A
MOVWF char ; is in BANK0
; CLRF PORTA ; Why?
; CLRF ANSEL ; ANSEL is at 9B default 1 is analog
MOVLW 90h ;Set SPEN and set CREN in RCSTA
MOVWF RCSTA
movf RCREG,w ;*clear uart receiver
movf RCREG,w ;* including fifo
movf RCREG,w ;* which is three deep.
movlw 0 ;*any character will do.
movwf TXREG ;*send out dummy character
;* to get transmit flag valid!
LOOP CALL RX
CALL TX
GOTO LOOP
;******************************************
; My replacement for RecvPC is RX *
; Received character is put in char. *
;******************************************
RX BANK0
BTFSC RCSTA,1 ; OERR is overrun error
CALL err ; error routine
BTFSC RCSTA,2 ; FERR is framing error ignore
CALL err
BTFSS PIR1,5 ; Test RCIF to see if a word is in.
XXX GOTO RX ; 1 means full=received
MOVF RCREG,w ; Get receive data 0 goes to w, 1 goes to f
MOVWF char
RETURN
err BCF RCSTA,4 ;clear CREN in RCSTA
movf RCREG,w ;flush fifo
movf RCREG,w ; all three elements.
movf RCREG,w
BSF RCSTA,4 ; set CREN in RCSTA
RETURN
;******************************************
; My replacement for SendPC is TX *
; The ASCII term to transmit is in char *
; 4MHz and 9.6 means SPBRG = 25. *
; No interrupts so check every millisecond if max speed *
; Send a character to the PC com port *
;******************************************
TX BANK0
BTFSS PIR1,4 ;check the TXIF bit in PIR1 (1 means empty)
GOTO $-1
MOVF char,w ; get char
; MOVLW 0X41 It sends the letter A just fine when this is swapped in.
MOVWF TXREG ; Put char in TXREG
RETURN ; wait 1 clock before checking PIR1
END