Comunicacion serial

Dependents:   comunicacion Bluetooth

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers SoftSerial.cpp Source File

SoftSerial.cpp

00001 #include "SoftSerial.h"
00002 
00003 SoftSerial::SoftSerial(PinName TX, PinName RX, const char* name) {
00004     tx_en = rx_en = false;
00005     if (TX != NC) {
00006         tx = new DigitalOut(TX);
00007         tx_en = true;
00008         tx->write(1);
00009         tx_bit = -1;
00010         txticker.attach(this, &SoftSerial::tx_handler);
00011     }
00012     if (RX != NC) {
00013         rx = new InterruptIn(RX);
00014         rx_en = true;
00015         out_valid = false;
00016         rxticker.attach(this, &SoftSerial::rx_handler);
00017         rx->fall(this, &SoftSerial::rx_gpio_irq_handler);
00018     }
00019     
00020     baud(9600);
00021     format();
00022 }
00023 
00024 SoftSerial::~SoftSerial() {
00025     if (tx_en)
00026         delete(tx);
00027     if (rx_en)
00028         delete(rx);
00029 }
00030 
00031 void SoftSerial::baud(int baudrate) {
00032     bit_period = 1000000 / baudrate;
00033 }
00034 
00035 void SoftSerial::format(int bits, Parity parity, int stop_bits) {
00036     _bits = bits;
00037     _parity = parity;
00038     _stop_bits = stop_bits;
00039     _total_bits = 1 + _bits + _stop_bits + (bool)_parity;
00040 }