PDA

View Full Version : Exploring Pic16F628a...



mbox
- 13th October 2008, 15:14
Hello everyone,
I'm trying to turn On Led1,Led2,Led3 with the Hyperterminal, using pic16F628a. I have this initial code that simply blinks 3 leds. Please guide me on how to configure the bits that enable RX in Rb1 using 4 Mhz internal clock. Please check my initial code below...
[codes]
Define OSC 4
CMCON = 7 ' PortA = digital I/O
VRCON = 0 ' A/D Voltage reference disabled
TRISB = %11110000

PortB = %00000000 ' Turn pins to low state
Led1 var byte ' Led1 as a byte(8 bits) variable
myvar var word ' myvar as a word(16 bits) variable

begin:
high PortB.0
pause 100
low PortB.0
pause 100
high PortB.1
pause 100
low PortB.1
pause 100
high PortB.2
pause 100
low PortB.2
pause 100
goto begin
[\codes]

skimask
- 13th October 2008, 17:17
I'm trying to turn On Led1,Led2,Led3 with the Hyperterminal, using pic16F628a. I have this initial code that simply blinks 3 leds. Please guide me on how to configure the bits that enable RX in Rb1 using 4 Mhz internal clock. Please check my initial code below...

Or...you could read the datasheet for the PIC16F628A and pick out the registers you need to set up and try to write some code...
Or...you could use the SERIN/SERIN2 commands built into PBP and not really have to worry about setting up any USART bits...AND, you wouldn't have to use a level converter (such as a MAX232) to get the right signal levels, since SERIN/SERIN2 can receive 'inverted' RS232 whereas the hardware unit can not...
Then, after you've written some code, and it does not work, or work in ways you did not anticipate, you post that code here. When that is done, I'm sure a lot of people will jump right in and help you troubleshoot it.

Archangel
- 13th October 2008, 17:38
OK you have come back. Here is the config, add this to the top of your code: @ device pic16F628A, INTRC_OSC_NOCLKOUT, wdt_on, mclr_on, lvp_off, protect_off 'it is correct, and it most likely will fail to compile, because PBP automaticly adds a default config to your code, which will need to be disabled.<br>
In your PBP root directory there is a file called 16F628A.inc , open it and add a "<font color=red> ;</font color> " where you see them in the code below which is a copy of that file.


;************************************************* ***************
;* 16F628A.INC *
;* *
;* By : Leonard Zerman, Jeff Schmoyer *
;* Notice : Copyright (c) 2003 microEngineering Labs, Inc. *
;* All Rights Reserved *
;* Date : 11/06/03 *
;* Version : 2.45 *
;* Notes : *
;************************************************* ***************
NOLIST
ifdef PM_USED
LIST
include 'M16F62xA.INC' ; PM header
;device pic16F628A, xt_osc, wdt_on, mclr_on, lvp_off, protect_off
XALL
NOLIST
else
LIST
LIST p = 16F628A, r = dec, w = -302
INCLUDE "P16F628A.INC" ; MPASM Header
;__config _XT_OSC & _WDT_ON & _MCLRE_ON & _LVP_OFF & _CP_OFF
NOLIST
endif
LIST

After doing this it should compile. All this was necessary because you want to use internal OSC instead of a 4 mhz resonator or crystal, which is what the DEFAULT configs are set up for.

Acetronics2
- 13th October 2008, 19:28
Hi, Joe and MBox

I remember Mel had released a really nice example of such interfacing ...

May be she will remind ...

Alain

PS : BINGO !


http://www.picbasic.co.uk/forum/showthread.php?t=573

Archangel
- 13th October 2008, 23:55
Hello everyone,
I'm trying to turn On Led1,Led2,Led3 with the Hyperterminal, using pic16F628a. I have this initial code that simply blinks 3 leds. Please guide me on how to configure the bits that enable RX in Rb1 using 4 Mhz internal clock. Please check my initial code below...
<font color = red>[codes]</font color>
Define OSC 4
CMCON = 7 ' PortA = digital I/O<font color=red> not quite, it means Comparators off.</font color>
VRCON = 0 ' A/D Voltage reference disabled
<font color=blue>"lets add this so PortA does not act like a radio antenna and create Ghost problems
PortA=0
TRISA=0
</FONT COLOR>
TRISB = %11110000 '<font color=green>ports7:4 inputs, ports 3:0, outputs, is that what you want? </font color>

PortB = %00000000 ' Turn pins to low state<font color=red> Put this before TRISB so when PortB becomes all outputs they initialize in a state set by you, instead of chance</font color>
Led1 var byte ' Led1 as a byte(8 bits) variable
myvar var word ' myvar as a word(16 bits) variable

begin:
high PortB.0
pause 100
low PortB.0
pause 100
high PortB.1
pause 100
low PortB.1
pause 100
high PortB.2
pause 100
low PortB.2
pause 100
goto begin
<font color = red>[\codes]</font color>
Should be CODE not CODES
<br>
<b> OH BY THE WAY, Congratulations, you have just written your first successful LED chaser, project using PBP and internal OSC, it works very nicely here.

mbox
- 17th October 2008, 00:40
Hello everyone!
I appologized the late reply, my computer broke down, had to replace the hard drive. I appreciate to everyone who is helping me alot(specially joe). Anyway this is my updated code.

[CODE]
@ device pic16F628A, INTRC_OSC_NOCLKOUT, wdt_on, mclr_on, lvp_off
VRCON = 0 ' A/D Voltage reference disabled
PortA=0
TRISA=0
PortB = %00000000
TRISB = %11110000
Led1 var byte
myvar var word

begin:
high PortB.0
pause 100
low PortB.0
pause 100
high PortB.1
pause 100
low PortB.1
pause 100
high PortB.2
pause 100
low PortB.2
pause 100
goto begin

[\CODE]

Joe, I followed your intruction to disable the line in the16f628.inc file. No errors when compiled. But before I disabled it, I add the line on top, compiled with no error also.

thanks in advance,
mbox

Archangel
- 17th October 2008, 08:29
But before I disabled it, I add the line on top, compiled with no error also.

Huh... it should have errored, oh well . . . continuing onward. This lesson we will put some information into those variables and do something based upon the content of those variables.
Oh and the reason it does not work for you to have code display in a code window is you used a backslash \ it should be a forward slash /, so


@ device pic16F628A, INTRC_OSC_NOCLKOUT, wdt_on, mclr_on, lvp_off, protect_off
Define OSC 4
CMCON = 7 ' PortA = digital I/O not quite, it means Comparators off.
VRCON = 0 ' A/D Voltage reference disabled
'"lets add this so PortA does not act like a radio antenna and create Ghost problems

PortA=0
TRISA=%00000111

TRISB = %11110000 'ports7:4 inputs, ports 3:0, outputs, is that what you want?

PortB = %00000000 ' Turn pins to low state Put this before TRISB so when PortB becomes all outputs they initialize in a state set by you, instead of chance
Led1 var byte ' Led1 as a byte(8 bits) variable
myvar var word ' myvar as a word(16 bits) variable
start:
include "modedefs.bas"





begin:
for myvar = 500 to 0 step - 25 ' 500 makes word variable necessary
high PortB.0
pause 100
low PortB.0
pause 100
high PortB.1
pause 100
low PortB.1
pause 100
high PortB.2
pause 100
low PortB.2
pause 100
'goto begin
next myvar

main:
led1 = 4
'
if porta.0 = 1 then led1 = 0
if portA.1 = 1 then led1 = 5
if PortA.2 = 1 then led1 = 7
if LED1 < 2 then high portb.0
if LED1 = 5 then high portB.1
if LED1 > 5 then high PortB.2
pause 1000
portb = 0 ' this turns off the leds
goto main
end

