這是程序內(nèi)容
#include <reg52.h>
#include "lcd1602.h"
#include "ds1302.h"
uchar phase; //電機(jī)4相的變換
uchar second ; //時(shí)間的計(jì)數(shù)
uchar change ; //設(shè)置電機(jī)轉(zhuǎn)速的中斷標(biāo)志
uchar state ; //中斷源的選擇狀態(tài)
uchar speed=40 ; //電機(jī)初始的速度
uchar speed_up,slow_down; //電機(jī)的加速與減速
uchar mot_spee1[]={" The Speed is "};
uchar mot_spee2[]={" 000 r/min "};
// uchar four_single[]={0x01,0x02,0x04,0x08}; //單4拍模式
uchar four_double[]={0x0c,0x06,0x03,0x09}; //雙4拍模式
void init_break() //中斷初始化函數(shù)
{
TMOD=0x11;
TH0=0x00;
TL0=0x00;
TH1=(65536-5000)/256; //定時(shí)5ms
TL1=(65536-5000)%256;
ET0=1;
ET1=1;
IT0=1;
EX0=1;
IT1=1;
EX1=1;
EA=1;
TR0=1;
TR1=1;
}
void main()
{
init_break();
lcd_init();
set_time();
while(1)
{
read_time();
display();
}
}
void int0()interrupt 0
{
state=P1;
}
void int1()interrupt 2
{
uchar value ; //電機(jī)的轉(zhuǎn)速
value=60000000/(second*65536+TH0*256+TL0)+1;
second=0;
TH0=0x00;
TL0=0x00;
mot_spee2[4]=value/100+'0';
mot_spee2[5]=value%100/10+'0';
mot_spee2[6]=value%100%10+'0';
}
void timer()interrupt 1
{
second++;
}
void timer1()interrupt 3
{
TH1=(65536-5000)/256; //定時(shí)5ms
TL1=(65536-5000)%256;
change++;
if(change>=speed)
{
change=0;
P2=four_double[phase]; //電機(jī)以雙4拍的模式轉(zhuǎn)動
// P2=single_four[phase]; //電機(jī)以單4拍的模式轉(zhuǎn)動
phase=++phase&0x03;
}
}
Ds1302
#include<reg52.h>
#include<intrins.h>
#include "ds1302.h"
extern uchar clk_xian1[]; //存放時(shí)鐘的數(shù)據(jù)
extern uchar clk_xian2[];
uchar time[] ={12,3,28,17,46,30};
uchar write_add[] ={0x8c,0x88,0x86,0x84,0x82,0x80}; //年月日時(shí)分秒的地址(寫)
uchar read_add [] ={0x8d,0x89,0x87,0x85,0x83,0x81}; //年月日時(shí)分秒的地址(讀)
void write_byte(uchar dat)
{
uchar i;
for(i=0;i<8;i++)
{
clk=0;
io=dat&0x01;
dat=dat>>1; //從數(shù)據(jù)的低位開始寫
clk=1;
}
}
void write(uchar add,uchar date)
{
rst=0;
_nop_();
clk=0;
_nop_();
rst=1;
_nop_();
write_byte(add);
write_byte(date);
rst=0;
_nop_();
clk=1;
io=1;
}
uchar read(uchar addr)
{
uchar i,temp;
rst=0;
_nop_();
clk=0;
_nop_();
rst=1;
_nop_();
write_byte(addr);
for(i=0;i<8;i++)
{
clk=0;
temp=temp>>1;
if(io)
temp=temp|0x80;
clk=1;
}
rst=0;
_nop_();
// clk=0;
_nop_();
// clk=1;
_nop_();
// io=0;
_nop_();
io=1;
return temp;
}
void set_time()
{
uchar i,j;
for(i=0;i<6;i++)
{
j=time/10; //數(shù)據(jù)轉(zhuǎn)換成BCD碼
time=time%10;
time=time+j*16;
}
write(0x8e,0x00); //去除寫保護(hù)
for(i=0;i<6;i++)
{
write(write_add,time);
}
write(0x8e,0x80); //加上寫保護(hù)
}
void read_time()
{
uchar i;
for(i=0;i<6;i++)
{
time=read(read_add);
}
clk_xian1[7]=time[0]%16+'0'; //時(shí)鐘數(shù)據(jù)送入液晶
clk_xian1[6]=time[0]/16+'0';
clk_xian1[10]=time[1]%16+'0';
clk_xian1[9]=time[1]/16+'0';
clk_xian1[13]=time[2]%16+'0';
clk_xian1[12]=time[2]/16+'0';
clk_xian2[7]=time[3]%16+'0';
clk_xian2[6]=time[3]/16+'0';
clk_xian2[10]=time[4]%16+'0';
clk_xian2[9]=time[4]/16+'0';
clk_xian2[13]=time[5]%16+'0';
clk_xian2[12]=time[5]/16+'0';
}
Lcd1632
#include<reg52.h>
#include"lcd1602.h"
extern uchar state ;
extern uchar speed ; //電機(jī)初始的速度
extern uchar speed_up,slow_down; //電機(jī)的加速與減速
extern uchar mot_spee1[];
extern uchar mot_spee2[];
uchar *t1,*t2; //換屏顯示
uchar clk_xian1[]="Date: 00-00-00 "; //初始時(shí)鐘
uchar clk_xian2[]="Time: 00:00:00 ";
void delay(uchar i)
{
uchar j,m;
for(j=0;j<=i;j++)
for(m=0;m<50;m++);
}
void write_com(uchar com)
{
rs=0;
rw=0;
P0=com;
delay(5);
e=1;
delay(5);
e=0;
}
void write_data(uchar date)
{
rs=1;
rw=0;
P0=date;
delay(5);
e=1;
delay(5);
e=0;
}
void lcd_init()
{
write_com(0x38);
write_com(0x06);
write_com(0x0c);
write_com(0x01);
}
void basic_display()
{
uchar i;
write_com(0x80);
for(i=0;*t1!=0;i++)
{
write_data(*t1);
t1++;
}
write_com(0xc0);
for(i=0;*t2!=0;i++)
{
write_data(*t2);
t2++;
}
}
void display()
{
switch(state)
{
case 0: t1=&mot_spee1; t2=&mot_spee2; break;
case 1: t1=&clk_xian1; t2=&clk_xian2; break;
case 2: speed_up =1; state=0; break;
case 3: slow_down =1; state=0; break;
}
if(speed_up) //電機(jī)的加速處理
{
speed--;
speed_up=0;
}
if(slow_down) //電機(jī)的減速處理
{
speed++;
slow_down=0;
}
basic_display();
}
歡迎光臨 (http://www.torrancerestoration.com/bbs/) | Powered by Discuz! X3.1 |