单片机按下按钮发送数据到串口
用8个按钮,每个按钮按下通过串口发送到pc端一组数字,比如按下按钮1发送1234,,按下按钮2发送3456,以此类推,电脑用串口工具显示内容即可积分不够,我可以支付宝付款...
用8个按钮,每个按钮按下通过串口发送到pc端一组数字,比如按下按钮1发送1234,,按下按钮2发送3456,以此类推,电脑用串口工具显示内容即可
积分不够,我可以支付宝付款
串口用9600波特率
8个按钮就p1端口 展开
积分不够,我可以支付宝付款
串口用9600波特率
8个按钮就p1端口 展开
4个回答
展开全部
看你一点头绪都没有,我就帮你写一个参考程序把,一下程序是按照你的要求,用P1端口做8个按键发送数据,测试成功如下:
/*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
**/
#include "REG52.H"
/*
* °´¼ü¶¨Òå
*/
sbit Key0_IN = P1^0;
sbit Key1_IN = P1^1;
sbit Key2_IN = P1^2;
sbit Key3_IN = P1^3;
sbit Key4_IN = P1^4;
sbit Key5_IN = P1^5;
sbit Key6_IN = P1^6;
sbit Key7_IN = P1^7;
//
unsigned char UARTByte;
unsigned char MACByte;
unsigned char Cont;
const unsigned char code UART_TABLE1[]={1, 2 , 3, 4};
const unsigned char code UART_TABLE2[]={2, 3 , 4, 5};
const unsigned char code UART_TABLE3[]={3, 4 , 5, 6};
const unsigned char code UART_TABLE4[]={4, 5 , 6, 7};
const unsigned char code UART_TABLE5[]={5, 6 , 7, 8};
const unsigned char code UART_TABLE6[]={6, 7 , 8, 9};
const unsigned char code UART_TABLE7[]={7, 8 , 9, 1};
const unsigned char code UART_TABLE8[]={8, 9 , 1, 1};
//
void UART_Send_Byte(unsigned char Byte);
//ÑÓʱ
void Delay_1ms(unsigned int time)
{
unsigned int a, b;
for(a=time; a>0; a--)
for(b=110; b>0; b--);
}
/*
* Ö÷º¯Êý
*/
int main(void)
{
unsigned char i;
PCON&= 0x7F;
TCON = 0x00;
SCON = 0x50;
//
TMOD = 0x20;
TH1 = 0xFD;
TL1 = 0xFD;
TR1 = 1;
ES = 1;
EA = 1;
//
while(1)
{
MACByte = P1;
if(MACByte != 0xFF)
{
MACByte = P1;
switch(MACByte)
{
case 0xFE: Cont = 1;break;
case 0xFD: Cont = 2;break;
case 0xFB: Cont = 3;break;
case 0xF7: Cont = 4;break;
case 0xEF: Cont = 5;break;
case 0xDF: Cont = 6;break;
case 0xBF: Cont = 7;break;
case 0x7F: Cont = 8;break;
default : Cont = 0;break;
}
for(i = 0; i < 4; i++)
{
if(Cont == 1)
UART_Send_Byte(UART_TABLE1[i]);
else if(Cont == 2)
UART_Send_Byte(UART_TABLE2[i]);
else if(Cont == 3)
UART_Send_Byte(UART_TABLE3[i]);
else if(Cont == 4)
UART_Send_Byte(UART_TABLE4[i]);
else if(Cont == 5)
UART_Send_Byte(UART_TABLE5[i]);
else if(Cont == 6)
UART_Send_Byte(UART_TABLE6[i]);
else if(Cont == 7)
UART_Send_Byte(UART_TABLE7[i]);
else if(Cont == 8)
UART_Send_Byte(UART_TABLE8[i]);
}
// UART_Send_Byte('\n');
Delay_1ms(1000);
}
}
}
// Ïò´®¿Ú·¢ËÍÒ»¸ö×Ö½ÚÊý¾Ý
void UART_Send_Byte(unsigned char Byte)
{
ES = 0;
TI = 0;
SBUF = Byte;
while(!TI);
TI = 0;
ES = 1;
}
//´®¿ÚÖжÏ
void UART_IRQ(void) interrupt 4
{
if(RI)
{
UARTByte = SBUF;
RI = 0;
}
}
/*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
*
**/
#include "REG52.H"
/*
* °´¼ü¶¨Òå
*/
sbit Key0_IN = P1^0;
sbit Key1_IN = P1^1;
sbit Key2_IN = P1^2;
sbit Key3_IN = P1^3;
sbit Key4_IN = P1^4;
sbit Key5_IN = P1^5;
sbit Key6_IN = P1^6;
sbit Key7_IN = P1^7;
//
unsigned char UARTByte;
unsigned char MACByte;
unsigned char Cont;
const unsigned char code UART_TABLE1[]={1, 2 , 3, 4};
const unsigned char code UART_TABLE2[]={2, 3 , 4, 5};
const unsigned char code UART_TABLE3[]={3, 4 , 5, 6};
const unsigned char code UART_TABLE4[]={4, 5 , 6, 7};
const unsigned char code UART_TABLE5[]={5, 6 , 7, 8};
const unsigned char code UART_TABLE6[]={6, 7 , 8, 9};
const unsigned char code UART_TABLE7[]={7, 8 , 9, 1};
const unsigned char code UART_TABLE8[]={8, 9 , 1, 1};
//
void UART_Send_Byte(unsigned char Byte);
//ÑÓʱ
void Delay_1ms(unsigned int time)
{
unsigned int a, b;
for(a=time; a>0; a--)
for(b=110; b>0; b--);
}
/*
* Ö÷º¯Êý
*/
int main(void)
{
unsigned char i;
PCON&= 0x7F;
TCON = 0x00;
SCON = 0x50;
//
TMOD = 0x20;
TH1 = 0xFD;
TL1 = 0xFD;
TR1 = 1;
ES = 1;
EA = 1;
//
while(1)
{
MACByte = P1;
if(MACByte != 0xFF)
{
MACByte = P1;
switch(MACByte)
{
case 0xFE: Cont = 1;break;
case 0xFD: Cont = 2;break;
case 0xFB: Cont = 3;break;
case 0xF7: Cont = 4;break;
case 0xEF: Cont = 5;break;
case 0xDF: Cont = 6;break;
case 0xBF: Cont = 7;break;
case 0x7F: Cont = 8;break;
default : Cont = 0;break;
}
for(i = 0; i < 4; i++)
{
if(Cont == 1)
UART_Send_Byte(UART_TABLE1[i]);
else if(Cont == 2)
UART_Send_Byte(UART_TABLE2[i]);
else if(Cont == 3)
UART_Send_Byte(UART_TABLE3[i]);
else if(Cont == 4)
UART_Send_Byte(UART_TABLE4[i]);
else if(Cont == 5)
UART_Send_Byte(UART_TABLE5[i]);
else if(Cont == 6)
UART_Send_Byte(UART_TABLE6[i]);
else if(Cont == 7)
UART_Send_Byte(UART_TABLE7[i]);
else if(Cont == 8)
UART_Send_Byte(UART_TABLE8[i]);
}
// UART_Send_Byte('\n');
Delay_1ms(1000);
}
}
}
// Ïò´®¿Ú·¢ËÍÒ»¸ö×Ö½ÚÊý¾Ý
void UART_Send_Byte(unsigned char Byte)
{
ES = 0;
TI = 0;
SBUF = Byte;
while(!TI);
TI = 0;
ES = 1;
}
//´®¿ÚÖжÏ
void UART_IRQ(void) interrupt 4
{
if(RI)
{
UARTByte = SBUF;
RI = 0;
}
}
展开全部
不要误人子弟,PC机往单片机每次可以发送任意个数据,MCU接收时候处理一下就OK了,
下面是我的程序,给你参考。
//中断方式接收PC机数据
/*\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
设计思路
接收到的数据存入到dat[50]数组中,
中断方式接收,每次中断接收后,开启定时器,
*/////////////////////////////
//
//陈仲库
#include <reg51.h>
#include <stdio.h>
int dat[20];
unsigned char num=0;
int time=0;
bit MCU_RXD_FLG=0;
void COM_Init(void)
{
SCON = 0x50;
TMOD &= 0x0F;
TMOD |= 0x20;
PCON |= 0x80;
TH1 = 0xFD;
ES=1;EA=1;
TR1 = 1;
TI = 1;
TH0=0xfc; //T0定义初始值
TL0=0x18;
TR0=0; //关闭T0计时
ET0=1;
}
//向串口发送一个字符
void MCU_TXD(unsigned char ch)
{
SBUF=ch;
while(TI==0);
TI=0;
}
//串口接收中断函数
void serial () interrupt 4 using 3
{
EA=0;
if(RI)
{
unsigned char ch;
TR0=0; time=0;
RI = 0;
ch=SBUF;
dat[num++]=ch;
TR0=1;
}
EA=1;
}
delay(int t){while(t--); }
/* PC发送数据必须以0XAA开头和以 0X55结束(帧头帧尾)*/
void main(void)
{
int i=0;
COM_Init();
while(1)
{
if(MCU_RXD_FLG)
{
EA=0;
TR0=0;
if(dat[0]==0xAA && dat[num-1]==0x55)
for(i=0;i<num;i++) printf("%0x ",dat[i]);
num=0;
MCU_RXD_FLG=0;
EA=1;
}
}
}
void timer0() interrupt 1 //T0中断 定时0.1ms
{
EA=0;
TH0=0xfc;
TL0=0x18;
MCU_RXD_FLG=1;
TR0=0;
EA=1;
}
关于AT24C02程序网上多的是,很成熟的程序
OK希望对你能有帮助
下面是我的程序,给你参考。
//中断方式接收PC机数据
/*\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
设计思路
接收到的数据存入到dat[50]数组中,
中断方式接收,每次中断接收后,开启定时器,
*/////////////////////////////
//
//陈仲库
#include <reg51.h>
#include <stdio.h>
int dat[20];
unsigned char num=0;
int time=0;
bit MCU_RXD_FLG=0;
void COM_Init(void)
{
SCON = 0x50;
TMOD &= 0x0F;
TMOD |= 0x20;
PCON |= 0x80;
TH1 = 0xFD;
ES=1;EA=1;
TR1 = 1;
TI = 1;
TH0=0xfc; //T0定义初始值
TL0=0x18;
TR0=0; //关闭T0计时
ET0=1;
}
//向串口发送一个字符
void MCU_TXD(unsigned char ch)
{
SBUF=ch;
while(TI==0);
TI=0;
}
//串口接收中断函数
void serial () interrupt 4 using 3
{
EA=0;
if(RI)
{
unsigned char ch;
TR0=0; time=0;
RI = 0;
ch=SBUF;
dat[num++]=ch;
TR0=1;
}
EA=1;
}
delay(int t){while(t--); }
/* PC发送数据必须以0XAA开头和以 0X55结束(帧头帧尾)*/
void main(void)
{
int i=0;
COM_Init();
while(1)
{
if(MCU_RXD_FLG)
{
EA=0;
TR0=0;
if(dat[0]==0xAA && dat[num-1]==0x55)
for(i=0;i<num;i++) printf("%0x ",dat[i]);
num=0;
MCU_RXD_FLG=0;
EA=1;
}
}
}
void timer0() interrupt 1 //T0中断 定时0.1ms
{
EA=0;
TH0=0xfc;
TL0=0x18;
MCU_RXD_FLG=1;
TR0=0;
EA=1;
}
关于AT24C02程序网上多的是,很成熟的程序
OK希望对你能有帮助
追问
是8个按钮按下就发送到pc端,不是pc给单片机发送
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
是需要编程吗?
语言?
语言?
追问
联系我qq聊,
追答
问题解决了吗?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |