10 years, 5 months ago.

Serial interrupts on transmit

I want to simply send the data pair 0xA5, 0x9D out as an RS232 signal from the Serial engine and keep doing it forever, so I can see the result on an analogue 'scope.. I tried:

#include "mbed.h"
 
Serial mydevice(p9, p10);  // tx, rx
DigitalOut ExtTrig(p8);

void keep_sending () {
  if(ExtTrig == 0) mydevice.putc(0xA5);
  if(ExtTrig == 1) mydevice.putc(0x9D);
  ExtTrig= !ExtTrig;
  wait_us(400);
}
 
int main() {
  mydevice.baud(19200);
  mydevice.format(8, Serial::None, 1);
  mydevice.putc(0xA5);
  mydevice.attach(&keep_sending, Serial::TxIrq);
  while(1) { }
}

This compiles, and seems to send the data. Pin ExtTrig goes up and down. However the "wait_us" call does nothing at all. I had hoped that it would separate the values being sent, but that doesn't happen. I suspect I am not clearing the Tx interrupt, but I don't see how to do that. I can see how to disable the Rx interrupt, but not the Tx. Normally I program exclusively in assembler, and I never have any problems there ... !!

Can anyone help?

Martin

1 Answer

10 years, 5 months ago.

You shouldn't have to clear the TX interrupt, and regardless I don't think it is related to your problem.

400 microseconds isn't a long time. At default baudrate that is 4 bits, so less than half a single character. So it puts a character in the output buffer, waits till roughly half the message is sent, and then goes waiting again. When it is finished sending the TX irq is called, and it immediatly sends another message. Putting the wait in front of the putc should probably make it do what you want it to do.

Accepted Answer