Continue to Site

# Whats wrong with my code ???

Status
Not open for further replies.

#### pike

##### Member
hey guys,

I tried making a binary counter to learn how to write and program PIC's a few weeks ago. I'm using PIC Basic Plus to compile these codes. Unsuccesful, I left it alone for a little while to cool down my temper...

Now that i've come back to give it another go, i'm still stuck!!! This is my code:

Initial:
poke trisb, 0 'set port b to output
poke portb, 0 'turn off all connected leds
Bincount:
for b0 = 0 to 255
poke portb, b0 'write the value of b0 into the register
next b0 'increase the value by 1
goto bincount 'keep repeating
end

The problem is that it won't let me compile the code it says:

Error at Line [7] In file [test.bas] *** Misplaced or Incorrect 'NEXT' command! ***

Whats wrong with the code???

#### natbit

##### New Member
Is your Bincount loop constructed corectlly ?

#### Eclipsed

##### New Member
I believe you have to initialize your variable(b0) to 0 first.

#### pike

##### Member
Yeah, the code was constructed correctly and yeah i have tried writing a value of 0 into b0.

This is REALLY starting to get me frustrated-If only i decided to take the assembly language course... :evil:

#### kinjalgp

##### Active Member
Have you declared the variable b0?
e.g.
Code:
Dim b0 as byte

#### pike

##### Member
hey, thank you very much kinjalgp. It worked!!!

But what does this command do ???

#### kinjalgp

##### Active Member
Before using any variable you have to explicitely define it or in simple words - notify the compiler that you are using a variable named xxx & what type of variable it is.
Some of the variable data types are
Byte - (8-bit) - Range: 0-255 or -128 to +127
Integer- (16-bit) - Range : 0 to 65536 or -32768 to +32767
Long (long integer) 4 bytes -2,147,483,648 to 2,147,483,647
Single (single-precision floating-point) 4 bytes -3.402823E38 to -1.401298E-45 for negative values; 1.401298E-45 to 3.402823E38 for positive values
etc.

Depending upon type a variable will use that much amount of RAM in your uC. Never use large data types unless needed.

#### pike

##### Member
is it possible to declare mutiple variables in one command ??

e.g.
Code:
Dim b0,b1,b2,b3 as byte

or do i have to do it separately to every variable??

#### kinjalgp

##### Active Member
Code:
Dim b0,b1,b2,b3 as byte
Accoding to Visual Basic if you declare variables like this, only b3 will be considered as byte. Rest all variables will be declared as variant data types which can accespt just anything you can think of and uses quite good amount of RAM. I don't know how PICBASIC treats this statement.
To be on the safer side and declare all variables as byte, use statements like this
Code:
Dim b0 as byte, b1 as byte, b2 as byte, b3 as byte

#### ivancho

##### New Member
You have to declare each variable seperate..... and use a DIM statement wit each one in PICBASIC PRO... I am not sure what PICBASIC PLUS is like :?

dog var byte
cat var bit
w0 var word

Also in PBP you can address the registers instead of having to use the poke command... so for poke trisb, 0 you would do

TRISB = 0

Ivancho

#### pike

##### Member
Thanks for your help guys !!!

Status
Not open for further replies.

Replies
3
Views
423
Replies
2
Views
2K
Replies
10
Views
1K
Replies
5
Views
3K
Replies
9
Views
1K