Who sees my stupid mistake?


+ Reply to Thread
Results 1 to 28 of 28
  1. #1
    Join Date
    Feb 2012
    Posts
    57

    Default Who sees my stupid mistake?

    Hello,

    After not having used picbasic for a couple of years, I decided to create some code to produce MIDI information.
    The intended use is to have five buttons to control a DAW.
    The code compiles without a problem, but I don't get any MIDI (31.250 Baud) output. Xtal = 20 MHz, PIC = 16F628.
    I think I made an obvious mistake, but I don't see what it could be...

    Code:
    '* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    '* MIDI Remote for Reaper            20 MHz resonator (HS)          
    '* PIC16F628                         Watchdog timer OFF              
    '* PORTA.0 = Output                                                  
    '* PORTB.0 = Rewind                                                  
    '* PORTB.1 = FastFwd.                                               
    '* PORTB.2 = Stop                                                   
    '* PORTB.3 = Play                                                   
    '* PORTB.4 = Record                                                 
    '* PORTB.5 = Switch between: Stop / Stop + Save
    '* Version 1.0                                      
    '* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
    
    TRISB= %00111111                                 ' PORTB.6/7=Output 0..5=Input
    CMCON =  7                                             ' Digitale mode
    DEFINE OSC 20                                        ' 20 MHZ OSC.
    
    Progstart: 
    
      if PORTB.0 = 1 then 
        Serout2 PORTB.7, 16384+12, [144, 44, 127]    ' Rewind
      endif  
      if PORTB.1 = 1 then 
        Serout2 PORTB.7, 16384+12, [144, 46, 127]    ' Fast Fwd.
      endif  
      if PORTB.2 = 1 then 
        if PORTB.5 = 0 then                          ' Normal STOP mode
          Serout2 PORTB.7, 16384+12, [144, 48, 127]  ' Stop
        else
          Serout2 PORTB.7, 16384+12, [144, 54, 127]  ' Stop + Save
        endif    
      endif  
      if PORTB.3 = 1 then 
        Serout2 PORTB.7, 16384+3313, [144, 50, 127]  ' Play  
      endif  
      if PORTB.4 = 1 then 
        Serout2 PORTB.7, 16384+3313, [144, 52, 127]  ' Record
      endif  
    
    Debounce:
      Pause 20                                          ' ? shorter ?
      If (PORTB.0=1) or (PORTB.1=1) or (PORTB.2=1) or (PORTB.3=1) or (PORTB.4=1) then
        goto Debounce                                ' Button still pressed?
      endif  
      Goto Progstart

  2. #2
    Join Date
    Feb 2012
    Posts
    57


    Did you find this post helpful? Yes | No

    Default Re: Who sees my stupid mistake?

    I forgot to mention that there are resistors to ground (to keep the input 'low' when no button is pressed) and that the buttons switch to the +5 Volts.

  3. #3
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default Re: Who sees my stupid mistake?

    16384+12 seems correct for (inverted) 31250 but should it BE inverted and why 16384+3313? (I don't know MIDI...)

    Can you do a standard baudrate like 9600 and verify that data is coming out by using a terminal program?

    The 16F628 does have UART so HSEROUT will work with it. I'd try that but you might need to invert the signal externally.

  4. #4
    Join Date
    Feb 2012
    Posts
    57


    Did you find this post helpful? Yes | No

    Default Re: Who sees my stupid mistake?

    The 16384+12 is correct, I have used that before.
    The calculation is: (1.000.000/Baudrate) - 20 , so 1.000.000 / 31250 = 32 -20 = 12.
    For inverted output set bit 14, = 16384.
    But even with a different baudrate I don't get any output.

    I did a quick test to trace the problem.
    With this code, nothing 'toggles':

    Code:
    Progstart: 
      if PORTB.0 = 1 then 
        Toggle PORTB.6
      endif  
    
    Debounce:
      Pause 20                                       ' ? shorter ?
      If PORTB.0=1 then
        goto Debounce                                ' Button still pressed?
      endif  
      Goto Progstart
    So I suspect the problem is in the line (and similar lines)
    Code:
      If PORTB.0=1 then
    Although I don't see what could be wrong with this...

  5. #5
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,599


    Did you find this post helpful? Yes | No

    Default Re: Who sees my stupid mistake?

    If I don't touch programming for a while, I start with blinking a LED to make sure I have the basics working right (config, circuit, programming, etc).
    My Creality Ender 3 S1 Plus is a giant paperweight that can't even be used as a boat anchor, cause I'd be fined for polluting our waterways with electronic devices.

    Not as dumb as yesterday, but stupider than tomorrow!

  6. #6
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,599


    Did you find this post helpful? Yes | No

    Default Re: Who sees my stupid mistake?

    Quote Originally Posted by RuudNL View Post
    ...
    If PORTB.0=1
    ...

    You're assuming it can read port B properly.

    I'd start even easier, just blink a LED, nothing else, then build from that.
    My Creality Ender 3 S1 Plus is a giant paperweight that can't even be used as a boat anchor, cause I'd be fined for polluting our waterways with electronic devices.

    Not as dumb as yesterday, but stupider than tomorrow!

  7. #7
    Join Date
    Feb 2012
    Posts
    57


    Did you find this post helpful? Yes | No

    Default Re: Who sees my stupid mistake?

    So nobody can spot a mistake in my sample code?
    Strange, because this line is handled correctly:
    Code:
    If (PORTB.0=1) or (PORTB.1=1) or (PORTB.2=1) or (PORTB.3=1) or (PORTB.4=1) then
    .....

  8. #8
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,599


    Did you find this post helpful? Yes | No

    Default Re: Who sees my stupid mistake?

    I pulled out one of my old 16F628 programs from mothballs, only thing I notice, you have no CONFIG statements.


    EDIT: I used this with DEFINE OSC 20

    @ __CONFIG _HS_OSC & _WDT_OFF & _PWRTE_ON & _BODEN_ON & _MCLRE_ON & _LVP_OFF & _CP_OFF
    My Creality Ender 3 S1 Plus is a giant paperweight that can't even be used as a boat anchor, cause I'd be fined for polluting our waterways with electronic devices.

    Not as dumb as yesterday, but stupider than tomorrow!

  9. #9
    Join Date
    Feb 2012
    Posts
    57


    Did you find this post helpful? Yes | No

    Default Re: Who sees my stupid mistake?

    I set the config parameters in the programmer (as usual).
    The values you mentioned are the defaults in my programmer, except the HS mode, that I set manually.
    The point is that everything (almost) works.
    If I create a loop that does nothing else than: serout2 xxx, delay 1000, I get MIDI information every second.
    My main suspect is that the switch(es) are not read correctly in: if PORTB.x =1 then ...
    Last edited by RuudNL; - 2nd March 2024 at 18:04.

  10. #10
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,599


    Did you find this post helpful? Yes | No

    Default Re: Who sees my stupid mistake?

    Is this the "read modify write" problem?
    (I have to look that up whenever I encounter weird problems)
    My Creality Ender 3 S1 Plus is a giant paperweight that can't even be used as a boat anchor, cause I'd be fined for polluting our waterways with electronic devices.

    Not as dumb as yesterday, but stupider than tomorrow!

  11. #11
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,599


    Did you find this post helpful? Yes | No

    Default Re: Who sees my stupid mistake?

    Quote Originally Posted by RuudNL View Post
    I set the config parameters in the programmer (as usual)...


    What if you set them in the program? Maybe something is wonky with your programmer config. (total wild guess, just eliminating stuff)
    My Creality Ender 3 S1 Plus is a giant paperweight that can't even be used as a boat anchor, cause I'd be fined for polluting our waterways with electronic devices.

    Not as dumb as yesterday, but stupider than tomorrow!

  12. #12
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,599


    Did you find this post helpful? Yes | No

    Default Re: Who sees my stupid mistake?

    Quote Originally Posted by RuudNL View Post
    ...If I create a loop that does nothing else than: serout2 xxx, delay 1000, I get MIDI information every second.
    My main suspect is that the switch(es) are not read correctly in: if PORTB.x =1 then ...
    Blinking a LED confirms if your timing is running at the speed you think it's running at.

    I usually go for LED 1/2 second OFF, 1/2 second ON, just to know my OSC is running as it's supposed to.

    EDIT: Hm, yeah, but you're getting a SEROUT every second already, so not sure what else to check.
    My Creality Ender 3 S1 Plus is a giant paperweight that can't even be used as a boat anchor, cause I'd be fined for polluting our waterways with electronic devices.

    Not as dumb as yesterday, but stupider than tomorrow!

  13. #13
    Join Date
    Feb 2012
    Posts
    57


    Did you find this post helpful? Yes | No

    Default Re: Who sees my stupid mistake?

    I never had problems setting the config parameters in the programmer, but I will try that anyway...
    I have a feeling that the problem is in the line:
    Code:
      if PORTB.0 = 1 then   '<---- this line?
        Serout2 PORTB.7, 16384+12, [144, 44, 127]    ' Rewind
      endif
    The strange thing is that the switch status IS correctly read in this part:
    Code:
    Debounce:
      Pause 20                                          ' ? shorter ?
      If (PORTB.0=1) or (PORTB.1=1) or (PORTB.2=1) or (PORTB.3=1) or (PORTB.4=1) then
        PORTB.6 = 1                                   ' ADDED TO CHECK!! 
        goto Debounce                                ' Button still pressed?
      else
        PORTB.6 = 0                                   ' ADDED TO CHECK!!
      endif  
      Goto Progstart
    So PORTB.6 is high as long as a button is pressed.

    And a loop Serout xxx/ Pause 1000 spits out a burst of MIDI data every second, so it doesn't look it is a config problem.

  14. #14
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,807


    Did you find this post helpful? Yes | No

    Default Re: Who sees my stupid mistake?

    Instead of this

    Code:
      if PORTB.0 = 1 then   '<---- this line?
        Serout2 PORTB.7, 16384+12, [144, 44, 127]    ' Rewind
      endif
    try this
    Code:
      if PORTB.0 = 1 then   '<---- this line?
        High LED
        Pause 100
        Low LED    ' Rewind
      endif
    Set LED to a Port and see if it blinks.

    Ioannis

  15. #15
    Join Date
    Feb 2012
    Posts
    57


    Did you find this post helpful? Yes | No

    Default Re: Who sees my stupid mistake?

    It doesn't. I tried already:

    Code:
      If PORTB.0 = 1 then
        PORTB.6 = 1
      else
        PORTB.6 = 0
        ' and loop...
    PORTB.6 never gets high!

    I can't wrap my head around it...
    In the past I've written considerably more complicated code, usually with success.
    I think I'll sleep on it and try again from scratch tomorrow!
    Thank you everyone for your thoughts!
    Last edited by RuudNL; - 2nd March 2024 at 19:41.

  16. #16
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,807


    Did you find this post helpful? Yes | No

    Default Re: Who sees my stupid mistake?

    Then fried PIC maybe?

    Ioannis

  17. #17
    Join Date
    Feb 2012
    Posts
    57


    Did you find this post helpful? Yes | No

    Default Re: Who sees my stupid mistake?

    I doubt it... It is a brandnew one!
    What I find extremely strange, is that it seems the status of PORTB.x is not read in:
    Code:
    if PORTB.0 = 1 then 
        Serout2 PORTB.7, 16384+12, [144, 44, 127]    ' Rewind
      endif
    But IS read in:
    Code:
    Debounce:
      Pause 20                                          ' ? shorter ?
      If (PORTB.0=1) or (PORTB.1=1) or (PORTB.2=1) or (PORTB.3=1) or (PORTB.4=1) then
        PORTB.6 = 1                                   ' ADDED TO CHECK!! 
        goto Debounce                                ' Button still pressed?
      else
        PORTB.6 = 0                                   ' ADDED TO CHECK!!
      endif  
      Goto Progstart
    If it was a faulty PIC, this wouldn't work in both cases.

  18. #18
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,807


    Did you find this post helpful? Yes | No

    Default Re: Who sees my stupid mistake?

    Is it possible that you have high capacitance on the port B pins?

    Try to add pause 10 every after command and see how it goes.

    Ioannis

  19. #19
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,599


    Did you find this post helpful? Yes | No

    Default Re: Who sees my stupid mistake?

    Defective breadboard?
    My Creality Ender 3 S1 Plus is a giant paperweight that can't even be used as a boat anchor, cause I'd be fined for polluting our waterways with electronic devices.

    Not as dumb as yesterday, but stupider than tomorrow!

  20. #20
    Join Date
    Feb 2012
    Posts
    57


    Did you find this post helpful? Yes | No

    Default Re: Who sees my stupid mistake?

    In the meantime there is some progress:
    Code:
    CMCON =  7                                       ' Digital mode
    DEFINE OSC 20                                    ' 20 MHZ OSC.                                                                      
    TRISB= %00111111                                 ' PORTB 6/7=Output 0..5=Input
    
    Progstart: 
      if (PORTB & 1) = 1 then 
        Serout2 PORTB.7, 16384+12, [144, 44, 127]    ' Rewind
      endif  
      if (PORTB & 2) = 2 then 
        Serout2 PORTB.7, 16384+12, [144, 46, 127]    ' Fast Fwd.
      endif  
      if (PORTB & 4) = 4 then 
        Serout2 PORTB.7, 16384+12, [144, 48, 127]    ' Stop  
      endif  
      if (PORTB & 8) = 8 then 
        Serout2 PORTB.7, 16384+12, [144, 50, 127]    ' Play  
      endif  
      if (PORTB & 16) = 16 then 
        Serout2 PORTB.7, 16384+12, [144, 52, 127]    ' Record
      endif  
    
    Debounce:
      Pause 100                                      ' ? shorter ?
      If (PORTB.0=1) or (PORTB.1=1) or (PORTB.2=1) or (PORTB.3=1) or (PORTB.4=1) then
           goto Debounce                                ' Button still pressed?
      endif  
      Goto Progstart
    This works, as long as I comment out these lines:
    Code:
     '  If (PORTB.0=1) or (PORTB.1=1) or (PORTB.2=1) or (PORTB.3=1) or (PORTB.4=1) then
     '     goto Debounce                                ' Button still pressed?
     '  endif
    The idea is of course to prevent that the same data is sent multiple times, so I want to wait until a button is released.
    But the these lines seem to avoid that PORTB is read again.
    Last edited by RuudNL; - 3rd March 2024 at 09:42.

  21. #21
    Join Date
    Feb 2012
    Posts
    57


    Did you find this post helpful? Yes | No

    Default Re: Who sees my stupid mistake?

    It seems the problem is solved. (Although I still don't understand why my first attempt didn't work...)
    Now I use BUTTON as included in Picbasic. I don't like all the labels, but at least this works now!
    Code:
    '* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
    '* MIDI Remote for Reaper            20 MHz resonator (HS)           *
    '* PIC16F628                                                         *
    '* PORTB.0 = Rewind                                                  * 
    '* PORTB.1 = FastFwd.                                                *
    '* PORTB.2 = Stop                                                    *
    '* PORTB.3 = Play                                                    *
    '* PORTB.4 = Record                                                  *
    '* PORTB.7 = Output                                                  *
    '* Version 1.0                       03 March 2024                   *
    '* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
            
    CMCON =  7                                       ' Digital mode
    DEFINE OSC 20                                    ' 20 MHZ OSC.       
    'DEFINE BUTTON_PAUSE 50                          ' Is this needed ?                                     
    TRISB= %00111111                                 ' PORTB 6..7=Output 0..5=Input
    
    BTN1 var Byte                                    ' Button variables for internal use 
    BTN2 var byte
    BTN3 var byte
    BTN4 var byte
    BTN5 var byte
    
    BTN1 = 0                                         ' Initialize button variables
    BTN2 = 0
    BTN3 = 0
    BTN4 = 0
    BTN5 = 0
    
    Progstart: 
      BUTTON PORTB.0, 1, 255, 0 , BTN1, 0 , But2
      Serout2 PORTB.7, 16384+12, [144, 44, 127]      ' Rewind
    But2:
      BUTTON PORTB.1, 1, 255, 0 , BTN2, 0 , But3
      Serout2 PORTB.7, 16384+12, [144, 46, 127]      ' Fast Fwd.
    But3:  
      BUTTON PORTB.2, 1, 255, 0 , BTN3, 0 , But4
      Serout2 PORTB.7, 16384+12, [144, 48, 127]      ' Stop  
    But4:  
      BUTTON PORTB.3, 1, 255, 0 , BTN4, 0 , But5
      Serout2 PORTB.7, 16384+12, [144, 50, 127]      ' Play  
    But5:
      BUTTON PORTB.4, 1, 255, 0 , BTN5, 0 , ProgStart
      Serout2 PORTB.7, 16384+12, [144, 52, 127]      ' Record
    Goto Progstart
    The moral: never try to reinvent the wheel!

  22. #22
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default Re: Who sees my stupid mistake?

    I think the first version should work - but do so quite unreliably.
    The reason: If the button being pressed happens to FIRST be checked in the debounce routine the code will be stuck there until the button is released and no action will be taken.
    One would think that if you have played with this for some time it would at least sometimes be detected at the "correct" place before falling into the debounce though.

    It would probably be a better idea to GOSUB the debounce routine after the SEROUT2 statement or simply:
    Code:
      if (PORTB & 1) = 1 then 
        Serout2 PORTB.7, 16384+12, [144, 44, 127]    ' Rewind
         WHILE PORTB.1 : WEND
         PAUSE 10  ' Or whatever
      endif

  23. #23
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,807


    Did you find this post helpful? Yes | No

    Default Re: Who sees my stupid mistake?

    Henrik, I think this does not explain why code in post #4 also does not work as the RuudNL says.

    Ioannis

  24. #24
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default Re: Who sees my stupid mistake?

    You mean this:
    Code:
    Progstart: 
      if PORTB.0 = 1 then 
        Toggle PORTB.6
      endif  
    
    Debounce:
      Pause 20                                       ' ? shorter ?
      If PORTB.0=1 then
        goto Debounce                                ' Button still pressed?
      endif  
      Goto Progstart
    If so then it's the same potential issue. The PIC runs thru this short program in a couple of microseconds (lets say 20), except for the PAUSE 20 where it spends 99.9% of its time. The likelyhood of the button being pressed during this time and therefor NOT cause the output to toggle is 999 in 1. In the initial code the PAUSE was even longer further decreasing the chanse of the button being polled outside of the debounce routine. Run the code, press the button a few thousand times it "should" toggle the output at least once.

    That's my theory and I'm sticking to it until someone comes up with a better one :-)

    /Henrik.

  25. #25
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,599


    Did you find this post helpful? Yes | No

    Default Re: Who sees my stupid mistake?

    Very good reasoning.
    😎
    My Creality Ender 3 S1 Plus is a giant paperweight that can't even be used as a boat anchor, cause I'd be fined for polluting our waterways with electronic devices.

    Not as dumb as yesterday, but stupider than tomorrow!

  26. #26
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,599


    Did you find this post helpful? Yes | No

    Default Re: Who sees my stupid mistake?

    What happens if you just sit on the button?
    My Creality Ender 3 S1 Plus is a giant paperweight that can't even be used as a boat anchor, cause I'd be fined for polluting our waterways with electronic devices.

    Not as dumb as yesterday, but stupider than tomorrow!

  27. #27
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,521


    Did you find this post helpful? Yes | No

    Default Re: Who sees my stupid mistake?

    Unless you happen to sit down at EXACTLY the right time your ass will be stuck in the debounce loop.

  28. #28
    Join Date
    Nov 2003
    Location
    Greece
    Posts
    3,807


    Did you find this post helpful? Yes | No

    Default Re: Who sees my stupid mistake?

    Indeed Henrik. You are absolutely right!

    Now I recall I had the same issue once and instead of loose my time trying to find the strange error, i went off to a state machine logic and kept my sanity!

    Ioannis

Similar Threads

  1. I don't understand my mistake
    By kik1kou in forum mel PIC BASIC Pro
    Replies: 3
    Last Post: - 31st January 2015, 12:56
  2. Stupid simple question.....
    By chien_fu in forum mel PIC BASIC
    Replies: 18
    Last Post: - 23rd February 2010, 13:21
  3. Stupid question about LCDOUT
    By Glenn in forum mel PIC BASIC Pro
    Replies: 21
    Last Post: - 7th October 2008, 21:37
  4. SERIN2 – SEROUT2 and Manchester mistake.
    By RCtech in forum Serial
    Replies: 8
    Last Post: - 4th September 2007, 22:55
  5. Stupid question
    By Meriachee in forum mel PIC BASIC Pro
    Replies: 15
    Last Post: - 20th July 2007, 05:47

Members who have read this thread : 13

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