PDA

View Full Version : First Day and need Help!



timmoore47
- 18th May 2006, 13:46
Just getting started but unfamiliar with this Micro Code Studio environment.

I get an error message about not supporting 16F877 on a sample program.

Well I want to program 16F628A and 16F84A Pics, so how do I do that?

Normally I'd start with

Device 16F628
Dim A, B
Define PortA %00000000
etc etc

but what do I need to get this off the ground?

A puzzled,

TIM

PS Any really noddy tutorials around?

mister_e
- 18th May 2006, 15:18
If the 16F877 is on the drop list it must work.

remove your config fuse to see. If there's any other problem, tell us your PBP version, Microcode Studio version and also post your code here.

timmoore47
- 18th May 2006, 15:36
If the 16F877 is on the drop list it must work.

remove your config fuse to see. If there's any other problem, tell us your PBP version, Microcode Studio version and also post your code here.

Err... I don't know where the drop list is. THe version of Micro Code Studio is v 2.2.1.1

The Read.me file says this:-
PICBASIC(TM) Compiler Ver. 1.45 README.TXT
Copyright 2005 microEngineering Labs, Inc.

I don't know if it's integrated correctly Into PIC BASIC Standard.

The code is one of the Samples:-


' Simulate BS2 Shiftin and Shiftout

Symbol DDIR = Dir0 ' Shift data pin direction is Dir0
Symbol DPIN = Pin0 ' Shift data pin is 0
Symbol CPIN = 1 ' Shift clock pin is 1

Symbol I = B2 ' Loop counter


' Shift in some data
Low CPIN ' Start shift clock low

Gosub shiftin ' Shift in some data

' Shift out some data
Low CPIN ' Start shift clock low

B0 = 100 ' Data to shift out
Gosub shiftout ' Go do it

End


' Subroutine to synchronously shift in one byte
shiftin: DDIR = 0 ' Set data pin direction to input

For I = 1 to 8 ' 8 bits to a byte
B0 = B0 * 2 ' Shift result 1 bit to the left
Toggle CPIN ' Toggle shift clock
Bit0 = DPIN ' Move data into LSB
Toggle CPIN ' Toggle shift clock once more
Next I ' Loop

Return ' Go back to caller


' Subroutine to synchronously shift out one byte
shiftout: DDIR = 1 ' Set data pin direction to output

For I = 1 to 8 ' 8 bits to a byte
DPIN = Bit0 ' Data out is LSB
Toggle CPIN ' Toggle shift clock
B0 = B0 / 2 ' Shift byte 1 bit to the right
Toggle CPIN ' Toggle shift clock once more
Next I ' Loop

Return

mister_e
- 18th May 2006, 15:59
Drop list <img src="http://www.picbasic.co.uk/forum/attachment.php?attachmentid=872&stc=1&d=1147960259">

Now with PBC... iiiish i'm sorry i couldn't be much help but i believe that...



Symbol DDIR = Dir0 ' Shift data pin direction is Dir0
Symbol DPIN = Pin0 ' Shift data pin is 0
Symbol CPIN = 1 ' Shift clock pin is 1

Symbol I = B2 ' Loop counter

Is invalid. I know you must use or redefine specific variable name.. about the PORTA, PORTB.. name it's sure you can't use Dir0, Pin0 and 1

Fortunately, there's some user here who use PBC and can help.

timmoore47
- 18th May 2006, 17:14
Ok, Has anyone got a very simple code fragment I could try?

So far I've not got off ground zero!

Maybe I've got a duff install?

