微波EDA网,见证研发工程师的成长!
首页 > 研发问答 > 嵌入式设计讨论 > MCU和单片机设计讨论 > 51单片机串口问题,求大神看看

51单片机串口问题,求大神看看

时间:10-02 整理:3721RD 点击:
以下是出问题的代码,我定义的变量caidan,K,K2两个按键来加减,相当于切换菜单,并打印当前的菜单,K3确定当前的菜单值,并发送相对应的mp3模块代码,可是编译通下载,可以正常切换菜单,都是无法确定,实际上说是没有发送命令吧,我已经测试了,都是Uart_SendCMD这个函数在我不使用按键直接在main()函数下发送然后长时间延时时正常,然后我把        K3=1;
        delays(1);
                if(K3==0)后面的switch删除直接K3确定就发送命令时还是不行,求解答。谢谢
#include"reg51.h"
#include"st7565.h"
#include"STK6037.h"
sbit K=P3^2;
sbit K2=P3^3;
sbit K3=P3^4;
sbit K4=P3^5;
sbit K5=P3^6;
extern unsigned int caidan;
unsigned int ss=0;
void delays(unsigned char j);
void key() interrupt 1 using 1
{
TL0=(8192-4608)%32;
TH0=(8192-4608)/32;
if(K==0)
        {
                K=1;
                delays(1);
                if(K==0)
                caidan++;
                Lcd12864_clean1(0,120,2);//当菜单值变化,菜单栏清屏,并打印菜单
                switch(caidan)
                {
                        case 0:        Lcd12864_Write16CnCHAR(0,2,"上一曲");break;
                        case 1:        Lcd12864_Write16CnCHAR(0,2,"下一曲");break;
                        case 2:        Lcd12864_Write16CnCHAR(0,2,"音量加");break;
                        case 3:        Lcd12864_Write16CnCHAR(0,2,"音量减");break;
                        case 4:        Lcd12864_Write16CnCHAR(0,2,"单曲循环开");break;
                        case 5:        Lcd12864_Write16CnCHAR(0,2,"单曲循环关");break;               
                        case 6:        Lcd12864_Write16CnCHAR(0,2,"模式");break;
                        default :if(caidan<1&&caidan>6)caidan=1;
                }
        }
       
if(K2==0)
        {
                K2=1;
                delays(1);
                if(K2==0)
                caidan--;
                Lcd12864_clean1(0,130,2);//当菜单值变化,菜单栏清空,并打印新的菜单
                switch(caidan)
          {
                        case 0:        Lcd12864_Write16CnCHAR(0,2,"上一曲");break;
                        case 1:        Lcd12864_Write16CnCHAR(0,2,"下一曲");break;
                        case 2:        Lcd12864_Write16CnCHAR(0,2,"音量加");break;
                        case 3:        Lcd12864_Write16CnCHAR(0,2,"音量减");break;
                        case 4:        Lcd12864_Write16CnCHAR(0,2,"单曲循环开");break;
                        case 5:        Lcd12864_Write16CnCHAR(0,2,"单曲循环关");break;               
                        case 6:        Lcd12864_Write16CnCHAR(0,2,"模式");break;
                        default :if(caidan<1&&caidan>6)caidan=1;
                }
        }
       

if(K3==0)  //如果确认当前菜单
        {
                K3=1;
        delays(1);
                if(K3==0)
                                {
                                Uart_SendCMD(0x02,0,0x00);
                               
                                /*        switch(caidan)//发送当前菜单命令
                                                        {
                                                                case 0:Uart_SendCMD(0x01,0,0x00);break;                                                                //命令,1应答(0不应答),参数
                                                                case 1:K5=0;Uart_SendCMD(0x02,0,0x00);delays(2);break;
                                                                case 2:Uart_SendCMD(0x04,1,0x00);break;
                                                                case 3:Uart_SendCMD(0x05,1,0x00);break;
                                                                case 4:Uart_SendCMD(0x19,1,0x00);break;
                                                                case 5:Uart_SendCMD(0x19,1,0x01);break;
                                                                case 6:ss++;if(ss>5)ss=0;Uart_SendCMD(0x07,1,ss);break;
                                                                default :Uart_SendCMD(0x01,0,0x00);break;
                                                        }*/
                                }
                }
}


