|
Forum Index : Microcontroller and PC projects : SerialRx. and SerialTx.
| Author | Message | ||||
| lew247 Guru Joined: 23/12/2015 Location: United KingdomPosts: 1702 |
I would like to change a couple of pins to be serial ports using the Embedded C code I've been reading the PDF's about how to do this in the CFunctions section of the download but I'm a bit confused If I wanted to change pin 23 and 39 to serial rx and tx could I do the following or not? I "IDEALLY" want to do the following but I don't think it will work as it uses an interrupt and I "THINK" you can't do that if you don't use the real com ports? I want to use the click board on the E100 to interface with the outdoor board but it only has one com port and I need 2 one for the ESP8266 and the other to talk to the outdoor board [code] Open "COM2:9600,1024,isr" As #1 'Open port for ESP8266 If head<>tail Then ' collect sentence from buffer B$ = CB$(tail) tail = (tail Mod 10)+1 EndIf if B$<>"" THEN SERIAL1 ' if data GOSUB Serial1 ESP8266 B$ = "" END IF Sub isr ' serial port handler Local I, S$ length 80 S$ = Input$(72,#1) ' grab data into temporary buffer Z$ = Right$(Z$,255-Len(S$))+S$ ' add to end of linear buffer I = Instr(Z$,Chr$(42)) ' check for eol marker (CR+LF) If I<>0 Then CB$(head) = Left$(Z$,I-1) ' transfer into circular buffer head = (head Mod 10)+1 If I<254 Then Z$ = Mid$(Z$,I+2) Else Z$="" EndIf End Sub SUB SERIAL1 Local N AS INTEGER N = GetFieldArray(B$) PRINT B$ IF FieldArray$(0) = "Today" THEN T1 'If the 1st array is "Today" then GOSUB T1 'IF FieldArray$(0) = "Day2" THEN T2 'If the 1st array is "Day2" then GOSUB T2 'If the 1st array is "Day2" then GOSUB T2 'IF FieldArray$(0) = "Day3" THEN T3 'If the 1st array is "Day3" then GOSUB T3 'IF FieldArray$(0) = "Day4" THEN T4 'If the 1st array is "Day4" then GOSUB T4 END IF END SUB[/code] |
||||
| Geoffg Guru Joined: 06/06/2011 Location: AustraliaPosts: 3308 |
I don't really understand what you are trying to do but the bottom line is that you cannot change the pins allocates to serial I/O. For COM1 they are fixed in hardware and for COM2 they are fixed in the firmware. However, if you use the serial I/O CFunctions you can use whatever pins that you want to. But then you must call the CFunction, not normal serial I/O (COM1, etc). Geoff Graham - http://geoffg.net |
||||
| lew247 Guru Joined: 23/12/2015 Location: United KingdomPosts: 1702 |
SerialRx This is a CFunction module which can add any number of serial input ports to the Micromite. SerialTx This is an embedded C module which can add any number of serial output ports to the Micromite. I want to make 2 of the pins on the E100 Click Board into Com 1 or 2 but it has to be able to be used as an interrupt on receive |
||||
| JohnS Guru Joined: 18/11/2011 Location: United KingdomPosts: 4126 |
As Geoff says, you can't. Instead you could do essentially what you want but must only use the CFunctions for that extra serial I/O. John |
||||
| CaptainBoing Guru Joined: 07/09/2016 Location: United KingdomPosts: 2171 |
You can have as many SerialTx pins (i.e. ordinary digital output) as you have available on the chip. You can have as many SerialRx pins (i.e. ordinary digital input) as you have available on the chip. What you cannot do is to call them COM1,2,3,4,..... Remember serial data is just up and down pulses of data with specific timings. traditionally it was reasonably tricky to do this yourself so chip manufacturers started putting hardware modules (called USARTs) on the chip and these integrtaed with everything else and took care of all the timings etc. SerialTx/Rx are simply two routines that synthesize these pulses in the right patterns to any pin you like, however as a generalization, the USARTs in the micromite (COM1, COM2)are still dedicated to specific pins of the PIC32 and you can't point them somewhere else. So to make a makeshift Com port at 9600 on pin 23, use SerialTx like so SerialTx(23,9600,"Hello world"+chr$(13)+chr$(10)) pin 23 at 9600 bits/s message string. chr$(13)+chr$(10) is the newline sequence and most equipment will expect it SerialRx is the same deal with a few controls (data length and timeout) If you wanted you could do this to make you feel more comfortable: Const COM8 = 23 SerialTx(COM8,9600,"Hello world"+chr$(13)+chr$(10)) These two CSubs are very good - I have used them often and they work nicely. I use them when I am only interested in one-way comms - I can't stomach using a whole COM port if I am never using the Rx side of it or similar. |
||||
| lew247 Guru Joined: 23/12/2015 Location: United KingdomPosts: 1702 |
I dont get it IF I wanted to use for example on the Explore 100 Pin 27 as Com5 TX and 82 as Com 5 RX could I do this? and use pin 82 as an interrupt? and then go to a subroutine on interrupt? Would this work? From the instructions it says You receive data simply by using the SerialRx function. For example: r = SerialRx( 2, 19200, s$, 1000 ) This to me says it cannot be done IF it cant be done, How can I tell the micromite to monitor the serial rx and do something if it gets an input? EDIT this was written before the last post got posted* I'm still not sure if I can use it as an interrupt or not |
||||
| CaptainBoing Guru Joined: 07/09/2016 Location: United KingdomPosts: 2171 |
You cannot "divert" any of the built in com pins, nor make up your own and expect OPEN to understand them. your point about the interrupt is an idea though... serial lines normally sit HI when idle. When comms is about to start, it goes LO for 1 bit width (the start bit) then the data starts rattling in then at last the pin goes (or stays) HI for (usually) 1 bit width (the stop bit). I am guessing you could configure the new serial pin (lets say 23) as an input with an interrupt on LO. When you get to your interrupt routine - this would be your serial receive section where you would have your SerialRX and a decent timeout... I wonder if SerialRX would be OK with not getting a full start bit (some time would be lost as you respond to the initial LO and reconfigure until you are finished receiving)... I haven't tested it but maybe something like this: in your setup SetPin 23,DIN,INTL,SerialISR then your serial routine Sub SerialISR SetPin 23,DIN r=SerialRx(23,9600,s$,1000) ' snag your data 'set up for interrupt next time SetPin 23,DIN,INTL,SerialISR End Sub you might just get away with it if you lower the baud rate to within the tolerance of the timings (so the lost section of the start bit could be excused) EDIT: Actually I don't think this will work - I am pretty certain SerialRX will need to see the HI-to-LO transition to get all it's timings right. |
||||
Grogster![]() Admin Group Joined: 31/12/2012 Location: New ZealandPosts: 9750 |
The MM COM ports are fixed, as Geoff says. COM1 and COM2(and COM3 and COM4 on the MM+) chip are dedicated serial ports with special features such as interrupts and buffers. The CSub serial ports are a software bit-banged serial stream, but cannot do anything other then send(the SerialTX CSub) or receive(the SerialRX CSub) a serial stream AT THE INSTANT THEY ARE CALLED. They have no buffering or ability to set off an interrupt. The native COM ports are fixed. You cannot change the pin numbers they operate on or otherwise assign them somewhere else. The bit-banged CSub serial ports can use any pins you like, but they are not referred to in code as a 'COM port' per-se. See this example code I have here, which uses the transmit CSub: ...and the CSub that is used with the above is the standard one, that I have just renamed to suit my purposes: In my case, the serial is output in this line: TXD(TXO,BAUD,D$), where TXD is the name of the CSub, TXO is the transmit-output pin for the serial data to be output, BAUD is the baud-rate, and D$ is the data to output. The native COM ports will return IMMEDIATELY when you send data, leaving the buffer to take care of sending the data for you so your program can get back to doing it's thing without waiting for the serial port. The CSub will NOT return until all the data has been sent, so using the CSub can and will generate small delays while the data is clocked out of the nominated pin. Hope that helps clarify it it a little more. ![]() EDIT: [Quote=lew247]IF it cant be done, How can I tell the micromite to monitor the serial rx and do something if it gets an input?[/Quote] You can't. Not with the CSub's/Cfunctions you can't. What you CAN do, is setup and interrupt that CALLS the SerialRX Cfunction, and use the timeout feature of the SerialRx Cfunction. Something like this: SetPin 82,INTL,RXD ...then have your interrupt call the Cfunction like this: [Code] SUB RXD DUMMY=SerialRX(82,9600,D$,250) END SUB [/Code] ...then you can check if there is anything in D$ as part of your main loop or whatever - IF D$ THEN.... kind of idea. That SHOULD work, but I have a feeling the first byte might be corrupted, as the Cfunction would miss the first bit of the first byte, reacting to the interrupt. If you CAN spare another I/O pin as an interrupt, then you could use THAT instead of pin 82 for your SetPin command. Then the external interrupt line can trigger the SerialRX Cfunction via the SUB. That should catch just about anything, PROVIDED the external source of the data can supply an interrupt line for you to tell the MM that it needs to call the SerialRX Cfunction if you catch my drift. If you absolutely MUST have 100% monitored serial input, and you have used your native COM ports, then you really need another chip. The PICAXE 8-pin chip is a marvellous COM port buffer, as it's PICAXE-BASIC code will WAIT for the prescribed data to arrive before sending anything on. Used with a qualifier, the PICAXE 08 makes a beautiful data-filter/receiver, is small and cheap. Something else to think about. Smoke makes things work. When the smoke gets out, it stops! |
||||
| lew247 Guru Joined: 23/12/2015 Location: United KingdomPosts: 1702 |
Thanks, that explained it perfectly |
||||
| lew247 Guru Joined: 23/12/2015 Location: United KingdomPosts: 1702 |
Just looking at the Picaxe 8 How could you use that in conjunction with the MM? I don't get Used with a qualifier, the PICAXE 08 makes a beautiful data-filter/receiver |
||||
Grogster![]() Admin Group Joined: 31/12/2012 Location: New ZealandPosts: 9750 |
Oh, right, probably should have elaborated a bit on that - sorry. With the PICAXE chips, the SERIN command can have a qualifier, which is just a pre-determined string of characters which MUST come before the main message. This can be anything you like. An example using PICAXE BASIC on the 8-pin chip(08M2) would be: serin 1,T2400,("ABCD"),b1,b2,b3,b4,b5 In that example, the PICAXE will wait forever until it gets "ABCD" FIRST, before it saves the next five bytes. See PICAXE BASIC COMMANDS manual, page 213. An 08M2 data-filter I still use to this day is: start: serin 1,T2400,("ABCD") 'Sniff for qualifier high 4 'Turn on LED serin 1,T2400,b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13'Input message bytes high 2 serout 2,T2400,("ABCD",b0,b1,b2,b3,b4,b5,b6,b7,b8,b9,b10,b11,b12,b13,CR)'Send message bytes to PC pause 150 'Delay for LED low 4 'Turn off LED goto start 'Do it again In this example, the 08M2 chip waits forever for the qualifier BEFORE it then inputs the data bytes, and then forwards that along to the main system. It was done this way, as the PICAXE was sitting on the output of a 'Dumb' RF receiver module, and when there is no data to output, the module just outputs garbage data representing the white-noise in the background. The PICAXE chip sits there and waits for the 'Real' data packet, then sends that along to the system, so the main system COM port does not fill up with white-noise. ![]() This is an old code from 2010, so is crude but effective. ![]() The M2 parts also have a hardware serial port that is buffered. See the HSERIN command, page 94 of the same commands manual. PICAXE chips call their hardware serial port buffer the 'Scratchpad'. HSERIN and HSEROUT commands do not allow for a qualifier though, which is why I used the plain old SERIN command above, but I felt I should mention that the M2 PICAXE parts also have a hardware serial port if speed is your thing. ![]() EDIT: Just had a wee read of the PICAXE hardware serial commands, and on the M2 parts, the buffer is only two bytes deep, so essentially nothing. The X2 parts(starting with the 20-pin chip) have the 1k or so scratchpad memory(buffer).Smoke makes things work. When the smoke gets out, it stops! |
||||
| The Back Shed's forum code is written, and hosted, in Australia. | © JAQ Software 2025 |