Hi guys, long time no see. Sorry for the long post, just seeking a bit of theory and practical knowledge really from anyone used to working with serial comms.
I have a few questions regarding the approach I should take to tackling a problem. Basically I want to log data to my PC from a couple of sensors, and store basic configuration data (such as refresh times just as an example) on the PIC.
I have no trouble actually setting up the pic (16f88) and having read the datasheet I've got a few examples up and running without too much hassle.
What I thought i'd do is have a (I think we call it a rolling stack?) array of bytes, say 8 bytes wide, and every time a new byte is received by the uart module, it would raise an interrupt, push it onto the bottom of the stack (position 7) and shuffle all the variables up by one position, eventually popping the top value off the top.
Then the top five bytes would be checked for a header signalling the start of a command, say '255, 255, 0, 0, 128', if they are present then the remaining three bytes at the bottom of the stack would be used to construct a command, one value being to signal the command, the other two being used to provide data for that command.
So basically it's a mini command interpreter. The reason why I've chosen this is because I want two way communication and multiple sensors, I need a way for the pic and the pc to identify between different requests. The pc is going to want to both read and write eeprom randomly and the pic is going to want to send sensor data based on whats being requested.
I suppose my first question is, is this a correct design philosophy? (i.e having a command structure) - or am I missing an obvious technique for defining the meaning of received data?
Finally, there is a slight problem - I wrote an example to test this theory, I wrote the firmware in MikroC, and I built a vb.net application to generate commands and interpret and display received commands. I simulated this set up in proteus using a virtual serial port 'pair'. In essence it works - if I send a command from one, the other receives it. The problem is, every now and then incorrect data is received, well actually, the data simply isn't received at all. I've verified from sniffing the datastream that the simulated pic is receiving all the data, but sometimes the pic doesn't respond.
Currently about 90% of data is being sent/received without a problem.
The baud rate is currently set a 933, data transfer rate isn't terribly critical, and with a 4mhz clock this gives an error of 0.0%. I was wondering if the load on the pc whilst simulating my pic and my vb.net program could be causing timing inconsistencies. Is this a logical explanation? I'm currently waiting for a protoboard that will enable me to test the system in hardware but it'l be a while before it arrives.
Secondly, another consideration I have is that perhaps checking for commands in the interrupt routine is taking too long, and thus the pic is missing data. However creating delays inbetween the sending of bytes has not had an improvement on the overall accuracy of data. So I'm a bit dubious about this theory.
What I do know is - if I transmit the same command-sequence of bytes twice, the chance of the command being executed is (almost) doubled.
So my question really is, does this sound like a baud rate/timing problem?
I don't want to post my entire code here (I can if you really want though) because my program is quite long and possibly incomprehensible simply due to it's length, I'm just seeking opinions really.
I have a few questions regarding the approach I should take to tackling a problem. Basically I want to log data to my PC from a couple of sensors, and store basic configuration data (such as refresh times just as an example) on the PIC.
I have no trouble actually setting up the pic (16f88) and having read the datasheet I've got a few examples up and running without too much hassle.
What I thought i'd do is have a (I think we call it a rolling stack?) array of bytes, say 8 bytes wide, and every time a new byte is received by the uart module, it would raise an interrupt, push it onto the bottom of the stack (position 7) and shuffle all the variables up by one position, eventually popping the top value off the top.
Then the top five bytes would be checked for a header signalling the start of a command, say '255, 255, 0, 0, 128', if they are present then the remaining three bytes at the bottom of the stack would be used to construct a command, one value being to signal the command, the other two being used to provide data for that command.
So basically it's a mini command interpreter. The reason why I've chosen this is because I want two way communication and multiple sensors, I need a way for the pic and the pc to identify between different requests. The pc is going to want to both read and write eeprom randomly and the pic is going to want to send sensor data based on whats being requested.
I suppose my first question is, is this a correct design philosophy? (i.e having a command structure) - or am I missing an obvious technique for defining the meaning of received data?
Finally, there is a slight problem - I wrote an example to test this theory, I wrote the firmware in MikroC, and I built a vb.net application to generate commands and interpret and display received commands. I simulated this set up in proteus using a virtual serial port 'pair'. In essence it works - if I send a command from one, the other receives it. The problem is, every now and then incorrect data is received, well actually, the data simply isn't received at all. I've verified from sniffing the datastream that the simulated pic is receiving all the data, but sometimes the pic doesn't respond.
Currently about 90% of data is being sent/received without a problem.
The baud rate is currently set a 933, data transfer rate isn't terribly critical, and with a 4mhz clock this gives an error of 0.0%. I was wondering if the load on the pc whilst simulating my pic and my vb.net program could be causing timing inconsistencies. Is this a logical explanation? I'm currently waiting for a protoboard that will enable me to test the system in hardware but it'l be a while before it arrives.
Secondly, another consideration I have is that perhaps checking for commands in the interrupt routine is taking too long, and thus the pic is missing data. However creating delays inbetween the sending of bytes has not had an improvement on the overall accuracy of data. So I'm a bit dubious about this theory.
What I do know is - if I transmit the same command-sequence of bytes twice, the chance of the command being executed is (almost) doubled.
So my question really is, does this sound like a baud rate/timing problem?
I don't want to post my entire code here (I can if you really want though) because my program is quite long and possibly incomprehensible simply due to it's length, I'm just seeking opinions really.
Last edited: