PDA

View Full Version : Pic Basic Pro Syntax



Finn
- 23rd January 2004, 20:37
I am programming the pic16f876 and was using the sample code on the Melabs website (key.bas) for the Lab X1 kit and had a question in regards to some of the commands. I am new to pic programing so please bare with me:
-If row != to $F
Does anyone know what the syntax $F or $F0 means.
I am also a little fuzzy about the "ncd,dcd" and ">>" commands. Can anyone help?

moby
- 24th January 2004, 14:25
The $ sign indicates a hex number i.e $F would be the same as 15 in decimal and $F0 would be 240 in decimal.

I'm afraid I'm fuzzy myself about ncd and dcd.

>> is shifting to the right for example var>>1 would be the same as dividing var by two.

Ingvar
- 26th January 2004, 15:16
Hi,

NCD will tell you which bit is set in a value(variable). If you have the binary value %00000001 NCD will return 1(dec). %00000010 will give 2, %00000100 will return 3 and so on. If there is more than one bit set NCD will return the highest. %00100001 will give you 6. if no bits are set(the value is zero) NCD will reurn 0. I don't really like the fact that it returns 1 when the LSB(Least Significant Bit) is set, in my brain that bit is bit 0. I suppose it's done that way to be able to return 0 if no bits are set. Example.......
in = 4 '4dec = 4hex = 00000100bin
out = NCD in 'check which bit is set
.....out will now contain 3 since it was the third bit that was set.

DCD will do it in reverse. You tell DCD which bit to set and it will return a value with that bit set. This command wants the bitnumber the way i like it, LSB is 0, not like NCD which wants LSB = 1. DCD 0 will give %00000001, DCD 3 gives %00001000, DCD 7 gives %10000000. Example.......
in = 4
out = DCD in
.....out will now contain %00010000 since it was bit number 4 that should be set.

The ">>" command means "shift right", this means that you move the bitpattern(one or more) steps to the right by "pushing" zeroes in from the left. How many times to shift is specified by the value following the">>" command. %10000000 >> 1 will return %01000000, %10000000 >> 3 will return %00010000, %00001111 >> 3 will return %00000001. Looking at it from a "mathematical" side we see that shifting one step right is the same as dividing a number by 2. You will divide the number by 2 each time you move it one step to the right. >> 3 will be the same as /2/2/2 = /2^3 = /8 .

I hope this makes things a bit less fuzzy.
/Ingvar

Finn
- 26th January 2004, 19:07
Thank you moby & Ingvar, your help has been well recieved and my understanding of how the code works has definetly been increased. Thanks Again!!!!