標題: 5110顯示屏源程序分享 [打印本頁]
作者: yunkai123 時間: 2017-6-1 17:03
標題: 5110顯示屏源程序分享
#include "reg52.h"//51單片機頭文件
#include "Delay.h" //延時函數(shù)文件
#include "nokia_5110.h"//Nokia_5110函數(shù)文件
#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; //設(shè)置波特率 9600
TL1 = 0xFd;
TR1 = 1; //啟動定時器1
ES = 1; //開串口中斷
EA = 1; //開總中斷
}
/****************************************************************
* 名稱 : PSend_Hex()
* 功能 : 發(fā)送字符串函數(shù),可控制發(fā)送長度
* 變量 : 無
* 返回值 :無
****************************************************************/
void Send_Hex(unsigned char *p,unsigned char num)
{
while(num--) //剩余發(fā)送的字符數(shù)
{
SBUF = *p; //將要發(fā)送的數(shù)據(jù)賦給串口緩沖寄存器
while(!TI);//等待發(fā)送結(jié)束
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;//結(jié)束碼
Send_Hex(Table,10);//發(fā)送指令數(shù)據(jù)
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()
* 功能 : 主函數(shù)
* 變量 : 無
* 返回值 :無
****************************************************************/
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);//顯示“狀態(tài)”
//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);
}
}
}
歡迎光臨 (http://www.torrancerestoration.com/bbs/) |
Powered by Discuz! X3.1 |