Basic Programming. Can someone help :-)

General discussion on mikroBasic.
Author
Message
Aries1470
Posts: 32
Joined: 01 Apr 2005 09:32
Location: Melbourne, Australia

Basic Programming. Can someone help :-)

#1 Post by Aries1470 » 01 Apr 2005 15:13

Hello all,
After reading some of the book, I want to try this circuit out, but I need to build on it.

http://www.mikroelektronika.co.yu/engli ... ook/07.htm

What I need is the following:

A counter.
It is to start from 0 (minimum) & reach up to 63 (maximum).
This counter will be moving up and down within those limits.

If a button is pressed (please read below), then the counter will go either up, or down, but NOT beyond the limit, and an appropriate output will be activated. If the limit is reached, then it will not pass on the signal.
It will also keep on the display the number where the counter is.

Pin1 – RA2, to be a “Dual” input. There will be two buttons connected to it. One contact will be from earth, the other from the +5v rail.

Pin2 & 3 – RA3 & 4, to be simple outputs.
When pin 1 goes “low” (button pressed, that is connected to earth), to subtract “1” from the counter.
When it goes “high”, to add 1
Here is a a generic drawing I made.
Image
The rest of the connection are the same as in the above mentioned page.

In "pseudo-basic" (does this still exist?) it would look something like this:

Code: Select all

dim A as byte      ’accept a number from 0 to 255
Port RA2 = In      ’pin #1 on Pic16F84
Port RA3 = Out     ’pin #2
Port RA4 = Out     ’pin #3
Let A = 0          ’initialise A to equal 0.

Main
      If RA2 = High gosub HELLO-A
      If RA2 = Low gosub HELLO-B
      Goto main.

HELLO-A
     While RA2 is high & A <=62   ’If button is still being pressed, do it again unless you reached the limit
     A=A+1 ’this should loop until 62 + 1 = 63
     Open port RA3
     Gosub DisplayAmount
     Delay_ms(250)    ’ wait for ¼ of a second
     Close port RA3
     wend
     Return

HELLO-B
     While RA2 is high & A >=1
     A=A-1 ’if it reaches 1 then " -1"  = 0
     Open port RA4
     Gosub DisplayAmount
     Delay_ms(250)    ’ wait for ¼ of a second
     Close port RA4
     wend
     Return

DisplayAmount
        Display the value of A on both digits.
        Return
I have a Pic 16F84 (not A), and I am going to build the test circuit on breadboard, as is on the site, plus the extra that I need.

Can someone please write the WHOLE - COMPLETE code, and explain how it is done?
Will it fit in the 16F84? Does it have enough memory?
I will try to build it over the weekend :)
I am just a beginner ;-)
Also are the transistors PN2222A? (PN100 – TO92 package)
Any help will be greatly appreciated.

LGR
Posts: 3204
Joined: 23 Sep 2004 20:07

Re: Basic Programming. Can someone help :-)

#2 Post by LGR » 01 Apr 2005 15:32

Aries1470 wrote: A counter.
It is to start from 0 (minimum) & reach up to 63 (maximum).
This counter will be moving up and down within those limits.

If a button is pressed (please read below), then the counter will go either up, or down, but NOT beyond the limit, and an appropriate output will be activated. If the limit is reached, then it will not pass on the signal.
It will also keep on the display the number where the counter is.

Pin1 – RA2, to be a “Dual” input. There will be two buttons connected to it. One contact will be from earth, the other from the +5v rail.

Pin2 & 3 – RA3 & 4, to be simple outputs.
When pin 1 goes “low” (button pressed, that is connected to earth), to subtract “1” from the counter. .
Comment - when using any kind of button input, you need to filter for "bounce". There is a library function to deal with this, but some people just write their own. If you don't do this, you will press the button and get several counts.
Aries1470 wrote: Can someone please write the WHOLE - COMPLETE code, and explain how it is done?.
No.
Aries1470 wrote: Will it fit in the 16F84? Does it have enough memory?.
Shouldn't be a problem. You can get a sense of how much flash and RAM is being used by compiling and looking at statistics. Even if the code isn't final, if it's close, the usage will be close.
Aries1470 wrote: Also are the transistors PN2222A? (PN100 – TO92 package)
Shouldn't be a problem. Many will work, but those are common and cheap.

