怎么实现双边模拟I2C通信啊?两个单片机,一个模拟发送一个模拟接收。请高手指点。
用的是AT89C52单片机,只要能发送数据即可,时序图我知道,对I2C工作过程也比较熟悉了,也上网查了资料,但都是些单方模拟,没用,采样那些都得软件实现,一时半会不知道从...
用的是AT89C52单片机,只要能发送数据即可,时序图我知道,对I2C工作过程也比较熟悉了,也上网查了资料,但都是些单方模拟,没用,采样那些都得软件实现,一时半会不知道从哪里下手,哪位高手帮帮忙
貌似在哪ctrl+c来的 展开
貌似在哪ctrl+c来的 展开
3个回答
展开全部
using System;
using System.IO.Ports;
namespace Midapexsoft.IO.Ports
{
/// <summary>
/// Interface of serial port
/// </summary>
public interface ISerialPort : IDisposable, Midapexsoft.IO.ITerminal
{
#region Property
string PortName
{
get;
set;
}
Parity Parity
{
get;
set;
}
int DataBits
{
get;
set;
}
StopBits StopBits
{
get;
set;
}
StandardBaudRate BaudRate
{
get;
set;
}
Handshake HandShake
{
get;
set;
}
#endregion
}
/// <summary>
/// Standard baud rate setting
/// </summary>
public enum StandardBaudRate
{
R110 = 110,
R300 = 300,
R600 = 600,
R1200 = 1200,
R2400 = 2400,
R4800 = 4800,
R9600 = 9600,
R14400 = 14400,
R19200 = 19200,
R38400 = 38400,
R57600 = 57600,
R76800 = 76800,
R115200 = 115200
};
}
//SerialPortX.h
#pragma once
#include "AsyncSerialPort.h"
#include "RuntimeException.h"
#include "SerialPortX.h"
#include "RuntimeException.h"
using namespace System;
using namespace System::Collections;
class CSerialPortObserver;
namespace Midapexsoft
{
namespace IO
{
namespace Port
{
public ref class SerialPortX: public IStartable, public ISerialPort
{
public:
SerialPortX(void);
~SerialPortX(void);
virtual void Stop();
virtual void SerialPortX::Start();
virtual void Write(array<byte> ^ data);
virtual event BytesEventHandler^ OnWriteData
{
void add(BytesEventHandler^ value)
{
writeDataHandler += value;
}
void remove(BytesEventHandler^ value)
{
writeDataHandler -= value;
}
void raise(Object^ sender, BytesEventArgs^ args)
{
if(writeDataHandler!=nullptr)
{
//writeDataHandler->BeginInvoke(sender, args,nullptr,nullptr);
writeDataHandler(sender,args);
}
}
}
virtual event BytesEventHandler^ OnReadData
{
void add(BytesEventHandler^ value)
{
readDataHandler += value;
}
void remove(BytesEventHandler^ value)
{
readDataHandler -= value;
}
void raise(Object^ sender, BytesEventArgs^ args)
{
if(readDataHandler!=nullptr)
{
//readDataHandler->BeginInvoke(sender, args,nullptr,nullptr);
readDataHandler(sender,args);
}
}
}
virtual event MessageEventHandler^ OnError
{
void add(MessageEventHandler^ value)
{
errorHandler += value;
}
void remove(MessageEventHandler^ value)
{
errorHandler -= value;
}
void raise(Object^ sender, MessageEventArgs^ args)
{
if(errorHandler!=nullptr)
{
//errorHandler->BeginInvoke(sender, args,nullptr,nullptr);
errorHandler(sender,args);
}
}
}
property String^ PortName
{
virtual String^ get() { return name;}
virtual void set(String^ newName)
{
//Get port number
int portNumberIndex = newName->IndexOf("COM");
if(portNumberIndex!=0)
{
throw gcnew Exception("Bad Port Name");
}
String^ portNumberS = newName->Substring(3);
portNumber = Convert::ToByte(portNumberS);
name= newName;
}
}
property Parity PortParity
{
virtual Parity get() { return parity;}
virtual void set(Parity parity)
{
try
{
if(IsOpen)
{
Hashtable^ parityTable = gcnew Hashtable();
parityTable->Add(L"None", 2);
parityTable->Add(L"Odd",3);
parityTable->Add(L"Even",gcnew int(0) );
parityTable->Add(L"Mark",1);
parityTable->Add(L"Space",4);
int parityMothord = (int) parityTable[parity.ToString()];
port->SetParity((CSerialPort::Parity)parityMothord);
}
this->parity = parity;
}
catch (CRuntimeException* e)
{
String^ info = gcnew String((LPCTSTR)e->GetErrorMessage());
throw gcnew Exception( info );
e->Delete();
}
}
}
property byte DataBytes
{
virtual byte get() { return dataBytes;}
virtual void set(byte dataBytes)
{
if(IsOpen)
{
port->SetDataBytes(dataBytes);
}
this->dataBytes = dataBytes;
}
}
property bool IsOpen
{
virtual bool get() { return port->IsOpen() != 0;}
}
property StopBits PortStopBits
{
virtual StopBits get() { return stopBits; }
virtual void set(StopBits stopBits)
{
try
{
if(IsOpen)
{
port->SetStopBits((CSerialPort::StopBits)(int)stopBits);
}
this->stopBits = stopBits;
}
catch (CRuntimeException* e)
{
String^ info = gcnew String((LPCTSTR)e->GetErrorMessage());
throw gcnew Exception( info );
e->Delete();
}
}
}
property StandardBaudRate BaudRate
{
virtual StandardBaudRate get() { return this->baudRate; }
virtual void set(StandardBaudRate newBaudRate)
{
try
{
if(IsOpen)
{
int baud = (int)newBaudRate;
port->SetBaud(baud);
}
this->baudRate = newBaudRate;
}
catch (CRuntimeException* e)
{
String^ info = gcnew String((LPCTSTR)e->GetErrorMessage());
throw gcnew Exception( info );
e->Delete();
}
}
}
property Handshake PortHandShake
{
virtual Handshake get() { return this->handshake;}
virtual void set( Handshake newHandShake)
{
try
{
if(IsOpen)
{
Hashtable ^ handshakeTable = gcnew Hashtable();
handshakeTable->Add("None",gcnew int(0));
handshakeTable->Add("XonXoff",5);
handshakeTable->Add("CtsRts",1);
handshakeTable->Add("DsrDtr",4);
//Handshake
int handshake = (int)handshakeTable[ newHandShake.ToString() ];
port->SetFlowControl((CSerialPort::FlowControl)handshake);
}
this->handshake = newHandShake;
}
catch (CRuntimeException* e)
{
String^ info = gcnew String((LPCTSTR)e->GetErrorMessage());
throw gcnew Exception( info );
e->Delete();
}
}
}
property bool CanStart
{
virtual bool get() { return canStart;}
virtual void set(bool canStart) { this->canStart = canStart; }
}
property bool IsRun
{
virtual bool get() { return isRun;}
}
virtual event EventHandler<StartableArgs^>^ OnStartableStatueChange
{
void add(EventHandler<StartableArgs^>^ value)
{
statusChangeHandler+= value;
}
void remove(EventHandler<StartableArgs^>^ value)
{
statusChangeHandler -= value;
}
void raise(Object^ sender, StartableArgs^ args)
{
if(statusChangeHandler!=nullptr)
{
statusChangeHandler->BeginInvoke(sender,args,nullptr,nullptr);
}
}
}
internal:
void OnPortWriteData(array<Byte>^ data)
{
OnWriteData(this, gcnew BytesEventArgs(data));
}
void OnPortReceiveData(array<Byte>^ data)
{
OnReadData(this, gcnew BytesEventArgs(data));
}
void ReportError(String^ errorMessage)
{
OnError(this, gcnew MessageEventArgs(errorMessage));
}
void SetRunStatus(bool isRun)
{
this->isRun = isRun;
OnStartableStatueChange(this, gcnew StartableArgs(this));
}
private:
CAsyncSerialPort* port;
CSerialPortObserver* observer;
private:
String^ name;
Parity parity;
byte dataBytes;
StandardBaudRate baudRate;
Handshake handshake;
StopBits stopBits;
byte portNumber;
bool canStart;
bool isRun;
BytesEventHandler^ writeDataHandler;
BytesEventHandler^ readDataHandler;
MessageEventHandler^ errorHandler;
EventHandler<StartableArgs^>^ statusChangeHandler;
};
}
}
}
看看有用么
using System.IO.Ports;
namespace Midapexsoft.IO.Ports
{
/// <summary>
/// Interface of serial port
/// </summary>
public interface ISerialPort : IDisposable, Midapexsoft.IO.ITerminal
{
#region Property
string PortName
{
get;
set;
}
Parity Parity
{
get;
set;
}
int DataBits
{
get;
set;
}
StopBits StopBits
{
get;
set;
}
StandardBaudRate BaudRate
{
get;
set;
}
Handshake HandShake
{
get;
set;
}
#endregion
}
/// <summary>
/// Standard baud rate setting
/// </summary>
public enum StandardBaudRate
{
R110 = 110,
R300 = 300,
R600 = 600,
R1200 = 1200,
R2400 = 2400,
R4800 = 4800,
R9600 = 9600,
R14400 = 14400,
R19200 = 19200,
R38400 = 38400,
R57600 = 57600,
R76800 = 76800,
R115200 = 115200
};
}
//SerialPortX.h
#pragma once
#include "AsyncSerialPort.h"
#include "RuntimeException.h"
#include "SerialPortX.h"
#include "RuntimeException.h"
using namespace System;
using namespace System::Collections;
class CSerialPortObserver;
namespace Midapexsoft
{
namespace IO
{
namespace Port
{
public ref class SerialPortX: public IStartable, public ISerialPort
{
public:
SerialPortX(void);
~SerialPortX(void);
virtual void Stop();
virtual void SerialPortX::Start();
virtual void Write(array<byte> ^ data);
virtual event BytesEventHandler^ OnWriteData
{
void add(BytesEventHandler^ value)
{
writeDataHandler += value;
}
void remove(BytesEventHandler^ value)
{
writeDataHandler -= value;
}
void raise(Object^ sender, BytesEventArgs^ args)
{
if(writeDataHandler!=nullptr)
{
//writeDataHandler->BeginInvoke(sender, args,nullptr,nullptr);
writeDataHandler(sender,args);
}
}
}
virtual event BytesEventHandler^ OnReadData
{
void add(BytesEventHandler^ value)
{
readDataHandler += value;
}
void remove(BytesEventHandler^ value)
{
readDataHandler -= value;
}
void raise(Object^ sender, BytesEventArgs^ args)
{
if(readDataHandler!=nullptr)
{
//readDataHandler->BeginInvoke(sender, args,nullptr,nullptr);
readDataHandler(sender,args);
}
}
}
virtual event MessageEventHandler^ OnError
{
void add(MessageEventHandler^ value)
{
errorHandler += value;
}
void remove(MessageEventHandler^ value)
{
errorHandler -= value;
}
void raise(Object^ sender, MessageEventArgs^ args)
{
if(errorHandler!=nullptr)
{
//errorHandler->BeginInvoke(sender, args,nullptr,nullptr);
errorHandler(sender,args);
}
}
}
property String^ PortName
{
virtual String^ get() { return name;}
virtual void set(String^ newName)
{
//Get port number
int portNumberIndex = newName->IndexOf("COM");
if(portNumberIndex!=0)
{
throw gcnew Exception("Bad Port Name");
}
String^ portNumberS = newName->Substring(3);
portNumber = Convert::ToByte(portNumberS);
name= newName;
}
}
property Parity PortParity
{
virtual Parity get() { return parity;}
virtual void set(Parity parity)
{
try
{
if(IsOpen)
{
Hashtable^ parityTable = gcnew Hashtable();
parityTable->Add(L"None", 2);
parityTable->Add(L"Odd",3);
parityTable->Add(L"Even",gcnew int(0) );
parityTable->Add(L"Mark",1);
parityTable->Add(L"Space",4);
int parityMothord = (int) parityTable[parity.ToString()];
port->SetParity((CSerialPort::Parity)parityMothord);
}
this->parity = parity;
}
catch (CRuntimeException* e)
{
String^ info = gcnew String((LPCTSTR)e->GetErrorMessage());
throw gcnew Exception( info );
e->Delete();
}
}
}
property byte DataBytes
{
virtual byte get() { return dataBytes;}
virtual void set(byte dataBytes)
{
if(IsOpen)
{
port->SetDataBytes(dataBytes);
}
this->dataBytes = dataBytes;
}
}
property bool IsOpen
{
virtual bool get() { return port->IsOpen() != 0;}
}
property StopBits PortStopBits
{
virtual StopBits get() { return stopBits; }
virtual void set(StopBits stopBits)
{
try
{
if(IsOpen)
{
port->SetStopBits((CSerialPort::StopBits)(int)stopBits);
}
this->stopBits = stopBits;
}
catch (CRuntimeException* e)
{
String^ info = gcnew String((LPCTSTR)e->GetErrorMessage());
throw gcnew Exception( info );
e->Delete();
}
}
}
property StandardBaudRate BaudRate
{
virtual StandardBaudRate get() { return this->baudRate; }
virtual void set(StandardBaudRate newBaudRate)
{
try
{
if(IsOpen)
{
int baud = (int)newBaudRate;
port->SetBaud(baud);
}
this->baudRate = newBaudRate;
}
catch (CRuntimeException* e)
{
String^ info = gcnew String((LPCTSTR)e->GetErrorMessage());
throw gcnew Exception( info );
e->Delete();
}
}
}
property Handshake PortHandShake
{
virtual Handshake get() { return this->handshake;}
virtual void set( Handshake newHandShake)
{
try
{
if(IsOpen)
{
Hashtable ^ handshakeTable = gcnew Hashtable();
handshakeTable->Add("None",gcnew int(0));
handshakeTable->Add("XonXoff",5);
handshakeTable->Add("CtsRts",1);
handshakeTable->Add("DsrDtr",4);
//Handshake
int handshake = (int)handshakeTable[ newHandShake.ToString() ];
port->SetFlowControl((CSerialPort::FlowControl)handshake);
}
this->handshake = newHandShake;
}
catch (CRuntimeException* e)
{
String^ info = gcnew String((LPCTSTR)e->GetErrorMessage());
throw gcnew Exception( info );
e->Delete();
}
}
}
property bool CanStart
{
virtual bool get() { return canStart;}
virtual void set(bool canStart) { this->canStart = canStart; }
}
property bool IsRun
{
virtual bool get() { return isRun;}
}
virtual event EventHandler<StartableArgs^>^ OnStartableStatueChange
{
void add(EventHandler<StartableArgs^>^ value)
{
statusChangeHandler+= value;
}
void remove(EventHandler<StartableArgs^>^ value)
{
statusChangeHandler -= value;
}
void raise(Object^ sender, StartableArgs^ args)
{
if(statusChangeHandler!=nullptr)
{
statusChangeHandler->BeginInvoke(sender,args,nullptr,nullptr);
}
}
}
internal:
void OnPortWriteData(array<Byte>^ data)
{
OnWriteData(this, gcnew BytesEventArgs(data));
}
void OnPortReceiveData(array<Byte>^ data)
{
OnReadData(this, gcnew BytesEventArgs(data));
}
void ReportError(String^ errorMessage)
{
OnError(this, gcnew MessageEventArgs(errorMessage));
}
void SetRunStatus(bool isRun)
{
this->isRun = isRun;
OnStartableStatueChange(this, gcnew StartableArgs(this));
}
private:
CAsyncSerialPort* port;
CSerialPortObserver* observer;
private:
String^ name;
Parity parity;
byte dataBytes;
StandardBaudRate baudRate;
Handshake handshake;
StopBits stopBits;
byte portNumber;
bool canStart;
bool isRun;
BytesEventHandler^ writeDataHandler;
BytesEventHandler^ readDataHandler;
MessageEventHandler^ errorHandler;
EventHandler<StartableArgs^>^ statusChangeHandler;
};
}
}
}
看看有用么
展开全部
1、可能延时时间不够,器件读写速度慢,跟不上。
void delay()
//延迟5毫秒左右;
你用多少的晶振?12M的晶振标准51核,只能延时2个微秒。
2、write_byte(0xaa);
respons();
像这种写入数据的操作需要时间比较长,你的等待时间可能不够。查看你器件的datasheet,看它的写入时间需要多久。
3、拿示波器看一下你IIC总线的波形的上升时间和下降时间能不能满足IIC规范的要求。
4、嵌入式系统从稳定性考虑,最好对每个函数的操作成功与否进行判断,系统要对错误的情况进行处理。比如楼主的respons();函数,比较好的一种处理方式是:如果等待超时,那么返回错误码,由上层函数决定如何处理错误。重发or放弃,等等。 楼主刚刚开始学习不必要深究,可以思考下这方面的问题。
void delay()
//延迟5毫秒左右;
你用多少的晶振?12M的晶振标准51核,只能延时2个微秒。
2、write_byte(0xaa);
respons();
像这种写入数据的操作需要时间比较长,你的等待时间可能不够。查看你器件的datasheet,看它的写入时间需要多久。
3、拿示波器看一下你IIC总线的波形的上升时间和下降时间能不能满足IIC规范的要求。
4、嵌入式系统从稳定性考虑,最好对每个函数的操作成功与否进行判断,系统要对错误的情况进行处理。比如楼主的respons();函数,比较好的一种处理方式是:如果等待超时,那么返回错误码,由上层函数决定如何处理错误。重发or放弃,等等。 楼主刚刚开始学习不必要深究,可以思考下这方面的问题。
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
为什么不行呢,你试一下,主机时钟可以定时器产生,从机可以用外部中断输入来接受时钟沿
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询