毕设迫在眉睫,仿真总有问题,求大神检查程序,感激不尽
#include <reg52.h> #include<stdio.h>
#include <intrins.h>
#include <absacc.h>
#define uchar unsigned char
#define uint unsigned int
#define Vref 2.5
//tlc5615
sbit cs=P2^4; //片选
sbit clk=P2^3; //时钟
sbit din=P2^5; //SPI口
//tlc2543
sbit clock = P3^1; //输入、输出时钟端
sbit input = P2^7; //数据输入端
sbit output = P2^6; //数据输出端
sbit CS1 = P3^0; //片选端,负电平有效
//lcd
sbit RS=P2^0;
sbit RW=P2^1;
sbit En=P2^2;
//uchar code table[16]={'0','1','2','3','4','5','6','7','8','9','a','b','c','d','e','f'};//数字
static uchar dis_buf[4]={0,0,0,0}; //显示缓存
uchar n,temp1;
uchar key;//键顺序码
uchar key_cli=0;//数字键被按下的次
/****************************************
函数声明
*****************************************/
keyscan();
void TLC56 (unsigned long dav);
void delay56();
void Write(uchar address,uchar dat);
void delayms(unsigned int x);
void jianz(uchar key) ;
void ADCchu(unsigned long aa);
void DispCharacter(uint x, uint y, uint data1);
void LCDReset();
void ShortDelay(uchar i);
void LongDelay(uint i);
void SetRS(bit i);
void SetRW(bit i);
void SetE(bit i);
void InitP0(bit i) ;
void WriteInstruc(uint Instruc);
uint Read_BF_AC();
void WriteData(uint data1);
uint ReadData(void);
bit StatusCheck();
void InitLCD();
void jianchu(uchar jianz,uint r);
/*****************************************
键盘程序
*******************************************/
keyscan()
{
P1=0xef;
if((P1&0x0f)!=0x0f)
{
ShortDelay(100);// 函数调用消抖动
if((P1&0x0f)!=0x0f)
{
temp1=P1;
switch(temp1)
{
case 0xee:
key=0;
break;
case 0xed:
key=4;
break;
case 0xeb:
key=8;
break;
case 0xe7:
key=12;
break;
}
while((P1&0x0f)!=0x0f);
jianz(key); //函数调用
}
}
P1=0xdf;
if((P1&0x0f)!=0x0f)
{
ShortDelay(100);// 函数调用消抖动
if((P1&0x0f)!=0x0f)
{
temp1=P1;
switch(temp1)
{
case 0xde:
key=1;
break;
case 0xdd:
key=5;
break;
case 0xdb:
key=9;
break;
case 0xd7:
key=13;
break;
}
while((P1&0x0f)!=0x0f);
jianz(key); //函数调用
}
}
P1=0xbf;
if((P1&0x0f)!=0x0f)
{
ShortDelay(100);// 函数调用消抖动
if((P1&0x0f)!=0x0f)
{
temp1=P1;
switch(temp1)
{
case 0xbe:
key=2;
break;
case 0xbd:
key=6;
break;
case 0xbb:
key=10;
break;
case 0xb7:
key=14;
break;
}
while((P1&0x0f)!=0x0f);
jianz(key);// 函数调用
}
}
P1=0x7f;
if((P1&0x0f)!=0x0f)
{
ShortDelay(100);// 函数调用消抖动
if((P1&0x0f)!=0x0f)
{
temp1=P1;
switch(temp1)
{
case 0x7e:
key=3;
break;
case 0x7d:
key=7;
break;
case 0x7b:
key=11;
break;
case 0x77:
key=15;
break;
}
while((P1&0x0f)!=0x0f);
jianz(key);
}
}
}
/***************************************
键盘按键功能函数
***************************************/
void jianz(uchar key) //键盘值处理中调用lcd显示
{
if(key<=9)
{
dis_buf[key_cli]=key; //得键值存储在其中
key_cli++;
}
if(key==12)//确定键送数
{
unsigned long out_temp=0; //十六位
uint j,i;
uchar dis_buf1[4]={0,0,0,0};
for(j=0;j<4;j++)
dis_buf1[j]=dis_buf[j];
jianchu(dis_buf1[0],0);
jianchu(dis_buf1[1],1);
jianchu(dis_buf1[2],2);
jianchu(dis_buf1[3],3);
for(i=0;i<key_cli;i++)
{
out_temp=out_temp*10+(dis_buf[i]); //给out-temp赋值为整数
}
//out_temp=2000-out_temp/2;//送5615口
//out_temp=out_temp/2; //数据的转换
TLC56((0x03dd-(2000-out_temp/2))); //(重点地方)数字信号从单片机来,这里可以传键盘值和键盘联系 TLC5615DAC的键盘值
ShortDelay(1);
//out_temp=out_temp/1024;//送5615口
//out_temp=out_temp*2*Vref; //数据的转换
//TLC56((out_temp)); //(重点地方)数字信号从单片机来,这里可以传键盘值和键盘联系 TLC5615DAC的键盘值
//ShortDelay(1);
}
if(key==13) //取消键
{
uint j;
uchar dis_buf2[4]={0,0,0,0};
for(j=0;j<4;j++)
dis_buf[j]=dis_buf2[j] ;
jianchu(dis_buf2[0],0);
jianchu(dis_buf2[1],1);
jianchu(dis_buf2[2],2);
jianchu(dis_buf2[3],3);
TLC56(0x0000); //(重点地方)数字信号从单片机来,这里可以传键盘值和键盘联系 TLC5615DAC的键盘值
ShortDelay(1);
}
if(key==10) //步进+键
{ uint j;
uchar dis_buf3[4];
for(j=0;j<4;j++)
{
dis_buf3[j]=dis_buf[j];//是保护dis_buf[]中的数据
}
n=3; //向前一两位来改变dis_buf的值
dis_buf3[n]=dis_buf3[n]+0x02; //步进值为0x02
if(dis_buf3[n]>=0x09) //输入值满十进位
{
dis_buf3[n]=0;
n=2;
dis_buf3[n]=dis_buf3[n]+0x01;
}
for(j=0;j<4;j++)
{
dis_buf[j]=dis_buf3[j];//将修改后的值传到dis_buf[]中
}
jianchu(dis_buf3[0],0);
jianchu(dis_buf3[1],1);
jianchu(dis_buf3[2],2);
jianchu(dis_buf3[3],3);
}
if(key==11) //步进-键
{
uint j;
uchar dis_buf4[4];
for(j=0;j<4;j++)
{
dis_buf4[j]=dis_buf[j];
}
n=3;
dis_buf4[n]=dis_buf4[n]-0x02;
if(dis_buf4[n]<=0x01)
{
n=2;
dis_buf4[n]=dis_buf4[n]-0x01;
n=3;
dis_buf4[n]=0x09;
}
for(j=0;j<4;j++)
dis_buf[j]=dis_buf4[j];
jianchu(dis_buf4[0],0);
jianchu(dis_buf4[1],1);
jianchu(dis_buf4[2],2);
jianchu(dis_buf4[3],3);
}
}
//LCD初始化
void InitLCD()
{
WriteInstruc(0x06);
LCDReset();
// InputMode(0x06); //增量方式,不移位
WriteInstruc(0x0c) ;
// DispControl(0x0c); //显示开,光标关,闪烁关
WriteInstruc(0x38) ;
// FunctionSet(0x38); //8位,2行,5×7
}
//短延时函数:ShortDelay()
void ShortDelay(uchar i)
{
for(;i>0;i--) ;
}
//寄存器选择信号:SetRS()
void SetRS(bit i)
{
if(i==1) RS = 1;
else RS = 0;
}
//读写操作控制信号:SetRW()
void SetRW(bit i)
{
if(i==1) RW = 1;
else RW = 0;
}
//使能信号:SetE()
void SetE(bit i)
{
if(i==1) En = 1;
else En = 0;
}
//IO输入输出控制
void InitP0(bit i)
{
if(i==1) P0 = 0xff;
else P0 = 0x00;
}
//写指令函数:WriteInstruc()
void WriteInstruc(uint Instruc)
{
while(StatusCheck());
InitP0(0);
SetRS(0);
SetRW(0);
//ShortDelay(1);
SetE(0);
P0 = Instruc;
//ShortDelay(1);
SetE(1);
ShortDelay(10);
SetE(0);
//ShortDelay(1);
SetRW(1);
SetRS(1);
}
//读BF以及AC的值函数:Read_BF_AC()
uint Read_BF_AC()
{
uint temp;
InitP0(1);
SetRS(0);
SetRW(1);
//ShortDelay(1);
SetE(0);
//ShortDelay(1);
SetE(1);
ShortDelay(1);
temp = P0;
ShortDelay(10);
SetE(0);
//ShortDelay(1);
SetRW(0);
SetRS(1);
return(temp);
}
//写数据到RAM函数:WriteData()
void WriteData(uint data1)
{
InitP0(0);
SetRS(1);
SetRW(0);
//ShortDelay(1);
SetE(0);
P0 = data1;
//ShortDelay(1);
SetE(1);
ShortDelay(10);
SetE(0);
//ShortDelay(1);
SetRW(1);
SetRS(0);
}
//检测LCD控制器状态函数:StatusCheck()
//返回一bit数:1 -- 忙;0 -- 闲
bit StatusCheck()
{
return((bit)(Read_BF_AC() & 0x80));
}
//在指定位置显示字符函数: DispCharacter()
// x为行号,y为列号,
// data为显示字符的码字数据
void DispCharacter(uint x, uint y, uint data1)
{
uint temp;
while(StatusCheck()); //若LCD控制器忙,则等待
temp = y & 0x0f;
x &= 0x01;
if(x) temp |= 0x40;
WriteInstruc(0x80|temp); //设置显示位置
WriteData(data1);
}
//LCD复位函数:LCDReset()
void LCDReset()
{ WriteInstruc(0x01) ;
WriteInstruc(0x02) ;
}
/******************************************
LCD1062显示函数
*******************************************/
void jianchu(unsigned char jianz,uint r) //键盘值处理并显示
{
if(r<4)
switch(jianz)
{
case 0:
DispCharacter(0,7+r, '0' ); break;
case 1:
DispCharacter(0,7+r, '1' ); break;
case 2:
DispCharacter(0,7+r, '2' ); break;
case 3:
DispCharacter(0,7+r, '3' ); break;
case 4:
DispCharacter(0,7+r, '4' ); break;
case 5:
DispCharacter(0,7+r, '5' ); break;
case 6:
DispCharacter(0,7+r, '6' ); break;
case 7:
DispCharacter(0,7+r, '7' ); break;
case 8:
DispCharacter(0,7+r, '8' ); break;
case 9:
DispCharacter(0,7+r, '9' ); break;
}
else
switch(jianz)
{
case 0:
DispCharacter(1,3+r, '0' ); break;
case 1:
DispCharacter(1,3+r, '1' ); break;
case 2:
DispCharacter(1,3+r, '2' ); break;
case 3:
DispCharacter(1,3+r, '3' ); break;
case 4:
DispCharacter(1,3+r, '4' ); break;
case 5:
DispCharacter(1,3+r, '5' ); break;
case 6:
DispCharacter(1,3+r, '6' ); break;
case 7:
DispCharacter(1,3+r, '7' ); break;
case 8:
DispCharacter(1,3+r, '8' ); break;
case 9:
DispCharacter(1,3+r, '9' ); break;
}
}
/*****************************************
TLC5615串口接收数据
*****************************************/
void TLC56 (unsigned long dav)
{
uchar i;
dav<<=6; //数据移位使高位在前
cs=0;
clk=0;
for(i=0;i<12;i++)
{
din=(bit)(dav&0x8000); //位位发送
dav<<=1;
clk=1;
ShortDelay(10);
clk=0;
}
cs=1;
}
/*************************************************
tlc2543采样程序
*************************************************/
//时延程序
//void delay(uchar N)
//{
//while(N--);
//}
//TLC2543转换程序 12位AD
void TLC2543(uchar CON_WORD) //选择的通道,为0--10
{
unsigned long ad=0;
uchar i;
CON_WORD<<=4; //当控制字位0x00时是十二位转换
clock=0;
CS1=0;
for(i=0;i<12;i++)
{
if(output==1) //读取DATAOUT
ad=ad|0x01;
if(CON_WORD&0x80) //读取控制字到INPUT
input=1;
else
input=0;
clock=1;
ShortDelay(10);
clock=0;
ShortDelay(10);
CON_WORD<<=1;
ad<<=1;
}
CS1=1;
ad>>=1;
ADCchu(ad);
}
/***********************************
处理TLC3543采样的值函数
************************************/
void ADCchu(unsigned long aa)//将16位转换成4个字符存储个、十、百、千
{
float fVoltage1;
uchar buf[4];
fVoltage1=(float)aa/4096*5/2.5*1000;
buf[0]=(uchar)(fVoltage1/1000);
buf[1]=(uchar) ((fVoltage1-buf[0]*1000)/100);
buf[2]=(uchar) ((fVoltage1-buf[0]*1000-buf[1]*100)/10);
buf[3]=(uchar) ((fVoltage1-buf[0]*1000-buf[1]*100-buf[2]*10));
jianchu(buf[0],4);
jianchu(buf[1],5);
jianchu(buf[2],6);
jianchu(buf[3],7);
}
/****************************
主函数
****************************/
void main()
{
InitP0(0);
// P2 = 0x00;
InitLCD();
//显示"MA"
DispCharacter(0,14,'M');
DispCharacter(0,15,'A');
DispCharacter(1,14,'M');
DispCharacter(1,15,'A');
DispCharacter(1,7,'0');
DispCharacter(1,8,'0');
DispCharacter(1,9,'0');
DispCharacter(1,10,'0');
DispCharacter(0,7,'0');
DispCharacter(0,8,'0');
DispCharacter(0,9,'0');
DispCharacter(0,10,'0');
while(1)
{
keyscan();//键盘扫描调jianz()
TLC2543(0);
//TLC ADC启动 调用ADCzhuan /
}
}