:(

Tim

mramos
- 31st May 2006, 23:00
I have a few, what type program are you after. I have one that runs two servos, to move my robot dog (PWM commands to servos). It is old and I would have to clean it up.

I have one that reads and LDR and buzzes a Piezo buzzer every 30 seconds.

What are you tryig to do?

timmoore47
- 1st June 2006, 09:12
Thank you for responding.

I'm building a video switching unit. To choose between 5 video channels and just switch one of them to a DVD Recorder. It's monitoring nocturnal wild Hedgehog activityin a UK garden.

I've got 5 PIR Sensors which kick out +5 volts when a hedgehog passes by.

So for each binary pattern on PortA, a single relay attatched to the first 5 of PortB pins is switched.

So I need to set PortA to be inputs, PortB to be outputs then read portA, make a decision, set a relay and delay, then do it again.

Easy enough on paper but PBC code seems to use different commands to other PicBasics I've used and I got confused badly on setting up and reading PortA and defining variables.

:)

Tim

mramos
- 1st June 2006, 12:48
I am heading for work now. So not much time.

You need to symbol portA and portB to there locations.
Then peek portA (say into B0), do any logic on it
Then poke portB with your results from B0.

What happened if two PIRs are tripped? Do you have a priority for them?
You will probably want to make sure there is only one bit set in B0 otherwise
you will have two or more relays flip on (multiple video coming in).

If this does not help, later I will see if I can send a sample. Just running late right now.

timmoore47
- 2nd June 2006, 09:40
Yes it does help!

To avoid two video channels is key to getting this program right.

With a 5 bit word there are 64 possible input patterns, mostly it will just be a logic '1' with it
just passing the data staight thro.

But it gets very interesting when several go off! That will need data translation based on the significance of the pattern.

IF A0 = 6 then PortB = 7 '4th video channel selected

etc

I'm not confident yet I've got the symbol/peek/poke syntax right. I've been posted the manual now, but feel very nervous about producing working code right now, so a code example would be brilliant!

Many thanks again for responding.

:)

Tim

paul borgmeier
- 2nd June 2006, 17:32
Tim,

Do not make the peek/poke thing harder than it is. Think of it like this: Peek is literally peeking (or reading) the value of a register (register is a fancy word for variable) and poke is literally poking (or writing) a new value to the register. I like to think of the whole thing as two filing cabinets – one for RAM (registers and variables) and one for Porgram Memory. With PBC, each filing cabinet has only one drawer.

Imagine if you were an accountant and wanted to know how much money was in the “Johnson” account, you would probably go the filing cabinet, open it up, find the “Johnson” folder, and peek inside to see the value. In this example, the “Johnson” folder is the “address” of physical location of the file so you can find it easily every time.

PICs work the same way; they keep specific data in easy to find locations (called addresses) so you (and the PIC) can find it easily. The map of where these locations are detailed in the data sheet. You MUST download the 16F877 data sheet and look on page 13. Did I mention that you must get the data sheet! Here you will see that PORTA data is kept in location 5 of the “filing cabinet”, PORTB data is kept at Location 6, TRISA data is kept at location 133 (=85 in hex), and TRISB data is kept at location 134 (= 86 in hex). That's it. So if you want to change the value of PORTB, you need to open your filing cabinet, pull out file number 6 and write (i.e, poke) new values. If you want to setup PORTA as outputs, go to your filing cabinet, pull out file 134 and write your new values. If you want to know what value are on PORTA, go to your filing cabinet and take a peek in file 5. It will become straight forward.

You can set up Symbols to help you – but they are not necessary (but I recommend it).

For example

Symbol TRISA=133
Peek TRISA, B0

does the same thing as
Peek 133, B0

so does this
Symbol BlahBlah=5
Peek BlahBlah, B0

Most people set up the symbols (which only need to be done once) early in the program to make this easier to read.

Back to your HedgeHogs...

Here is a sample code that might get you going a bit

Symbol TRISA=133
Symbol TRISB=134
Symbol PORTA=5
Symbol PORTB=6

Symbol HogPIR = B0 ' B0 goes from 0 - 255


Symbol TimeOn=B1 ' B1 goes from 0-255

Poke TRISA, 31 ' make RA0-RA4 Inputs, rest outputs
Poke PORTA, 0 ' make PORTA outputs low