下面是串口
#include"st7565.h"
#include"reg51.h"
#define TIMER0_H   (65536-1800)/256//?¨ê±2Ms
#define TIMER0_L   (65536-1800)%256
#define SYS_Fosc        11059200  //?§???μ?ê                          
#define COMM_BAUD_RATE  9600      //′??ú2¨ì??ê
#define OSC_FREQ        11059200  //11059200  

typedef        unsigned char        BYTE;
typedef        unsigned int        WORD;
typedef  unsigned char   BOOL ;
typedef  unsigned char   INT8U ;
typedef  signed char     INT8S ;
typedef  unsigned int    INT16U ;
typedef  signed int      INT16S ;
typedef  unsigned long   INT32U ;
typedef  signed long     INT32S ;
static INT8U Send_buf[10] = {0};
static INT8U Recv_buf[10] = {0};
static INT8U SendDataLen = 0;
static INT8U ResendDataLen = 0;

//??DD?§??£o11.05926MHZ

/******************************′??ú1μ?2¨ì??ê********************************/
//T1×÷2¨ì??ê·¢éú?÷
//?ú2¨ì??ê?ó±??é????
#define BAUD_57600                            256 - (OSC_FREQ/192L)/57600L    // 254 FF
#define BAUD_28800                            256 - (OSC_FREQ/192L)/28800L    // 254 FE
#define BAUD_19200                            256 - (OSC_FREQ/192L)/19200L    // 253 FD
#define BAUD_14400                            256 - (OSC_FREQ/192L)/14400L    // 252 FC
#define BAUD_9600                             256 - (OSC_FREQ/192L)/9600L     // 250 FA


//////////////////////////////////′??úoˉêy////////////////////////////////////////////////////

/*****************************************************************************************************
- 1|?ü?èê?£o ′??ú13?ê??ˉ
- á¥ê??£?é£o ía2?
- 2?êy?μ?÷£o ?T
- ·μ???μ?÷£o ?T
- ×¢£o              ??ê?96002¨ì??ê
*****************************************************************************************************/
void Serial_init(void)
{
        TMOD = 0x20;                // éè?? T1 ?a2¨ì??ê·¢éú?÷
        SCON = 0x50;                // 0101,0000 8??êy?Y??, ?T????D£?é
                                                  
        PCON = 0x00;                //PCON=0;
        TH1=256-(SYS_Fosc/COMM_BAUD_RATE/32/12);//éè???a96002¨ì??ê
        TL1=256-(SYS_Fosc/COMM_BAUD_RATE/32/12);
    TR1     = 1;                            //?¨ê±?÷1′ò?a
    REN     = 1;                           //′??ú1?óê?ê1?ü
    ES      = 1;                           //′??ú1?D??ê1?ü
}

/********************************************************************************************
- 1|?ü?èê?£o ′??ú·¢?íò???×??ú
- á¥ê??£?é£o ía2?
- 2?êy?μ?÷£o
- ·μ???μ?÷£o
- ×¢£o             
********************************************************************************************/
void Uart_PutByte(INT8U ch)
{
    SBUF  = ch;
    while(!TI){;}
    TI = 0;
}
/*****************************************************************************************************
- 1|?ü?èê?£o ′??ú·¢?íò???êy?Y
- á¥ê??£?é£o ?ú2?
- 2?êy?μ?÷£o
- ·μ???μ?÷£o
- ×¢£o?T     
*****************************************************************************************************/
void SendCmd(INT8U len)
{
    INT8U i = 0 ;
    Uart_PutByte(0x7E); //?eê?
    for(i=0; i<len; i++)//êy?Y
    {
                Uart_PutByte(Send_buf[i]) ;
    }
    Uart_PutByte(0xEF) ;//?áê?
}
/********************************************************************************************
- 1|?ü?èê?£o?óoíD£?é
- á¥ê??£?é£o
- 2?êy?μ?÷£o
- ·μ???μ?÷£o
- ×¢£o      oíD£?éμ????·è???
             ·¢?íμ???á?£?è¥μ??eê?oí?áê??£???D??μ?6??×??ú??DDà??ó£?×?oóè?·′??
             ?óê????í???óê?μ?μ?ò???êy?Y£?è¥μ??eê?oí?áê??£???D??μ?êy?Yà??ó£??ù?óé??óê?μ?μ?D£?é
             ×??ú?£??o??a0.?a?ù?í′ú±í?óê?μ?μ?êy?Yíêè??yè·?£
********************************************************************************************/
void DoSum( INT8U *Str, INT8U len)
{
    INT16U xorsum = 0;
    INT8U i;
    for(i=0; i<len; i++)
    {
        xorsum  = xorsum + Str[i];
    }
        xorsum     = 0 -xorsum;
        *(Str+i)   = (INT8U)(xorsum >>8);
        *(Str+i+1) = (INT8U)(xorsum & 0x00ff);
}

