Uart help needed!
BrianD
Posts: 2
I'm having a problem with my Javelin.· I've written a program that counts pulses coming in on pin8 and totaling the
count into a variable.· I'm simply watching the pin and looking for a transition from high to low then incrementing the
counter.· The counting part work fine and at any pulse rate.
·
In the same loop I'm checking the COM/UART port for a request to send the current count.· But I've discovered
that whenever I send any data through the COM port using the txUart.sendstring() my program loop is interrupted
and I miss counts coming in on pin8.
·
The major reason for using the Javelin was because of the uart virtual peripheral, as my understanding is that a vp
would not interrupt the program but every 8.68 microseconds and then only for a short time allowing the program
to run virtually uninterrupted.
I'm not sure if the virtual peripheral is not working right or the Javelin stamp is bad or what.
·
I've double check everything I can and have verified that pulses are missed using an OPTAscope·when using the
txUart.sendstring() to send anything.
·
If anyone can give me an idea what to do to fix this problem,· it would be a great help.
·
I can post the source code if that would be of help.
·
·
Thanks for the help.·· Brian
count into a variable.· I'm simply watching the pin and looking for a transition from high to low then incrementing the
counter.· The counting part work fine and at any pulse rate.
·
In the same loop I'm checking the COM/UART port for a request to send the current count.· But I've discovered
that whenever I send any data through the COM port using the txUart.sendstring() my program loop is interrupted
and I miss counts coming in on pin8.
·
The major reason for using the Javelin was because of the uart virtual peripheral, as my understanding is that a vp
would not interrupt the program but every 8.68 microseconds and then only for a short time allowing the program
to run virtually uninterrupted.
I'm not sure if the virtual peripheral is not working right or the Javelin stamp is bad or what.
·
I've double check everything I can and have verified that pulses are missed using an OPTAscope·when using the
txUart.sendstring() to send anything.
·
If anyone can give me an idea what to do to fix this problem,· it would be a great help.
·
I can post the source code if that would be of help.
·
·
Thanks for the help.·· Brian
Comments
Russ
The uart VP operates only on bytes stored in the uart buffer. To put bytes in that buffer (by using sendString for example) takes time and so you miss pulses.
You must use your own sendString method like this:
private int mySendString(Uart myUart, String S) {
· int i=S.length; int counts = 0;
· for (int k=0; k<i; k++) {
··· myUart.sendByte(S.charAt(k)); //send 1 byte
··· counts += myCount(); //call your pulse check method
· }
· return counts;
}
Upon return you add the returned value to the count you already
have.
regards peter
·
Sending a ‘c’ to the Javelin will make it respond back with the current count and an ‘x’ clears the counter.·
Pin6 has an LED attached for mirroring the input from pin8.· This is where I am watching the pulses going in to the stamp (pin8) and out (pin6).· Missing pulses on pin6 shows me that the counter can not incrementing correctly.
The problem came to light when the ‘c’ key pressed and allowed to auto repeat.· This nearly eliminated all counting.·
·
Thank you for all the help.
·
Brian
·
PS.· The input is coming from a Keyence FS-V11 fiber optic sensor with a NPN output, monitoring a rotating pulley on a motor.
********** Now the Soure code.·**********
import stamp.core.*;
public class Pulse_count {
··················································· // COM Port (9-pin serial)
· final static int SERIAL_TX_PIN = CPU.pin0;······· ·//···· 2
· final static int SERIAL_RTS_PIN = CPU.pin1;······ //···· 7
· final static int SERIAL_CTS_PIN = CPU.pin2;······ //···· 8
· final static int SERIAL_RX_PIN = CPU.pin3;······ · //···· 3
· static Uart txUart = new Uart( Uart.dirTransmit, SERIAL_TX_PIN, Uart.dontInvert,
························· SERIAL_RTS_PIN, Uart.dontInvert, Uart.speed38400,
························· Uart.stop1 );
· static Uart rxUart = new Uart( Uart.dirReceive, SERIAL_RX_PIN, Uart.dontInvert,
························· SERIAL_CTS_PIN, Uart.dontInvert, Uart.speed38400,
························· Uart.stop1 );
· static StringBuffer buffer = new StringBuffer(32);
· public static void main(){
··· int in_char;········· // Setup Vars and clear them
··· int cc;
····· cc=0;
··· int counter;
····· counter=0;
··· boolean temp;
· txUart.sendString("Counter started!\n\r");
··· do{
····· temp=CPU.readPin(CPU.pin8);····· //read pin8 for pulse.
····· CPU.writePin(CPU.pins[noparse][[/noparse]6],temp);· //feedback led, mirror pin8 out to pin6.
····· if (!temp){······························· // Check if a count came in.
······· cc=0;··································· // Just seeing when it goes
······· }else{·································· // from high to low.
············· if (cc==0){····················· // Increment counter but only
············· counter++;····················· // once for each pulse.
············· cc=1;
············· }
··········· }
····· if (rxUart.byteAvailable()){···············// see if there is data from
········ in_char = (rxUart.receiveByte());··· // computer.
········· if (in_char == 99){····················· // Check for a 'c' from computer
············· buffer.clear();······················· // and send count if there is.
············· buffer.append("Current count is:");
············· buffer.append(counter);
············· buffer.append("\n\r");
············· txUart.sendString(buffer.toString());
·········· }
········· if (in_char == 120){·················· // Check for a 'x' -- Clear counter
············ txUart.sendString("clear counter\n\r");
············ counter=0;
············ }
······· }
····· }while(CPU.readPin(CPU.pin9));········· // check abort switch and end
·················································· ······ // if pressed.
··· txUart.sendString("Counter Stopped!\n\r");
··· buffer.clear();·························· //send final count to computer
··· buffer.append("Final count is:");
··· buffer.append(counter);
··· buffer.append("\n\r");
··· txUart.sendString(buffer.toString());
· }
}
I changed your program a little. Note that I only check for
PC data just after a high->low transition on pin8. That
really should make a difference.
Let me know the result.
regards peter
//********** Now the Soure code. **********
import stamp.core.*;
public class Pulse_count {
··················································· // COM Port (9-pin serial)
· final static int SERIAL_TX_PIN = CPU.pin0;········ //···· 2
· final static int SERIAL_RTS_PIN = CPU.pin1;······ //···· 7
· final static int SERIAL_CTS_PIN = CPU.pin2;······ //···· 8
· final static int SERIAL_RX_PIN = CPU.pin3;········ //···· 3
· static Uart txUart = new Uart( Uart.dirTransmit, SERIAL_TX_PIN, Uart.dontInvert,
························· SERIAL_RTS_PIN, Uart.dontInvert, Uart.speed38400,
························· Uart.stop1 );
· static Uart rxUart = new Uart( Uart.dirReceive, SERIAL_RX_PIN, Uart.dontInvert,
························· SERIAL_CTS_PIN, Uart.dontInvert, Uart.speed38400,
························· Uart.stop1 );
· static StringBuffer buffer = new StringBuffer(32);
· public static void main(){
··· int counter=0;
··· txUart.sendString("Counter started!\n\r");
··· byte oldstate = CPU.readPort(CPU.PORTB);
··· byte newstate,change;
··· do{
····· //faster detect high->low pin 8
····· newstate = CPU.readPort(CPU.PORTB);
····· change = (byte)((oldstate ^ newstate) & 0x01 & oldstate); //extract pin 8
····· if (change != 0) { //only check for command from PC directly after pulse detect
························ //so sending text only while pin 8 just gets low
······· counter++;
······· if (rxUart.byteAvailable()){ // see if there is data from PC
········· switch (rxUart.receiveByte()) { //faster than ifs
··········· case 99:······················ // Check for a 'c' from computer
··················· buffer.clear();······················· // and send count if there is.
··················· buffer.append("Current count is:");
··················· buffer.append(counter);
··················· buffer.append("\n\r");
··················· txUart.sendString(buffer.toString());
··················· break;
··········· case 120:················· // Check for a 'x' -- Clear counter
··················· txUart.sendString("clear counter\n\r");
··················· counter = 0;
········· }
······· }
····· }
··· } while(CPU.readPin(CPU.pin9));········· // check abort switch and end
························································· // if pressed.
··· txUart.sendString("Counter Stopped!\n\r");
··· buffer.clear();·························· //send final count to computer
··· buffer.append("Final count is:");
··· buffer.append(counter);
··· buffer.append("\n\r");
··· txUart.sendString(buffer.toString());
· }
}
Forgot an important line:
Insert as last line in the do-while loop:
· oldstate = newstate;
regards peter
·