Poke TRISB, 0 ' make PORTB all outputs
Poke PORTB, 0 'make PORTB outputs low

PAUSE 2000 ' pause for 2 seconds for things to settle

Main:
Pause 1000 ' 1 second delay
TimeOn = TimeOn + 1

If TimeOn < 30 then SamplePIR
Poke PORTB, 0 ' turn off all VCRs – no PIR trip for 30 seconds

SamplePIR:
Peek PORTA, HogPIR
if HogPIR=0 then Main ; wait for Hog

TimeOn = 0 ' reset VCR time on

Poke PORTB, HogPIR ' make VCR relays on PORTB match PIR sensors of PORTA
goto Main

Note – or Tim, instead of PORTB mirroring PORTA for high signals, you could do smart sampling

If HogPIR = 1 then VCR0 ' RA0 = 1 so make RB0 = 1
If HogPIR = 2 then VCR1 ' RA1 = 1 so make RB1 = 1
If HogPIR = 3 then VCR0 ' RA0 and RA1 = 1 but only make VCR0 = 1
etc


VCR0:
POKE PORTB, 1
goto Main

VCR1:
POKE PORTB, 2
goto Main

VCR2:
POKE PORTB, 4
goto Main

VCR0_3:
Poke PORTB, 9
goto Main

etc.

I do not have PBC so I have not tried this but believe this should work

HTH,

Paul Borgmeier
Salt Lake City, Utah
USA

mramos
- 2nd June 2006, 17:36
You can also test the bits in the priority that you want them and jump to the right routine to flip the relay on, then no fancy stuff (masking, tables, lookups, etc).

like (below is an idea, no idea if it will run) And it will look bad in PBC do to lack of IF ENDIF, but this should get you rolling. Assuming port A and B are PIR0 is RELAY0. Note I do not have the PBC manual with me and just received it and did not start using it do to conditionals, so below is just an idea. Take below and clean it up. Also, do not recall the two commands, the lookup and one other you can index and jump to them. Hope this helps.

Symbol PortA = 5 'assuming it is 5
Symbol PortB = 6 'check the ports in the PIC datasheet
'set PortA and B directions and make sure PortA is in digital mode (some pic need that)

top:
peek PortA, B0 'might have these backwards (I am at work no PBC here
if bit4 = 1 then turnon4 'highest priority camera
if bit2 = 1 then turnon2 'etc
goto top

turnon4:
poke PortB,%00010000
goto top

turnon2:
poke PortB,%00000100
goto top

timmoore47
- 2nd June 2006, 20:26
Many thanks for that I'll take a very close look at it.

Should watchdog fuse be ticked or not at time of flashing?

Whilst I don't understand bits of it, I've been messing about and freely adapted a chunk of code posted by Lauren Barta

Code:-

Symbol TRISB = 134 'Symbol For TrisB Is Decimal 134
Symbol TRISA = 133 'Symbol For TrisA Is Decimal 133
Symbol PORTB = 6 'Symbol For PortB Is Decimal 6
Symbol PORTA = 5 'Symbol For PortA Is Decimal 5


Poke 134, 0 'Makes All PortB Pins Outputs
Poke 133, 255 'Makes All PortA Pins Inputs

Start:

Poke 6, 0 'Makes PortB Pins Low
Pause 10 'Short Pause

Run:
LET B2 = 0 'Making B2 a zero
Pause 10 'Short Pause
Peek PortA, B0 'Look at PortB, put results in B0

Filters:
IF B0 = 3 THEN Run1 'start of 24 filters, 23 yet to be added
poke 6, B0
Pause 7500 'Wait 3 seconds
goto Run 'Start all over again!

Run1:
B0 = 5
poke 6, B0
Pause 7500 'Wait 3 seconds 10 Mhz clock
goto Run 'Start all over again!

End

mramos
- 3rd June 2006, 01:28
This is what I meant when I mail the last post from work. Use a couple of your lines.



