极客工坊

 找回密码
 注册

QQ登录

只需一步,快速开始

查看: 6862|回复: 4

atmega16 不支持软串口(SoftwareSerial)吗?

[复制链接]
发表于 2015-9-9 00:36:15 | 显示全部楼层 |阅读模式
atmega16 不支持软串口(SoftwareSerial)吗?atmega8也不支持?除了他俩其他芯片好像都可以!



本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x
回复

使用道具 举报

 楼主| 发表于 2015-9-9 00:48:13 | 显示全部楼层
本帖最后由 cciew 于 2015-9-9 00:52 编辑

来自:http://forum.arduino.cc/index.php?topic=336196.0

If it needs to use PCINT pins, yeah, you're out of luck on the '16/32

The 16/32 does provide three external interrupts though (int0,int1, int2), and the software serial library could be modified to use those (though that of course constrains your choice of which pin to use for it) - there may be such a library already available.

Yep.  That means that "as written" the current software serial library won't run on mega16/mega32 (or ATmega8, either, which is closer to being "officially supported.")
The pin change interrupts give the software serial library the ability to "catch" input characters even when the sketch is not explicitly doing a "read" operation.  This is very useful, but is not ALWAYS required.  You might be able to find an older SS library that doesn't use  PCINT that would still do what you need.

SoftwareSerial uses pin change interrupts because it provides maximum pin selection flexibility -- provided no other library wants to use pin change interrupts (it grabs them all). But changing the code to use external interrupts instead is relatively easy. You could troll someone here into writing a modified version for you. I'd do it! Ha ha.

Robin2 wrote a piece of code that also does this, using external interrupts. Look here(http://forum.arduino.cc/index.ph ... &topic=251764.0).

If you can't use the external interrupt pins then you'd have to do it by polling, like westfw is talking about. That'll work just fine if you don't have anything else to do at the same time. Actually, SoftwareSerial kind of takes over your processor anyways, so it might as well be polling.
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-9-9 00:55:39 | 显示全部楼层
http://forum.arduino.cc/index.ph ... &topic=251764.0
In a recent Thread the usual complaints were made about the SoftwareSerial library not functioning alongside the Servo library. In Reply #17 @oric_dan suggested a limited version of SoftwareSerial for the Uno that would work in parallel with other libraries.

That prompted the realization that I had a few projects that together probably contained all of the concepts necessary to achieve that.

The attached code sss.ino is the result. It uses Pins 3 (INT1) and 4 and Timer2A and seems to work quite happily with the Servo library. It is only intended for use on an Uno (or standalone Atmega 328).

The file DemoSimpleSoftSerial.ino illustrates the use of sss.ino. And the file DemoPartner.ino is a program that I have run on a Mega (because it has multiple hardware Serial Ports) to receive data from and send data to the Uno.

I concluded that there is little advantage in converting the code into a Class or into a proper Library. Because of its simplicity there cannot be multiple instances of it - in other words you can only use it to add one Software Serial connection to an Uno. Because of the way the Arduino manages projects the variables in sss.ino are not accessible to the code in the main .ino file but the various functions are. I have prefixed the names of all the functions and variables with sss to reduce the possibility of clashing with names in other parts of a project.

To use the code you just need to add a copy of the file sss.ino into the Arduino project. Using the demo program attached you would put both of the file DemoSimpleSoftSerial.ino and sss.ino into a directory called DemoSimpleSoftSerial.

Interestingly the code works exactly as I expected it would when the idea first occurred to me but I could fill a whole book with the stupid mistakes that wasted time while bringing the various pieces together.

...R
回复 支持 反对

使用道具 举报

发表于 2015-9-9 09:23:27 | 显示全部楼层
软串口需要用到port信号改变的中断,atmega8和16貌似都没有,所以用不了
回复 支持 反对

使用道具 举报

 楼主| 发表于 2015-9-9 20:39:14 | 显示全部楼层
“zhb1190”解释的好,但是想用,能添加外部中断吗?
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|联系我们|极客工坊

GMT+8, 2026-6-17 01:29 , Processed in 0.037968 second(s), 19 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表