/*
NewSoftSerial.cpp - Multi-instance software serial library
Copyright (c) 2006 David A. Mellis. All rights reserved.
Interrupt-driven receive and other improvements by ladyada
Tuning, circular buffer, derivation from class Print,
multi-instance support, porting to 8MHz processors by
Mikal Hart
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
The latest version of this library can always be found at
http://arduiniana.org.
*/
// When set, _DEBUG co-opts pins 11 and 13 for debugging with an
// oscilloscope or logic analyzer. Beware: it also slightly modifies
// the bit times, so don't rely on it too much at high baud rates
#define _DEBUG 0
//
// Includes
//
#include <stdlib.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>
#include "WConstants.h"
#include "NewSoftSerial.h"
//
// Lookup table
//
typedef struct _DELAY_TABLE
{
long baud;
unsigned short rx_delay_centering;
unsigned short rx_delay_intrabit;
unsigned short rx_delay_stopbit;
unsigned short tx_delay;
} DELAY_TABLE;
#if F_CPU == 16000000
static const DELAY_TABLE PROGMEM table[] =
{
// baud rxcntr rxintra rxstop tx
{ 115200, 0, 0, 0, 8, },
{ 57600, /*7*/2, /*25,*/24, /*31,28*/25, 28, },
{ 38400, /*17*/8, 45, /*51*/38, 48, },
{ 31250, 23, 56, 62, 62, },
{ 28800, 27, 64, /*70*/66, 66, },
{ 19200, 46, 102, 108, 108, },
{ 14400, 68, 146, 152, 152, },
{ 9600, 105, 220, 226, 226, },
{ 4800, 224, 458, 464, 464, },
{ 2400, 462, 934, 940, 940, },
{ 1200, 926, 1862, 1868, 1868, },
{ 300, 3710, 7430, 7436, 7436, },
};
#elif F_CPU == 8000000
static const DELAY_TABLE table[] PROGMEM =
{
// baud rxcntr rxintra rxstop tx
{ 57600, 0, /*5*/5, /*8*/3, /*6*/8, },
{ 38400, /*5*/1, /*15*/16, /*18*/16, /*16*/18, },
{ 31250, /*8*//*2*/3, /*21*/23, /*24, 23*/23, 24, },
{ 28800, /*10*//*3*/4, /*25*/26, /*28*/24, /*26*/28, },
{ 19200, /*19*//*3,5*/5, /*44,45*/45, 43, 47, },
{ 14400, /*30*/8, 66, /*69*/66, 69, },
{ 9600, 49, 103, 106, 106, },
{ 4800, 108, 222, 225, 225, },
{ 2400, 227, 460, 463, 463, },
{ 1200, 459, 924, 927, 927, },
{ 300, 1851, 3708, 3711, 3711, },
};
#else
#error This version of NewSoftSerial supports only 16 and 8MHz processors
#endif
//
// Statics
//
NewSoftSerial *NewSoftSerial::active_object = 0;
char NewSoftSerial::_receive_buffer[_NewSS_MAX_RX_BUFF];
volatile uint8_t NewSoftSerial::_receive_buffer_tail = 0;
volatile uint8_t NewSoftSerial::_receive_buffer_head = 0;
//
// Private methods
//
/* static */
void NewSoftSerial::tunedDelay(uint16_t delay) {
uint8_t tmp=0;
asm volatile("sbiw %0, 0x01 \n\t"
"ldi %1, 0xFF \n\t"
"cpi %A0, 0xFF \n\t"
"cpc %B0, %1 \n\t"
"brne .-10 \n\t"
: "+r" (delay), "+a" (tmp)
: "0" (delay)
);
}
// This function sets the current object as the "active"
// one and returns true if it replaces another
bool NewSoftSerial::activate(void)
{
if (active_object != this)
{
_buffer_overflow = false;
uint8_t oldSREG = SREG;
cli();
_receive_buffer_head = _receive_buffer_tail = 0;
active_object = this;
SREG = oldSREG;
return true;
}
return false;
}
//
// The receive routine called by the interrupt handler
//
void NewSoftSerial::recv()
{
#if GCC_VERSION < 40302
// Work-around for avr-gcc 4.3.0 OSX version bug
// Preserve the registers that the compiler misses
// (courtesy of Arduino forum user *etracer*)
asm volatile(
"push r18 \n\t"
"push r19 \n\t"
"push r20 \n\t"
"push r21 \n\t"
"push r22 \n\t"
"push r23 \n\t"
"push r26 \n\t"
"push r27 \n\t"
::);
#endif
char i, d = 0;
// If RX line is high, then we don't see any start bit
// so interrupt is probably not for us
if (!digitalRead(_receivePin))
{
// Wait approximately 1/2 of a bit width to "center" the sample
tunedDelay(_rx_delay_centering);
// Read each of the 8 bits
for (i=0; i<8; i++)
{
#if _DEBUG // for scope: bring pin 13 high
PORTB |= _BV(5);
#endif
tunedDelay(_rx_delay_intrabit); // digitalread takes some time
#if _DEBUG // for scope: bring pin 13 low
PORTB &= ~_BV(5);
#endif
if (digitalRead(_receivePin))
d |= (1 << i);
else // else clause added to ensure function timing is ~balanced
d &= ~(1 << i);
}
// skip the stop bit
tunedDelay(_rx_delay_stopbit);
// if buffer full, set the overflow flag and return
if ((_receive_buffer_tail + 1) % _NewSS_MAX_RX_BUFF != _receive_buffer_head)
{
// save new data in buffer: tail points to where byte goes
_receive_buffer[_receive_buffer_tail] = d; // save new byte
_receive_buffer_tail = (_receive_buffer_tail + 1) % _NewSS_MAX_RX_BUFF;
}
else
{
#if _DEBUG // for scope: bring pin 11 high: overflow indicator
PORTB |= _BV(3);
#endif
_buffer_overflow = true;
}
}
#if GCC_VERSION < 40302
// Work-around for avr-gcc 4.3.0 OSX version bug
// Restore the registers that the compiler misses
asm volatile(
"pop r27 \n\t"
"pop r26 \n\t"
"pop r23 \n\t"
"pop r22 \n\t"
"pop r21 \n\t"
"pop r20 \n\t"
"pop r19 \n\t"
"pop r18 \n\t"
::);
#endif
}
//
// Interrupt handling
//
/* static */
inline void NewSoftSerial::handle_interrupts(int lopin, int hipin)
{
if (active_object &&
active_object->_receivePin >= lopin &&
active_object->_receivePin <= hipin)
{
active_object->recv();
}
}
ISR(PCINT0_vect)
{
NewSoftSerial::handle_interrupts(8, 13);
}
ISR(PCINT1_vect)
{
NewSoftSerial::handle_interrupts(14, 21);
}
ISR(PCINT2_vect)
{
NewSoftSerial::handle_interrupts(0, 7);
}
//
// Constructor
//
NewSoftSerial::NewSoftSerial(uint8_t receivePin, uint8_t transmitPin) :
_rx_delay_centering(0),
_rx_delay_intrabit(0),
_rx_delay_stopbit(0),
_tx_delay(0),
_buffer_overflow(false)
{
setTX(transmitPin);
setRX(receivePin);
}
//
// Public methods
//
void NewSoftSerial::setTX(uint8_t tx)
{
_transmitPin = tx;
pinMode(_transmitPin, OUTPUT);
digitalWrite(_transmitPin, HIGH);
}
void NewSoftSerial::setRX(uint8_t rx)
{
_receivePin = rx;
pinMode(_receivePin, INPUT);
digitalWrite(_receivePin, HIGH); // pullup!
}
void NewSoftSerial::begin(long speed)
{
_rx_delay_centering = _rx_delay_intrabit = _rx_delay_stopbit = _tx_delay = 0;
for (unsigned i=0; i<sizeof(table)/sizeof(table[0]); ++i)
{
long baud = pgm_read_dword(&table[i].baud);
if (baud == speed)
{
_rx_delay_centering = pgm_read_word(&table[i].rx_delay_centering);
_rx_delay_intrabit = pgm_read_word(&table[i].rx_delay_intrabit);
_rx_delay_stopbit = pgm_read_word(&table[i].rx_delay_stopbit);
没有合适的资源?快使用搜索试试~ 我知道了~
URM37.rar_URM37_超声波
共74个文件
o:26个
svn-base:18个
all-wcprops:6个
1.该资源内容由用户上传,如若侵权请联系客服进行举报
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
2.虚拟产品一经售出概不退款(资源遇到问题,请及时私信上传者)
版权申诉
0 下载量 143 浏览量
2022-09-20
16:39:09
上传
评论
收藏 219KB RAR 举报
温馨提示
超声波库,朋友给的用来参考用,我觉得应该很有帮助吧
资源推荐
资源详情
资源评论
收起资源包目录
URM37.rar (74个子文件)
URMSerial.cpp 3KB
URMSerial.o 8KB
NewSoftSerial.o 28KB
NewSoftSerial.cpp 11KB
examples
BySoftwareSerial
applet
wiring.c.o 9KB
BySoftwareSerial.hex 17KB
WMath.cpp.o 7KB
HardwareSerial.cpp.o 14KB
BySoftwareSerial.cpp 863B
wiring_digital.c.o 8KB
core.a 94KB
Print.cpp.o 22KB
wiring_analog.c.o 6KB
WInterrupts.c.o 5KB
BySoftwareSerial.eep 13B
wiring_serial.c.o 7KB
BySoftwareSerial.elf 60KB
wiring_pulse.c.o 6KB
wiring_shift.c.o 4KB
BySoftwareSerial.cpp.o 11KB
.svn
all-wcprops 804B
prop-base
BySoftwareSerial.elf.svn-base 53B
props
tmp
prop-base
props
text-base
text-base
BySoftwareSerial.cpp.svn-base 863B
BySoftwareSerial.eep.svn-base 13B
BySoftwareSerial.elf.svn-base 60KB
BySoftwareSerial.hex.svn-base 17KB
BySoftwareSerial.pde.svn-base 464B
entries 1010B
BySoftwareSerial.pde 464B
pins_arduino.c.o 3KB
.svn
all-wcprops 226B
prop-base
props
tmp
prop-base
props
text-base
text-base
BySoftwareSerial.pde.svn-base 464B
entries 388B
BySoftwareSerial.pde 464B
ByStandardSerial
applet
ByStandardSerial.elf 28KB
wiring.c.o 9KB
ByStandardSerial.hex 7KB
WMath.cpp.o 7KB
HardwareSerial.cpp.o 14KB
ByStandardSerial.cpp.o 7KB
wiring_digital.c.o 8KB
core.a 94KB
Print.cpp.o 22KB
wiring_analog.c.o 6KB
WInterrupts.c.o 5KB
wiring_serial.c.o 7KB
ByStandardSerial.cpp 2KB
wiring_pulse.c.o 6KB
wiring_shift.c.o 4KB
ByStandardSerial.eep 13B
.svn
all-wcprops 804B
prop-base
ByStandardSerial.elf.svn-base 53B
props
tmp
prop-base
props
text-base
text-base
ByStandardSerial.hex.svn-base 7KB
ByStandardSerial.eep.svn-base 13B
ByStandardSerial.cpp.svn-base 2KB
ByStandardSerial.elf.svn-base 28KB
ByStandardSerial.pde.svn-base 2KB
entries 1011B
ByStandardSerial.pde 2KB
pins_arduino.c.o 3KB
.svn
all-wcprops 226B
prop-base
props
tmp
prop-base
props
text-base
text-base
ByStandardSerial.pde.svn-base 2KB
entries 389B
ByStandardSerial.pde 2KB
.svn
all-wcprops 75B
prop-base
props
tmp
prop-base
props
text-base
text-base
entries 250B
NewSoftSerial.h 3KB
.svn
all-wcprops 450B
prop-base
props
tmp
prop-base
props
text-base
text-base
NewSoftSerial.h.svn-base 3KB
URMSerial.h.svn-base 2KB
URMSerial.cpp.svn-base 3KB
NewSoftSerial.cpp.svn-base 11KB
entries 794B
URMSerial.h 2KB
共 74 条
- 1
资源评论
小波思基
- 粉丝: 72
- 资源: 1万+
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 部署yolov8的tensorrt模型支持检测分割姿态估计的C++源码+部署步骤.zip
- 以简单、易用、高性能为目标、开源的时序数据库,支持Linux及Windows, Time Series Database.zip
- python-leetcode面试题解之第198题打家劫舍-题解.zip
- python-leetcode面试题解之第191题位1的个数-题解.zip
- python-leetcode面试题解之第186题反转字符串中的单词II-题解.zip
- 一个基于python的web后端高性能开发框架,下载可用
- python-leetcode面试题解之第179题最大数-题解.zip
- python-leetcode面试题解之第170题两数之和III数据结构设计-题解.zip
- python-leetcode面试题解之第168题Excel表列名称-题解.zip
- python-leetcode面试题解之第167题两数之和II输入有序数组-题解.zip
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功