PDA

View Full Version : undocumented code



ilteris
- 19th October 2005, 02:13
Hi list,
I just have found about this code in this forum but when I tried to make it work, it seems it is not working, I am trying to keep my pins in an array to re-call them later and it seems this way is no go, so if you advice me a way to do it I'd appreciate it so much.

ilteris.

TRISC = 0
myArray var PORTC
i var byte

main:
for i = 0 to 7
myArray.0[i]= 1
myArray.0[i-1] = 0
pause 500
next
goto main

mikefox
- 19th October 2005, 02:30
I'm not at home, so I can't compile this, but I think it should work. I assumed you wanted to write back to the port again. As far as i know, you can't write an array in one go, you need to write each byte seperately.

TRISC = 0
i var byte

main:
for i = 0 to 7
PORTC.i = 1
PORTC.(i-1) = 0
pause 500
next
goto main

ilteris
- 19th October 2005, 04:36
thanks for the quick reply mike. I dont have any problems compiling the code although when I try it on my circuit it is giving me weird things, only making the first pin high mode and then setting it low and giving to the rest a lil bit current, I can see a very brief light on them.

All I want is just to makethem high and make them stay at that way.

any comments?

Bruce
- 19th October 2005, 05:35
You don't mention which PIC you're using, but assuming it doesn't have some peripheral functions on portC, you're using limiting resistors on the LED's, and have them connected properly, your code should work fine.

You may want to change the myArray.0[i-1] = 0 however.

On the 1st pass, 0-1 = 255 which probably isn't doing what you may expect.
On the last pass, 7-1 = 6, and you'll never clear RC7.

Try something like this;


main:
for i = 0 to 7
myArray.0[i]= 1
pause 50 ' or whatever
myArray.0[i]= 0
next
goto main
If I run the above example on a 16F876A, it turns on/off 8 LED's on portb in ~50mS intervals. I don't have a board with 8 LED's available on portc, but it should work all the same on any available 8-bit port. Assuming this port on the PIC you're using doesn't have some peripheral functions you have enabled.

ilteris
- 19th October 2005, 06:41
Thanks for the response Bruce. I am using 18f452 and what if I try to change your code like this below:



main:
for i = 0 to 7
myArray.0[i]= 1
next
goto main


does this mean they are all going to be high and stay at that position? I have been trying to do this for 2 days now wit no dice.

hmr
- 19th October 2005, 10:34
Hi Ilteris,

If I do remember what the manual says about pins, using the index on a 40 pin chip it will make reference to Port B and C. So index from 0 to 7 refers to Port B and 8 to 15 to port C.

So if you want to high pin PortC.0 you can address to it like this:

High PortB.0[8]

so your code should look look like this:

TRISC = 0
myArray var PORTB
i var byte

main:
for i = 8 to 15
myArray.0[i]= 1
myArray.0[i-1] = 0
pause 500
next
goto main

Hope this help.

Hans

Bruce
- 19th October 2005, 16:09
Yes.

If you use the following, then each pin on portC will stay high.


main:
for i = 0 to 7
myArray.0[i]= 1
next
goto main
All you are doing with myArray var PORTC is creating an alias or another name for portC. This does NOT create a normal variable using RAM. It only creates another name or alias to portC that is used only at compile time. Each reference to myArray will reference portC.

Using myArray.0[i] just indexes individual bits or pins on portC. The .0 after the name just tells PBP it's a bit index.

You would get the same result with something like;


main:
for i = 0 to 7
PORTC.0[i]= 1 ' <-- make PORTC.Bit i high
next
goto main
The method mentioned above by Hans will work also. However, just which pins this will work on is dependent on the PIC you're compiling for.

You have to be careful with this method of array.bit indexing (or any other array) since PBP does perform bounds checking.

I.E. if you have an array (myArray) limited to 16-bits in size, it would hold 16-bits from 0 to 15. You can still index a bit position outside the bounds of this array with something like myArray.0[16]=1. This will not generate an error at compile time, and it will set a bit outside the bounds of your array. So you need to be careful. You can affect areas outside the bounds of an array.