PDA

View Full Version : pins in arrays?



scorpion
- 29th March 2005, 18:12
can i do something like this?
ok a better question:
how can i do something like this

outpin[0] var porta.0
outpin[1] var porta.1
outpin[2] var porta.2

for i = 0 to 2
high outpin[i]
pause 500
next i

for i = 0 to 2
low outpin[i]
pause 500
next i

Melanie
- 29th March 2005, 18:25
For the ONLY reliable answer, see your PBP manual section 4.11 Pins.

There are other solutions, but their behaviour is not consistant across different PIC types - so best not go there for now.

Here's a reprint of a posting from April 2003 (on the email forum) giving a code example of pin addressing...



Melanie's Bouncing Ball
===================

Example of what we've been talking about... No - I've not had a
sex-change! Everyone does Cylons or Knight Rider as an example
(boring - that's for kid's)... we'll be different (adult, grown-up,
X-rated version)... there I was all dressed up in my tight leather
catwoman outfit with chromed spikes, playing with someone's ball (*see
note at end) the other day and watching it bounce...

' MelsBall.bas
' Melanie's Bouncing Ball
' ----------------
' No wise cracks!

' 8 LED's on PORTB, connected Anode to PIC
' all Cathodes commoned and via a single
' 270R Resistor to Vss. since no two LED's
' will be on at a time, we can be economical
' with the Resistors... they're expensive...

'
' PIC Defines
' -----------
@ DEVICE pic16F628, INTRC_OSC_NOCLKOUT
' System Clock Options
@ DEVICE pic16F628, WDT_ON
' Watchdog Timer
@ DEVICE pic16F628, PWRT_ON
' Power-On Timer
@ DEVICE pic16F628, MCLR_OFF
' Master Clear Options (Internal)
@ DEVICE pic16F628, BOD_ON
' Brown-Out Detect
@ DEVICE pic16F628, LVP_OFF
' Low-Voltage Programming
@ DEVICE pic16F628, CPD_OFF
' Data Memory Code Protect
@ DEVICE pic16F628, PROTECT_OFF
' Program Code Protection

'
' Software Defines
' ----------------
PortBPin var byte
DecayBounce var byte
GravityDelay var word

'
' Initialise Hardware
' -------------------
CMCON=%00000111
TRISA=%00000000
TRISB=$00000000
PORTB=0 ' All LED's OFF
' Ground Level = PORTB.0
' Highest Bounce point = PORTB.7

'
' Main Program Loop
' -----------------
DoAgain:
'
' Drop Ball
' ---------
DecayBounce=7
DecayLoop:
GravityDelay=256
For PortBPin=DecayBounce to 0 step -1
High PortBPin
Gosub Accelerate
Low PortBPin
Next PortBPin
'
' Rebound
' -------
DecayBounce=DecayBounce-1
For PortBPin=0 to DecayBounce
High PortBPin
Gosub Decelerate
Low PortBPin
Next PortBPin
'
' New Apex
' --------
If DecayBounce=0 then
High 0
Pause 1500
Low 0
Pause 1500
Goto DoAgain
endif
Goto DecayLoop

'
' Subroutine Accelerates Ball
' ---------------------------
Accelerate:
GravityDelay=GravityDelay/2
Pause GravityDelay
Return

'
' Subroutine Decelerates Ball
' ---------------------------
Decelerate:
GravityDelay=GravityDelay*2
Pause GravityDelay
Return

end

Die-hard's will tell me it's not the formula for gravitational
acceleration - I don't care... it looks good and what do you expect
for a five-minute bit of code and eight LED's. If you got time to
burn, tweak your own formula's...

* Note - Tennis Ball *smiles*

Melanie

mister_e
- 29th March 2005, 18:45
the best way to learn is to try... what can happen in the worst case, compilation error. NOT MUCH. Then once you try everything, you have read the whole PBP manual and all the thread related to your problem/question on this forum AND you bang your head more than hundreds times and your face is full of blood, you'd cry for few hours and get divorced, now you can ask some question :)


No it's a bit too much but this question is asked maybe once a month or so. You'll definetely win to look into your PBP manual as Melanie suggested.

**update** great previous post edit Melanie

Darrel Taylor
- 24th August 2005, 08:58
Hi Melanie,

I was bored tonight, so i thought i'd play with your Ball.

Press Play.

<embed SRC="http://www.darreltaylor.com/files/MelaniesBall.wmv" AUTOSTART="false">