General comment: You should download and read the mikroBasic manual in PDF form. It is a little long, but the answers are all in there. In particular, note the library functions "button" and "testbit". Button is for debounce, and testbit is to check the inputs. You also need to download the 16F84 data sheet from microchip, and understand how the ports work, in particular the TRIS registers.
If you know what you're doing, you're not learning anything.

Craig
Posts: 105
Joined: 21 Feb 2005 01:08
Location: California, USA

re: help

#3 Post by Craig » 01 Apr 2005 20:38

Aries, your psuedo-code is close enough to what you need that you should have no problems with writing the program. Just fill in the details of syntax per the manual. Memory use should not be a problem.

What the manual will not tell you: The button input needs to be a Schmidt trigger type to obtain a 'dead band' between high and low states, and it needs to be biased in the center of that dead band when neither button is pressed. A 10k resistor connected across each button will meet this requirement. Check the data sheet for the PIC you are using to be sure that RA2 is a Schmidt trigger. At least one Port A input should be of this type. If RA2 isn't it, use one that is. You must debounce per LGR's hint in any event.

One problem with this particular circuit is that if both buttons are pressed, the result is a short-circuit of the +5 volt supply. This must be guarded against if the person who will use the device is someone other than yourself.

Everyone starts coding through a trial-and-error process -- that is how we learn. Just keep 'playing' with it, and you will get it. The next project will be easier for the experience you have gained. Learn-by-doing is actually easier than having someone do it for you and try to explain what they did.

Aries1470
Posts: 32
Joined: 01 Apr 2005 09:32
Location: Melbourne, Australia

#4 Post by Aries1470 » 02 Apr 2005 05:45

Thank you to both of you.

Ok, after careful consideration, I thought on just focusing on the button side of things 1st.
I just want to get that working, and to then start with the led display. Hmm, come to think of it, it might be easier if I used an LCD, but I don’t have one :-) hehe. I will try that some time in the future. (that way I can use two independent lines for the input, instead of tying two buttons to 1 input).

This is what I found:

On page 21 of manual, it states the following:

5.1 PORTA and TRISA Registers
PORTA is a 5-bit wide latch. RA4 is a Schmitt Trigger
input and an open drain output. All other RA port pins
have TTL input levels and full CMOS output drivers. All
pins have data direction bits (TRIS registers) which can
configure these pins as output or input.
Setting a TRISA bit (=1) will make the corresponding
PORTA pin an input, i.e., put the corresponding output
driver in a hi-impedance mode. Clearing a TRISA bit
(=0) will make the corresponding PORTA pin an output,
i.e., put the contents of the output latch on the selected
pin.

Ok, so that means that I will change the input to pin 3/ RA4, am I correct in this assumption?

Also, if I understood correctly, since my electronics are extremely basic, I can put two diodes (1n400x), connecting from the power line to the switch, & from ground, the other way around for the other button, so then at least there will not be any short circuit (to the power lines? ). And to connect across the switches leads, a 10k resistor. That is to "bias" the electricity.

I could use a switch that has a “center off” (DP3T), and connect it accordingly, but the “looks” & design (& pricing ;-) (I’m in-between jobs) ), need just two simple push-buttons. I am open to any solutions for this problem :-D.

I will tackle each thing one by one. First I will just connect two l.e.d.’s with their R’s, the Xtal & C’s, and then the push buttons with their diodes and resisters :-)

If I get this part to work, then I will move on to the next :-), of which includes the 7-segment display’s :-) and more headaches, hehe.

Thank you, to everyone. I hope this code will be portable for other PIC’s too ;-)

Now to understand where to declare what, and how to use the button commands :-)
Hvala (thank you?) ;-)[/i]

P.S. I will be replying to this thread with my progress :-) when I can, just so I can have feedback if my programming is ok or not, since I have to program for more than 17 years :cry:.

