Announcement

Collapse
No announcement yet.

HID or PIC18f4550 problem. I need HELP.

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • HID or PIC18f4550 problem. I need HELP.

    Hi everybody. I'm new with microcontroller programing and don't know am I on the right place but it's worth to try. I connect my homemade cnc router with PC with pic18f4550. Everything is working fine, I send a pack of bytes (speed, x, y, z movement), router is doing what I'm sending, but every time after 30 sending, PIC don't receive or send anything like it's blocked. I don't know what I need to do. To reset PIC clock or erase his USB buffer after 29 receiving. I tried to put transistor to reset a PIC every 25 instruction sending and it's working, but then variables in PIC code also starts from beginning so thats bad. Please help.

  • #2
    but every time after 30 sending
    30 what ?
    bits,bytes,packets,words,lines or even hours, minutes seconds

    if you're serious at least post the code with a sketch or schematic of how things are connected , along with a coherent description of how its expected to function versus how it actually does function

    Comment


    • #3
      30 doesn't exactly match the stack depth used for GOSUB but with the limited information provided that's still where I'd start looking. Check your code, make sure that you don't have a subroutine ending with something other than a RETURN.

      /Henrik.

      Comment


      • #4
        The USB buffer is 64 bytes per Endpoint. That math doesn't work either.

        USB requires a periodic refresh/response to maintain connection. It sounds like you specified a packet size that you are exceeding. Each "Send" should be a Data Packet, which you must define in the Enumeration process. With virtually nothing to go on, I'd recommend checking what you are defining in your enumeration.

        Secondly, there are Ping-Pong options. You can limit transactions to Buffer0 (which is used primarily for Enumeration), and extend to Buffer1_IN and _Out, or use Ping-Pong and use all 32 buffers. However, you must keep track of where the data is, going both ways.

        Good luck. Post more information if you want more help.
        We can crack this cotton PIC'n thang!

        Comment


        • #5
          Well...how I understand this. USB buffer in PIC!8f4550 receive in one package 8 bytes. In that pack I send speed, movement of a cnc table. When I send over 29 those packs of bytes PIC don't response any more
          Her is my code:
          DEFINE OSC 48
          DEFINE LOADER_USED 1
          INTCON = %00000000
          TRISB = %00000000
          TRISD = %00001100
          TRISC = %11111111
          PORTB = %00000000
          PORTD = %00001110
          PORTC = %00000000
          'Include "Modedefs.Bas"
          'symbol SCL = PORTB.1
          'SYMBOL SDA = PORTB.0
          I var word
          pocce var byte
          zosa var byte
          xosa var byte
          yosa var byte
          DozvolaNulavanjaX var bit
          DozvolaNulavanjaZ var bit
          DozvolaNulavanjaY var bit
          brojstepova var word
          'Addr var byte
          LoopCount var byte
          'EE_ByteOut var byte
          'EE_ByteIn var byte
          USBBufferSizeMax con 8 ' maximum buffer size
          USBBufferSizeTX con 8 ' input
          USBBufferSizeRX con 8 ' output

          ' the USB buffer...
          USBBuffer Var Byte[USBBufferSizeMax]
          USBBufferCount Var Byte
          USBBuffer1 Var Byte[USBBufferSizeMax]

          ' ************************************************************
          ' * main program loop - remember, you must keep the USB *
          ' * connection alive with a call to USBService every couple *
          ' * of milliseconds or so... *
          ' ************************************************************

          usbinit ' initialise USB...
          FOR LoopCount = 1 TO 8 ' clear USB receive buffer
          USBBuffer[LoopCount] = 0
          USBBuffer1[LoopCount] = 0
          NEXT LoopCount
          pause 100
          PORTD = %00001100
          DoUSBIn:
          USBService ' Must service USB regularly
          USBBufferCount = 8 ' Specify input buffer size
          USBIn 1, USBBuffer, USBBufferCount, DoCheck
          brojstepova = USBBuffer[1]* 255 + USBBuffer[5]
          if USBBuffer[3] = 50 then
          DozvolaNulavanjaX = 1
          DozvolaNulavanjaY = 1
          DozvolaNulavanjaZ = 1
          gosub nulovanje
          endif
          if USBBuffer[3] = 51 then
          DozvolaNulavanjaX = 1
          gosub nulovanje
          Endif
          if USBBuffer[3] = 52 then
          DozvolaNulavanjaY = 1
          gosub nulovanje
          Endif
          if USBBuffer[3] = 53 then
          DozvolaNulavanjaZ = 1
          gosub nulovanje
          Endif
          if USBBuffer[3] = 30 then gosub Reset
          if USBBuffer[3] = 40 then gosub CheckComunication
          if USBBuffer[4] = 3 then
          if USBBuffer[3] = 10 then gosub ZOsaNazad
          if USBBuffer[3] = 20 then gosub ZOsaNapred
          endif
          if USBBuffer[4] = 1 then
          if USBBuffer[3] = 10 then gosub XOsaLevo
          if USBBuffer[3] = 20 then gosub XOsaDesno
          endif
          if USBBuffer[4] = 2 then
          if USBBuffer[3] = 10 then gosub YOsaDole
          if USBBuffer[3] = 20 then gosub YOsaGore
          endif
          goto DoUSBIn
          Reset:
          PORTD = %00001101
          pauseus 100
          PORTD = %00001100
          return
          CheckComunication:
          USBBuffer1 = 100
          goto DoUSBOut
          Return
          XOsaLevo:
          For I = 1 to brojstepova
          USBService
          xosa = xosa + 1
          if xosa = 5 then xosa = 1
          PAUSEus USBBuffer[2]*50
          gosub xosastep
          PAUSEus USBBuffer[2]*50
          PORTB = %00000000
          NEXT I
          USBBuffer1 = 45
          goto DoUSBOut
          return
          XOsaDesno:
          For I = 1 to brojstepova
          USBService
          xosa = xosa - 1
          if xosa = 0 then xosa = 4
          PAUSEus USBBuffer[2]*50
          gosub xosastep
          PAUSEus USBBuffer[2]*50
          PORTB = %00000000
          NEXT I
          USBBuffer1 = 45
          goto DoUSBOut
          return
          YOsaDole:
          For I = 1 to brojstepova
          USBService
          yosa = yosa + 1
          if yosa = 5 then yosa = 1
          PAUSEus USBBuffer[2]*50
          gosub yosastep
          PAUSEus USBBuffer[2]*50
          PORTD = %00000010
          NEXT I
          USBBuffer1 = 45
          goto DoUSBOut
          return
          YOsaGore:
          For I = 1 to brojstepova
          USBService
          yosa = yosa - 1
          if yosa = 0 then yosa = 4
          PAUSEus USBBuffer[2]*50
          gosub yosastep
          PAUSEus USBBuffer[2]*50
          PORTD = %00000010
          NEXT I
          USBBuffer1 = 45
          goto DoUSBOut
          return
          ZOsaNazad:

          For I = 1 to brojstepova
          USBService
          zosa = zosa + 1
          if zosa = 5 then zosa = 1
          PAUSEus USBBuffer[2]*50
          gosub zosastep
          PAUSEus USBBuffer[2]*50
          PORTB = %00000000
          NEXT I
          USBBuffer1 = 45
          goto DoUSBOut
          return
          ZOsaNapred:

          For I = 1 to brojstepova
          USBService
          zosa = zosa - 1
          if zosa = 0 then zosa = 4
          PAUSEus USBBuffer[2]*50
          gosub zosastep
          PAUSEus USBBuffer[2]*50
          PORTB = %00000000
          NEXT I
          USBBuffer1 = 45
          goto DoUSBOut
          return
          nulovanje:
          If DozvolaNulavanjaY = 1 then
          while portc.0 = 0
          USBService
          yosa = yosa - 1
          if yosa = 0 then yosa = 4
          PAUSEus 2000
          gosub yosastep
          PAUSEus 2000
          PORTD = %00000000
          wend
          while portc.0 = 1
          USBService
          yosa = yosa + 1
          if yosa = 5 then yosa = 1
          PAUSEus 2000
          gosub yosastep
          PAUSEus 2000
          PORTD = %00000000
          wend
          endif
          if DozvolaNulavanjaZ = 1 then
          while portc.0 = 0
          USBService
          zosa = zosa + 1
          if zosa = 5 then zosa = 1
          PAUSEus 5000
          gosub zosastep
          PAUSEus 5000
          PORTB = %00000000
          wend
          while portc.0 = 1
          USBService
          zosa = zosa - 1
          if zosa = 0 then zosa = 4
          PAUSEus 5000
          gosub zosastep
          PAUSEus 5000
          PORTB = %00000000
          wend
          endif
          if DozvolaNulavanjaX = 1 then
          while portc.0 = 0
          USBService
          xosa = xosa + 1
          if xosa = 5 then xosa = 1
          PAUSEus 5000
          gosub xosastep
          PAUSEus 5000
          PORTB = %00000000
          wend
          while portc.0 = 1
          USBService
          xosa = xosa - 1
          if xosa = 0 then xosa = 4
          PAUSEus 5000
          gosub xosastep
          PAUSEus 5000
          PORTB = %00000000
          wend
          endif
          DozvolaNulavanjaX = 0
          DozvolaNulavanjaY = 0
          DozvolaNulavanjaZ = 0
          USBBuffer1 = 25
          goto DoUSBOut
          return
          DoCheck:
          If Portd.2 = 0 then
          'toggle PORTB.5
          'USBBuffer1 = 15
          goto DoUSBOut
          endif
          If Portd.3 = 0 then
          'toggle PORTB.4
          'USBBuffer1 = 25
          goto DoUSBOut
          endif
          goto DoUSBIn
          DoUSBOut:

          USBService ' Must service USB regularly
          USBOut 1, USBBuffer1, 8, DoUSBIn ' Send the bytes back
          goto DoUSBIn
          xosastep:
          if xosa = 1 then PORTB = %00001000
          if xosa = 2 then PORTB = %00000100
          if xosa = 3 then PORTB = %00000010
          if xosa = 4 then PORTB = %00000001
          return
          yosastep:
          if yosa = 1 then PORTD = %10000000
          if yosa = 2 then PORTD = %01000000
          if yosa = 3 then PORTD = %00100000
          if yosa = 4 then PORTD = %00010000
          return
          zosastep:
          if zosa = 1 then PORTB = %10000000
          if zosa = 2 then PORTB = %01000000
          if zosa = 3 then PORTB = %00100000
          if zosa = 4 then PORTB = %00010000
          return
          end .

          Comment


          • #6
            Is it possible that "USBBuffer1 = 45" should be "USBBuffer[1] = 45"?
            Also, I'm seeing several "IF" statements with insufficient "ENDIF"'s to satisfy the nesting. You may be experiencing a Stack Overflow condition.
            We can crack this cotton PIC'n thang!

            Comment


            • #7
              USBBuffer1 is variable that send a byte from PIC to PC when one operation is done.Then PC is sending bytes for next operation. I changed it to USBBuffer1[2] and nothing changed. Steel blocking on 29 operation. Microcode Basic will make an error if I don't write enough ENDIF statements. It's confusing that PIC is working fine for 29 or 30 operation.
              Last edited by Tatilus; 03-01-2019, 12:27 PM.

              Comment


              • #8
                No offense but that's pretty hard to follow, please at least use the code tags when posting ;-)

                I'll reiterate what I said earlier, look very hard at how and where you do GOSUB vs GOTO and if you actually (ever) RETURN from a subroutin called with GOSUB. For example, in one place you have
                Code:
                if USBBuffer[3] = 10 then gosub ZOsaNazad
                That subroutine looks like this (with my indentation added):
                Code:
                ZOsaNazad:
                
                For I = 1 to brojstepova
                USBService
                zosa = zosa + 1
                if zosa = 5 then zosa = 1
                PAUSEus USBBuffer[2]*50
                gosub zosastep
                PAUSEus USBBuffer[2]*50
                PORTB = %00000000
                NEXT I
                USBBuffer1 = 45
                goto DoUSBOut
                return
                Where you GOTO DoUSBOut so the RETURN you've got at the end of the subroutine never executes. There's nothing REALLY wrong with GOTOing something from within a subroutine as long as you eventually end up at RETURN but in your case you just seem to nest yourself deeper and deeper.
                In DoUSBOut you GOTO DoUSBIn from where you GOSUB nulovanje from where you GOSUB yosastep (from where you actually do a RETURN).

                /Henrik.

                EDIT: OK, the standard code tags doesn't seem to work so...

                Comment


                • #9
                  Jeah...got it. I will repair that and replay soon.

                  Comment


                  • #10
                    maybe the code tags are case sensitive ,need to be uc


                    if USBBuffer[3] = 10 then gosub ZOsaNazad



                    Code:
                    	
                    	
                    	
                    
                    
                    Code:
                    ZOsaNazad:
                    
                    For I = 1 to brojstepova
                    USBService
                    zosa = zosa + 1
                    if zosa = 5 then zosa = 1
                    PAUSEus USBBuffer[2]*50
                    gosub zosastep
                    PAUSEus USBBuffer[2]*50
                    PORTB = %00000000
                    NEXT I
                    USBBuffer1 = 45
                    goto DoUSBOut
                    return
                    edit , or they don't work any more and this forum is broken
                    Last edited by richard; 03-01-2019, 01:17 PM.

                    Comment


                    • #11
                      In an alternate timeline there was a problem with the CODE tag, it was discussed at length, a fix was found and the posts discussing it were deleted.

                      Also while that timeline existed I was a million dollars richer, 6 inches taller and 74% better looking. And I told the truth exclusively.

                      Carry on.
                      Charles Leo
                      ME Labs, Inc.
                      http://melabs.com

                      Comment


                      • #12
                        edit , or they don't work any more and this forum is broken
                        I don't know what CODE tags are. I suppose they are explanation of line in CODE so...

                        ZOsaNazad:
                        For I = 1 to brojstepova --------'Numbers of steps for Z coordinate stepper motor
                        USBService
                        zosa = zosa + 1 ------------------'one of four coils in stepper motor will be energized
                        if zosa = 5 then zosa = 1 -------' Stepper has 4 coils and 4 steps per cycle so must go from beginning
                        PAUSEus USBBuffer[2]*50 -----' Pause between steps of stepper motor
                        gosub zosastep ------------------' go to Z coordinate and make one step
                        PAUSEus USBBuffer[2]*50 -----' pause between energizing and releasing a coil
                        PORTB = %00000000 ----------' releasing a coil
                        NEXT I ------------------------------' next step
                        USBBuffer1 = 45 -----------------' 45 is a byte(code), and it's sending back to PC for information when operation is done
                        goto DoUSBOut ------------------' go and send that operation is done
                        return

                        Like I sad before I am new in all this (microcontroller, forums) so I apologize for my mistakes here in forum.
                        Last edited by Tatilus; 03-02-2019, 03:13 AM.

                        Comment


                        • #13
                          Thank You all for helping me. I replaced "goto DoUSBOUT" with "USBOut 1, USBBuffer1, 8, DoUSBIn" like Henrik Olsson suggest and it's working perfectly. Thank You a lot.

                          Comment

                          Working...
                          X