#include <reg51.h>
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned char
#define LCDSIG P0 //lcd的信号控制引脚
sbit lcdrs=P1^3;
sbit lcdrw=P1^4;
sbit lcdep=P1^5;
sbit p16=P1^6;
sbit p17=P1^7;
sbit ISDEOM = P2^3; //ISD25120控制引脚
sbit ISDPR = P2^2;
sbit ISDPD = P2^4;
sbit ISDCE = P2^5;
//sbit key1=P1^0; //定义按键
sbit key2=P1^2; // add1 key
sbit key3=P1^1; //select key
sbit key4=P1^0; //playkey
uchar code str1[]={"Sum:0 Remain:120"};
uchar code str2[]={"Present:0 "};
uchar code str3[]={"PLAY MODE "};
uchar code str4[]={"Section:1 "};
uchar code str_clean[]={"0 "};
uint num; //存储时间并传送到显示屏
uint timerFlag=0,stopFlag = 0,over=0;
uint recordingFlag=0,playingFlag = 0;
uint count;
uint sum=0,remain=120;
uchar str_num[3]={""};
uchar str_count[3]={""};
uchar str_sum[3],str_remain[3];
//ISD25120
uint time=0;
uint add[25];
void delay(uint x)
{
uint y;
for(x;x>0;x--)
for(y=110;y>0;y--);
}
uint tiqu(uint number,char *p) //提取数组并放入字符串数组中,正序
{
int a,/*10的倍数*/b/*数组长度*/;
int c,/*临时盛放每一位数*/i/*添加的位置*/;
if (number == 0)
{
*p=0+48;
*(p+1)='\0';
return 0;
}
else
{
for(a=1,b=0;number / a;a*=10,b+=1);
for(a=1,i=0; number/a ;i++,a*=10)
{
c=number/a%10;
*(p+b-1-i)=c+48;
}
*(p+b)='\0'; //为数组添加结尾
}
return 0;
}
bit lcd_bz() // 读忙信号
{
bit result;
lcdrs=0; // 读忙信号
lcdrw=1;
lcdep=1;
_nop_();
_nop_();
_nop_();
_nop_();
result = (bit)(LCDSIG&0x80);
lcdep=0;
return result;
}
void lcd_wcmd (uchar cmd) //写命令
{
while (lcd_bz());
lcdrs=0;
lcdrw=0;
lcdep=0;
_nop_();
_nop_();
LCDSIG=cmd ;
_nop_();
_nop_();
_nop_();
_nop_();
lcdep=1;
_nop_();
_nop_();
_nop_();
_nop_();
lcdep=0;
}
void lcd_pos (uchar pos) //写位置
{
lcd_wcmd (pos|0x80);
}
void lcd_wdat (uchar dat) //写数据
{
while (lcd_bz());
lcdrs=1;
lcdrw=0;
lcdep=0;
_nop_();
_nop_();
LCDSIG=dat ;
_nop_();
_nop_();
_nop_();
_nop_();
lcdep=1;
_nop_();
_nop_();
_nop_();
_nop_();
lcdep=0;
}
void init () //初始化
{
//lcdinit
lcd_wcmd (0x38);
delay (1);
lcd_wcmd (0x0c);
delay (1);
lcd_wcmd (0x06);
delay (1);
lcd_wcmd (0x01);
delay (1);
//init timer
EA=1; //总中断打开
TMOD |= 0x01; //使用模式1,16位定时器,使用"|"符号可以在使用多个定时器时不受影响
TH0=(65536-50000)/256; //重新赋值
TL0=(65536-50000)%256;
ET0=1; //定时器中断打开
TR0=0;
//isd25120
}
void record()
{
ISDPR = 0;
ISDPD = 0;
ISDCE = 0;
}
void playback()
{
ISDPR = 1;
ISDPD = 0;
ISDCE = 0;
}
void delievr(int time)
{
int sect;
sect=time/0.2;
if(sect<256)
{ P3=sect; p16=0; p17=0; }
else
{
if(sect<512)
{ P3=sect-256; p16=1; p17=0; }
else
{ P3=sect-512; p16=0; p17=1; }
}
}
void show(char *p,uint pos)
{
int i=0;
lcd_pos (pos);
while (*(p+i)!='\0')
{
lcd_wdat (*(p+i));
i++;
}
}
void end()
{
tiqu(sum,str_sum);
show(str_sum,0x80+4);
tiqu(remain,str_remain);
show(str_remain,0x80+13);
}
void recordmode()
{
while(timerFlag)
{
if(over)
{
end();
recordingFlag=!recordingFlag;
over = 0;
}
tiqu(num,str_num);
show(str_num,0x40+8);
timerFlag=0;
}
if(stopFlag)
{
show(str_clean,0x80+13);
show(str_clean,0x40+8);
tiqu(sum,str_sum);
show(str_sum,0x80+4);
tiqu(remain,str_remain);
show(str_remain,0x80+13);
stopFlag = 0 ;
}
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void play()
{
while(playingFlag)
{
int i=1;
int count = 1 ;
show(str3,0);
show(str4,0x40);
while(i)
{
if(key4==0)
{
delay(5);
if(key4==0)
{
while(!key4);
///播放******
if(count==1)
{
delievr(add[0]);
playback();
delay(add[0]); 这里想delay数组中元素数值那么长时间,一直没有实现,不知道问题出在了哪里。
ISDPD=1; 上面playback函数可以使用,就是无法延时
}
else
{
delievr(add[count-1]-add[count-2]);
playback();
delay(add[count-1]-add[count-2]);
ISDPD=1;
}
}
}
if(key3==0)
{
delay(5);
if(key3==0)
{
while(!key3);
i=0;
playingFlag=0;
show(str1,0);
show(str2,0x40);
show(str_clean,0x80+13);
tiqu(sum,str_sum);
show(str_sum,0x80+4);
tiqu(remain,str_remain);
show(str_remain,0x80+13);
}
}
if(key2==0)
{
delay(5);
if(key2==0)
{
while(!key2);
if(count < sum)
{
count++;
tiqu(count,str_count);
show(str_count,0x40+8);
}
else
{
count = 1;
tiqu(count,str_count);
show(str_count,0x40+8);
}
}
}
}
}
}
void keyscan()
{
if(key4==0)
{
delay(5);
if(key4==0)
{
while(!key4);
recordingFlag = !recordingFlag;
if(recordingFlag)
{
TR0=1;
delievr(time);
record();
}
else
{
TR0=0;
stopFlag =1;
sum++;
remain-=num;
time+=num;
num=0;
////////
ISDPD=1;
add[sum-1]=time;
////////
}
}
}
if(!recordingFlag && key3 == 0)
{
delay(5);
if(key3==0)
{
while(!key3);
playingFlag=1;
}
}
}
void main ()
{
init ();
show(str1,0x80);
show(str2,0x40);
num=0;
while(1)
{
keyscan();
recordmode();
play();
}
}
void Timer0_isr(void) interrupt 1 using 1 //计时器中断程序
{
TH0=(65536-50000)/256; //重新赋值
TL0=(65536-50000)%256;
count++;
if(count==19)
{
count=0;
num++;
timerFlag=1;
}
if(num == remain)
{
TR0=0;
over=1;
sum++;
remain = 120;
}
}
|