Edit:
Alternatively, I could have the Pic to output in BCD format, and connect a 4511 or a 74LS248 (doesn't need resistors, and can connect led's directly. If i understood correctly). I think I may just follow this idea, more simpler :-) ;-) Not to mention I have 74LS248's laying around anyway. Now I have to put my brain to work. hehe.

LGR
Posts: 3204
Joined: 23 Sep 2004 20:07

#5 Post by LGR » 02 Apr 2005 06:17

Aries1470 wrote:Thank you to both of you.

Ok, after careful consideration, I thought on just focusing on the button side of things 1st.
I just want to get that working, and to then start with the led display. Hmm, come to think of it, it might be easier if I used an LCD, but I don’t have one :-) hehe. I will try that some time in the future. (that way I can use two independent lines for the input, instead of tying two buttons to 1 input).

This is what I found:

On page 21 of manual, it states the following:

5.1 PORTA and TRISA Registers
PORTA is a 5-bit wide latch. RA4 is a Schmitt Trigger
input and an open drain output. All other RA port pins
have TTL input levels and full CMOS output drivers. All
pins have data direction bits (TRIS registers) which can
configure these pins as output or input.
Setting a TRISA bit (=1) will make the corresponding
PORTA pin an input, i.e., put the corresponding output
driver in a hi-impedance mode. Clearing a TRISA bit
(=0) will make the corresponding PORTA pin an output,
i.e., put the contents of the output latch on the selected
pin.

Ok, so that means that I will change the input to pin 3/ RA4, am I correct in this assumption?
I'm not sure why Craig is recommending schmidt trigger for a buttton. Any input should work.
Aries1470 wrote: Also, if I understood correctly, since my electronics are extremely basic, I can put two diodes (1n400x), connecting from the power line to the switch, & from ground, the other way around for the other button, so then at least there will not be any short circuit (to the power lines? ). And to connect across the switches leads, a 10k resistor. That is to "bias" the electricity.
You shouldn't need to bias anything on a digital input, but you will need to pull up or down. Is that what you meant?
Aries1470 wrote: I could use a switch that has a “center off” (DP3T), and connect it accordingly, but the “looks” & design (& pricing ;-) (I’m in-between jobs) ), need just two simple push-buttons. I am open to any solutions for this problem :-D.
I would use seperate inputs.
BTW - when I first started fiddling with PIC, I thought that I was going to do it cheaply with a cheap programmer and a breadboard. After working with that system for a while, it dawned on me that it would be nice if I had a board which combined in-circuit programming with some standard peripherals, such as LCD, LEDS, buttons, etc. I actually started bulding one on a perfboard, when I realized that there is a reason why people pay the money for development boards like the easyPIC. It's because thay have all that stuff built in. I then decided to get one. It saves a lot of fiddling, and a lot of uncertainty; breadboard connections aren't very reliable.
Aries1470 wrote: I will tackle each thing one by one. First I will just connect two l.e.d.’s with their R’s, the Xtal & C’s, and then the push buttons with their diodes and resisters :-)

If I get this part to work, then I will move on to the next :-), of which includes the 7-segment display’s :-) and more headaches, hehe.

Thank you, to everyone. I hope this code will be portable for other PIC’s too ;-)
No absolute guarantees, but generally the mikro compilers offer good portability.
Aries1470 wrote:
Now to understand where to declare what, and how to use the button commands :-)
Hvala (thank you?) ;-)[/i]

P.S. I will be replying to this thread with my progress :-) when I can, just so I can have feedback if my programming is ok or not, since I have to program for more than 17 years :cry:.
I had to adjust to the microcontroller paradigm a while back, too. The physical I/O is an annoying detail, but the part that really takes getting used to is the limited RAM and the organization of flash, RAM and EEPROM. Learning the language specifics is easy, working within the constraints is more demanding.
If you know what you're doing, you're not learning anything.

Aries1470
Posts: 32
Joined: 01 Apr 2005 09:32
Location: Melbourne, Australia

#6 Post by Aries1470 » 02 Apr 2005 06:31

