16f628a question

Discussion in 'General Electronics Chat' started by bluebrakes, Nov 30, 2009.

  1. bluebrakes

    Thread Starter Active Member

    Oct 17, 2009
    245
    7
    i'm using a pic16f628a and i'm wondering if i can have inputs and outputs both on portAs?

    so for example have an output on RA1 and an input on RA2.

    Would someone be able to show a snippet of code to how it's done?

    i'm still learning. :)

    thanks.
     
  2. SgtWookie

    Expert

    Jul 17, 2007
    22,182
    1,728
    I don't have any code to show you because I don't know what language you're programming in.

    However, PORTA pin directions are controlled by the TRISA register.
    If you want RA1 to be an output, you set bit 1 of TRISA to 0.
    If you want RA2 to be an input, you set bit 2 of TRISA to 1.

    There is sample Assembler code on page 29 of the PIC16F62x Data Sheet for initializing TRISA for output pins.
     
  3. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    One point to watch is that port A can be configured either as digital port (normal I/O) or as a analogue port. It defaults to analogue. To turn it into a digital port you have to disable the analogue comparators which you do by writing hex 07 to the CMCON register.
     
  4. bluebrakes

    Thread Starter Active Member

    Oct 17, 2009
    245
    7
    thanks guys. Sorry, i didn't mention i was programming in assembly code.

    alex, does that mean i have to configure each port independently as regards to digital/analog or can i turn them all to digital but each selected as an input/output?
     
  5. BMorse

    Senior Member

    Sep 26, 2009
    2,675
    234
    writing hex 07 to the CMCON register sets all IO's as digital, use the TRISA register to set the direction, sgtwookie suggested.

    Code ( (Unknown Language)):
    1. CLRF PORTA ;Initialize PORTA by setting output data latches
    2. MOVLW 0x07 ;Turn comparators off and
    3. MOVWF CMCON ;enable pins for I/O functions
    4. BCF STATUS, RP1
    5. BSF STATUS, RP0 ;Select Bank1
    6. MOVLW 0x0C ;Value used to initialize data direction
    7. MOVWF TRISA ;Set RA<1:0> as outputs, RA<2:3> as Inputs TRISA<5> always read as‘1’. TRISA<7:6> depend on oscillator mode
     
  6. bluebrakes

    Thread Starter Active Member

    Oct 17, 2009
    245
    7
    maybe this a bit of a challenging project, but the attached schematic is essentially what i would like to create.

    at the moment the schematic is a bit crude. however it has everything i would like to control and the various inputs.
     
  7. bluebrakes

    Thread Starter Active Member

    Oct 17, 2009
    245
    7
    forgot to mention, its a countdown timer. when timer reaches zero an LED/Buzzer turns on, etc.
     
  8. SgtWookie

    Expert

    Jul 17, 2007
    22,182
    1,728
    Pretty good - I've seen far, far worse ;)

    Looks like you have used RA2 twice on the uC.

    You may find it easier to drive the LCD if you use the lower four bits of a register to drive the LCD's data bits.

    You have R6 through R8 as 1k. If the LEDs have a Vf (forward voltage) of 2.2v, that will give them (5v-2.2v)/1000 = 2.8/1000 = 2.8mA. They'll be mighty dim.

    Re-calculate them as:
    Rlimit >= (5v - Vf_LED) / 15mA
    The 15mA is to keep the I/O pin current well below the 20mA maximum.

    Example:
    Vcc=5
    Vf=2.2
    I=15mA
    V(Rlimit) = 5v-2.2 = 2.8v
    Rlimit >= 2.8v/15mA
    Rlimit >= 186.666...
    Standard resistance values: http://www.logwell.com/tech/components/resistor_values.html
    Closest standard value >= 186.666... is 200 Ohms.
    2.8v/200 Ohms = 14mA.

    Also, you don't have capacitors on both sides of the 7805 regulator. You will need them, or the output voltage may not be stable. 0.33uF on the input and 1.0uF on the output is recommended.

    You should have a 0.1uF capacitor across the Vdd/Vss pins of the uC.
     
    Last edited: Dec 1, 2009
  9. bluebrakes

    Thread Starter Active Member

    Oct 17, 2009
    245
    7
    thanks wookie.

    i've attached the diagram with the your amendments. Is this what you mean?
     
  10. SgtWookie

    Expert

    Jul 17, 2007
    22,182
    1,728
    Yes, but you still need a cap across the uC's Vdd/Vss pins. It's a good idea to do the same for the LCD.

    Just a note on basic rules for schematics:
    1) More positive voltages towards the top (+V, Vcc, Vdd) and more negative towards the bottom (GND, -V, Vee, Vss). I know it seemed more convenient to draw the battery and regulator upside-down for your schematic, but it makes it more difficult to understand.

    2) Inputs should come from the left, outputs should be towards the right.

    I tend to place voltage supplies on the left of the circuit, and use symbols to connect the voltages wherever they're needed. Using symbols helps to cut down on "clutter" caused by wires running everywhere, and makes the schematic easier to understand.

    Your schematic drawing largely follows the basic guidelines/rules, which is great. :) A bit of "tweaking" and it'll be up to snuff. ;)
     
Loading...