|
#include <reg52.h>
#include <math.h>
#define uint unsigned int
#define uchar unsigned char
//=======================按键定义==========================
#define up 0xbb
#define down 0x7b
#define set 0x7d
#define ok 0x7e
#define KeyPort P1
#define pi 3.14
#define LCM_Data P0
#define Busy 0x80
//============================液晶引脚定义=================
sbit LCM_RS=P2^7;
sbit LCM_RW=P2^6;
sbit LCM_E =P2^5;
//========================SPI引脚配置=======================
sbit CS=P2^0;
sbit OIN=P2^1;
sbit CLK=P2^2;
uchar dis0[16],dis1[16];
uchar set_v[1];
uint f=0,w=0,v=0; //为什么不用char捏??
unsigned char code CGRAM_1602[8]=
{
0x08,0x12,0x15,0x15,0x15,0x0e,0x02,0x00,//φ
};
//=======================延时======================================
void Delay_ms (uchar a)
{
uint i;
for(;a>0;a--)
{
for(i=0;i<1203;i++);
}
}
//=======================读按键====================================
uchar Read_key(void) //键盘扫描函数,使用行列反转扫描法
{
uchar cord_h,cord_l;//行列值中间变量
KeyPort=0x0f; //行线输出全为0
cord_h=KeyPort&0x0f; //读入列线值
if(cord_h!=0x0f) //先检测有无按键按下
{
Delay_ms(10); //去抖
if((KeyPort&0x0f)!=0x0f)
{
cord_h=KeyPort&0x0f; //读入列线值
KeyPort=cord_h|0xf0; //输出当前列线值
cord_l=KeyPort&0xf0; //读入行线值
while((KeyPort&0xf0)!=0xf0);//等待松开并输出
return(cord_h+cord_l);//键盘最后组合码值
}
}return(0xff); //返回该值
}
uchar ReadDataLCM(void) //读数据
{unsigned char temp;
LCM_RS = 1;
LCM_RW = 1;
LCM_E = 1;
temp=LCM_Data;
LCM_E = 0;
return(temp);
}
//=====================================================================
void ReadStatusLCM(void) //读状态
{LCM_Data = 0xFF; //输入口
LCM_RS = 0;
LCM_RW = 1;
LCM_E = 1;
while (LCM_Data & Busy); //检测忙信号
LCM_E = 0;
return ;
}
void WriteDataLCM(unsigned char WDLCM) //写数据
{
ReadStatusLCM(); //检测忙
LCM_Data = WDLCM;
LCM_RS = 1;
LCM_RW = 0;
LCM_E = 1; //若晶振速度太高可以在这后加小的延时
LCM_E = 0;
}
//============================================================================
void WriteCommandLCM(unsigned char WCLCM, unsigned char BuysC) //写指令
{ //BuysC为0时忽略忙检测
if (BuysC) ReadStatusLCM(); //根据需要检测忙
LCM_Data = WCLCM;
LCM_RS = 0;
LCM_RW = 0;
LCM_E = 1;
LCM_E = 0;
}
//=======================================================================
void LCMInit(void) //LCM初始化
{WriteCommandLCM(0x38,0); //三次显示模式设置,不检测忙信号
Delay_ms (5);
WriteCommandLCM(0x38,0);
Delay_ms (1);
WriteCommandLCM(0x38,1); //8位总线,两行显示,开始要求每次检测忙信号
WriteCommandLCM(0x08,1); //关闭显示
WriteCommandLCM(0x01,1); //显示清屏
WriteCommandLCM(0x06,1); //显示光标移动设置
WriteCommandLCM(0x0C,1); //显示开及光标设置
}
//------------------------按指定位置显示一个字符---------------------------------------
void DisplayOneChar(unsigned char X, unsigned char Y, unsigned char DData)
{
X &= 0xF; //限制X不能大于15,Y不能大于1
if (Y) X |= 0x40; //当要显示第二行时地址码+0x40;
X |= 0x80;
WriteCommandLCM(X, 1); //发送地址码
WriteDataLCM(DData);
}
//----------------------------按指定位置显示一串字符--------------------------------------
void DisplayListChar(unsigned char X, unsigned char Y, unsigned char *DData, unsigned char num)
{unsigned char i;
X &= 0xF; //限制X不能大于15,Y不能大于1
if (Y) X |= 0x40; //当要显示第二行时地址码+0x40;
X |= 0x80;
WriteCommandLCM(X, 1); //发送地址码
X&=0x0f;
for(i=0;i<num;i++) //发送num个字符
{WriteDataLCM(DData[i]); //写并显示单个字符
if ((++X)> 0xF)break; //每行最多16个字符,已经到最后一个字符
}
}
//==========================显示函数=================================================
void display(void)
{ dis0[2]=f/1000%10+'0';
dis0[3]=f/100%10+'0';
dis0[4]=f/10%10+'0';
dis0[5]=f%10+'0';
dis0[11]=w/100%10+'0';
dis0[12]=w/10%10+'0';
dis0[13]=w%10+'0';
dis1[7]=v/10000%10+'0';
dis1[8]=v/1000%10+'0';
dis1[9]=v/100%10+'0';
dis1[10]=v/10%10+'0';
dis1[12]=v%10+'0';
DisplayListChar(0,0,dis0,15);
DisplayListChar(0,1,dis1,15);
}
//===========SPI发送函数====================
void SPI_send(uint d){
uchar i;
CLK=0;
CS=0;
for(i=0;i<19;i++)
{
if(d&0x40000)
OIN=1;
else
CLK=1;
Delay_ms(5);
d=d<<1;
OIN=0;
CLK=0;
}
CS=1;
}
//===============主函数==================================================
main()
{
uint N0,N1; //传输频率比较值
uint n0; //传输占空比较值
uint temp1,temp2,temp3;
uchar i,k,s,Flag;
Delay_ms(20);
LCMInit();
WriteCommandLCM(0x40,1);//CGRAM
for(i=0;i<8;i++)WriteDataLCM(CGRAM_1602[i]);
dis0[0]='A';dis0[1]=' ';dis0[6]='H';dis0[7]='z';dis0[8]=' ';dis0[9]=0;dis0[10]=' ';dis0[14]=' ';dis0[15]=' ';
dis1[11]='.';dis1[13]='m';dis1[14]='V';dis1[0]=' ';dis1[1]=' ';dis1[2]=' ';dis1[3]=' ';dis1[4]=' ';dis1[5]=' ';
dis1[6]=' ';dis1[15]=' ';
while(1){
display();
k=Read_key();
if(k==set)
{
while(k!=0xff)k=Read_key();
Delay_ms(10);
WriteCommandLCM(0x0f,1); //开光标并闪烁
Flag=0;
for(s=1;s<13;s++){
switch(s)
{
case 1: WriteCommandLCM(0x82,1);break;
case 2: WriteCommandLCM(0x83,1);break;
case 3: WriteCommandLCM(0x84,1);break;
case 4: WriteCommandLCM(0x85,1);break;
case 5: WriteCommandLCM(0x8b,1);break;
case 6: WriteCommandLCM(0x8c,1);break;
case 7: WriteCommandLCM(0x8d,1);break;
case 8: WriteCommandLCM(0xc7,1);break;
case 9: WriteCommandLCM(0xc8,1);break;
case 10: WriteCommandLCM(0xc9,1);break;
case 11: WriteCommandLCM(0xca,1);break;
case 12: WriteCommandLCM(0xcc,1);break;
}
while(k!=0xff)k=Read_key();
Delay_ms(10);
while(1){
WriteCommandLCM(0x0f,1); //开光标并闪烁
k=Read_key();
if(k==set){
while(k!=0xff)k=Read_key();
Delay_ms(10);
break;
}
if(k==up)
{ while(k!=0xff)k=Read_key();
Delay_ms(10);
// WriteCommandLCM(0x0c,1); //关光标并闪烁
switch(s)
{
case 1:f=f+1000; break;
case 2:f=f+100; break;
case 3:f=f+10; break;
case 4:f=f+1; break;
case 5:w=w+100;break;
case 6:w=w+10;break;
case 7:w=w+1;break;
case 8:v=v+10000;break;
case 9:v=v+1000;break;
case 10:v=v+100;break;
case 11:v=v+10;break;
case 12:v=v+1;break;
} WriteCommandLCM(0x0c,1); //关光标并闪烁
if(f>1000)f=0;
if(w>359)w=0;
if(v>30000)v=0;
if(k==ok){
temp1=f;
temp2=w;
temp3=v;}
WriteCommandLCM(0x0f,1); //开光标并闪烁
display();
switch(s)
{
case 1: WriteCommandLCM(0x82,1);break;
case 2: WriteCommandLCM(0x83,1);break;
case 3: WriteCommandLCM(0x84,1);break;
case 4: WriteCommandLCM(0x85,1);break;
case 5: WriteCommandLCM(0x8b,1);break;
case 6: WriteCommandLCM(0x8c,1);break;
case 7: WriteCommandLCM(0x8d,1);break;
case 8: WriteCommandLCM(0xc7,1);break;
case 9: WriteCommandLCM(0xc8,1);break;
case 10: WriteCommandLCM(0xc9,1);break;
case 11: WriteCommandLCM(0xca,1);break;
case 12: WriteCommandLCM(0xcc,1);break;
}
while(k!=0xff)k=Read_key();
Delay_ms(10);
}
else if(k==down)
{ while(k!=0xff)k=Read_key();
Delay_ms(10);
WriteCommandLCM(0x0c,1); //关光标并闪烁
switch(s)
{
case 1:f=f-1000; break;
case 2:f=f-100; break;
case 3:f=f-10; break;
case 4:f=f-1; break;
case 5:w=w-100;break;
case 6:w=w-10;break;
case 7:w=w-1;break;
case 8:v=v-10000;break;
case 9:v=v-1000;break;
case 10:v=v-100;break;
case 11:v=v-10;break;
case 12:v=v-1;break;
}
if(f<0)f=0;
if(w<0)w=0;
if(v<0)v=0;
if(k==ok){
temp1=f;
temp2=w;
temp3=v;}
WriteCommandLCM(0x0c,1); //关光标并闪烁
WriteCommandLCM(0x0f,1); //开光标并闪烁
display();
switch(s)
{
case 1: WriteCommandLCM(0x82,1);break;
case 2: WriteCommandLCM(0x83,1);break;
case 3: WriteCommandLCM(0x84,1);break;
case 4: WriteCommandLCM(0x85,1);break;
case 5: WriteCommandLCM(0x8b,1);break;
case 6: WriteCommandLCM(0x8c,1);break;
case 7: WriteCommandLCM(0x8d,1);break;
case 8: WriteCommandLCM(0xc7,1);break;
case 9: WriteCommandLCM(0xc8,1);break;
case 10: WriteCommandLCM(0xc9,1);break;
case 11: WriteCommandLCM(0xca,1);break;
case 12: WriteCommandLCM(0xcc,1);break;
}
while(k!=0xff)k=Read_key();
Delay_ms(10);
}
else if(k==ok)
{
N0=9999;
n0=asin((temp3*pi*pi)/(12*3.3))*N0/pi;
N1=100000000/(temp1+10000)-1;
//n[1]=asin((set_v[1]*pi*pi)/(4*3.3*3))*N[1]/pi;
//N[2]=100000000/(temp1+10000)-1;
//N[3]=100000000/(set_value[1]+10000)-1;
//m=(((N[3]*set_value[2])/180)+n[1]-n[0])/2;
SPI_send(N0|(0x00<<11));
Delay_ms(200);
SPI_send(0x20<<11|n0);
Delay_ms(200);
SPI_send(N1|0x40<<11);
Delay_ms(200);
Flag=1;
WriteCommandLCM(0x0c,1);//关光标并闪烁
break;
}
}
if(Flag)break;
WriteCommandLCM(0x0c,1); //关光标并闪烁
}
}
}
} |
阿莫论坛20周年了!感谢大家的支持与爱护!!
曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
|