LGR wrote: I'm not sure why Craig is recommending schmidt trigger for a buttton. Any input should work.
Aries1470 wrote: Also, if I understood correctly, since my electronics are extremely basic, I can put two diodes (1n400x), connecting from the power line to the switch, & from ground, the other way around for the other button, so then at least there will not be any short circuit (to the power lines? ). And to connect across the switches leads, a 10k resistor. That is to "bias" the electricity.
You shouldn't need to bias anything on a digital input, but you will need to pull up or down. Is that what you meant?
If I understood Craig correctly, I think he mentioned that, because the leg/pin/input, would be floating. It will not have any reference to anything. I am still new to the concepts, so I am just guessing at this stage and not 100% sure if that is what he implied.

Wow, thank you for your prompt reply :-) That was very quick.

LGR
Posts: 3204
Joined: 23 Sep 2004 20:07

#7 Post by LGR » 02 Apr 2005 16:23

I routinely use pullup (or pulldown) resistors. Schmidt trigger has a deadband characteristic that is useful in certain instances, but buttons are routinly used with pullup resistors on standard port pins. The only reason that I point this out is that while it is possible for an output to have three states, an input can olny have two. You have to have two inputs to do up/down counting, because there are three different states. Unless you are using is like an analog input, anyway (if you're a newbie, you don't wanna go there).
If you know what you're doing, you're not learning anything.

Craig
Posts: 105
Joined: 21 Feb 2005 01:08
Location: California, USA

re: programming

#8 Post by Craig » 03 Apr 2005 10:01

Aries, you are understanding me correctly. A floating digital input (which RA4 is when neither button is pressed) will tend to appear to the PIC as either high or low. If there is sufficient noise pickup, it could even appear to have a clock connected to it!

The Schmitt trigger provides a 'third' state via the dead band centered around 1/2 V+. This makes triggers useful for noise suppression on sometimes-floating inputs. The resistors are used to hold the input in the center of that dead band when neither button is pressed. The idea was that this would keep the program from seeing false button presses. But...

LGR is on to something here: when the no-button-pressed state is read, it will read as a logic 0, since there is no '2' in binary. To read two buttons with one pin would then require use of the internal ADC. It isn't all that difficult, it's just that simpler solutions exist, such as using dedicated pins for each switch. By the way, there is no way to connect diodes that would prevent the short circuit that happens when both buttons are pressed. Both diodes would have to be facing the same direction in order for the switches to work, which places the diodes in forward series when both buttons are pressed.

Much easier all the way around to use two pins. I would still use a 10k resistor to the opposite supply rail to avoid noise problems. In other words, use a pull up to +5 with a switch to common, and a pull down to common with a switch to +5. With two pins, both can be connected the same way. The program determines the meaning of the switch press. Pressing both buttons now is a 'don't care', or could be interpreted by the program as commanding some other function if you like.

If you just can't spare another pin (I know how that goes, since my current project uses every available pin of an 18F452), you'll have to go the ADC route. I can HELP you with that, but not do it for you. :wink: Try to find 'bandwidth' (spare port pins) first.

Figuring out such things really is fun, once you learn to make simple examples work. I find that to be a good approach to more complex projects as well: break them down into simple steps that can be tested individually. That way, you always move from the known to the unknown. That and 'always try the simplest solution first' are two maxims that will prevent a lot of headaches.

LGR
Posts: 3204
Joined: 23 Sep 2004 20:07

#9 Post by LGR » 03 Apr 2005 15:37

BTW - as I alluded to earlier, you can use an analog input for several states, if you are totally out of pins. Microchip has an application note on this, and I have seen the workings of a Sony CD changer that has 35 buttons connected to 7 lines this way, so it certainly can be done. It's just not for newbies.
If you know what you're doing, you're not learning anything.

Aries1470
Posts: 32
Joined: 01 Apr 2005 09:32
Location: Melbourne, Australia

Bug in mikroBasic?

#10 Post by Aries1470 » 05 Apr 2005 09:49

Some progress?

