单片机端采用C51实现

上传人:公**** 文档编号:498288549 上传时间:2024-03-03 格式:DOCX 页数:23 大小:64.07KB
返回 下载 相关 举报
单片机端采用C51实现_第1页
第1页 / 共23页
单片机端采用C51实现_第2页
第2页 / 共23页
单片机端采用C51实现_第3页
第3页 / 共23页
单片机端采用C51实现_第4页
第4页 / 共23页
单片机端采用C51实现_第5页
第5页 / 共23页
点击查看更多>>
资源描述

《单片机端采用C51实现》由会员分享,可在线阅读,更多相关《单片机端采用C51实现(23页珍藏版)》请在金锄头文库上搜索。

1、单个单片机与PC串口通信:1) 测试通信状态先在文本框中输入字符串“Hello”,单击“测试”按钮,将字符串Hello”发送到单片 机,若PC与单片机通信正常,在PC程序的文本框中显示字符串“OK!”,否则显示字符串 “ERROR!”。2) 循环计数单击“开始”按钮,文本框中数字从0开始累加,0、1、2、3,并将此数发送到单片 机的显示器上显示;当累加到10时,回到0重新开始累加,依次循环;任何时候,单击“停 止”按钮, PC 程序中和单片机显示器都停止累加,再单击“开始”按钮,接着停下的数继 续累加。3) 控制指示灯在单片机继电器接线端子的2个通道上分别接上2个指示灯,在PC程序画面上选择指

2、 示灯号,如 1 号灯,单击画面“打开”按钮,单片机上1 号灯亮,同时蜂鸣器响;单击画面 “关闭”按钮, 1 号灯灭,蜂鸣器停止响;同样控制2号灯的亮灭(蜂鸣器同时动作)。#include #include#define uchar unsigned char#define uint unsigned intsbit a1=P0A0;sbit a2=P0A1;sbit a3=P0A2;sbit a4=P0A3;sbit a5=P0A4;sbit a6=P0A5;sbit a7=P0A6;sbit a8=P0A7;void delay(uint x)/延时uchar i;while(x-)for

3、(i=0;i120;i+);void uart(void) interrupt 4 /把接收到的数据写入 ucReceiveData()TI=0;RI=0;if(SBUF=h)接收到H字符发送OK SBUF=o; while(TI=0)TI=0;else if(SBUF=1) a1=0;else if(SBUF=2) a1=1;else if(SBUF=3) a2=0;else if(SBUF=4) a2=1;else if(SBUF=5) a3=0;else if(SBUF=6) a3=1;else if(SBUF=7) a4=0;else if(SBUF=8) a4=1;else if(S

4、BUF=9) a5=0;else if(SBUF=a) a5=1;else if(SBUF=b) a6=0;else if(SBUF=c) a6=1;else if(SBUF=d) a7=0;else if(SBUF=e) a7=1;else if(SBUF=f) a8=0;else if(SBUF=g) a8=1;void main(void)uchar b=0xfe;TMOD=0x20;IE=0x12;PCON=0x80;SCON=0x50;TL1=0xFa;/0xF4;TH1=0xFa;/0xF4;0xfa 19200 0xfdTR1=1;ES=1;EA=1;/ P0=0;while(1

5、)/定时器 1-方式 2中断控制设置,串口、T2开中断 /电源控制/方式 1/12MHZ 晶振,波特率为4800 0xf3 48009600/11.0592MHZ 晶振,波特率为 4800 0xf4/启动定时P1=0;using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Drawing.Drawing2D;na

6、mespace lsd1public partial class Form1 :Form int a = 0,b=2,c=0,d=0;int m;int sur = 0;int su = 0, sf = 0;public Form1()InitializeComponent(); private void Form1_Load(object sender, EventArgs e) this.skinEngine1.SkinFile = WaveColor1.ssk; GraphicsPath gp = new GraphicsPath(); gp.AddEllipse(pictureBox1

7、.ClientRectangle); Region region = new Region(gp);pictureBox1.Region = region; pictureBox2.Region = region; pictureBox3.Region = region; pictureBox4.Region = region; pictureBox5.Region = region; pictureBox6.Region = region; pictureBox7.Region = region; pictureBox8.Region = region; gp.Dispose(); regi

8、on.Dispose(); label5.ForeColor = Color.Red; timer1.Enabled = false;timer2.Enabled = false;private void button8_Click(object sender, EventArgs e) if (textBox1.Text != )if (serialPort1.IsOpen)pictureBox1.BackColor = Color.DarkSeaGreen; serialPort1.Write(2);pictureBox2.BackColor = Color.RosyBrown; seri

9、alPort1.Write(4);pictureBox3.BackColor = Color.DarkSeaGreen; serialPort1.Write(6);pictureBox4.BackColor = Color.RosyBrown; serialPort1.Write(8);pictureBox5.BackColor = Color.DarkSeaGreen; serialPort1.Write(a);pictureBox6.BackColor = Color.RosyBrown; serialPort1.Write(c);pictureBox7.BackColor = Color

10、.DarkSeaGreen; serialPort1.Write(e);pictureBox8.BackColor = Color.RosyBrown; serialPort1.Write(g);sur = 20;serialPort1.Close();button8.Text = 打开串口 ; textBox1.Enabled = true;elsesur = 0;serialPort1.PortName = comboBox1.Text; serialPort1.BaudRate =Convert.ToInt32(comboBox2.Text);tryserialPort1.Open();

11、button8.Text = 关闭串口 ; textBox1.Enabled = false; comboBox2.Enabled = false; comboBox1.Enabled = false; timer1.Interval = Convert.ToInt32(textBox1.Text);timer1.Enabled = true;timer2.Enabled = true;catchMessageBox.Show(“串口打开失败了! nn可能是串口已补占用。);elseMessageBox.Show(“请输入延时时间);private void button1_Click(obj

12、ect sender, EventArgs e)sur = 1; sf = 0;su = 0; b = 2;= Color.DarkSeaGreen;= Color.RosyBrown;= Color.DarkSeaGreen;pictureBox1.BackColorserialPort1.Write(2);pictureBox2.BackColorserialPort1.Write(4);pictureBox3.BackColorserialPort1.Write(6);pictureBox4.BackColor= Color.RosyBrown;= Color.DarkSeaGreen;

13、= Color.RosyBrown;= Color.DarkSeaGreen;= Color.RosyBrown;private void button2_Click(object sender,EventArgs e)= Color.DarkSeaGreen;= Color.RosyBrown;= Color.DarkSeaGreen;= Color.RosyBrown;= Color.DarkSeaGreen;= Color.RosyBrown;= Color.DarkSeaGreen;= Color.RosyBrown;serialPort1.Write(8);pictureBox5.B

14、ackColorserialPort1.Write(a);pictureBox6.BackColorserialPort1.Write(c);pictureBox7.BackColorserialPort1.Write(e);pictureBox8.BackColorserialPort1.Write(g);b = 2; sur = 2;sf = 0;su = 0;pictureBox1.BackColorserialPort1.Write(2);pictureBox2.BackColorserialPort1.Write(4);pictureBox3.BackColorserialPort1.Write(6);pictureBox4.BackColorserialPort1.Write(8);pictureBox5.BackColorserialPort1.Write(a);pictureBox6.BackColorserialPort1.Write(c);pictureBox7.BackColorserialPort1.Write(e);pictureBox8.BackColor serialPort1.Write

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 学术论文 > 其它学术论文

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号