Menu

Newbie; setting and reading ports on a PIC18F67K40

2023-07-26
2023-08-03
  • Steve Scott

    Steve Scott - 2023-07-26

    Hello All-
    New to this compiler. Have used PIC Basic Pro for over 30 years but they have not added much in recent - so I am looking for another compiler. While free, this looks to be quite capable - Kudos to All involved!
    In learning this complier, I am trying to simply read a couple of bits and turn on a couple of LEDs - not working at all.
    It is so simple that I must be violating some blatant setting - but cannot find it. Read and looked at many examples (much easier that PIC Basic Pro) but my simple program does not work.
    Thanks in advance for any support.
    The test file is here:

     
  • Ccin E Crout

    Ccin E Crout - 2023-07-26
    //first, chip config
    #CHIP 18F67K40                  'Who we are
    //Defines
    #define BLED    PORTF.0         'BLUE lede, active LOW
    #define RLED    PORTF.1         'RED led, active LOW
    //#define bledIN  PORTE.0         'Input for Blue led
    bledIN=PORTE.0                  'An alternative to above to test
    //#define rledIN  PORTE.1         'Input for Red led
    
    //#option Volatile PORTE.0          'This did not help
    
    //Init
    dir PORTE In                    'Sets up PORTE for INPUTS
    dir BLED out                    'Sets the DIR
    dir RLED out 
    BLED=1                          'Turns OFF the led
    RLED=1                          'Same as above
    
    //Main
    //Here we go Boyz......
    Do
        BLED=bledIN                 'Hopefully led will follow input
     //   RLED=rledIN                 'same as above
    Loop
    

    Here's what I would try:

    //first, chip config
    #CHIP 18F67K40                  'Who we are
    //Defines
    #define BLED    PORTF.0         'BLUE lede, active LOW
    #define RLED    PORTF.1         'RED led, active LOW
    #define bledIN  PORTE.0         'Input for Blue led
    bledIN=PORTE.0                  'An alternative to above to test
    #define rledIN  PORTE.1         'Input for Red led
    
    //#option Volatile PORTE.0          'This did not help
    
    Dim SwitchStateRLed As Bit
    Dim SwitchStateBLed As Bit
    Let SwitchStateRLed = 0
    Let SwitchStateBLed = 0
    //Init
    dir PORTE In                    'Sets up PORTE for INPUTS
    dir BLED out                    'Sets the DIR
    dir RLED out 
    BLED=1                          'Turns OFF the led
    RLED=1                          'Same as above
    
    //Main
    //Here we go Boyz......
    Do
       Let SwitchStateRLed = RLedIn
       Let SwitchStateBLed = BLedIn
        Let BLED=SwitchStateBLed                 'Hopefully led will follow input
        Let RLED=SwitchStateRLed                 'same as above
    Loop
    

    Untested but I wonder if you are not assigning the 'state' of the input to your output, but the value of its port position - which will always be the same regardless of whether the button is pressed or not.

     

    Last edit: Ccin E Crout 2023-07-26
  • Steve Scott

    Steve Scott - 2023-07-26

    Thanks Ccin E Crout - your idea was the same.
    With no input the LEDs are ON, when bringing the input to a '1' the LED still stays ON.
    If we remark the DO loop, the LEDs stay off as supposed to.
    It seems that I still cannot read an input - even with your suggestion.
    This should be quite simple....... now frustrating.

     
    • Ccin E Crout

      Ccin E Crout - 2023-07-28

      Sorry that did not help. I had a distant recollection that I'd had a similar problem and found that referencing the inputs indirectly helped me in that case. I may have been trying to do maths with the result of an input which may have been my problem. I easily may have mixed things up totally. Yet again.

      I certainly have been able to read inputs simply using GCB, but I'm not familiar with the device you are using.

      As it appears that something else is wrong. I'll leave you in the hands of someone with much more knowledge than me.

       
  • Anobium

    Anobium - 2023-07-26

    adding #option explicit would hilight the issue.

    bledIN is not declared.
    If you change to a constant using the '#define then the program should work.

    The ASM looks correct when the constant is used.

    Where F.0 is set off, then F.0 is set only if E.0 is High,

    ;BLED=bledIN                 'Hopefully led will follow input
        BCF LATF,0,ACCESS
        BTFSC   PORTE,0,ACCESS
        BSF LATF,0,ACCESS
    
     
  • Steve Scott

    Steve Scott - 2023-07-26

    Thanks Anobium-
    I have gone back to my original (code attached) but the LEDs do not change from inputs on the 2 bits on the port.
    I feel this is such a simple problem and searching the forum has not had any 'illumination'.

     
  • Anobium

    Anobium - 2023-07-26

    Please add to the top of your program the following:

    #IFDEF Var(ANSELF)
        ANSELF = 0
      #ENDIF
    

    The root cause of the issue is that port has not been added to the initsys sub routine. I will add for the next release. But, for now. add that code.

     

    Last edit: Anobium 2023-07-26
  • Steve Scott

    Steve Scott - 2023-07-26

    Thanks and feeling somewhat better that it is not me.......
    Appreciate your looking at this.
    However, it still does not change state of the LEDs based on the input of the port.
    Code Attached.

     
  • Steve Scott

    Steve Scott - 2023-07-26

    Hit the key too soon.
    Here is the file.....

     
  • Steve Scott

    Steve Scott - 2023-07-26

    Damn, hit the wrong button!
    Really, here it is.....

     
  • Anobium

    Anobium - 2023-07-27

    What now works ? what changed? I cannot help with 'that is somewhat better'.

    Here to help - best regards.

     
  • Steve Scott

    Steve Scott - 2023-07-27

    Apologies for my unclear message. I am somewhat better with the knowledge that the issue I am having is not me but something in the compiler.
    Your suggestion did not help, but I do however, appreciate your efforts.
    I have attached the program one more time - hopefully doing it correctly this time <grin>.
    Regards.</grin>

     
  • Anobium

    Anobium - 2023-07-27

    As I away, on vacation at the moment, I do not have a chip here....

    So, we need to elimate a few things.

    Change the program to use PORTA. This will prove if the basics are operating as expected.

     
  • Steve Scott

    Steve Scott - 2023-07-27

    I was not aware you are on vacation - and you are willing to help - that is great!
    The target has already been designed and it is using PORTA.
    I wondered the same line of reasoning however.
    I will find some other pins and try the same test and be sure to let you know.
    I programmed the board in PIC Basic Pro by the way but am looking to find an other compiler.
    Regards.

     
  • Anobium

    Anobium - 2023-07-28

    Pleasure.

    Add the following where a..h is A thru to H. So, add multiple time.

    #IFDEF Var(ANSELa..h)
        ANSELa..h = 0
      #ENDIF
    

    Then, change the PORTE for the inputs to LATE

    #define BLedIn  LATE.0         'Input for Blue led
    #define RLedIn  LATE.1         'Input for Red led
    

    So, there is an issue deep the compiler, easily fixed, hence the conditional statements. I cannot fix here in France.

    Test above changes and report back.

     
    • Anobium

      Anobium - 2023-07-28

      What is puzzling me... the compiler has had this issue since 2013 or before. The BSR register was being set directly, which failed (silently and correctly) and then the subsequent setting of ANSELx was not operating as expected.

      I will resolve properly in the next build.

       
  • Steve Scott

    Steve Scott - 2023-07-28

    Anobium-
    Thank you for confirming that it may not be my very simple 'test' prog. the test program is so simple I did not see how it could fail - given my past history with another compiler however.
    I am mentoring a bright young person and unfortunately I have given him my board specific programming cable and need to wait for the RJ-12 connectors in order for me to test anything. They should be arriving later today - Texas time.
    I can test some of the registers you wanted, but again, this board is already designed and known to work properly albeit with a different compiler. I can change those registers associated with a LCD and 2 8-bit ports later when I re-build a board-specific programming cable.
    Again, I am impressed with the compiler thus far, and your interest in solving the compiler issue is heart-felt.
    Enjoy your time in France.
    Regards.

     
  • Anobium

    Anobium - 2023-07-28

    Thank you.

    Test the simple program. I need to know it works. I see the bit changing in MPLAB-X so I am very hopeful.

     
  • Steve Scott

    Steve Scott - 2023-07-28

    Thank you.
    There seems to be a problem with my programming cable.
    Target does not agree with the programmer.
    I am out of town starting to9morrow until Thursday (Central Time) morning.
    Will be able to get back to it then.
    Regards,
    Steve

     
  • Steve Scott

    Steve Scott - 2023-07-28

    Anobium-
    I have fixed the programming cable (simple) and have used your suggestion for ONE port which is input PORTD.
    I am having no luck with your suggestion if I understand it correctly.
    Program is attached.
    Have a glass .......
    Regards,
    Steve

     
  • Anobium

    Anobium - 2023-07-29

    ok. Wait until your return - ping me when you are back.

     
  • Steve Scott

    Steve Scott - 2023-08-03

    Anobium-
    I am back and can help further this issue.

     

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.