Symbol TrisB = 134
Symbol TrisA = 133
Symbol PortB = 6
Symbol PortA = 5

Poke TrisB, 0 'Makes All PortB Pins Outputs
Poke TrisA, 255 'Makes All PortA Pins Inputs

Poke PortB, 0 'Makes PortB Pins Low

'depending on PIC type, make sure PortA is set for Digital IO

top:
pause 7500
peek PortA, B0 'read PIRs or sensors

'check the sensor bits
if bit4 = 1 then turnon4 'highest priority camera
if bit2 = 1 then turnon2 'etc
if bit1 = 1 then turnon1 'etc
if bit3 = 1 then turnon3 'etc
if bit5 = 1 then turnon5 'etc
if bit0 = 1 then turnon0 'etc

goto top

turnon0:
poke PortB,%00000001
'76543210 'The relay to turn on is 0
goto top

turnon1:
poke PortB,%00000010
'76543210 'The relay to turn on is 1
goto top

turnon2:
poke PortB,%00000100
'76543210 'The relay to turn on is 2
goto top

turnon3:
poke PortB,%00001000
'76543210 'The relay to turn on is 3
goto top

turnon4:
poke PortB,%00010000
'76543210 'The relay to turn on is 4
goto top

turnon5:
poke PortB,%00100000
'76543210 'The relay to turn on is 5
goto top

timmoore47
- 3rd June 2006, 20:30
Hi Paul & MrAmos,

Thank you both so much for your substantial samples of code. Because of them I was able to complete and test the code using a test jig of a bunch of switches and led's for driving the video switch and pir inputs.

FANTASTIC! IT WORKED AOK!

I've got to finish off the PIR Inputs circuits and the video switch circuits but the hardware is easy compared to driving the PICs.

I've avoided assembler which would have slowed the project down loads.

The Hedgehog (European Wild variety) breeding season is upon us and in a about 3 weeks we hope to be able to track and record movement of the hoglets. We hope somewhere between 3 and 9 will arrive in the end of June batch and similar numbers in September and October.

Curently we track 13 adult hedgehogs manually up to about 3am most nights and are VERY keen automate it. Since January we have filled 136 DVD's full of data burnt out two DVD Recorders and now use a Pioneer 80GB Recorder *LOL*.
Many thanks again!

:) :) :)

Tim

paul borgmeier
- 3rd June 2006, 22:03
Tim,

Post a picture to this thread of a hoglet camptured by PBC code - everyone would enjoy that (unless they do not like hedgehogs). We do not have hedgehogs here in Utah as far as I know.

As a side note, you might want to check out this guy's stuff - awesome homebrew PIR camera stuff. I have made some of these camera modifications and they work quit well.

http://www.jesseshunting.com/forums/index.php?showforum=50

As always
Good Luck,

Paul Borgmeier
Salt Lake City, Utah
USA

timmoore47
- 3rd June 2006, 22:15
Many thanks Paul, I'll do that. Meanwhile here is a link to a video we made of a Hedgehog plus some other wild and not so wild life. The last few minutes is the best.

Totally tame for non wild-life folk, BTW>

;)

Tim

link:-

http://video.google.com/videoplay?docid=5993272146235684890

mramos
- 3rd June 2006, 23:00
Tim:

Good stuff. My kids loved it (3 boys).. We used to, a few years ago, I would put a PIR an a long cat 5 wire in the yard, and a battery and buzzer at the other end. When a raccoon would come up (I would leave sardines in front of the PIR) go on the balcony and watch them eat. They loved it..

It was funny, you hear the buzzer, and kids running up the stairs.. It reminded me of those days.

It us know if you have any questions. Or hardware issues to resolve.

timmoore47
- 3rd June 2006, 23:13
Thanks MrAmos! Your Racoons sound fun!

Hardware should be OK but I'll keep in touch as to the progress!

:)

Tim