Ok, here is all of the code I have come up with, just for the buttons, as I had mentioned earlier :)
I have run it through the compiler , but I am getting an error.

also, at this stage I will just use the buttons as they are, and later I will implement the button routine too, for the debouncing :-)

I need the code portable, since this initial code will also be used for a 10f series part :-) where can I locate the usage & size of the finished program and the hex + asm ;) ?

I have NOT checked anything in the "Config" panel!
It says that the default settings are as follows:
High Speed Oscillator [HS]- enabled
Watch Dog Timer[WDT]- disabled
Low Voltage Programming[LVP]- disabled

So I pressed "default", and some boxes were checked. :-)

The compiler does NOT like my WHILE STATEMENT!!! Why?

Code: Select all

program Using16f
dim A as byte      ' accept a number from 0 to 255

'main procedure
main:
A = 0          ' initialise A to equal 0.
PORTA.2 = 0    ' pin #1 on Pic16F84
PORTA.3 = 0    ' pin #2
PORTA.0 = 0    ' pin #18
PORTA.1 = 0    ' pin #17
TRISA.2 = 1    ' Input #1 Using 255 or $FF brings an error. I hope this is correct. I mean, does this make it an input?
TRISA.3 = 1    ' Input #2
TRISA.0 = 0    ' Output #1
TRISA.1 = 0    ' Output #2

' All port values are 0. Reference to ground. PortA0, A1 = Output
' PortA2, A3 = Input

main1:
if PORTA.2 = 1 then

    goto HelloA

  end if

if PORTA.3 = 1 then

    goto HelloB

  end if

      Goto Main1
HelloA:
     While (PORTA.2 = 1) & (A <= 62)   ' If button is still being pressed, do it again unless you reached the limit
     A=A+1                 ' this should loop until 62 + 1 = 63
     PORTA.0 = 1           ' send signal out. Continue the "button press"
          Delay_ms(250)    ' wait for ¼ of a second
     PORTA.0 = 0           ' close the "contact"
     wend
     goto Main1

HelloB:
     While (PORTA.3 = 1) & (A >= 1)
     A=A-1 ' if it reaches 1 then " -1"  = 0
     PORTA.1 = 1
          Delay_ms(250)    '  wait for ¼ of a second
     PORTA.1 = 0
     wend
     goto Main1

end.
Using or not using the parenthesis, does not make any difference :cry:
It gives the following error:
(Line: 34, Column: 32) 100 Syntax Error: exprected '=', but '<=' found
(Line: 34, Column: 32) 101 '=' is not a valid identifier
(Line: 34, Column: 31) 112 Undeclared identifier '='
(Line: 34, Column: 30) 112 Undeclared identifier '='

If I use "=" ONLY, THEN AND ONLY THEN WILL IT CONTINUE! How can I overcome this?
It is very odd, since" < " & " > " are valid :-) hehe.

Craig:
That is something I might be looking into later ;-) ahhmm... hehe. When I can figure out these more simpler things ;-)

LGR:
Really? where, where, where :-) what is the title of that application note? I know it will be out of my league, for now ;-) but no harm in having something on the "shelf" for future reference, that I can look-up when I will need it :-)


Solution found!!!
BUG IN PROGRAM!!! IN mikro Basic!

Code: Select all

While (PORTA.2 = 1) & (A <= 62)
this will give an error! BUT if I use this

Code: Select all

While (A <= 62) & (PORTA.2 = 1)
It shows:
(Line: 0, Column: 0) 0 Success
IS THIS CORRECT? and if YES, can you please explain why? since of what I remember, you were allowed to use your arguments as you liked :D

Aries1470
Posts: 32
Joined: 01 Apr 2005 09:32
Location: Melbourne, Australia

#11 Post by Aries1470 » 05 Apr 2005 10:12

WoW!!! :shock: I can use this code even in a PIC10F200!!!
With room to spare. Wohoo!
um, ok, I will calm down now ;-) hehe.

