Jeff Weinmann - 2022-08-17

Evan and William,

I'm working with Northern Software's .NET SDK and it has the capability of connecting, resetting and programing PICs all via .NET software. So it's a VERY useful tool. I'm currently working on a VB.NET front end to take advantage of all those things.

One thing that is also possible is the ability to connect via a USART channel. I can create two way communication between the NS chip and the target MCU. (My recent posts are related to this capability)

Communication is a bit quirky when it comes to sending data from NS to a PIC, but I found that if I send only 1 character byte commands from NS to PIC, it seems to work quite well. The goal is to control the PIC via a 1 character command set to do some special stuff (ie, count frequencies to calibrate clocks!)

I found on the Great Cow Basic side of things, that reading data from NS in USART BLOCKING mode is rock solid, but the problem with blocking is just that, it BLOCKS my Forever loop.

So I pieced together my own version of the ring buffer. In my case, I really don't need the extra complication of a buffer at all, I'm just expecting to read a 1 byte command on USART channel 2.

So I setup an interrupt to do so, and it seems to work very well. (It took me a while to figure out that my PIC doesn't accept UsartRX2Ready as an interrupt but UART2ReceiveInterrupt)

I only have a few specific commands I want to request the PIC perform, so I will never run out of 1 character commands. In my example below, I am looking for command character '5' and sending back data to NS.

It works, but if you have better suggestions for my case I would love any feedback!

Jeff

'Mini' ring buffer lol:

#CHIP PIC18F16Q40
#CONFIG LVP=ON
#option explicit


#startup InitPPS, 85
#define PPSToolPart 18f16q40

Sub InitPPS
        'Module: UART1
        RC6PPS = 0x0010    'TX1 > RC6
        U1RXPPS = 0x0017   'RC7 > RX1

        'Module: UART2
        RC0PPS = 0x0013    'TX2 > RC0
        U2RXPPS = 0x0002   'RA2 > RX2

        'Module: UART pin directions
        Dir PORTC.6 Out    ' Make TX1 pin an output
        Dir PORTC.7 In    ' Make RX1 pin an input
        Dir PORTC.0 Out    ' Make TX2 pin an output
        Dir PORTA.2 In    ' Make RX2 pin an input
End Sub


'USART settings for UART1 and UART2
'#define USART_BAUD_RATE 9600
'#define USART_BLOCKING
#define USART2_BAUD_RATE 9600
#define USART2_BLOCKING

Dim InChar as Byte

wait 3 s

'Setup USART2 Interrupt
InitUSART2

'Main routine
Do Forever
  'Do stuff

Loop

'Setup Interrupt
Sub InitUSART2
        'Interrupt Handler - some have RCIE and some have U1RXIE, so handle
        #IFDEF BIT( RCIE )
            On Interrupt UsartRX2Ready Call ReadUSART2
        #ENDIF
        #IFDEF BIT( U2RXIE )
            On Interrupt UART2ReceiveInterrupt Call ReadUSART2
        #ENDIF
End Sub

Sub ReadUSART2
    'Get Command char from USART 2.   It reading the data in blocking mode knowing theres data works best for me
    InChar = HSerReceive2

    Select Case InChar
      Case 53 'byte value for '5' This uart channel will ONLY be expecting 1 character commands so no ring buffer needed
        HSerPrint "Calibration seeing how much information we can send",2
        HSerPrintCRLF,2

      Case 54 'byte value for '6' This uart channel will ONLY be expecting 1 character commands so no ring buffer needed
        HSerPrint "It looks like you want me to perform command 6",2
        HSerPrintCRLF,2

      Case 55 'byte value for '7' This uart channel will ONLY be expecting 1 character commands so no ring buffer needed
        HSerPrint "Looks like command 7 has been pressed.",2
        HSerPrintCRLF,2

      Case Else
       'send back char to UART 2, ignoring in real life
        HSerSend InChar,2
        HSerprintCRLF,2
    end select
End Sub
 

Last edit: Jeff Weinmann 2022-08-17