in this code we have all of your previous code so the led chaser will chase for a while then read the PortA lines, and then perfor action based upon the values written in the variables based upon the inputs of portA storing those values into the variables.<br>This code will switch the LEDs upward pretty fast but for reasons I do not yet grasp they switch downward very slowly, but they do switch.<br> Next installment we will send variable information via serial data.

mbox
- 18th October 2008, 00:26
Good morning Joe,
I compiled your updated code with no errors, and it says 183 words used....Running leds from PortB.0-PortB.2. until myvar reached 0 value (very nice).

On the main label, As you mentioned and as I understand it, if any high value reads on PortA.0-PortA.2 a numeric value on variable Led1 will be assigned and test if the conditions are true to turn on(high) PortB.0 - PortB.2.

thanks,
mbox

Archangel
- 18th October 2008, 07:49
Hi mbox,
Yep, that's essentially it, now all you have to do is receive some information from outside the PIC, store it in the variables and test IF TRUE. So . . .

serin portA.3,N9600,["A"],Led1 ' pretty much right from the book
' reads data from PC data received after "A" stored in LED1 comment out
' PortA IF THEN loops

Add this to your code and comment out the portA if then loops, send it some data from another PIC or your personal confuser ugh . . . computer and you have what you asked for, what's more, now you understand your code and can make whatever variables and outputs you want.
Look at BRUCE's website, check these 2 links:
http://rentron.com/PIC16F84.htm
http://rentron.com/PicBasic1.htm
He has lot's of great examples, and he sells stuff too! Highly recommend.

mbox
- 18th October 2008, 13:39
Good evening Joe,
Thanks for this helpful tutorial, but I'm not really sure if I doing it right regarding the SERIN command...I tried to simulate it in Proteus software and no result...



@ device pic16F628A, INTRC_OSC_NOCLKOUT, wdt_on, mclr_on, lvp_off
Define OSC 4
CMCON = 7 ' PortA = digital I/O not quite, it means Comparators off.
VRCON = 0 ' A/D Voltage reference disabled
'"lets add this so PortA does not act like a radio antenna and create Ghost problems

PortA=0
TRISA=%00000111

TRISB = %11110000 'ports7:4 inputs, ports 3:0, outputs, is that what you want?

PortB = %00000000 ' Turn pins to low state Put this before TRISB so when PortB becomes all outputs they initialize in a state set by you, instead of chance
Led1 var byte ' Led1 as a byte(8 bits) variable
myvar var word ' myvar as a word(16 bits) variable
start:
include "modedefs.bas"
begin:
for myvar = 500 to 0 step - 25 ' 500 makes word variable necessary
high PortB.0
pause 100
low PortB.0
pause 100
high PortB.1
pause 100
low PortB.1
pause 100
high PortB.2
pause 100
low PortB.2
pause 100
'goto begin
next myvar
PortB = 0
main:
led1 = 4
loop:
'************************************************* ******
serin portA.3,N9600,Led1

if Led1 = 0 then led1 = 1
if Led1 = 5 then led1 = 5
if Led1 = 7 then led1 = 7
if LED1 = 1 then high portb.0
if LED1 = 5 then high portB.1
if LED1 > 5 then high PortB.2
pause 1000
PortB = 0 ' this turns off the leds

goto loop
end


My question is, should I use RB1(TX) as input instead of PORTA.3, because I wanted to use the builtin USUART. and do I need to add any configuration to enable it?

Thanks in advance,
mbox

Archangel
- 18th October 2008, 18:33
Hserin is always better than serin, works in the background while other code executes as it is hardware based vs bitbang software. I think you are going to need to use a crystal vs intosc because of timing issues especially if you want to use high speed communications, the example I gave you is really too fast for internal OSC, should probably slow it down to something like 300. If you want to use HSERIN you will have to use the appropriate ports, so yes RB1 (RX) as input, there is some code you have to add to use HSERIN too, lots of example code in this forum.

mbox
- 18th October 2008, 23:01
:) Thanks for the help Joe appreciate it very much.

Regards,
mbox