Welcome, Guest
Username: Password: Secret Key Remember me
  • Page:
  • 1
  • 2

TOPIC: New USB Stack in PDS

Re: New USB Stack in PDS 6 years 11 months ago #12400

  • dmtulsa
  • dmtulsa's Avatar
  • Offline
  • Fresh Boarder
  • Thanks received: 1
Ok FireFox crashed on me so I don't know what got sent. I'll stat over.

First the PDS source:
'Firmware for NORDAM TRCU LJ,AS,DS units
'USB to PC app 
'SPI to Port Expanders MCP23s17  
'Set & read A/D from MUX 74HC4051
'7-9-2012 Doug McDonald
'Rev 0.0.1

;-------------------------------------------------------------------------------
;**** Added by Fuse Configurator ****
; Use the Fuse Configurator plug-in to change these settings

Device = 18F4550
Xtal = 48
'Stack_Size = 20
Clear
Declare Dead_Code_Remove = On 
All_Digital = True  'set all ports to digital

'Include "SPI.inc"
Include "HID_Descriptor.inc"

Symbol cs1 = LATA.2
Symbol cs2 = LATA.3

Dim i As Byte
Dim j As Byte
Dim ADval As Word
Dim ADL As ADval.LowByte
Dim ADH As ADval.HighByte
'MCP23S17 PORT EXPANDER REGISTER VARIABLES
Dim bRetVal As Byte
Dim Exp1L As Byte
Dim Exp1H As Byte
Dim Exp2L As Byte
Dim Exp2H As Byte
'Dim SPI_TX As Word        'send to device
Dim SPItx_H As Byte 
Dim SPItx_L As Byte
'Dim SPI_RX As Word        'read from  device
Dim SPIrx_H As Byte
Dim SPIrx_L As Byte




'**************setup A/D*****************************

' Read the value from channel 0 of the ADC and place in variable Var1.
'Declare Adin_Res = 10 ' 10-bit result required
'Declare Adin_Tad = FRC ' RC OSC chosen
'Declare Adin_Stime = 50 ' Allow 50us sample time
ADCON0 = %00000001  'A/D channel 0, A/D enabled
ADCON1 = %00001110 ' Set analogue input on PortA.0
ADCON2 = %11100101  'right justified,8-Tad,FOSC/4 

'USB---------------------------------
Dim USBmsgFlag As Byte        
Dim USBBufferTx[64] As Byte      ' USB Output buffer
Dim USBBufferRX[64] As Byte     ' USB Input buffer


' port settings-----------------------------
TRISA = 0x01 'outputs  A0 A/D input
TRISB = 0x01 'outputs  B0-input
TRISC = 0 'outputs    first three bits 0-2 are mux address, bits 3-4-5 is enable
TRISD = 0 'outputs
TRISE = 0 'outputs
LATA = 0
LATB = 0
LATC = 0
LATD = 0xff       'set mux inactive (active low)
LATE = 0


cs1 = 1 'CHIP DISABLE  MCP23s17 
cs2 = 1 'CHIP DISABLE  MCP23s17 


Clrf SSPSTAT
Clrf SSPCON1
SSPSTAT = 0x80 '10000000
SSPCON1 = 0x31 '00110001

DelayMS 250  'setteling

Clear USBBufferTx   ' ensure buffers are empty
Clear USBBufferRX
Clear HID_Report_In

GoSub INIT_Exp 'INITIALIZE MCP23S17
DelayMS 50  'setteling
'*****************************************
Main:

     If USB_tAttached = True Then  
        GoSub ChkUSBMsg
     End If 

GoTo Main


'************* USB routines *******************************

' ************************************************************
' * receive data from the USB bus                            *
' ************************************************************
ChkUSBMsg:  
     mUSBService()               'I assume this checks for a message
     HID_RxReport(USBBufferRX,64) ' read message into buffer
     USBmsgFlag = USBBufferRX[0]  'get byte 0, I send this from PC app
         Select Case  USBmsgFlag  'test byte 0, I send this from PC app
           Case 1               'test for message type 1 from pc
                GoSub Set_Exp_Ports
                
           Case 2                'test for message type 1 from pc
                GoSub Read_MUX 
                
         End Select
         'clear buffers
         Clear USBBufferTx 
         Clear USBBufferRX
         Clear HID_Report_In         

  

'     USBIn 1, USBBufferRX,  Auto
'     If STATUS.0 = 1 Then  Return
'         USBmsgFlag = USBBufferRX[0]
'         Select Case  USBmsgFlag
'           Case 1
'               GoSub Set_Exp_Ports     'need to set up in FF
'           Case 2
'               GoSub Read_MUX
'               'GoSub Set_Exp_Ports
'         End Select
Return