ok, just a question, what should I do with the rest of the pins on the pic16f84?
Pin4 is MCLR - Reset.
Pins 15 & 16 are for the crystal.
Pins 17, 18, 1 & 2 are my in & out.
Pins 5 & 14 are the power pins.
the other pins I just connect to ground, or leve them "free" (floating)?
Do I connect them all to ground?

nikola
mikroElektronika team
Posts: 137
Joined: 03 Aug 2004 12:44
Contact:

Re: Bug in mikroBasic?

#12 Post by nikola » 05 Apr 2005 10:25

Aries1470 wrote:Solution found!!!
BUG IN PROGRAM!!! IN mikro Basic!

Code: Select all

While (PORTA.2 = 1) & (A <= 62)
this will give an error! BUT if I use this

Code: Select all

While (A <= 62) & (PORTA.2 = 1)
It shows:
(Line: 0, Column: 0) 0 Success
IS THIS CORRECT? and if YES, can you please explain why? since of what I remember, you were allowed to use your arguments as you liked :D
Ahem.. I believe that "&" has no meaning in mikroBasic 1.x. What you need is an "AND" operator.

Btw, "&" character will be used for string concatenation in mikroBasic 2.0...

-

Aries1470
Posts: 32
Joined: 01 Apr 2005 09:32
Location: Melbourne, Australia

#13 Post by Aries1470 » 05 Apr 2005 10:35

:cry:
I just found the asm & hex files.
It is very odd, since the hex file is exactly 750 bytes, but the compiler says that it only uses 120 bytes :-(
Still just enough to squeze it in to a 10F202/6.
The odd thing is, that I changed my pic, to simulate the space/ size of a smaller memory. The only one I found was the Pic16C554 in the library. It said that it still had plenty of room.
Very odd, since its memory is 896, and its words are 512x14. So if I uploaded it to this pic (of which I don't have), it would just get to squeeze in there :-)
Let me know if it is just me, or if there is a bug in the program, in regards to reporting the actual size.

Thank you.
Btw, I might end up using the Pic10F, when I buy one, and use for the rest just descret components :-)

Thank you all.

p.s. Any help for the button debounce, on how to implement it in my code would be most welcome ;-) Since reading the examples just made me more confused, and I may just end up using a MC14490.

Aries1470
Posts: 32
Joined: 01 Apr 2005 09:32
Location: Melbourne, Australia

Re: Bug in mikroBasic?

#14 Post by Aries1470 » 05 Apr 2005 10:51

nikola wrote:
Aries1470 wrote:Solution found!!!
BUG IN PROGRAM!!! IN mikro Basic!

Code: Select all

While (PORTA.2 = 1) & (A <= 62)
this will give an error! BUT if I use this

Code: Select all

While (A <= 62) & (PORTA.2 = 1)
It shows:
(Line: 0, Column: 0) 0 Success
IS THIS CORRECT? and if YES, can you please explain why? since of what I remember, you were allowed to use your arguments as you liked :D
Ahem.. I believe that "&" has no meaning in mikroBasic 1.x. What you need is an "AND" operator.

Btw, "&" character will be used for string concatenation in mikroBasic 2.0...

-
I used lowercase "and", and no errors. BUT if I use UPPERCASE, it produces errors :-(
It says it expects syntax error: expected 'AND/OR', but 'AND' found
And then goes on to say about "undeclared identifier 'D'

What is the correct one to use, and How do I make my "while" statement operate the way I want it to?

Aries1470
Posts: 32
Joined: 01 Apr 2005 09:32
Location: Melbourne, Australia

#15 Post by Aries1470 » 05 Apr 2005 10:55

Sorry for posting in such quick succesions :-)

But writing this, goes through the compilor ok:

Code: Select all

... some code

HelloB:
     While (A >= 1) and (PORTA.3 = 1)
     A=A-1 ' if it reaches 1 then " -1"  = 0
     PORTA.1 = 1
          Delay_ms(250)    '  wait for ¼ of a second
     PORTA.1 = 0
     wend
     goto Main1
but if I use capital "AND" creates bugs :-( will my code work with the lowercase?
i ask, since I will be building a proper programmer on the weekend. it is a pic16pro.

Post Reply

Return to “mikroBasic General”