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

Dependents:   TestVirtualisation Bf_SoftSerial_IR

Committer:
Sissors
Date:
Sat Apr 26 20:04:49 2014 +0000
Revision:
4:c010265ed202
Parent:
2:9e01a38606b4
Child:
6:517082212c00
IRQs added

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 4:c010265ed202 25 fpointer[TxIrq].call();
Sissors 0:8edaa7abe724 26 return;
Sissors 0:8edaa7abe724 27 }
Sissors 2:9e01a38606b4 28
Sissors 0:8edaa7abe724 29 //Flip output
Sissors 0:8edaa7abe724 30 int cur_out = tx->read();
Sissors 0:8edaa7abe724 31 tx->write(!cur_out);
Sissors 2:9e01a38606b4 32
Sissors 0:8edaa7abe724 33 //Calculate when to do it again
Sissors 0:8edaa7abe724 34 int count = bit_period;
Sissors 0:8edaa7abe724 35 tx_bit++;
Sissors 2:9e01a38606b4 36 while(((_char >> tx_bit) & 0x01) == !cur_out) {
Sissors 0:8edaa7abe724 37 count+=bit_period;
Sissors 0:8edaa7abe724 38 tx_bit++;
Sissors 0:8edaa7abe724 39 }
Sissors 2:9e01a38606b4 40
Sissors 1:f8b4b764ace7 41 txout.attach_us(this, &SoftSerial::tx_handler, count);
Sissors 0:8edaa7abe724 42 }
Sissors 0:8edaa7abe724 43
Sissors 2:9e01a38606b4 44 void SoftSerial::prepare_tx(int c)
Sissors 2:9e01a38606b4 45 {
Sissors 2:9e01a38606b4 46 _char = c << 1;
Sissors 2:9e01a38606b4 47
Sissors 2:9e01a38606b4 48 bool parity;
Sissors 2:9e01a38606b4 49 switch (_parity) {
Sissors 2:9e01a38606b4 50 case Forced1:
Sissors 2:9e01a38606b4 51 _char |= 1 << (_bits + 1);
Sissors 2:9e01a38606b4 52 case Even:
Sissors 2:9e01a38606b4 53 parity = false;
Sissors 2:9e01a38606b4 54 for (int i = 0; i<_bits; i++) {
Sissors 2:9e01a38606b4 55 if (((_char >> i) & 0x01) == 1)
Sissors 2:9e01a38606b4 56 parity = !parity;
Sissors 2:9e01a38606b4 57 }
Sissors 2:9e01a38606b4 58 _char |= parity << (_bits + 1);
Sissors 2:9e01a38606b4 59 case Odd:
Sissors 2:9e01a38606b4 60 parity = true;
Sissors 2:9e01a38606b4 61 for (int i = 0; i<_bits; i++) {
Sissors 2:9e01a38606b4 62 if (((_char >> i) & 0x01) == 1)
Sissors 2:9e01a38606b4 63 parity = !parity;
Sissors 2:9e01a38606b4 64 }
Sissors 2:9e01a38606b4 65 _char |= parity << (_bits + 1);
Sissors 0:8edaa7abe724 66 }
Sissors 0:8edaa7abe724 67
Sissors 2:9e01a38606b4 68 _char |= 0xFFFF << (1 + _bits + (bool)_parity);
Sissors 2:9e01a38606b4 69 _char &= ~(1<<_total_bits);
Sissors 2:9e01a38606b4 70 }