'****************74HC4051 MUX routines *************************
   'set mux  first three bits 0-2 are mux address, bits 3-4-5 is Enable
 Read_MUX:  
  ' LATC.0 = 1 
   LATD = 0xff   'clear all  low is active
   DelayCS 5    'delay 1 cycle
   j = 1
   For i = 0 To 7
      LATD = 0x60 + i  '0x60 is enable for mux 1 0110xxxx address is low 3 bits
      ADval = ADIn 0
      USBBufferTx[j + 1] = ADH      'high byte
      USBBufferTx[j] = ADL  'low byte
      j = j + 2
   Next
   
   LATD = 0xff  'clear all  low is active
   DelayCS 5    'delay 1 cycle
   For i = 0 To 7
      LATD = 0x50 + i  
      ADval = ADIn 0
      USBBufferTx[j + 1] = ADH      'high byte
      USBBufferTx[j] = ADL  'low byte
      j = j + 2
   Next
   
   LATD = 0xff  'clear all  low is active
   DelayCS 5    'delay 1 cycle
   For i = 0 To 7
       LATD = 0x30 + i  
      ADval = ADIn 0
      USBBufferTx[j + 1] = ADH      'high byte
      USBBufferTx[j] = ADL  'low byte
      j = j + 2
   Next     
   LATD = 0xff  'clear all  low is active
   HID_TxReport(USBBufferTx,64)
   DelayMS 1
'  Repeat
'   USBOut 1, USBBufferTx,64        'send to PC       
'  Until STATUS.0 = 0             ' Wait for control over the buffer RAM
 
Return

'***********************************************************

'******************* MCP23S17 Routines ********************
INIT_Exp:
  '****set up Expander 1 *****  
  Clear cs1           ' Enable the SPI interface
  DelayCS 1              ' A one cycle delay before starting
  SPItx_H = %11110000   '0xF0 Force ChipAdd to 000
  GoSub Do_SPI_B
  SPItx_H = 0x02         ' Writing to IOCONA writes also to IOCONB  was 0x0a
  GoSub Do_SPI_B
  SPItx_H = %00011000   '0x18 Init value for IOCON
  GoSub Do_SPI_B
  Set cs1             ' Disable the SPI interface
  DelayCS 1              ' A one cycle delay before starting
 
  

' Write to the TRIS of PORTB and set it to all outputs

  Clear cs1           ' Enable the SPI interface
  DelayCS 1              ' A one cycle  delay before starting
  SPItx_H = %01000000   '0x40 Target address 000 and set the R/W bit to Write(0)
  GoSub Do_SPI_B
  SPItx_H = 0x01         ' We want to access the IODIRB register
  GoSub Do_SPI_B
  SPItx_H = %00000000   ' Set to all outputs
  GoSub Do_SPI_B  
  Set cs1             ' Disable the SPI interface  
  DelayCS 1              ' A one cycle delay before starting
 

' Write to the TRIS of PORTA and set it to all outputs
'
  Clear cs1           ' Enable the SPI interface
  DelayCS 1              ' A one cycle  delay before starting
  SPItx_H = %01000000   '0x40 Target address 000 and set the R/W bit to Write(0)
  GoSub Do_SPI_B
  SPItx_H = 0x00         ' We want to access the IODIRA register
  GoSub Do_SPI_B
  SPItx_H = %00000000   ' Set to all outputs
  GoSub Do_SPI_B
  Set cs1             ' Disable the SPI interface 
  DelayCS 1              ' A one cycle delay before starting
  
'****set up Expander 2 *****
  Clear cs2           ' Enable the SPI interface
  DelayCS 1              ' A one cycle delay before starting
  SPItx_H = %11110001   '0xF1 Force ChipAdd to 000
  GoSub Do_SPI_B
  SPItx_H = 0x02         ' Writing to IOCONA writes also to IOCONB
  GoSub Do_SPI_B
  SPItx_H = %00011000   '0x18 Init value for IOCON
  GoSub Do_SPI_B
  Set cs2             ' Disable the SPI interface
  DelayCS 1              ' A one cycle delay before starting
  
' Write to the TRIS of PORTB and set it to all outputs
'
  Clear cs2           ' Enable the SPI interface
  DelayCS 1              ' A one cycle  delay before starting
  SPItx_H = %01000000   '0x40 Target address 000 and set the R/W bit to Write(0)
  GoSub Do_SPI_B
  SPItx_H = 0x01         ' We want to access the IODIRB register
  GoSub Do_SPI_B
  SPItx_H = %00000000   ' Set to all outputs
  GoSub Do_SPI_B
  Set cs2             ' Disable the SPI interface  
  DelayCS 1              ' A one cycle delay before starting' 

' Write to the TRIS of PORTA and set it to all outputs
'
  Clear cs2           ' Enable the SPI interface
  DelayCS 1              ' A one cycle  delay before starting
  SPItx_H = %01000000   '0x40 Target address 000 and set the R/W bit to Write(0)
  GoSub Do_SPI_B
  SPItx_H = 0         ' We want to access the IODIRA register
  GoSub Do_SPI_B
  SPItx_H = %00000000   ' Set to all outputs
  GoSub Do_SPI_B
  Set cs2             ' Disable the SPI interface 
  DelayCS 1              ' A one cycle delay before starting' 
 
 Return
 
 Set_Exp_Ports: 'set the expander ports to some value

