Microcontroller serial port device - Page 1
Forum section: electronics / beginners

author: swmcl
author_rank: Contributor
timestamp: April 14, 2017, 09:48:56 pm
content: Hi,Can someone give me a more birds'eye view of how things work with microcontrollers please?I want to build a small project that is able to receive and transmit data on a serial port. It is placed on an RS485 multidrop network.There is data flowing to and fro continuously more or less. There will be some pauses.The data packets will be up to 32 bytes I believe.I assume the program for the device would be written such that there would be an interrupt on the serial register? The register sees one byte at a time. Those bytes then need to be stored in a memory in sequence. When the full packet has been stored it can then be deciphered and acted upon. So the checking of the various pin-states would happen while there wasn't any serial port activity? Or can happen while the register is being loaded...I am asking for a sort of flow chart understanding of how a microcontroller works in this situation or how best to implement a solution given the limitations of a microcontroller ...Cheers,Steve

author: rstofer
author_rank: Super Contributor
timestamp: April 14, 2017, 10:03:26 pm
content: You pretty much have it dialed in. There will be an RS485->TTL (or CMOS) transceiver chip that connects to the RS485 bus and interfaces with Rx and Tx from the uC UART. There will also need to be a control signal to tell the transceiver when to transmit.Incoming characters will be caught in an interrupt handler and, usually, put into a circular queue. Someplace in the main code, there is a function that pulls chars from the queue and processes them. This might just be building up a sentence for later processing or it could be a state machine that assembles the sentence and executes something.Regardless of how you go about it, the idea of using an interrupt to grab asynchronous events is the way to go. Polling in a loop will sometimes work if the loop is fast enough but it isn't my preferred way of doing things. The circular queue was invented for this kind of thing.This ROBIN code has nothing to do with your project but it may give you some ideas about how to manage the bus or how to be a good actor on a previously defined bus:http://www.bdmicro.com/code/robin/

author: igeorge
author_rank: Contributor
timestamp: April 14, 2017, 10:28:50 pm
content: Hello Steve,The microcontroller it is a powerful beast.But first, lets clarify.In your case you have two propblems to solve.1 is 4852 is USART (232) control and flow.1 you need a 485==>232 converter. Like the one in this link from Digikeyhttps://www.digikey.ca/products/en?keywords=MIKROE-9252 On the processor you must first set the receive interrupt on your interrupt subroutine.When a packet arrive, your interrupt bit is set.Now you have to take data from the received buffer, and check it against your set parameters to make sure the packet is for you, check the checksum to be sure it is not corrupted data, then move it to a working buffer.After you do all this, you clear the receive interrupt flag and enable the interrupt again.It is a very complicated scenario.I suggest to start in the following order if you have to boards to experiment.Here it is a link on you tube of one of my programhttps://www.youtube.com/watch?v=ZrJKRvydDJg&feature=youtu.beHere it is a link where where i explained the codehttps://forum.mikroe.com/viewtopic.php?f=97&t=53585And here it is a link where you can download the codehttps://forum.mikroe.com/viewtopic.php?f=97&t=53816The code is in MikroBasic from MikroElektronika.You can download a demo version which works like the paid version, with the limitation at 2K of code.Over 2k you have to pay.Good luck.