#include "reg52.h"//51单片机头文件 #include "Delay.h" //延时函数文件 #include "nokia_5110.h"//Nokia_5110函数文件 #include "STCEEPROM.h" sbit KEY1 = P3^3; sbit KEY2 = P3^4; sbit KEY3 = P3^5; sbit KEY4 = P3^6; sbit KEY5 = P3^7; /**************************************************************** * 名称 : UART_Init() * 功能 : 串口初始化,晶振11.0592,波特率9600 * 变量 : 无 * 返回值 :无 ****************************************************************/ void UART_Init(void) { TMOD = 0x20; //定时器模式选择 PCON = 0x00; SCON = 0x50; TH1 = 0xFd; //设置波特率 9600 TL1 = 0xFd; TR1 = 1; //启动定时器1 ES = 1; //开串口中断 EA = 1; //开总中断 } /**************************************************************** * 名称 : PSend_Hex() * 功能 : 发送字符串函数,可控制发送长度 * 变量 : 无 * 返回值 :无 ****************************************************************/ void Send_Hex(unsigned char *p,unsigned char num) { while(num--) //剩余发送的字符数 { SBUF = *p; //将要发送的数据赋给串口缓冲寄存器 while(!TI);//等待发送结束 TI = 0; //软件清零 p++; //指针加一 } } void Music_DOWN(void) { unsigned char Table[10]; Table[0]= 0x7E; Table[1]= 0xFF; Table[2]= 0x06; Table[3]= 0x01; Table[4]= 0x00; Table[5]= 0x00; Table[6]= 0x00; Table[7]= 0xFE; Table[8]= 0xFA; Table[9]= 0xEF; Send_Hex(Table,10); } void Music_UP(void) { unsigned char Table[10]; Table[0]= 0x7E; Table[1]= 0xFF; Table[2]= 0x06; Table[3]= 0x02; Table[4]= 0x00; Table[5]= 0x00; Table[6]= 0x00; Table[7]= 0xFE; Table[8]= 0xF9; Table[9]= 0xEF; Send_Hex(Table,10); } void DoSum(unsigned char *Str,unsigned char len)//校验位计算 { unsigned int xorsum = 0; unsigned char i; for(i=1;i<len;i++) { xorsum = xorsum +Str; } xorsum = 0 - xorsum; *(Str+i) = (unsigned char)(xorsum >> 8); *(Str+i+1) = (unsigned char)(xorsum & 0x00ff); } void Music_Vol(unsigned char Vol) { unsigned char Table[10]; Table[0] = 0x7E; Table[1] = 0xFF; Table[2] = 0x06; Table[3] = 0x06; //指令 Table[4] = 0x00; Table[5] = 0x00; Table[6] = Vol;//音量 DoSum(Table,7);//计算校验码 Table[9] = 0xEF;//结束码 Send_Hex(Table,10);//发送指令数据 Send_Hex(Table,10); } void Music_STOP(void) { unsigned char Table[10]; Table[0]= 0x7E; Table[1]= 0xFF; Table[2]= 0x06; Table[3]= 0x0E; Table[4]= 0x00; Table[5]= 0x00; Table[6]= 0x00; Table[7]= 0xFE; Table[8]= 0xED; Table[9]= 0xEF; Send_Hex(Table,10); } /**************************************************************** * 名称 : main() * 功能 : 主函数 * 变量 : 无 * 返回值 :无 ****************************************************************/ void main(void) { unsigned char M_VOL; unsigned char Table[2]; Delay_ms(1000); LCD_init(); //初始化液晶 LCD_clear(); LCD_write_chinese_string(10,0,12,5,0,0);//显示“音乐播放器” LCD_write_chinese_string(0,2,12,2,5,0);//显示“状态” //LCD_write_chinese_string(40,2,12,2,9,0);//显示“播放” LCD_write_chinese_string(40,2,12,2,11,0);//显示“停止” LCD_write_chinese_string(0,4,12,2,7,0);//显示“音量” UART_Init(); Delay_ms(100); M_VOL = 25; Music_Vol(M_VOL); Table[0] = M_VOL/10+0x30; Table[1] = M_VOL%10+0x30; LCD_write_english_string(40,4,Table); while(1) { if(!KEY2)//上一首 { Delay_ms(20); Music_UP(); LCD_write_chinese_string(40,2,12,2,9,0);//显示“播放” while(!KEY2); Delay_ms(500); } if(!KEY3)//下一首 { Delay_ms(20); Music_DOWN(); LCD_write_chinese_string(40,2,12,2,9,0);//显示“播放” while(!KEY3); Delay_ms(500); } if(!KEY4)//音量加 { Delay_ms(20); M_VOL++; if(M_VOL>30)M_VOL= 30; Music_Vol(M_VOL); Table[0] =M_VOL/10+0x30; Table[1] =M_VOL%10+0x30; LCD_write_english_string(40,4,Table); Delay_ms(500); } if(!KEY5)//音量减 { Delay_ms(20); if(M_VOL<1)M_VOL= 1; M_VOL--; Music_Vol(M_VOL); Table[0] =M_VOL/10+0x30; Table[1] =M_VOL%10+0x30; LCD_write_english_string(40,4,Table); Delay_ms(500); } if(!KEY1)//停止 { Delay_ms(20); Music_STOP();//停止 LCD_write_chinese_string(40,2,12,2,11,0);//显示“停止” while(!KEY5); Delay_ms(500); } } }
|