Posted: 02:26pm 21 Dec 2016 |
Copy link to clipboard |
 Print this post |
|
I make no claim to being a good programmer whatsoever but have put a well commented example below. It was run on a micromite 470 (Peter M's great backpack board) under MMBasic 5.2 by Geoff Graham. Please feel free to comment in any way 
If you copy the code and plug it into Jim's MMEdit, the colour coding will help you navigate through the code better.
' Sample COM port Interrupt Service Routine ' using MMBasic v5.2 by Geoff Graham ' Doug Pankhurst 2016 ' The example below is designed to be an interrupt driven routine ' to handle a serial stream received by the Micromite of fixed length ' at regular or semi regular intervals. In this case, 14 characters every 2 secs. ' As we know "14 characters every 2 sec", when a receive interrupt is triggered, ' we need to ensure that all 14 characters have been received before reading ' the Rx buffer, so we trigger the interrupt after 14 characters received ' For the purposes of the test, put a jumper between COM1 Tx and Rx to ' loop the characters sent back into the receive input.
' If you don't know how many characters and/or how often, it is more complex ' and I will do some code for that later.
' Lets setup and define variables option explicit dim Tx_test$ = "Hello World." ' test string to transmit dim Rx_test$ ' string to hold received data dim Rx_Flag = 0 ' 1 means data received by Rx interrupt service routine, ' 0 means nother received yet dim Rx_data_good_Flag = 0 ' Rx data length flag - if 1, Rx data error
' Now we need to set up some code to send a serial stream out the COM1 Tx port ' at 9600 baud every two seconds to test the Rx interrupt code. settick 2000,Tx_Int ' every seconds, generate an interrupt that will send ' the characters Hello World. out the COM1 port
open "COM1:9600, 30, Rx_Int, 14" as #1 ' set up COM1 as our serial I/O ' Note we are specifying 9600 baud with a 30 character buffer (the default ' is 256), the name of the Rx interrupt service routine and importantly, ' the number of characters in the Rx buffer before the interrupt will be triggered.
' ****** ' This will be our main program loop - it can be anything you want but in this ' case it just checks a flag that is set by the receive interrupt service routine ' to see whether any characters have been received Main: ' just a label print "Program to demonstrate COM1 receive interrupt service" do ' anything you like in here - this is where all your actual data processing and ' display output goes ' in here somewhere, you would check the Rx_Flag to check if receive data ' is available to use ' eg. If Rx_Flag = 1 then print time$," Data Received: ",Rx_test$ Rx_Flag = 0 ' clear ready for next test if Rx_data_good_Flag = 0 then print "Rx data good." else print "Receive data error" ' if needed, handle the data error here endif endif ' other code as desired loop
' ***** main program code end
'***** Subroutines ******* '*** the all important Rx interrupt service routine sub Rx_Int ' the receive interrupt has occured ' at this point we know two things; we have received data and there are 14 characters ' waiting in the Rx buffer, so set the Rx data flag and read the data. ' That is all we need to do in here - get in, set the flag, get the data, get out! Rx_Flag = 1 Rx_test$ = input$(30,#1) 'we will try to read 30 characters just to ensure ' the buffer is empty. If there are only 14 characters ' then that is how many will be read ' Note that the input buffer is now empty - the data is ' in Rx_test$ Rx_data_good_Flag = 0 ' clear flag ready for test If len(Rx_test$) <> 14 then Rx_data_good_Flag = 1 ' check how many characters actually read - if more than 14 ' then there is a receive data problem ' - if you wanted, you could include a checksum in the Tx ' data sent and test it here endif ' the sub routine will return with data in Rx_test$, Rx_Flag set and ' Rx_data_good_Flag clear if data length correct ( a very simple error check) end sub
'
' Set Tick timer interrupt service routine, called every second sub Tx_Int Rx_Flag = 0 ' clear the Rx flag ready for the Rx interrupt to set it later print #1,Tx_test$ ' just send the test string with CR LF at the end ' - 14 characters in all. Note that these ' characters will go out at MMBasic instruction speed ' to the Tx buffer - about 20uSec at which time the subroutine ' will return but actual transmit ' speed out the COM port will be 14mSec end sub
PS. You should also have a look at Rob's GPS Serial with Circular Line Buffer post recently.
Doug.Edited by panky 2016-12-23 ... almost all of the Maximites, the MicromMites, the MM Extremes, the ArmMites, the PicoMite and loving it! |