PDA

View Full Version : pic16F628



daydream
- 2nd May 2012, 05:06
hi,can someone explain to me what this code does and how should i connect my lcd to pic16f628-with picture?
thx
------------------------------------
'Name : Frekans-628.BAS *
'Author : [Erol ERDAL) *

DE@ DEVICE pic16F628
@ DEVICE pic16F628, WDT_off
@ DEVICE pic16F628, PWRT_ON
@ DEVICE pic16F628, PROTECT_OFF
@ DEVICE pic16F628, MCLR_ON
@ DEVICE pic16F628, HS_OSCFINE OSC 10
DEFINE LCD_DREG PORTA
DEFINE LCD_DBIT 0
DEFINE LCD_RSREG PORTB
DEFINE LCD_RSBIT 2
define LCD_RWREG PORTB
DEFINE LCD_RWBIT 1
DEFINE LCD_EREG PORTB
DEFINE LCD_EBIT 0
DEFINE LCD_BITS 4
DEFINE LCD_LINES 2
DEFINE LCD_COMMANDUS 4000
DEFINE LCD_DATAUS 250
TRISA=0
TRISB=111100
fout var word
foutL var fout.byte0
foutH var fout.byte1
mode var byte
Delay VAR WORD
Delay_Us var WORD
x var word
tenth var byte
Z VAR BIT
'-------------------------------------------------------------------
Z=0
CMCON=7
mode=0
'************************* Main***************************
START : GOSUB TUS 'this is for mode select
SELECT CASE MODE
CASE 0
Delay=1000elay_Us=400
GOSUB OLC
Lcdout $FE,$1,"0-65 KHz : MOD=",DEC MODE
Lcdout $FE,$C0,#fout," Hz":PAUSE 100
goto START

CASE 1
Delay_US=100Delay=100
GOSUB OLC :Fout=Fout/100
Lcdout $FE,1,"65-650KHz: MOD=",DEC MODE
Lcdout $FE,$C0,#fout," KHz":PAUSE 100
GOTO START

CASE 2
Delay_US=996Delay=9
GOSUB OLC:Fout=Fout/10:tenth=(fout//1000)/10
Lcdout $FE,1,"650-6,5MHz:MOD=", DEC MODE
Lcdout $FE,$C0,#fout/1000,",",#tenth," MHz":PAUSE 100
GOTO START

CASE 3
Delay_US=992Delay=0
GOSUB OLC:tenth=(fout//1000)/10:Fout=Fout/1000
Lcdout $FE,1,"6,5-30 MHz:MOD=" ,DEC MODE
Lcdout $FE,$C0,#fout,",",#tenth," MHz":PAUSE 100
GOTO START
END SELECT
Z=0
GOTO START

TUS: PAUSE 70:if PortB.3=1 then
IF Z=1 THEN CIK
Mode=Mode+1pause 10
IF MODE=4 THEN MODE=0
Z=1
RETURN
ENDIF
Z=0
CIK: RETURN

' ---------------frequency counting ------------------------------------
OLC:
if portb.6=0 then
x=x+1
if x>(Delay+Delay_Us) then return
endif
TMR1L=0
TMR1H=0
T1CON=7
pause Delay
pauseus Delay_US
T1CON=6
FoutL=TMR1L
foutH=TMR1H
RETURN
END

Archangel
- 3rd May 2012, 13:59
Hello Daydream,
Mostly this code demonstrates why "copy/paste" does not work well for newbies, example:


DE@ DEVICE pic16F628
@ DEVICE pic16F628, HS_OSCFINE OSC 10


So allow me to direct you to threads which will help you.
http://www.picbasic.co.uk/forum/showthread.php?t=543

the above thread explaines the first 6 lines of your code.

BTW this code as listed probably will not work due to errors in those 6 lines, and those 6
lines will only work in PBP 2.6 and below, as PBP 3. does not support
PB assembler, only MPASM(read the listed thread carefully).

now the lcd defines
I will comment the code you listed so as to facilitate your understanding,
BTW this IS IN THE BOOK.


DEFINE LCD_DREG PORTA ' use port a for LCD
DEFINE LCD_DBIT 0 ' first data bit on porta.0
' D4 or LCD pin 11
' so PortA.1 = D5, A.2 = D6,A.3=D7
DEFINE LCD_RSREG PORTB 'LCD RS reg is on port B
DEFINE LCD_RSBIT 2 'use PortB.2 as rs bit Pin4 on LCD
define LCD_RWREG PORTB'LCD RW register is port B define should be ALLCAPS
DEFINE LCD_RWBIT 1 'use PORTB.1 as read/write bit pin 5on LCD
DEFINE LCD_EREG PORTB 'use PORTB as E register
DEFINE LCD_EBIT 0 'use PORTB.0 for e bit pin 6 on LCD
DEFINE LCD_BITS 4'tells compiler you are using 4 bit data
'you could be using 8 or a whole port

DEFINE LCD_LINES 2 ' this is more confusing a typical 4 line LCD is really a 2 line
' cut in half and stacked together

DEFINE LCD_COMMANDUS 4000 ' 4000 micro second delay
DEFINE LCD_DATAUS 250 '250 micro second data delay
TRISA=0 'sets all of portA to output status
TRISB=%00111100' sets PortB.2:5 to input status
'error as portb.2 should be an output

fout var word ' make word variable named fout
foutL var fout.byte0 'alias fout lower byte as foutL
foutH var fout.byte1 'alias fout upper byte as foutH

mode var byte 'create byte variable named mode
Delay VAR WORD 'create word variable named Delay
Delay_Us var WORD'create word variable named Delay_Us
x var word 'create word variable named x
tenth var byte 'create word variable named tenth
Z VAR BIT'use up a byte to create a bit variable named Z
'-------------------------------------------------------------------
Z=0' set value (initalize) bit variable to zero
CMCON=7 'turn OFF analog comparators
mode=0 ' set value (initalize) byte variable named mode to zero

daydream
- 3rd May 2012, 15:33
hi archangel,i have pic16f628a.what are comparators?how do I turn them off?with this CMCON=7?
and if i connect my lcd and use a 4mhz cristal and pin RB6 for input,will lcd show the freq?
thank you so much.

mister_e
- 4th May 2012, 09:00
try it, read the datasheet...

Archangel
- 6th May 2012, 08:51
hi archangel,i have pic16f628a.what are comparators?how do I turn them off?with this CMCON=7?


A comparator is like an OP AMP, your 628A has them as analog inputs, so to use those pins as digital you disable them with in this case CMCON = 7