Melanie
- 24th August 2005, 11:00
Hey, I notice you had two! Must be a typical male thing... (not that I know - I'm just an innocent kinda gal)... you get bored and what's the first thing you play with?

Ioannis
- 24th August 2005, 20:27
(not that I know - I'm just an innocent kinda gal)...

Yeah! right. With the "...tight leather catwoman outfit with chromed spikes...".

Quite innocent I say...!

Ioannis

mister_e
- 25th August 2005, 03:26
LMAO... Melanie, that leather kit will stick to you 'till you'll die :)

Chance are that you're still young and fit... 'till you get married, have children, become fat, then divorce, lose weight to make it fit again!!

Probably you have some regret now to talk about that kit... many months/years ago... naaah i don't think so!

Just kidding!

Melanie
- 25th August 2005, 10:30
> Chance are that you're still young and fit

You would dare question that? Sounds like you need a visit from Miss Whiplash!

mister_e
- 26th August 2005, 00:24
<img src=http://www.picbasic.co.uk/forum/attachment.php?attachmentid=445&stc=1>

Melanie
- 26th August 2005, 03:09
hmmmm... she's having a bad hairday... I could comment but this isn't an adult rated forum... *laughs*

Ioannis
- 26th August 2005, 07:42
hmmmm... she's having a bad hairday... I could comment but this isn't an adult rated forum... *laughs*

Mmm, well pointed comment! I have a nice PIC driven hair tool for her (or anyone else too...!). With some 10KV of voltage and an thermal stabilized roller, that hair would look better, I think.

Ioannis

obaskirt
- 16th September 2005, 06:21
For PIC16F877. I have to use "ADCON1=7" instead of "CMCON=%00000111". Is it correct?

Darrel Taylor
- 16th September 2005, 07:06
YES, unless you have a PIC16F877A. In which case, you would need both of them.

Not quite sure how that fits in with this topic though.
<br>

Melanie
- 16th September 2005, 07:18
I was about to say that - you've beaten me to it - but I assume obaskirt's referring to the original example code at the top of this thread which was for a PIC16F628...

Darrel Taylor
- 16th September 2005, 15:16
Yup, I guess that would fit.

I was still thinking about your balls. Got me all flustered. :o
<br>

mister_e
- 17th September 2005, 00:24
be carefull of your wording :)

obaskirt
- 17th September 2005, 08:18
I connected 4 buttons to PORTA's first four pin and also I connected 8 leds to PORTB and a buzzer PORTD 2. pin. You know that PIC16F877's PORTA comes in a analog mode so I used ADCON1=7 and I turned them to digital mode. (If you use PORTA and you do not use ADCON1=7, the code won't work. I tried and I saw that.)

Then, I used this code and it worked, Thanks everybody...



MY CODE:

'---> PORT DECLARATIONS
TRISB = %00000000 ' Make Port B's all pins output
OUTPUT PORTD.2 ' Make Port D's 2. pin output
TRISA = %00001111 ' Make Port A's 0-4 pins input
PORTB = 0 ' Make Port B's all pins 0 Volt
PORTD.2 = 0 ' Make Port D's 2. pin 0 Volt

'---> MAIN CODE
counter VAR BYTE ' Counter for different sounds.
PortBPin VAR BYTE ' This is for turning on leds sequential order.

'Any PICmicro MCU with analog inputs, such as the PIC16C7xx,
'PIC16F87x and PIC12C67x series devices, will come up in analog mode.
'You must set them to digital if that is how you intend to use them:
ADCON1 = 7

loop: 'Our main loop

' For Button T0
IF PORTA.0 = 0 THEN ' If button T0 is pressed then generate different sounds. Counter increment rate is 10.
FOR counter=10 to 130 STEP 10
SOUND PORTD.2, [counter,10,60,10] ' Generate Sound
NEXT counter
ENDIF

' For Button T1
IF PORTA.1 = 0 THEN ' If button T1 is pressed then turn on leds 0 to 7
PORTD.2 = 0 ' Make Port D's 2. pin 0 Volt
FOR PortBPin=0 to 7 STEP 1
HIGH PortBPin
PAUSE 200
LOW PortBPin
NEXT PortBPin
ENDIF

' For Button T2
IF PORTA.2 = 0 THEN ' If button T1 is pressed then turn on leds 7 to 0
PORTD.2 = 0 ' Make Port D's 2. pin 0 Volt
FOR PortBPin=7 to 0 STEP -1
HIGH PortBPin
PAUSE 200
LOW PortBPin
NEXT PortBPin
ENDIF

' For Button T3
IF PORTA.3 = 0 THEN ' If button T3 is pressed then turn on leds inside to outside then outside to inside.
PORTD.2 = 0 ' Make Port D's 2. pin 0 Volt (Buzzer'ı kapatır.)
PORTB = %00011000 ' Port B's middle 4. and 5. pins are turns on.
PAUSE 300 ' Wait 300 ms
PORTB = %00100100 ' Port B's middle 3. and 6. pins are turns on.
PAUSE 300 ' Wait 300 ms
PORTB = %01000010 ' Port B's middle 2. and 7. pins are turns on.
PAUSE 300 ' Wait 300 ms
PORTB = %10000001 ' Port B's middle 1. and 8. pins are turns on.
PAUSE 300 ' Wait 300 ms
PORTB = %01000010 ' Port B's middle 2. and 7. pins are turns on.
PAUSE 300 ' Wait 300 ms
PORTB = %00100100 ' Port B's middle 3. and 6. pins are turns on.
PAUSE 300 ' Wait 300 ms
PORTB = %00011000 ' Port B's middle 4. and 5. pins are turns on.
PAUSE 300 ' Wait 300 ms
ENDIF

GOTO loop ' Do it forever
END ' End of our program.