Setting a Pin a As a Ground? (GreatCowBasic)

Discussion in 'Embedded Systems and Microcontrollers' started by ajm113, Jun 28, 2011.

  1. ajm113

    Thread Starter Member

    Feb 19, 2011
    Hello I have a PIC 12F675 and I'm using great cow basic and I wrote a program that turns a fan on and off and I want to use a PNP transistor to act as a switch, but I need a pin to act as a ground so it works.

    But for the life of me I can't figure out how to set it as a ground in Great Cow Basic.

    I'm trying set GPIO 0 as ground.

    Here is the code in GCBASIC:
    Code ( (Unknown Language)):
    2. ;Chip Settings
    3. #chip 12F675,0
    4. #config MCLRE=OFF, WDT=OFF
    6. ;Variables
    7. Dim byOffDelay As integer
    8. Dim byOnDelay As integer
    9. Dim OutputVar As byte
    10. Dim CountStartTimer As integer
    12. byOffDelay = 1000
    13. byOnDelay = 1000
    14. CountStartTimer = 0
    15. Dir GPIO.0 Out
    16. Dir GPIO.1 In
    17. Dir GPIO.2 In
    18. Dir GPIO.4 In
    19. Set GPIO.0 On
    20. Goto DelayOff
    21. DelayOn:
    22. Repeat byOnDelay
    23.     Pot GPIO.4, OutputVar
    24.     Wait 1 ms
    25.     If GPIO.1 = On Then
    26.     Else
    27.         CountStartTimer = CountStartTimer + 1
    28.         If CountStartTimer >= 300 Then
    29.             CountStartTimer = 0
    30.             Set GPIO.0 On
    31.             Goto DelayOff
    32.         End If
    33.         byOffDelay = 1000 * [integer]OutputVar
    34.     End If
    35.     If GPIO.2 = On Then
    36.     Else
    37.         byOnDelay = 1000 * [integer]OutputVar
    38.     End If
    39. End Repeat
    40. Set GPIO.0 On
    41. Goto DelayOff
    42. DelayOff:
    43. Repeat byOffDelay
    44.     Pot GPIO.4, OutputVar
    45.     Wait 1 ms
    46.     If GPIO.1 = On Then
    47.         CountStartTimer = 0
    48.     Else
    49.         CountStartTimer = CountStartTimer + 1
    50.         byOffDelay = 1000 * [integer]OutputVar
    51.     End If
    52.     If GPIO.2 = On Then
    53.     Else
    54.         byOnDelay = 1000 * [integer]OutputVar
    55.     End If
    56. End Repeat
    57. Set GPIO.0 Off
    58. Goto DelayOn
    If you perfer assembly language let me know and I'll post it on a copy and paste website, I'm in a rush atm.

    Thank you!
  2. nickelflipper

    Active Member

    Jun 2, 2010
    Just invert the sense of GPIO.0 to Off, in place of On. Wouldn't hurt to add a higher value pullup resistor (47k) to the base of the pnp, or gate of a pfet.
  3. ajm113

    Thread Starter Member

    Feb 19, 2011
    Really? I wouldn't think just setting it to "off" would solve it and putting a 47K at the base. Wouldn't setting it "off" just simply create a low current instead and cause problems such as fried parts?

    I'm just wondering cause I'm new to this and I really don't want to fry anything with volts from 4v-13v. I'll give it a try when I get a chance though, it just kinda seems different or unexpected to me.
    Last edited: Jun 28, 2011
  4. nickelflipper

    Active Member

    Jun 2, 2010
    Yes if the source is above 5v then using the pullup would then depend on the pics internal clamping diodes, not so good without an additional buffer. Since the pin won't be floating then it shouldn't be a problem. I was thinking of a circuit with a pfet driver that did the level translation to a higher source voltage, my fault.

    When in doubt I try my logic out on leds to start with, then move up from there.
    Last edited: Jun 29, 2011
  5. nickelflipper

    Active Member

    Jun 2, 2010
    Well I jumped in the muck of level translating with a pnp, and it looks like web says that a npn is required to control the base of the pnp. Interestingly, a pullup is used in that configuration on the pnp base.

    I'm used to using logic level mosfets with microcontrollers. The easiest way would be to use something like an IRL510 nfet to control the low side of the fan.
  6. ajm113

    Thread Starter Member

    Feb 19, 2011
    Hmm, well I tried a few methods such as using the NPN only and by using a 1.5K pull up resistor and it seems to work a bit better, what I find strange about the hole thing is if the base resistor is more then 10K then the NPN seems to ignore it completely and I've tried methods such as this one:

    So instead of using a PNP I went ahead and used a 5v relay switch that was controlled by a NPN and it made the job faster and simpler since the fan was only going to go on and off ever few minutes.