PDA

View Full Version : Very first use of PBP (newbie!)



rjones2102
- 15th September 2007, 15:44
Hi all,

I just got my first program working on PBP. I have bought lots of books but am waiting for them to arrive. I was just wondering why I had this problem getting the blink.bas example working.

Portb.0 doesn't work. I tried portb.1 by chance and it worked. As I understand it, there are all sorts of registers to set and I have got the datasheet for the PIC I am using (16F886) but for someone as new as me, the datasheet doesn't tell me why. I have got a lot to learn!

I thought High and Low caused pins to become outputs so I didn't need to do anthing special with registers.

I have done quite a bit of work with PICAXE and when I change the osc freq on PICAXE the pause command is affected. I tried changing the freq from 4 to 8 in this code example and expected the LED to blink twice as fast but it didn't. Is PBP cleverer or have I not actually doubled the clock freq. I am using the internal osc.

Can anybody lend me a small hand with getting started on understanding datasheets and this specific problem.

Thanks

Rich


define osc 8
led var Portb.0
led2 var Portb.1
loop: High led ' Turn on LED connected to PORTB.0
High led2 ' Turn on LED connected to PORTB.0
Pause 500 ' Delay for .5 seconds

Low led ' Turn off LED connected to PORTB.0
low led2
Pause 500 ' Delay for .5 seconds

Goto loop ' Go back to loop and blink LED forever
End

Kamikaze47
- 15th September 2007, 16:55
This may not be the problem, but try setting all the I/O to digital (i.e not analog) by adding the following to the top of your code:


ANSEL=0
ANSELH=0

For info on the ANSEL registers, see pages 42 and 50 in the 16F886's datasheet.

BobK
- 15th September 2007, 17:56
Hi Rich,

Just declaring DEFINE OSC 8 does not set the clock completely. Are you using and external crystal or resonator or are you using the internal oscillator?

You program looks like it should work once the oscillator question is determined. It would hurt to add at the start of your program: TRISB = %00000000. 0's set the port pins to outputs and 1's set the pins as inputs. So if you wanted Port B.2 for example to be an input and the rest of Port B to be outputs then TRISB = %00000100 would take care of that. High and low statements make the particular output pin either high or low. You could set PortB.0 to low by saying PortB.0=0 or in your case just like you have it in your program.

I was trying to get online with Microchip to look at the datasheet for your PIC but they seem to be down right now. Look at the pin descriptions near the from of the datasheet. It describes the features available on each pin. If the Port B pins can be analog as well as digital then you must disable the analog feature as Kamikaze was saying.

I really think from the very limited information you have provided the problem is in the oscillator. Give us more information on that: internal vs external: if external are you using a crystal or resonator, 4 or 8mhz. I won't be back until tonight so maybe someone else might jump in here to help out, but I think what I have put here should help you get on track.

HTH,

BobK

GrandPa
- 15th September 2007, 19:00
Rich,

"define osc 8" Is NOT changing oscillator frequency, It's just telling PBP at which frequency the oscillator is running. And most important, all defines must be in UPPERCASE when using PBP. Not 100% sure if required, but I would also try writing PORTB.0 uppercase.

BobK is right too, try adding define TRISB = %00000000. I don't exactly remember why, but I had some problem a while ago using high and low without having defined the port as an output, (but PORTB.0=1 was working fine!)

Picaxe is fantastic to learn PIC, but is missing all details related with PIC registers. When you're programming with PBP you have to read and understand the datasheet of the chip you're using. Then you will be able enjoy PBP and use all the flexibility of you're PIC.

J-P

rjones2102
- 16th September 2007, 05:56
Hi all,

thanks for your help everyone. It ended up being the setting of ANSEL and ANSELH. I added the ANSEL line and portb.0 started working. So I tried to alternate all portb pins between low and high in same block of code. Then the portb.0 through portb.5 pins didn't work but portb6 and 7 did. So I added ANSELH=0 and then they all started working.

I have learnt something. Read and understand the datasheet. This is my first experience of translating the datasheet into programming registers and now I know how important it is. It's not a PICAXE!!

So if DEFINE OSC 8 doesn't change internal osc freq, what does I wonder. I am of course, not using an external osc and the chip supports 8mhz. I will read datasheet and see if I can work it out but if I haven't posted back here by the time someone sees this, please enlighten me!

Thanks again guys

Rich

Kamikaze47
- 16th September 2007, 06:05
Check out page 64 of the datasheet. It will tell you how to set the internal oscillator frequency.

To set the oscillator to 8Mhz for example:


define osc 8
OSCON=%01110000

or 4Mhz:


define osc 4
OSCON=%01100000

*edit* Page 64 according to adobe anyway - page 62 according to the page numbers at the bottom of the pages.

Archangel
- 16th September 2007, 06:13
Hi Rich,
You picked a PIC with lots of options that differ from run of the mill, Multi config registers and the like. Hard to learn to drive in a performance car, maybe start out with a Rambler or a Fiat:) check your other post for answers.
JS

rjones2102
- 16th September 2007, 11:19
Cool guys, very cool. I now understand how to use the registers a lot better. Setting the osc freq makes perfect sense!

I am using the 16f886 because I used to do a lot with Picaxe and it is the chip that the 28x1 Picaxe chip is based on. I have a few of them so I am reusing.

When I was young I used to do lots of handbrake turns and all sorts of crazy stuff. Whilst I'm sure I am lucky to still be alive, I really believe it has made me a better driver!

Thanks again

Rich