PDA

View Full Version : Stupid simple question.....



chien_fu
- 14th February 2010, 12:47
I'm trying to make a button work...
here is my test code:


input Pin4

loop:

if Pin4 = 1 then cycle

goto loop


cycle:

if Pin4 = 0 then loop

high 0 : pause 100
high 1 : pause 100
high 2 : pause 100
low 0 : pause 100
low 1 : pause 100
low 2 : pause 100


goto cycle


end

When I connect pin4 to high nothing happens... Do I need to pull the pin high or low or something? Should I have something other than the "input Pin4" command?

This seems like it should be super fricking simple, but it just doesn't work... I feel like I'm taking crazy pills!! Someone please help.

Darrel Taylor
- 14th February 2010, 18:52
Pin4 is usually PORTB.4, and not actually Pin4 on the chip.

Which chip are you using?
<br>

chien_fu
- 15th February 2010, 11:00
I'm using a 12F683, GP4 (which I believe is the same as PORTB.4?) is Pin3.

chien_fu
- 15th February 2010, 17:35
I could use some help here... I am completely at a loss. I can't get any input to work at all, am I going crazy?

Darrel Taylor
- 15th February 2010, 17:48
For a 12F683, you need to turn off the Analog inputs and Comparator.

SYMBOL ANSEL = $9F
SYMBOL CMCON0 = $19

POKE ANSEL, 0
POKE CMCON0, 7

hth,

chien_fu
- 15th February 2010, 18:33
I cannot express enough gratitude. It works! Thank you so much kind sir. Now where might one learn a bit of information like that? That seems fairly basic, but I don't recall coming across that in any of my "pic'n" books or the 12F datasheet...

HenrikOlsson
- 15th February 2010, 19:10
Hi,
If you look at section 4 in the datasheet (http://ww1.microchip.com/downloads/en/DeviceDoc/41211D_.pdf) you'll see a note there saying:
Note: The ANSEL and CMCON0 registers must be initialized to configure an analog channel as a digital input. Pins configured as analog inputs will read ‘0’.
There's also an example showing how to do it but it's in ASM so it's not that easy to understand.

If you then look at figure 8.4 you'll see the various configurations for the comparator module and in the bottom right hand corner it shows that you should write 7 (the three lower bits are set) to CMCON0 in order to disable the comparator.

If you then look at the bottom of table 2.2 it'll show you that ANSEL "comes up" with the lower 4 bits set and if you look at register 4-3 (page 33) you'll see that when bit 0-3 is set it means that the pin is in "analog mode".

As for the rest, $9F is the adress for ANSEL register and $19 is the adress for the CMCON0 register and that can be found if you look at figure 2.2 in the same datasheet. PBP makes it a lot easier as it allows you to write directly the the registers without having to look up the actual adress of it "manually".


It's a very common thing to overlook untill you've been bitten by it, next time a pin doesn't work as you think it should it'll be the first thing you'll think of checking ;-)

/Henrik.

chien_fu
- 15th February 2010, 19:38
oy...
Thanks guys, you rock..
How I wish I could be using my Arduino for this project..

chien_fu
- 23rd February 2010, 03:07
I really hate this stuff.... it makes me want to stab myself in the eyeball with a fork.

I'm measuring 3v from Pin1 (GP1) on my 12F683.
I'm using a 3v coin cell. It measures 3v.
I connect a LED to Pin1, it lights.
YAY! Next frickin challenge....

I connect a small vibe motor to my 3v battery, it vibrates like crazy. Yay.
I connect my small vibe motor to Pin1 (measuring 3v) It does absolutely nothing.
I connect the LED to Pin1.. it lights..
I connect the motor to Pin1 ... it does nothing
I punch stuff... that doesn't help.

I don't know what else to try...
The vibe motor draws about 35-40mA

I see that the IO pins supply 25 mA but the GPIO can supply 90mA...
What's the difference?

ScaleRobotics
- 23rd February 2010, 03:29
The vibe motor draws about 35-40mA

I see that the IO pins supply 25 mA but the GPIO can supply 90mA...
What's the difference?

A single pin can put out a max of 25mA, but the entire GPIO (all output pins) can put out a max of 90mA. You could use a transistor, or you could have two pins control the motor (and hope start current would not be too high).

chien_fu
- 23rd February 2010, 03:36
So I tried driving it with two pins but something else is screwy now....

This is what I have:


OUTPUT Pin1
output Pin2

Startup:

for startupalarm = 1 to 4
high 2
high 1
pause 5000
low 2
low 1
pause 5000
next startupalarm

It seems that "high 2" does not actually switch the pin high, but just ticks it high for a split second. "high 1" switches the pin high and it is held high for 5 seconds until it is switched low...

This is the same regardless of which pins I use. The line of code immediately preceding the pause command works, all others do not. Nothing else is connected to the chip, it's all by itself on a breadboard.............

HenrikOlsson
- 23rd February 2010, 07:39
Hi,
You're suffering from what is called a RMW problem (read-modify-write). The way the PIC works when setting a single bit in a register, like the GPIO-port in this case, is to first read the whole register, then set the bit in question and finally writing the whole register "back".

Because your motor presents a large a load to the pin (especially at startup) the voltage on the pin will sag or be low. So what is happening is this:
First you have the High 2. This reads the port (as a byte), sets bit 2 and writes the whole byte back to the port.
Now you have High 1 which does the same thing but because pin2 is heavily loaded it will read as 0 when the port is read, the PIC then sets bit1 and write back to the port resulting in bit 2 now being reset to 0.

The best bet is to use a transistor to drive the motor. If that's not an option then write to the port directly, setting both pins at the same time. You do this in the same way as Darrel showed you how to write to CMCON etc - look up the physical adress of GPIO and use POKE to write to it.

Melanie
- 23rd February 2010, 07:45
Remember also, you must treat your Motor as an INDUCTOR or RELAY. The back emf could destroy your PIC pin or Transistor switching it. Put that all important Diode reverse-biased across the Motor.

Acetronics2
- 23rd February 2010, 08:14
+1 .....

Good answer from Henrik ...

HenrikOlsson
- 23rd February 2010, 08:35
Nah, if you look at section 9.1 (http://ww1.microchip.com/downloads/en/devicedoc/31009a.pdf) of the mid-range manual you'll find the following:
All write operations (such as BSF and BCF instructions) are read-modify-write operations. Therefore a write to a port implies that the port pins are read, this value is modified, and then written to the port data latch.
So I'll stick to my theory ;-)

Acetronics2
- 23rd February 2010, 08:42
Ok Henrik ...

One red tomato for me ... ( was the question ...)

this also explains why it acts the same whith heavy capacitive loads ... driving directly Big Mosfets gates i.e.

Alain

PS: but what an Idea µChip had to proceed like that ???

chien_fu
- 23rd February 2010, 11:47
Why is it that when I enter:

GPIO = %11111111

Picbasic says "Undefined variable"?

HenrikOlsson
- 23rd February 2010, 13:18
Hi,
Because the variable GPIO isn't defined....

This is because you're using PBC and it doesn't provide a way to directly write to the registers as you're trying to do - only PBP does that. As I wrote in my previous message you need to do as Darrel showed you earlier in the thread:


SYMBOL GPIO = $05 '$05 is derived from the memory map in the datasheet for 12F683
SYMBOL TRISIO = $85

Poke TRISIO, 0 'Set all available GPIO pins as outputs
Poke GPIO, 255 'All availble GPIO pins high.

chien_fu
- 23rd February 2010, 13:21
Cool, thanks.