/********************************************************************************************
- 1|?ü?èê?£o ′??ú?òía·¢?í?üá?[°üà¨????oí2é?ˉ]
- á¥ê??£?é£o ía2?
- 2?êy?μ?÷£o CMD:±íê???????á?£???2é????á?±í£??1°üà¨2é?ˉμ??à1???á?
              feedback:ê?·?Dèòaó|′e[0:2?Dèòaó|′e£?1:Dèòaó|′e]
              data:′??íμ?2?êy
- ·μ???μ?÷£o
- ×¢£o      
********************************************************************************************/
void Uart_SendCMD(INT8U CMD ,INT8U feedback , INT16U dat)
{
    Send_buf[0] = 0xff;    //±£á?×??ú
    Send_buf[1] = 0x06;    //3¤?è
    Send_buf[2] = CMD;     //??????á?
    Send_buf[3] = feedback;//ê?·?Dèòa·′à?
    Send_buf[4] = (INT8U)(dat >> 8);//datah
    Send_buf[5] = (INT8U)(dat);     //datal
    DoSum(&Send_buf[0],6);        //D£?é
    SendCmd(8);       //·¢?í′???êy?Y
}

void initkey()
{
TMOD=0x00;
TL0=(8192-4608)%32;
TH0=(8192-4608)/32;
ET0=1;
EA=1;
TR0=1;
}
下面是主函数
#include"st7565.h"
#include"STK6037.h"
//extern unsigned int caidan;
//---存一个图片--//
unsigned int caidan=0;
unsigned char code pic[]={//这是一个开机画面
/*--  宽度x高度=128x64  --*/
0x00,0x00,0x00,0x00,0x00,0x40,0x40,0x40,0x40,0x40,0xC0,0x00,0x00,0xF0,0x80,0x80,
0x80,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x10,0x60,0x00,0x00,0xE0,0x20,0x10,0x00,0xE0,0x20,
0x20,0xE0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x70,0x40,0x40,0x40,0x40,0x40,0xF0,0x40,0x40,
0x40,0x40,0x40,0x40,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0xC0,0x40,0x40,0x40,0x40,0xC0,0x40,0x40,0x40,0x40,
0xC0,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x82,0x64,0x18,0x26,0xC9,0x04,0x83,0x60,0x1C,0x60,
0x80,0x02,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x02,0x02,0x02,0xFE,0x00,0x00,0xFF,0x40,0x20,0x00,0xFF,0x20,
0x40,0x3F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x08,0x06,0xFF,0x00,0x00,0x1E,0x52,0x92,0x12,0xFF,0x12,0x12,
0x12,0x12,0x1E,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0x44,0x44,0x44,0x44,0xFF,0x44,0x44,0x44,0x44,
0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x01,0x00,0x00,0x00,0x08,0x04,0x02,0x01,0x00,0x00,0x00,
0x01,0x02,0x04,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x04,0x02,0x01,0x02,0x04,0x04,0x04,0x04,0x04,0x07,0x04,
0x04,0x04,0x04,0x04,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x0F,0x00,0x08,0x08,0x04,0x02,0x01,0x02,0x04,0x04,
0x08,0x08,0x08,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x08,0x06,0x01,0x00,0x00,0x00,0x00,0x0F,0x00,0x00,0x08,0x10,
0x0F,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x80,
0xC0,0xC0,0xC0,0xE0,0xE0,0xE0,0xF0,0xF0,0xF0,0xE0,0x80,0x80,0x80,0xC0,0xC0,0xC0,
0xC0,0xC0,0xC0,0xC0,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x80,0x80,0x80,0xC0,0xC0,0xC0,0xC0,0xC0,
0x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xFF,0xFF,0xFF,0xF3,0xF1,0xF0,0xF0,0xF0,0xE0,0xE0,0xE0,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x03,0x03,
0x07,0x07,0x0F,0x0F,0x1F,0x1F,0x3F,0x3E,0x7E,0x7C,0x7C,0xFC,0xF8,0xF8,0xF8,0xF0,
0xF0,0xF8,0xF8,0xF8,0xFC,0x7C,0x7C,0x7E,0x3E,0x3F,0x1F,0x1F,0x0F,0x0F,0x07,0x07,
0x03,0x03,0x01,0x01,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x03,0x03,0x07,
0x07,0x0F,0x0F,0x1F,0x1F,0x1F,0x3F,0x3E,0x3E,0x7C,0x7C,0x7C,0xF8,0xF8,0xF8,0xF8,
0x7C,0x7C,0x7C,0x3E,0x3E,0x3F,0x1F,0x1F,0x1F,0x0F,0x0F,0x07,0x07,0x03,0x03,0x01,
0x01,0x00,0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x03,0x03,0x03,
};
//////////////////////////////////////////////////////////定义串口////////////////////////////