' get I/O values from pc
  Exp1L = USBBufferRX[1]
  Exp1H = USBBufferRX[2]
  Exp2L = USBBufferRX[3]
  Exp2H = USBBufferRX[4]



'******* Expander 1 *****
 
' Write to the Latch of PORTA
  Clear cs1           ' Enable the SPI interface
  DelayCS 1              ' A one cycle  delay before starting
  SPItx_H = %01000000   ' 0x40 Target address 000 and set the R/W bit to Write(0)
  GoSub Do_SPI_B
  SPItx_H = 0x14         ' We want to access the OLATA register
  GoSub Do_SPI_B
  SPItx_H = Exp1L     ' Set all I/O 0= off 1 = on
  GoSub Do_SPI_B
  Set cs1             ' Disable the SPI interface  
  DelayCS 1

' Write to the Latch of PORTB
  Clear cs1           ' Enable the SPI interface
  DelayCS 1              ' A one cycle  delay before starting
  SPItx_H = %01000000   ' 0x40 Target address 000 and set the R/W bit to Write(0)
  GoSub Do_SPI_B
  SPItx_H = 0x15         ' We want to access the OLATB register
  GoSub Do_SPI_B
  SPItx_H = Exp1H      ' Set all I/O 0= off 1 = on
  GoSub Do_SPI_B
  Set cs1             ' Disable the SPI interface  
  DelayCS 1  

  '******* Expander 2 *****

' Write to the Latch of PORTA
  Clear cs2           ' Enable the SPI interface
  DelayCS 1              ' A one cycle  delay before starting
  SPItx_H = %01000000   ' 0x40 Target address 000 and set the R/W bit to Write(0)
  GoSub Do_SPI_B
  SPItx_H = 0x14         ' We want to access the OLATA register
  GoSub Do_SPI_B
  SPItx_H = Exp2L    ' Set all outputs high
  GoSub Do_SPI_B
  Set cs2             ' Disable the SPI interface  
  DelayCS 1

' Write to the Latch of PORTB
  Clear cs2           ' Enable the SPI interface
  DelayCS 1              ' A one cycle  delay before starting
  SPItx_H = %01000000   ' 0x40 Target address 000 and set the R/W bit to Write(0)
  GoSub Do_SPI_B
  SPItx_H = 0x15         ' We want to access the OLATB register
  GoSub Do_SPI_B
  SPItx_H = Exp2H    ' Set all outputs high
  GoSub Do_SPI_B
  Set cs2             ' Disable the SPI interface 
  DelayCS 1     
Return

'*************************************************************   
'MSSP "hardware" SPI routine
'*************************************************************
'Do_SPI_W: 'SPI read / Write  word

   'reset SPI config
'   clrf SSPSTAT
'   clrf SSPCON1
'   SSPSTAT = %10000000 ' %X1XXXXXX ' 1 = transmitted On falling Clock Edge
'   SSPCON1 = %10100001 ' %xx1xxxxx ' 1 = ENABLES SCK SDO & SDI 
       
'    SSPBUF = SPItx_H  'highbyte
'    While PIR1.3 = 0: Wend ' TX/RX COMPLETE?
'    SPIrx_H = SSPBUF
'    PIR1.3 = 0

'    SSPBUF = SPItx_L   'lowbyte
'    While PIR1.3 = 0: Wend ' TX/RX COMPLETE?
'    SPIrx_L = SSPBUF
'    PIR1.3 = 0

'Return

Do_SPI_B: 'SPI read / Write byte

        
    SSPBUF = SPItx_H  'highbyte
    While PIR1.3 = 0: Wend ' TX/RX COMPLETE?
    SPIrx_H = SSPBUF
    PIR1.3 = 0



Return

Next the PowerBasic ver 10.02 using FireFly 3.6 is attached I hope.

Next I'll see about the board layout on monday. I will be a pdf not the AREAS / ISS files

Thank you
Doug
Attachments:

Re: New USB Stack in PDS 6 years 11 months ago #12401

  • dmtulsa
  • dmtulsa's Avatar
  • Offline
  • Fresh Boarder
  • Thanks received: 1
check out both USB sections in the PDS forum............it got pretty heated to say the least

Re: New USB Stack in PDS 6 years 10 months ago #12474

  • Roger
  • Roger's Avatar
  • Offline
  • Fresh Boarder
  • Thanks received: 1
Thanks for that Doug. I built similar hardware for a control project and needed to make a monitor for the system. Your PowerBasic and PIC USB will be a great help.

Roger

Re: New USB Stack in PDS 6 years 10 months ago #12493

  • W4GNS
  • W4GNS's Avatar
  • Offline
  • Fresh Boarder
  • Posts: 11
  • Thanks received: 43

Englands contribution's to the world]


Who da Beatles ?
  • Page:
  • 1
  • 2
Time to create page: 0.255 seconds