PDA

View Full Version : Advice please - data tables



malc-c
- 11th June 2006, 16:59
I'm learing PBP and although its been a steep learning curve, the support of this forum has been great and I've taken onboard what I've been given.

I'm just playing about with the "flash some LEDs", basic stuff really, and I now have the basis for what could turn into a nice project for some disco lights. I've detailed my code below, which basically has a chase pattern, the speed of which is set by a 10k pot.



PORTA = 0 ' declare port level BEFORE port direction = safe power-on
CMCON = 7 ' PortA Digital inputs
CCP1CON = 0 ' PWM off
VRCON = 0 ' Voltage reference disabled
OPTION_REG.7 = 0

TRISA=%00000111 'set PORTA as all output apart from A0, A1 and A2
TRISB=%00000000 'set PORTB as all output

DATA @1,word 625,0,word 545,word 750 ' pre-setting EEPROM

@RC_OSC_NOCLKOUT
@WDT_ON
@PWRT_ON
@MCLR_OFF
@BOD_ON
@LVP_OFF
@CPD_OFF
@PROTECT_OFF

;set up varibles

PORTBbit var byte
i var byte ;used for for next loops
D var byte ;used to store the result of the pot on port A1
scale var byte ;used in the POT command
Scale = 254 ;used to set range
SW1 var PORTA.2 ;up switch
SW2 var PORTA.0 ;down switch
SWcount var byte ;used as a place holder for patterns
swcount=1 ;set place holder to default to pattern 1

;main program

Main:
Pot PORTA.1,scale,D ;used to read value from 10k pot
if sw1=0 then swcount=swcount+1 ;check to see if up button pressed
if sw2=0 then swcount=swcount-1 ;check to see if down button pressed
pause 120 ;debounce delay
If swcount>2 then swcount=2 ;error trap for exceeding max patterns
If SWcount<1 then swcount=1 ;error trap for exceeding min patterns
if swcount=1 then pattern1 ;goto selected pattern based on value
if swcount=2 then pattern2


pattern1: ;standard chase pattern 1 - 8
For PORTBbit=0 to 7
if D<5 then D=5 ;set the minimum pause time to 5ms
HIGH PORTBbit
pause D
LOW PORTBbit
pause D
Next
goto main

pattern2: ;standard pattern reveresed 8 -1
For PORTBbit=7 to 0 step -1
if D<5 then D=5
HIGH PORTBbit
pause D
LOW PORTBbit
pause D
Next
goto main


However now I would like to add further patterns and ideally store / code them as data strings or binary patters that are repeated, eg

[code]
10000001
01000010
00100100
00011000
00100100
01000010
10000001

or
129, 66, 36, 24, 36, 66,129
[code]

I've browswed through the manual, but can't see any clear reference on how to write these values in a loop to portB. ie something like

[code]
for i = 1 to number of steps
write value to port
make port high
delay
port low
next
[code]

My evential goal would be to use the UART in the chip to communicate with a PC and allow the additional patterns to be uploaded to the PIC.. but I think I had better learn to walk before I start running ;)

Early1
- 11th June 2006, 20:26
This is easy to do.

Create a variable byte

i byte
Myvar byte[10] make the array the same size as the number of paterns you would like.
Myvar[0]= some patern
Myvar[1]= next patern
Myvar[2]= another patern

etc

for i =0 to 10
portb=myvar[i]
delay
portb=0
next i

malc-c
- 12th June 2006, 09:20
This is easy to do.

Create a variable byte

i byte
Myvar byte[10] make the array the same size as the number of paterns you would like.
Myvar[0]= some patern
Myvar[1]= next patern
Myvar[2]= another patern

etc

for i =0 to 10
portb=myvar[i]
delay
portb=0
next i


Thanks for the reply, when you say myvar[0] = some pattern, how would I define the arrays? Would it be "00110011" etc or decimal numbers "xxx, xx, xx"

Yeah I too was looking for the option of deleting my own posts after multiple post occured, but it looks like that is only available to the mods

malc-c
- 12th June 2006, 09:33
Actually, I don't think that's what I'm after having read the code more. I'm really after the ability of having the patterns as single lines, in your example MyVAR[0] would contain all the steps of the pattern, and not the first step.

eg:
MyVAR[0] = "129, 66, 36, 24, 36, 66,129" would send decimal 129 to PORTB, then 66, then 36 and so on, then repeat this pattern over and over again.

If I understand your suggestion correcly, it would be the same as having
MyVar[0] = "129"
MyVar[1] = "66"
etc

Any comments ?

Early1
- 12th June 2006, 17:21
That is exactly what I meant.

All I need now is for some one to help me with my little problem.

Cheers

Jerson
- 13th June 2006, 05:36
Pattern: var byte ' hold the pattern for PortB
Gen0: var byte ' general purpose reg

ChasePattern: data 129, 66, 36, 24, 36, 66,129, -1

OutPattern:
Gen0 = ChasePattern ' Gen0 is a pointer to the pattern
while 1 ' forever
read Gen0, Pattern
if Pattern = -1 then ' this is the end marker,
goto OutPattern ' see it, start over
else
PortB = Pattern
endif
wend

Mind you, you need to configure PortB as outputs etc. which I havent done for you.
Sorry I cannot find how to format the code even though I have done it earlier.

Jerson

malc-c
- 13th June 2006, 09:45
Jerson,

Thanks for replying. I started a new thread last night and have a similar suggestion from Steve using for next loops, but I'll also try your suggestion of using the while and wend commands and adding an end of line marker such as -1 (its all go practice ;) )