以下是出问题的代码,我定义的变量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"
***it K=P3^2;
***it K2=P3^3;
***it K3=P3^4;
***it K4=P3^5;
***it 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 //´®¿Ú²¨ÌØÂÊ
#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;
//ÔËÐо§Õñ£º11.05926MHZ
/******************************´®¿Ú1µÄ²¨ÌØÂÊ********************************/
//T1×÷²¨ÌØÂÊ·¢ÉúÆ÷
//ÔÚ²¨ÌØÂʼӱ¶Çé¿öÏÂ
#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
//////////////////////////////////´®¿Úº¯Êý////////////////////////////////////////////////////
/*****************************************************************************************************
- ¹¦ÄÜÃèÊö£º ´®¿Ú1³õʼ»¯
- Á¥ÊôÄ£¿é£º Íⲿ
- ²ÎÊý˵Ã÷£º ÎÞ
- ·µ»Ø˵Ã÷£º ÎÞ
- ×¢£º ¶¼ÊÇ9600²¨ÌØÂÊ
*****************************************************************************************************/
void Serial_init(void)
{
TMOD = 0x20; // ÉèÖà T1 Ϊ²¨ÌØÂÊ·¢ÉúÆ÷
SCON = 0x50; // 0101,0000 8λÊý¾Ýλ, ÎÞÆæżУÑé
PCON = 0x00; //PCON=0;
TH1=256-(SYS_Fosc/COMM_BAUD_RATE/32/12);//ÉèÖÃΪ9600²¨ÌØÂÊ
TL1=256-(SYS_Fosc/COMM_BAUD_RATE/32/12);
TR1 = 1; //¶¨Ê±Æ÷1´ò¿ª
REN = 1; //´®¿Ú1½ÓÊÕʹÄÜ
ES = 1; //´®¿Ú1ÖжÏʹÄÜ
}
/********************************************************************************************
- ¹¦ÄÜÃèÊö£º ´®¿Ú·¢ËÍÒ»¸ö×Ö½Ú
- Á¥ÊôÄ£¿é£º Íⲿ
- ²ÎÊý˵Ã÷£º
- ·µ»Ø˵Ã÷£º
- ×¢£º
********************************************************************************************/
void Uart_PutByte(INT8U ch)
{
SBUF = ch;
while(!TI){;}
TI = 0;
}
/*****************************************************************************************************
- ¹¦ÄÜÃèÊö£º ´®¿Ú·¢ËÍÒ»Ö¡Êý¾Ý
- Á¥ÊôÄ£¿é£º ÄÚ²¿
- ²ÎÊý˵Ã÷£º
- ·µ»Ø˵Ã÷£º
- ×¢£ºÎÞ
*****************************************************************************************************/
void SendCmd(INT8U len)
{
INT8U i = 0 ;
Uart_PutByte(0x7E); //Æðʼ
for(i=0; i
{
Uart_PutByte(Send_buf[i]) ;
}
Uart_PutByte(0xEF) ;//½áÊø
}
/********************************************************************************************
- ¹¦ÄÜÃèÊö£ºÇóºÍУÑé
- Á¥ÊôÄ£¿é£º
- ²ÎÊý˵Ã÷£º
- ·µ»Ø˵Ã÷£º
- ×¢£º ºÍУÑéµÄ˼·ÈçÏÂ
·¢Ë͵ÄÖ¸ÁȥµôÆðʼºÍ½áÊø¡£½«ÖмäµÄ6¸ö×Ö½Ú½øÐÐÀÛ¼Ó£¬×îºóÈ¡·´Âë
½ÓÊն˾ͽ«½ÓÊÕµ½µÄÒ»Ö¡Êý¾Ý£¬È¥µôÆðʼºÍ½áÊø¡£½«ÖмäµÄÊý¾ÝÀÛ¼Ó£¬ÔÙ¼ÓÉϽÓÊÕµ½µÄУÑé
×Ö½Ú¡£¸ÕºÃΪ0.ÕâÑù¾Í´ú±í½ÓÊÕµ½µÄÊý¾ÝÍêÈ«ÕýÈ·¡£
********************************************************************************************/
void DoSum( INT8U *Str, INT8U len)
{
INT16U xorsum = 0;
INT8U i;
for(i=0; i
{
xorsum = xorsum + Str[i];
}
xorsum = 0 -xorsum;
*(Str+i) = (INT8U)(xorsum >>8);
*(Str+i+1) = (INT8U)(xorsum & 0x00ff);
}
/********************************************************************************************
- ¹¦ÄÜÃèÊö£º ´®¿ÚÏòÍâ·¢ËÍÃüÁî[°üÀ¨¿ØÖƺͲéѯ]
- Á¥ÊôÄ£¿é£º Íⲿ
- ²ÎÊý˵Ã÷£º CMD:±íʾ¿ØÖÆÖ¸ÁÇë²éÔÄÖ¸Áî±í£¬»¹°üÀ¨²éѯµÄÏà¹ØÖ¸Áî
feedback:ÊÇ·ñÐèÒªÓ¦´ð[0:²»ÐèÒªÓ¦´ð£¬1:ÐèÒªÓ¦´ð]
data:´«Ë͵IJÎÊý
- ·µ»Ø˵Ã÷£º
- ×¢£º
********************************************************************************************/
void Uart_SendCMD(INT8U CMD ,INT8U feedback , INT16U dat)
{
Send_buf[0] = 0xff; //±£Áô×Ö½Ú
Send_buf[1] = 0x06; //³¤¶È
Send_buf[2] = CMD; //¿ØÖÆÖ¸Áî
Send_buf[3] = feedback;//ÊÇ·ñÐèÒª·´À¡
Send_buf[4] = (INT8U)(dat >> 8);//datah
Send_buf[5] = (INT8U)(dat); //datal
DoSum(&Send_buf[0],6); //УÑé
SendCmd(8); //·¢ËÍ´ËÖ¡Êý¾Ý
}
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,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x87,0x07,0x07,0x0F,0x0F,0x0F,0x0F,0x0F,0x0F,0x1F,0x1F,0x1F,0x1F,
0x1F,0x1F,0x1F,0x3E,0x3E,0x3E,0x3E,0x3E,0xBE,0xFE,0xFC,0xFC,0xFC,0xFC,0xFC,0xFC,
0xF8,0xFC,0xFE,0xFF,0xFF,0xFF,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++;
}
}
|