Software UART program using Infra-Red LED and IR-Detector

Dependents:   TestVirtualisation Bf_SoftSerial_IR

Committer:
Sissors
Date:
Sat Apr 26 16:21:42 2014 +0000
Revision:
2:9e01a38606b4
Parent:
1:f8b4b764ace7
Child:
4:c010265ed202
TX optimised to handle 19200 on LPC1768

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Sissors 0:8edaa7abe724 1 #include "SoftSerial.h"
Sissors 0:8edaa7abe724 2
Sissors 2:9e01a38606b4 3 int SoftSerial::_putc(int c)
Sissors 2:9e01a38606b4 4 {
Sissors 0:8edaa7abe724 5 while(!writeable());
Sissors 2:9e01a38606b4 6 prepare_tx(c);
Sissors 0:8edaa7abe724 7 tx_bit = 0;
Sissors 0:8edaa7abe724 8 tx_handler();
Sissors 0:8edaa7abe724 9 return 0;
Sissors 2:9e01a38606b4 10 }
Sissors 0:8edaa7abe724 11
Sissors 2:9e01a38606b4 12 int SoftSerial::writeable(void)
Sissors 2:9e01a38606b4 13 {
Sissors 0:8edaa7abe724 14 if (!tx_en)
Sissors 0:8edaa7abe724 15 return false;
Sissors 0:8edaa7abe724 16 if (tx_bit == -1)
Sissors 0:8edaa7abe724 17 return true;
Sissors 0:8edaa7abe724 18 return false;
Sissors 0:8edaa7abe724 19 }
Sissors 0:8edaa7abe724 20
Sissors 2:9e01a38606b4 21 void SoftSerial::tx_handler(void)
Sissors 2:9e01a38606b4 22 {
Sissors 2:9e01a38606b4 23 if (tx_bit == _total_bits) {
Sissors 0:8edaa7abe724 24 tx_bit = -1;
Sissors 0:8edaa7abe724 25 return;
Sissors 0:8edaa7abe724 26 }
Sissors 2:9e01a38606b4 27
Sissors 0:8edaa7abe724 28 //Flip output
Sissors 0:8edaa7abe724 29 int cur_out = tx->read();
Sissors 0:8edaa7abe724 30 tx->write(!cur_out);
Sissors 2:9e01a38606b4 31
Sissors 0:8edaa7abe724 32 //Calculate when to do it again
Sissors 0:8edaa7abe724 33 int count = bit_period;
Sissors 0:8edaa7abe724 34 tx_bit++;
Sissors 2:9e01a38606b4 35 while(((_char >> tx_bit) & 0x01) == !cur_out) {
Sissors 0:8edaa7abe724 36 count+=bit_period;
Sissors 0:8edaa7abe724 37 tx_bit++;
Sissors 0:8edaa7abe724 38 }
Sissors 2:9e01a38606b4 39
Sissors 1:f8b4b764ace7 40 txout.attach_us(this, &SoftSerial::tx_handler, count);
Sissors 0:8edaa7abe724 41 }
Sissors 0:8edaa7abe724 42
Sissors 2:9e01a38606b4 43 void SoftSerial::prepare_tx(int c)
Sissors 2:9e01a38606b4 44 {
Sissors 2:9e01a38606b4 45 _char = c << 1;
Sissors 2:9e01a38606b4 46
Sissors 2:9e01a38606b4 47 bool parity;
Sissors 2:9e01a38606b4 48 switch (_parity) {
Sissors 2:9e01a38606b4 49 case Forced1:
Sissors 2:9e01a38606b4 50 _char |= 1 << (_bits + 1);
Sissors 2:9e01a38606b4 51 case Even:
Sissors 2:9e01a38606b4 52 parity = false;
Sissors 2:9e01a38606b4 53 for (int i = 0; i<_bits; i++) {
Sissors 2:9e01a38606b4 54 if (((_char >> i) & 0x01) == 1)
Sissors 2:9e01a38606b4 55 parity = !parity;
Sissors 2:9e01a38606b4 56 }
Sissors 2:9e01a38606b4 57 _char |= parity << (_bits + 1);
Sissors 2:9e01a38606b4 58 case Odd:
Sissors 2:9e01a38606b4 59 parity = true;
Sissors 2:9e01a38606b4 60 for (int i = 0; i<_bits; i++) {
Sissors 2:9e01a38606b4 61 if (((_char >> i) & 0x01) == 1)
Sissors 2:9e01a38606b4 62 parity = !parity;
Sissors 2:9e01a38606b4 63 }
Sissors 2:9e01a38606b4 64 _char |= parity << (_bits + 1);
Sissors 0:8edaa7abe724 65 }
Sissors 0:8edaa7abe724 66
Sissors 2:9e01a38606b4 67 _char |= 0xFFFF << (1 + _bits + (bool)_parity);
Sissors 2:9e01a38606b4 68 _char &= ~(1<<_total_bits);
Sissors 2:9e01a38606b4 69 }