实现以下功能:串口调试助手向单片机发送控制码,单片机向PC回送控制码,0:熄灭;1:LED从上往下循环;2:LED从下往上循环;3:LED闪烁;4:蜂鸣器响
电路设计:USB供电及自动下载原理图
串口转USB以及蜂鸣器原理图
软件代码: 方案一:#include<8052.h>
__sfr __at (0xE8) P4;
__sbit __at (0xEC) P4_4;
#define LSA P1_5
#define LSB P1_6
#define LSC P1_7
#define BUZZ P4_4
/*LED初始化*/
void LedInit()
{
LSA = 0;
LSB = 0;
LSC = 0;
P0 = 0xff;
}
/*延迟函数*/
void Delayms(unsigned int xms)
{
unsigned int i,j;
for (i = xms;i > 0;i--)
{
for (j = 110;j > 0;j--);
}
}
/*LED从上向下循环3次*/
void Up2Down(int x){
int i, j;
unsigned char sel=0xfe;
for(i=0;i>1;
}
sel=0x7f;
}
}
/*LED闪烁*/
void Led_Shan(int x){
unsigned int i;
for (i = 0;i < x;i++){
P0 = 0xff;
Delayms(500);
P0 = 0x00;
Delayms(500);
}
P0 = 0xff;
}
void Buzz()
{
BUZZ=0;
Delayms(1000);
BUZZ=1;
Delayms(1000);
}
void ConfigUART(unsigned int baud)//UART波特率初始化
{
SCON = 0x50; //串口工作方式1
TMOD &= 0x0F;
TMOD |= 0x20;
TH1 = 256 - (11059200/12/32)/baud;
TL1 = TH1;
ET1 = 0;
ES = 1;
TR1 =1;
}
void InterruptUART(void) __interrupt 4 //串口中断
{
unsigned char temp;
if (RI==1)
{ //串口接收
RI = 0;
temp = SBUF;
if ((temp == '0')||(temp == '1')||(temp == '2')||(temp == '3')||(temp == '4'))
{
TI = 1;
}
}
if (TI==1){ //串口发送
TI = 0;
SBUF = temp;
while(TI == 0);
if (temp == '0')
{
P0 = 0xff;
}
if (temp == '1')
{
Up2Down(3);
}
if (temp == '2')
{
Down2Up(3);
}
if (temp == '3')
{
Led_Shan(3);
}
if (temp == '4')
{
Buzz();
}
TI = 0;
}
}
void main(){
EA = 1;
//开启总中断
ConfigUART(9600); //波特率设为9600
LedInit(); //LED初始化
while(1);
}
方案二:
#include
#include
sbit LSA = P2^2;
sbit LSB = P2^3;
sbit LSC = P2^4;
sbit beep= P1^5;
void U2D(unsigned int xms,unsigned char m);
void D2U(unsigned int xms,unsigned char m);
void delayms(unsigned int xms);
void Shinning(unsigned int xms,unsigned char m);
void ConfigUART(unsigned int baud);
void delayms(unsigned int xms)
{
unsigned int i,j;
for(i=0;i
程序改进:修改程序,能把串口接收到控制码显示到数码管上。
数码管接口电路原理图
方案一:#include<8052.h>
__sfr __at (0xE8) P4;
__sbit __at (0xEC) P4_4;
#define LSA P1_5
#define LSB P1_6
#define LSC P1_7
#define BUZZ P4_4
unsigned char smg[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0x88,0x83,0xc6,0xa1,0x86,0x8e};
/*LED初始化*/
void LedInit()
{
LSA = 0;
LSB = 0;
LSC = 0;
P0 = 0xff;
}
/*延迟函数*/
void Delayms(unsigned int xms)
{
unsigned int i,j;
for (i = xms;i > 0;i--)
{
for (j = 110;j > 0;j--);
}
}
/*LED从上向下循环3次*/
void Up2Down(int x){
int i, j;
unsigned char sel=0xfe;
for(i=0;i>1;
}
sel=0x7f;
}
}
/*LED闪烁*/
void LedShan(int x){
unsigned int i;
for (i = 0;i < x;i++){
P0 = 0xff;
Delayms(500);
P0 = 0x00;
Delayms(500);
}
P0 = 0xff;
}
void Buzz()
{
BUZZ=0;
Delayms(1000);
BUZZ=1;
Delayms(1000);
}
void ConfigUART(unsigned int baud)//UART波特率初始化
{
SCON = 0x50; //串口工作方式1
TMOD &= 0x0F;
TMOD |= 0x20;
TH1 = 256 - (11059200/12/32)/baud;
TL1 = TH1;
ET1 = 0;
ES = 1;
TR1 =1;
}
void SMG(int x)
{
LSA=1;
LSB=0;
LSC=0;
P0=smg[x];
}
void InterruptUART(void) __interrupt 4 //串口中断
{
unsigned char temp;
if (RI==1)
{ //串口接收
RI = 0;
temp = SBUF;
if ((temp == '0')||(temp == '1')||(temp == '2')||(temp == '3')||(temp == '4'))
{
TI = 1;
}
}
if (TI==1){ //串口发送
TI = 0;
SBUF = temp;
while(TI == 0);
LSA = 0;
LSB = 0;
LSC = 0;
if (temp == '0'){
P0 = 0xff;
SMG(0);
}
if (temp == '1') {
Up2Down(3);
SMG(1);
}
if (temp == '2') {
Down2Up(3);
SMG(2);
}
if (temp == '3') {
LedShan(3);
SMG(3);
}
if (temp == '4') {
Buzz();
SMG(4);
}
TI = 0;
}
}
void main(){
EA = 1; //开启总中断
ConfigUART(9600); //波特率设为9600
LedInit(); //LED初始化
while(1);
}
方案二:
#include
#include
#include
#include
void ConfigUART(unsigned int baud);
void delay(unsigned int xms);
unsigned char code smg[]={0xc0,0xf9,0xa4,0xb0,0x99};
unsigned char temp;
void main()
{
LSA=LSB=LSC=0;
EA=1;
ConfigUART(9600);
while(1)
{
if(temp==0)
{
LedScan(0,smg[0]);
}
else if(temp==1)
{
LedScan(0,smg[1]);
}
else if(temp==2)
{
LedScan(0,smg[2]);
}
else if(temp==3)
{
LedScan(0,smg[3]);
}
else if(temp==4)
{
LedScan(0,smg[4]);
}
}
}
void ConfigUART(unsigned int baud)
{
SCON=0x50;
TMOD&=0x0F;
TMOD|=0x20;
TH1=256-(11059200/12/32)/baud;
TL1=TH1;
ET1=0;
ES=1;
TR1=1;
void interruptUART(void) interrupt 4
{
if(RI)
{
RI=0;
temp=SBUF;
SBUF=temp;
}
if(TI)
{
TI=0;
}
}
void delay(unsigned int xms)
{
unsigned char m,n;
for(m=0;m
STC89C52RC.h
#ifndef _STC89C52RC_H_
#define _STC89C52RC_H_
sfr AUXR=0X8E;
sfr AUXR1=0XA2;
sfr IPH=0xb7;
sfr XICON=0XC0;
sfr P4=0xE8;
sbit P44=P4^4;
sbit P43=P4^3;
sbit P42=P4^2;
sbit P41=P4^1;
sbit P40=P4^0;
#endif
led.c
void LedScan(unsigned char pos,unsigned char dispCode)
{
switch(pos)
{
case 0:
LSA=1;
LSB=0;
LSC=0;break;
case 1:
LSA=0;
LSB=1;
LSC=0;break;
}
P0=dispCode;
delay(1);
P0=0xff;
LSA=1;
LSB=1;
LSC=1;
}
led.h
#ifndef __LED_H_
#define __LED_H_
#include
sbit LSA=P1^5;
sbit LSB=P1^6;
sbit LSC=P1^7;
void LedScan(unsigned char pos, unsigned char dispCode);
void delay(unsigned int xms);
#endif
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)