//////////////////////////////////定义串口完成//////////////////////////////////////////////////////




/*******************************************************************************
* 函 数 名         : main
* 函数功能                   : 主函数
* 输    入         : 无
* 输    出         : 无
*******************************************************************************/
void Serial_init(void);
void delays(uchar i);
void main(void)
{
        uchar i , j,k;
        uint n;
        Lcd12864_Init();
        Lcd12864_ClearScreen();        //清屏
        for (i=0;i<8;i++)
        {  
            //--设置初始显示开始位置--//
                LcdSt7565_WriteCmd(0x40);
                //--设置Y坐标--//
                LcdSt7565_WriteCmd(0xB0+i);
               
                //--设置X坐标--//
                LcdSt7565_WriteCmd(0x10);
                LcdSt7565_WriteCmd(0x04);
                for(j=0; j<128; j++)
                {
                        LcdSt7565_WriteData(~pic[n]);
                        n=n+1;
                }
        }
                
        delays(5);
  Lcd12864_ClearScreen();//清屏
        Lcd12864_Write16CnCHAR(0,2,"出面初始化");
        Lcd12864_Write16CnCHAR(0,4,"请稍后啊");
  Serial_init() ;
  initkey();          //初始化
        delays(6);
        Lcd12864_ClearScreen();//清屏
  for(k=0;k<10000;k++)
  {
unsigned int f;
                f++;
}
}

原理图


串口mp3


拉丝机专用变频器技术方案
具有恒定滑差控制功能,两路脉冲输入检测口检测拉丝速度与收线速度,通过与设定滑差比较来控制收线增速和收线减速,滑差滞环控制能够防止频繁动作。
滑差可以通过端子选择或电位器进行设置。工艺要求:
■保持恒线速度收线;
■启动停止平稳;   
系统方案:
    具有恒定滑差控制功能,两路脉冲输入检测口检测拉丝速度与收线速度,通过与设定滑差比较来控制收线增速和收线减速,滑差滞环控制能够防止频繁动作。滑差可以通过端子选择或电位器进行设置。
独特优势:
■成本最优:可以完全省掉PLC;
■系统简洁:丰富的输入输出功能使控制逻辑更为简洁、可靠;
■增强功能:通过检测滑差异常进行断线判断,无需增加额外的断线检测部件;
变频伺服PLC驱动生产解决方案提供商
深圳市伊瑞软件技术有限公司   
Shenzhen Erik Software Technology Co., Ltd
联系电话13823137464

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top