Labview程序如下图所示:
条件为真的时候程序框图如下:
条件为假的时候的程序框图如下:
前面板显示界面如下:
单片机开发板图片如下:
其中8个LED灯接在P0口上面。采用共阳的接法,即给IO口低电平时,点亮LED灯。高电平时LED灯熄灭。
实现现象:
实验用的串口为com4,根据自己的实际端口号选择。开发板上电后,接收区显示开机信息。
在发送区发送1时,第一个LED灯被点亮。依次类推。发送8时,第八个LED被点亮。发送0时,LED灯全灭。同时在接收区会有从单片机返回来的数据。告诉你哪一个灯被点亮。
单片机程序如下:
#include "reg51.h"
#include "intrins.h"
typedef unsigned char BYTE;
typedef unsigned int WORD;
#define FOSC 11059200L //System frequency
#define BAUD 9600 //UART baudrate
/*Define UART parity mode*/
#define NONE_PARITY 0 //None parity
#define ODD_PARITY 1 //Odd parity
#define EVEN_PARITY 2 //Even parity
#define MARK_PARITY 3 //Mark parity
#define SPACE_PARITY 4 //Space parity
#define PARITYBIT NONE_PARITY //Testing even parity
sbit led1 = P0^0;
sbit led2 = P0^1;
sbit led3 = P0^2;
sbit led4 = P0^3;
sbit led5 = P0^4;
sbit led6 = P0^5;
sbit led7 = P0^6;
sbit led8 = P0^7;
sbit bit9 = P2^2; //P2.2 show UART data bit9
bit busy;
bit Flag;
int var; //接收字符
void SendData(BYTE dat);
void SendString(char *s);
void main()
{
#if (PARITYBIT == NONE_PARITY)
SCON = 0x50; //8-bit variable UART
#elif (PARITYBIT == ODD_PARITY) || (PARITYBIT == EVEN_PARITY) || (PARITYBIT == MARK_PARITY)
SCON = 0xda; //9-bit variable UART, parity bit initial to 1
#elif (PARITYBIT == SPACE_PARITY)
SCON = 0xd2; //9-bit variable UART, parity bit initial to 0
#endif
TMOD = 0x20; //Set Timer1 as 8-bit auto reload mode
TH1 = TL1 = -(FOSC/12/32/BAUD); //Set auto-reload vaule
TR1 = 1; //Timer1 start run
ES = 1; //Enable UART interrupt
EA = 1; //Open master interrupt switch
SendString("杨帆电路设计,微信交流:ok570217\r\n");
while(1)
{
if(Flag==0)
{
switch(var)
{
case 0x31:{led1=0;SendString("第1个LED灯点亮\r\n");} break;
case 0x32:{led2=0;SendString("第2个LED灯点亮\r\n");} break;
case 0x33:{led3=0;SendString("第3个LED灯点亮\r\n");} break;
case 0x34:{led4=0;SendString("第4个LED灯点亮\r\n");} break;
case 0x35:{led5=0;SendString("第5个LED灯点亮\r\n");} break;
case 0x36:{led6=0;SendString("第6个LED灯点亮\r\n");} break;
case 0x37:{led7=0;SendString("第7个LED灯点亮\r\n");} break;
case 0x38:{led8=0;SendString("第8个LED灯点亮\r\n");} break;
case 0x30:{P0=0xff;SendString("全部关闭LED灯\r\n");} break;
}
Flag = 1;
}
}
}
/*----------------------------
UART interrupt service routine
----------------------------*/
void Uart_Isr() interrupt 4
{
if (RI)
{
RI = 0; //Clear receive interrupt flag
var = SBUF; //P0 show UART data
bit9 = RB8; //P2.2 show parity bit
Flag = 0;
}
if (TI)
{
TI = 0; //Clear transmit interrupt flag
busy = 0; //Clear transmit busy flag
}
}
/*----------------------------
Send a byte data to UART
Input: dat (data to be sent)
Output:None
----------------------------*/
void SendData(BYTE dat)
{
while (busy); //Wait for the completion of the previous data is sent
ACC = dat; //Calculate the even parity bit P (PSW.0)
if (P) //Set the parity bit according to P
{
#if (PARITYBIT == ODD_PARITY)
TB8 = 0; //Set parity bit to 0
#elif (PARITYBIT == EVEN_PARITY)
TB8 = 1; //Set parity bit to 1
#endif
}
else
{
#if (PARITYBIT == ODD_PARITY)
TB8 = 1; //Set parity bit to 1
#elif (PARITYBIT == EVEN_PARITY)
TB8 = 0; //Set parity bit to 0
#endif
}
busy = 1;
SBUF = ACC; //Send data to UART buffer
}
/*----------------------------
Send a string to UART
Input: s (address of string)
Output:None
----------------------------*/
void SendString(char *s)
{
while (*s) //Check the end of the string
{
SendData(*s++); //Send current char and increment string ptr
}
}
我最近在学习Labview,欢迎大家一起交流学习。