整了一天程序 , 写了个上位机程序和单片机程序 。总结下 。。。
上位机部分:
相关参考资料:http://wenku.baidu.com/view/5ea8670403d8ce2f00662322.html
1.先添加各控件 , 添加后的对话框如下图:
2.再添加对应的成员变量 。
3.最后添加函数(事件处理函数) 。
3.1初始化 , 设置串口各参数 。
BOOLCLEDControlDlg::OnInitDialog(){CDialog::OnInitDialog();// 设置此对话框的图标 。当应用程序主窗口不是对话框时 , 框架将自动// 执行此操作SetIcon(m_hIcon, TRUE); // 设置大图标SetIcon(m_hIcon, FALSE); // 设置小图标// TODO: 在此添加额外的初始化代码m_ctrlComm.put__CommPort(5); //选择com3m_ctrlComm.put_InputMode(1); //以二进制方式输入m_ctrlComm.put_InBufferSize(1024);m_ctrlComm.put_OutBufferSize(512);//设置串口,波特率115200 , 无校验位 , 数据位8 , 停止位1m_ctrlComm.put_Settings(_T("115200,n,8,1"));//参数1表示当接受缓冲区有多于等于1个字符//引发OnCommm_ctrlComm.put_RThreshold(1);if(!m_ctrlComm.get_PortOpen()){m_ctrlComm.put_PortOpen(TRUE);//打开串口}m_ctrlComm.put_InputLen(0); //设置当前缓冲区为0m_ctrlComm.get_Input(); //先预读缓冲区returnTRUE; // 除非将焦点设置到控件 , 否则返回 TRUE} 3.2处理串口发来的数据 。
voidCLedControlDlg::OnComm(){// TODO: 在此处添加消息处理程序代码VARIANT variant_inp;COleSafeArray safearray_inp;LONGlen,k;BYTErxdata[2048]; //设置BYTE数组 An 8-bit integerthat is not signed.CString strtemp;if(m_ctrlComm.get_CommEvent()==2) //事件值为2表示接收缓冲区内有字符{ ////////以下你可以根据自己的通信协议加入处理代码variant_inp=m_ctrlComm.get_Input(); //读缓冲区safearray_inp=variant_inp; //VARIANT型变量转换为ColeSafeArray型变量len=safearray_inp.GetOneDimSize(); //得到有效数据长度for(k=0;k
voidCLedControlDlg::OnButtonManualsend(){// TODO: 在此添加控件通知处理程序代码UpdateData(true);m_ctrlComm.put_Output(COleVariant(m_strEditTXData));UpdateData(false);}
【上位机控制单片机LED】单片机部分:
#include #include #include #include//标准函数#include//I/O口函数#define _Nop() _nop_()#define uchar unsigned char#define uint unsigned int#define LEDCTL0Low P4&=~0x01;#define LEDCTL0High P4|=0x01;#define LEDCTL1Low P4&=~0x02;#define LEDCTL1High P4|=0x02;#define LEDCTL2Low P4&=~0x04;#define LEDCTL2High P4|=0x04;#define LEDCTL3Low P4&=~0x08;#define LEDCTL3High P4|=0x08;//--------------------------------------------------------------------// 全局常量//--------------------------------------------------------------------#define SYSCLK 22118400 // SYSCLK frequency in Hz#define BAUDRATE 115200 // Baud rate of UART in bps#define MAX_LEN 50 //receive buf MAX lengthuchar readCounts;uchar trdata[MAX_LEN] = {'0', '0', '0', '0'}; //四个LED数码管初始显示都为0uchar sendlen;uchar message[]="Receive Buf is full";bit readFlag;bit frameFlag;chardata SFRPAGE_SAVE;uchar code LEDSEG1[]= {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0xc6,0X89};//0~9,C,H,正uchar code LEDSEG2[]= {0xC0,0xCF,0xA4,0x86,0x8B,0x92,0x90,0xC7,0x80,0x82,0xF0,0X89};//0~9,C,H,倒//--------------------------------------------------------------------//函数声明//--------------------------------------------------------------------voidSYSCLK_Init (void);voidPORT_Init(void);voidUART0_Init(void) ;voidSend_Char(uchar ch) ;voidsend_string_com(unsigned char*str,unsigned intstrlen);voiddisplay(unsigned char*str);voidDelay(uint tt);//--------------------------------------------------------------------// 主函数//--------------------------------------------------------------------voidmain (void){WDTCN = 0xde;WDTCN = 0xad; //关闭WDTSYSCLK_Init ();PORT_Init();UART0_Init();EA=1;ES0=1;SFRPAGE = LEGACY_PAGE;while(1){if(frameFlag){frameFlag=0;send_string_com(trdata,sendlen);}display(trdata);}}//-----------------------------------------------------------------------------// 系统时钟配置//-----------------------------------------------------------------------------voidSYSCLK_Init (void){inti = 0;//char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR pageSFRPAGE = CONFIG_PAGE; // set SFR pageOSCXCN = 0x67; // start external oscillator with// 22.1184MHz crystalfor(i=0; i < 3000; i++) ; // XTLVLD blanking interval (>1ms)while(!(OSCXCN & 0x80)) ; // Wait for crystal osc. to settleCLKSEL = 0x01; //SYSCLK derived from the External Oscillator circuitOSCICN &= ~0x80; //Internal Oscillator Disabled}//-----------------------------------------------------------------------------// I/O配置//-----------------------------------------------------------------------------voidPORT_Init(){SFRPAGE = CONFIG_PAGE;// set SFR page before writing
推荐阅读