14K50USB


I recently got my Main Firewing board to which I noticed a 18F14K50 MCU, Being that I'm a keen programmer of the 18F series MCU and Swordfish Basic, David kindly gave me the Code for the Main board 18F14K50 USB interface, the code was originally compiled using Firewing. This is my version to which I have ported to Swordfish Basic, and thought I would share.

The 18F14K50 MCU is used to enable you to both program your main board, send and receive serial data to and from your PC.

Note: The USB library used is based on the update by Jerry Messina, SF USB Library V1.4.2.

Since this was posted the SF USB Library has had a few bug fixes, check out the following link SF usb library fixes.

You will also need the USB Descriptor which can also be downloaded below.

Swordfish Code

' device and clock...
Device = 18F14K50
Clock = 48  

' configuration...
Config CPUDIV = NOCLKDIV
Config USBDIV = OFF
Config FOSC = HS          
Config PLLEN = ON
Config PWRTEN = ON
Config BOREN = OFF
Config WDTEN = OFF   

' configure and import the USB and Usart modules...
#option USB_DESCRIPTOR = "firewingDescriptor.bas"
#option USB_BUS_POWER = 250     ' bus power
#option USB_SERVICE = false     ' non interrupts based polling   
#option USB_VID = $1781         ' VID - 6017 is Mecanique
#option USB_PID = $03E8         ' PID - 1000 for Firewing CDC  
Include "usbcdc.bas"
Include "Usart.bas"

' ISRTimer module - used for blinky LEDs...
#option TIMER_AVAILABLE = 3
#option TIMER_AUTO_RELOAD = false  ' added to ensure proper timing
Include "ISRTimer.bas"

Dim txLED As PORTC.3
Dim rxLED As PORTC.4  
Const ledOnTime = 25  
Const resetTime = 100            

' reset pin - the firewing reset pin is connected to this pin... 
Dim resetPin As PORTB.4
Dim hasBeenReset As Boolean
Dim data As Byte           

' OnTimer1 event... (CDC.DataAvailable)
Event OnTimer1()
   Timer.Items(0).Interval = ledOnTime
   Low(rxLED)     
End Event

' OnTimer2 event... (USART.DataAvailable)
Event OnTimer2()
   Timer.Items(1).Interval = ledOnTime
   Low(txLED)     
End Event

' OnTimer3 event...
Event OnTimer3()
   Timer.Items(2).Interval = resetTime
   High(resetPin)
End Event

' device startup code - set all digital for this device...
    ANSEL = $00
    ANSELH = $00
    High(resetPin)
    hasBeenReset = false

' configure Usart Baudrate... 
   USART.SetBaudrate(br38400) 

' activate the timer module...
   Timer.Initialize

' configure timers...
   Timer.Items(0).Interval = ledOnTime    ' RX
   Timer.Items(0).OnTimer = OnTimer1      ' timer event handler
   Timer.Items(1).Interval = ledOnTime    ' TX
   Timer.Items(1).OnTimer = OnTimer2      ' timer event handler
   Timer.Items(2).Interval = resetTime    ' DTR low
   Timer.Items(2).OnTimer = OnTimer3      ' timer event handler

' enable all timers...
   Timer.Items(0).Enabled = true
   Timer.Items(1).Enabled = true
   Timer.Items(2).Enabled = true

' start processing all timers...
   Timer.Start

' main program loop...
While true   

      ' if CDC has data, read a byte and then write
      ' to the hardware USART...
      If CDC.DataAvailable Then  
         data = CDC.ReadByte
         USART.WriteByte(data)   
         High(rxLED)
         Timer.Items(0).Enabled = true 
      End If    

      If USART.DataAvailable Then
         data = USART.ReadByte
         CDC.WriteByte(data)       
         High(txLED)
         Timer.Items(1).Enabled = true         
      End If     

      ' check to see if USB buffer has overrun...   
      If CDC.Overrun Then
         CDC.ClearOverrun
      End If  

      ' check to see if USART has overrun...   
      If USART.Overrun Then
         USART.ClearOverrun
      End If  

      ' look for a DTR event   
      If CDC.DTR = 1 And Not hasBeenReset Then 
         hasBeenReset = true
         Low(resetPin) 
         Timer.Items(2).Enabled = true 
      ElseIf CDC.DTR = 0 Then
         hasBeenReset = false
      End If    

      ' service the USB connection! 
      CDC.USBService           
Wend

Downloads

The source code including the usb descriptor can be download from here.