主题:[原创]VB通过串口控制单片机读写24C02源代码下载
///////////VB程序///////////
Const READIIC As Byte = &H1
Const WRITEIIC As Byte = &H2
Const RESETIIC As Byte = &H0
Option Explicit
Dim buff(0 To 32) As Byte '缓冲区
Dim filebuff(0 To 255) As Byte '发送文件缓冲区
Dim recebuff(0 To 255) As Byte '接收文件缓冲区
Dim mycount As Integer '计数器
Private Sub Command1_Click()
Me.Enabled = False
mycount = 0
'发送写命令和数据
buff(0) = WRITEIIC
Dim i As Integer
For i = 1 To 32
buff(i) = filebuff(i - 1)
Next i
Mcom.Output = buff
End Sub
Private Sub Command2_Click()
Me.Enabled = False
'发送读命令和数据
mycount = 0
buff(0) = READIIC
Mcom.Output = buff
End Sub
Private Sub Command3_Click()
Dim buff() As Byte
Dim i As Integer
Dim str As String
'打开对话框
CD1.InitDir = "c:\"
CD1.ShowOpen
Text3.Text = CD1.FileName
If Len(Text3.Text) = 0 Then Exit Sub
Command1.Enabled = True
'打开文件
Open Text3.Text For Binary As #1
If LOF(1) = 0 Then End
'--------------------------------
If (LOF(1) > 0) And (LOF(1) < 256) Then
ReDim buff(0 To LOF(1)) As Byte
Get 1, , buff()
For i = 0 To LOF(1) - 1
filebuff(i) = buff(i)
Next i
For i = LOF(1) To 255
filebuff(i) = 0
Next i
Else
Get 1, , filebuff()
End If
For i = 0 To 255
If filebuff(i) > 15 Then
str = str & Hex(filebuff(i)) & "H "
Else
str = str & "0" & Hex(filebuff(i)) & "H "
End If
Next i
Text1.Text = str
Close #1
End Sub
Private Sub Form_Load()
If Mcom.PortOpen = False Then Mcom.PortOpen = True
End Sub
Private Sub Form_Unload(Cancel As Integer)
If Mcom.PortOpen = True Then Mcom.PortOpen = False
End Sub
Private Sub Mcom_OnComm()
Dim str As String
Dim aa As Variant
Dim i As Integer
'接收数据
If Mcom.CommEvent = 2 Then
aa = Mcom.Input
For i = 1 To 32
recebuff(mycount * 32 + i - 1) = aa(i)
Next i
mycount = mycount + 1
If mycount = 8 Then
mycount = 0
For i = 0 To 255
If recebuff(i) > 15 Then
str = str & Hex(recebuff(i)) & "H "
Else
str = str & "0" & Hex(recebuff(i)) & "H "
End If
Next i
Text2.Text = str
Me.Enabled = True
Else
For i = 1 To 32
buff(i) = filebuff(mycount * 32 + i - 1)
Next i
Mcom.Output = buff
End If
End If
End Sub
////////////////////////////////////////////////////////////////////
51单片机端的代码[em9]
//////////////////main.c////////////////////////////
#include"reg51.h"
#include"absacc.h"
#include"stdio.h"
//波特率:4.8K
#define RESETIIC 0
#define READIIC 1
#define WRITEIIC 2
//IIC读写函数
extern unsigned char Read24c02_OneByte(unsigned char addr) ;
extern void Write24c02_OneByte(unsigned char addr,unsigned char ddata);
void init();
void send();
unsigned char buff[33]; //串口缓冲区
//数据结构
/*
buff[0]------------------命令:0 复位编程器,1 读IIC ,2 写IIC
buff[1]-buff[32]---------数据
*/
unsigned Address=0;
unsigned char flag=0;
void main()
{
unsigned char index;
init();
while(1)
{
while(flag)
{
flag=0;
if(buff[0]==RESETIIC)
{//0 复位编程器
for(index=0;index!=33;index++) buff[index]=0;
Address=0;
}
if(buff[0]==READIIC)
{//1 读IIC
for(index=1;index!=33;index++) buff[index]=Read24c02_OneByte(Address++);
send();
}
if(buff[0]==WRITEIIC)
{//2 写IIC
for(index=1;index!=33;index++) Write24c02_OneByte(Address++,buff[index]);
send();
}
}
}
}
void init()
{
unsigned char i;
TMOD=0x20; //Timer1 模式2
TH1=0xF3; //
TL1=0xF3; //
TR1=1; //启动Timer1
PCON|=0x80; //波特率加倍
SCON=0x50; //设置串口
TI=0;
for(i=0;i!=33;i++) buff[i]=0;
ES=1;
EA=1;
}
void com_isr() interrupt 4
{
unsigned char i;
EA=0;
RI=0;
//接收33个字节
buff[0]=SBUF;
for(i=1;i!=33;i++)
{
while(!RI);RI=0;
buff[i]=SBUF;
}
flag=1;
EA=1;
}
//发送33个字节
void send()
{
unsigned char i;
EA=0;
for(i=0;i!=33;i++)
{
SBUF=buff[i];
while(!TI);TI=0;
}
EA=1;
}
////////////24C02.C////////////////////////
#include "reg51.h"
#include "intrins.h"
#include"51usb.h" //51USB实验板的头文件
#define uchar unsigned char
#define uint unsigned char
#define true 1
#define false 0
#define bool bit
#define Byte unsigned char
#define Word unsigned int
#define read 0x80
#define write 0x00
//私有内部函数
#define \
SomeNOP(); \
_nop_();_nop_();_nop_();_nop_();
//私有内部函数
void IICStart(void)
{
EA = 0;
SDA = 1;SCL = 1;SomeNOP();
SDA = 0;SomeNOP();
SCL = 0; SDA = 1;
}
//私有内部函数
void IICStop(void)
{
SCL = 0;SDA = 0;SomeNOP();
SCL = 1;SomeNOP();SDA = 1;
EA = 1;
}
//私有内部函数
void SendAck(void)
{
SDA = 0;SomeNOP();
SCL = 1;SomeNOP();
SCL = 0;
}
//私有内部函数
void SendNotAck(void)
{
SDA = 1;SomeNOP();
SCL = 1;SomeNOP();
SCL = 0;
}
//私有内部函数
void IICSendByte(uchar ch)
{
uchar i = 8;
while(i--)
{
SCL = 0;_nop_();
SDA = (bit)(ch&0x80);ch<<=1;SomeNOP();
SCL = 1;SomeNOP();
}
SCL = 0;
}
//私有内部函数
uchar IICReceiveByte(void)
{
uchar i = 8;
uchar ddata = 0;
SDA = 1;
while(i--)
{
ddata<<=1;
SCL = 0;SomeNOP();
SCL = 1;SomeNOP();
if(SDA)ddata |= 0x01;
}
SCL = 0;
return ddata;
}
//私有内部函数
bool WaitAck(void)
{
SDA = 1;SomeNOP();
SCL = 1;SomeNOP();
while(SDA){IICStart(); IICSendByte(0xa0);SDA = 1;SomeNOP(); SCL = 1;SomeNOP();}
SCL = 0;
return true;
}
//***********************************
//**公有函数,外部接口 **
//***********************************
//写一个数据
void Write24c02_OneByte(unsigned char addr,unsigned char ddata)
{
IICStart();
IICSendByte(0xa0);
WaitAck();
IICSendByte(addr);
WaitAck();
IICSendByte(ddata);
WaitAck();
IICStop();
SomeNOP();
}
//读一个数据
unsigned char Read24c02_OneByte(unsigned char addr)
{
uchar temp;
IICStart();
IICSendByte(0xa0); //先写入地址,设置写
WaitAck();
IICSendByte(addr);
WaitAck();
IICStart();
IICSendByte(0xa1); //设置读
WaitAck();
temp = IICReceiveByte();
SendNotAck();
IICStop();
return temp;
}
[img]I:\WEB\IIC读写程序.JPG[/img]
详细介绍
http://www.lgmcu.com/51USB_II.htm
Const READIIC As Byte = &H1
Const WRITEIIC As Byte = &H2
Const RESETIIC As Byte = &H0
Option Explicit
Dim buff(0 To 32) As Byte '缓冲区
Dim filebuff(0 To 255) As Byte '发送文件缓冲区
Dim recebuff(0 To 255) As Byte '接收文件缓冲区
Dim mycount As Integer '计数器
Private Sub Command1_Click()
Me.Enabled = False
mycount = 0
'发送写命令和数据
buff(0) = WRITEIIC
Dim i As Integer
For i = 1 To 32
buff(i) = filebuff(i - 1)
Next i
Mcom.Output = buff
End Sub
Private Sub Command2_Click()
Me.Enabled = False
'发送读命令和数据
mycount = 0
buff(0) = READIIC
Mcom.Output = buff
End Sub
Private Sub Command3_Click()
Dim buff() As Byte
Dim i As Integer
Dim str As String
'打开对话框
CD1.InitDir = "c:\"
CD1.ShowOpen
Text3.Text = CD1.FileName
If Len(Text3.Text) = 0 Then Exit Sub
Command1.Enabled = True
'打开文件
Open Text3.Text For Binary As #1
If LOF(1) = 0 Then End
'--------------------------------
If (LOF(1) > 0) And (LOF(1) < 256) Then
ReDim buff(0 To LOF(1)) As Byte
Get 1, , buff()
For i = 0 To LOF(1) - 1
filebuff(i) = buff(i)
Next i
For i = LOF(1) To 255
filebuff(i) = 0
Next i
Else
Get 1, , filebuff()
End If
For i = 0 To 255
If filebuff(i) > 15 Then
str = str & Hex(filebuff(i)) & "H "
Else
str = str & "0" & Hex(filebuff(i)) & "H "
End If
Next i
Text1.Text = str
Close #1
End Sub
Private Sub Form_Load()
If Mcom.PortOpen = False Then Mcom.PortOpen = True
End Sub
Private Sub Form_Unload(Cancel As Integer)
If Mcom.PortOpen = True Then Mcom.PortOpen = False
End Sub
Private Sub Mcom_OnComm()
Dim str As String
Dim aa As Variant
Dim i As Integer
'接收数据
If Mcom.CommEvent = 2 Then
aa = Mcom.Input
For i = 1 To 32
recebuff(mycount * 32 + i - 1) = aa(i)
Next i
mycount = mycount + 1
If mycount = 8 Then
mycount = 0
For i = 0 To 255
If recebuff(i) > 15 Then
str = str & Hex(recebuff(i)) & "H "
Else
str = str & "0" & Hex(recebuff(i)) & "H "
End If
Next i
Text2.Text = str
Me.Enabled = True
Else
For i = 1 To 32
buff(i) = filebuff(mycount * 32 + i - 1)
Next i
Mcom.Output = buff
End If
End If
End Sub
////////////////////////////////////////////////////////////////////
51单片机端的代码[em9]
//////////////////main.c////////////////////////////
#include"reg51.h"
#include"absacc.h"
#include"stdio.h"
//波特率:4.8K
#define RESETIIC 0
#define READIIC 1
#define WRITEIIC 2
//IIC读写函数
extern unsigned char Read24c02_OneByte(unsigned char addr) ;
extern void Write24c02_OneByte(unsigned char addr,unsigned char ddata);
void init();
void send();
unsigned char buff[33]; //串口缓冲区
//数据结构
/*
buff[0]------------------命令:0 复位编程器,1 读IIC ,2 写IIC
buff[1]-buff[32]---------数据
*/
unsigned Address=0;
unsigned char flag=0;
void main()
{
unsigned char index;
init();
while(1)
{
while(flag)
{
flag=0;
if(buff[0]==RESETIIC)
{//0 复位编程器
for(index=0;index!=33;index++) buff[index]=0;
Address=0;
}
if(buff[0]==READIIC)
{//1 读IIC
for(index=1;index!=33;index++) buff[index]=Read24c02_OneByte(Address++);
send();
}
if(buff[0]==WRITEIIC)
{//2 写IIC
for(index=1;index!=33;index++) Write24c02_OneByte(Address++,buff[index]);
send();
}
}
}
}
void init()
{
unsigned char i;
TMOD=0x20; //Timer1 模式2
TH1=0xF3; //
TL1=0xF3; //
TR1=1; //启动Timer1
PCON|=0x80; //波特率加倍
SCON=0x50; //设置串口
TI=0;
for(i=0;i!=33;i++) buff[i]=0;
ES=1;
EA=1;
}
void com_isr() interrupt 4
{
unsigned char i;
EA=0;
RI=0;
//接收33个字节
buff[0]=SBUF;
for(i=1;i!=33;i++)
{
while(!RI);RI=0;
buff[i]=SBUF;
}
flag=1;
EA=1;
}
//发送33个字节
void send()
{
unsigned char i;
EA=0;
for(i=0;i!=33;i++)
{
SBUF=buff[i];
while(!TI);TI=0;
}
EA=1;
}
////////////24C02.C////////////////////////
#include "reg51.h"
#include "intrins.h"
#include"51usb.h" //51USB实验板的头文件
#define uchar unsigned char
#define uint unsigned char
#define true 1
#define false 0
#define bool bit
#define Byte unsigned char
#define Word unsigned int
#define read 0x80
#define write 0x00
//私有内部函数
#define \
SomeNOP(); \
_nop_();_nop_();_nop_();_nop_();
//私有内部函数
void IICStart(void)
{
EA = 0;
SDA = 1;SCL = 1;SomeNOP();
SDA = 0;SomeNOP();
SCL = 0; SDA = 1;
}
//私有内部函数
void IICStop(void)
{
SCL = 0;SDA = 0;SomeNOP();
SCL = 1;SomeNOP();SDA = 1;
EA = 1;
}
//私有内部函数
void SendAck(void)
{
SDA = 0;SomeNOP();
SCL = 1;SomeNOP();
SCL = 0;
}
//私有内部函数
void SendNotAck(void)
{
SDA = 1;SomeNOP();
SCL = 1;SomeNOP();
SCL = 0;
}
//私有内部函数
void IICSendByte(uchar ch)
{
uchar i = 8;
while(i--)
{
SCL = 0;_nop_();
SDA = (bit)(ch&0x80);ch<<=1;SomeNOP();
SCL = 1;SomeNOP();
}
SCL = 0;
}
//私有内部函数
uchar IICReceiveByte(void)
{
uchar i = 8;
uchar ddata = 0;
SDA = 1;
while(i--)
{
ddata<<=1;
SCL = 0;SomeNOP();
SCL = 1;SomeNOP();
if(SDA)ddata |= 0x01;
}
SCL = 0;
return ddata;
}
//私有内部函数
bool WaitAck(void)
{
SDA = 1;SomeNOP();
SCL = 1;SomeNOP();
while(SDA){IICStart(); IICSendByte(0xa0);SDA = 1;SomeNOP(); SCL = 1;SomeNOP();}
SCL = 0;
return true;
}
//***********************************
//**公有函数,外部接口 **
//***********************************
//写一个数据
void Write24c02_OneByte(unsigned char addr,unsigned char ddata)
{
IICStart();
IICSendByte(0xa0);
WaitAck();
IICSendByte(addr);
WaitAck();
IICSendByte(ddata);
WaitAck();
IICStop();
SomeNOP();
}
//读一个数据
unsigned char Read24c02_OneByte(unsigned char addr)
{
uchar temp;
IICStart();
IICSendByte(0xa0); //先写入地址,设置写
WaitAck();
IICSendByte(addr);
WaitAck();
IICStart();
IICSendByte(0xa1); //设置读
WaitAck();
temp = IICReceiveByte();
SendNotAck();
IICStop();
return temp;
}
[img]I:\WEB\IIC读写程序.JPG[/img]
详细介绍
http://www.lgmcu.com/51USB_II.htm