找回密码
 注册会员
搜索附件  
MCU资讯论坛 附件中心 单片机论坛 51单片机论坛 新建_Microsoft_Word_文档_(2).doc

新建_Microsoft_Word_文档_(2).doc

 

程序调试不成功:
我编了一个AT89C4051与C++ Builder 5的串口通信(COM1)程序,P1.2接LED灯的负极,初始时是亮的,希望通过通信使其熄灭,但通信总是不成功,请高手指点!
C++ Builder 5的上位机程序如下所示:

//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "Unit7.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
HANDLE hComm;
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
: TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
char *ComNo;
DCB dcb;
String Temp;
Temp="COM" +IntToStr(RadioGroup1->ItemIndex+1);
ComNo=Temp.c_str();
hComm=CreateFile(ComNo,
GENERIC_READ|GENERIC_WRITE,
0,
NULL,
OPEN_EXISTING,
FILE_FLAG_OVERLAPPED,
0);
if(hComm==INVALID_HANDLE_VALUE)
{
MessageBox(0,"打开端口失败","Comm Error",MB_OK);
return;
}
GetCommState(hComm,&dcb);
dcb.BaudRate=CBR_9600;
dcb.ByteSize=8;
dcb.Parity=NOPARITY;
dcb.StopBits=ONESTOPBIT;
if(!SetCommState(hComm,&dcb))
{
MessageBox(0,"通信设置错误","Set Error",MB_OK);
CloseHandle(hComm);
return;
}
Shape1->Brush->Color=clRed;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button2Click(TObject *Sender)
{
if(hComm!=INVALID_HANDLE_VALUE)
CloseHandle(hComm);
exit(EXIT_SUCCESS);
Shape1->Brush->Color=clBlue;
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button3Click(TObject *Sender)
{
char SendData[1];
unsigned long lrc,BS;
BS=1;
SendData[0]=0xff ;
WriteFile(hComm,SendData,BS,&lrc,NULL);
}
//---------------------------------------------------------------------------
单片机程序如下:

#include<reg52.h>
#include <stdio.h>
#define uchar unsigned char
void main()
{
int i;
SCON=0x50;
TMOD=0x20;
TH1=0xfd;
TL1=0xfd;
PCON&=0x7f;
EA=1;
TR1=1;
P1=0x00;
RI=0;
while(!RI);
RI=0;
for(i=0;i<20000;i++)
P1=0xff;
}



QQ|手机版|MCU资讯论坛 ( 京ICP备18035221号-2 )|网站地图

GMT+8, 2024-11-24 14:28 , Processed in 0.035838 second(s), 8 queries , Redis On.

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

返回顶部