Set bits of Word problem


Closed Thread
Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2005
    Posts
    6

    Default Set bits of Word problem

    I am trying to set individual bits of a word to correspond to the state of input pins so that I can test the word as a decimal value. Here is a snipet of the code.

    dip var byte
    input porta.1
    input porta.2
    input porta.3

    dip.0 = porta.1
    dip.1 = porta.2
    dip.2 = porta.3

    if dip = 0 then
    high portb.0
    enif
    if dip = 1 then
    high portb.1
    endif
    ...etc.

    I've also tried to use Select Case instead of the If thens. If anyone sees something I'm doing wrong, any help would be greatly appreciated. Thanks in advance.

  2. #2
    Join Date
    May 2004
    Location
    NW France
    Posts
    3,614


    Did you find this post helpful? Yes | No

    Cool Rtfm ...

    Hi, Pdegior

    I saw something in the manual ....$ 4.17.14 ...

    Have a look to it ...

    Alain

  3. #3
    Join Date
    Sep 2004
    Location
    montreal, canada
    Posts
    6,898


    Did you find this post helpful? Yes | No

    Default Freebie!!!

    Code:
    TRISA=255  ' Set all PORTA pins to Input
    
    dip var byte
    
    Start:
        dip=PORTA
        select case dip
            case 0
                '
                ' Code for PORTA=0
                '
            case 1
                '
                ' Code for PORTA=1
                '
            case 2
                '
                ' Code for PORTA=2
                '
            end select
        goto start
    And you're using a BYTE sized variable to do it.
    Steve

    It's not a bug, it's a random feature.
    There's no problem, only learning opportunities.

  4. #4


    Did you find this post helpful? Yes | No

    Default

    Hi Pdegior. I tested this program with only part of PORTA and it works. Hope this helps.

    'CODE COMPILED FOR 16F818
    OSCCON = $60 'set int osc to 4mhz
    adcon1 = 7 ' set inputs to digital
    TRISA = %11111111 'ALL PORTA INPUTS
    TRISB = %00000000 'ALL PORTB OUTPUTS
    PORTB = 0 'ALL OUTPUTS LOW
    @ DEVICE MCLR_ON, INTRC_OSC, WDT_ON, LVP_OFF, BOD_ON, PWRT_ON, PROTECT_ON
    DIP VAR BYTE

    START:
    PORTB = 0 'off all leds
    LET DIP = 0 'clear the variable DIP


    LET DIP.BIT0 = 0
    IF PORTA.0 = 1 Then LET DIP.BIT0 = 1 'dip bits are 0 unless PORTA bit is high

    LET DIP.BIT1 = 0
    IF PORTA.1 = 1 Then LET DIP.BIT1 = 1

    LET DIP.BIT2 = 0
    IF PORTA.2 = 1 Then LET DIP.BIT2 = 1

    LET DIP.BIT3 = 0
    IF PORTA.3 = 1 Then LET DIP.BIT3 = 1

    IF DIP = 0 Then High PORTB.0 'light corresponding LED
    IF DIP = 1 Then High PORTB.1
    IF DIP = 2 Then High PORTB.2
    IF DIP = 3 Then High PORTB.3
    Pause 1000 'wait a second
    PORTB = 0 'off led
    GoTo START 'do it again

  5. #5
    Join Date
    Jul 2005
    Posts
    6


    Did you find this post helpful? Yes | No

    Default

    Thanks,

    that seems to have done it. Still not too sure why the way I had it doesn't work but oh well.

Similar Threads

  1. Bits, Bytes Words and Arrays
    By Melanie in forum FAQ - Frequently Asked Questions
    Replies: 24
    Last Post: - 14th June 2016, 07:55
  2. 16f877A and tmr1 external crystal question
    By comwarrior in forum General
    Replies: 3
    Last Post: - 13th July 2009, 00:40
  3. Interruptus Frustratus
    By Byte_Butcher in forum General
    Replies: 16
    Last Post: - 17th April 2009, 20:36
  4. 16F628A - Stops if release power switch.
    By dene12 in forum General
    Replies: 16
    Last Post: - 14th February 2009, 07:57
  5. How to isolate 10 bits from word variable?
    By rcbandwidth123 in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 14th April 2008, 20:18

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts