#include<reg51.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
sbit CLK1=P2^4;
sbit CW1=P2^5;
sbit CLK2=P2^6;
sbit CW2=P2^7;
int k,i;
int m,n,e1,e2,e;
uint h;
/***************************************
延时函数
/**************************************/
delay(){
for(m=0;m<10000;m++){;}
for(m=0;m<10000;m++){;}
for(m=0;m<10000;m++){;}
for(m=0;m<10000;m++){;}
for(m=0;m<10000;m++){;}
}
/***************************************
时钟函数
/**************************************/
void ds1302(void)
{ sbit SCK=P0^7;
sbit SDA=P2^0;
sbit RST=P1^7;
/*复位脚*/
#define RST_CLR RST=0/*电平置低*/
#define RST_SET RST=1/*电平置高*/
/*双向数据*/
#define IO_CLR SDA=0/*电平置低*/
#define IO_SET SDA=1/*电平置高*/
#define IO_R SDA/*电平读取*/
/*时钟信号*/
#define SCK_CLR SCK=0/*时钟信号*/
#define SCK_SET SCK=1/*电平置高*/
#define ds1302_sec_add 0x80 //秒数据地址
#define ds1302_min_add 0x82 //分数据地址
#define ds1302_hr_add 0x84 //时数据地址
#define ds1302_date_add 0x86 //日数据地址
#define ds1302_month_add 0x88 //月数据地址
#define ds1302_day_add 0x8a //星期数据地址
#define ds1302_year_add 0x8c //年数据地址
#define ds1302_control_add 0x8e //控制数据地址
#define ds1302_charger_add 0x90
#define ds1302_clkburst_add 0xbe
uchar
time_buf1[8] = {20,9,3,15,6,51,00,6};//空年月日时分秒周
uchar time_buf[8] ;//空年月日时分秒周
ds1302_init();
ds1302_write_time();
while(1)
{
ds1302_read_time();
}
}
void delayus(uchar a) //延迟
{
uchar i,j;
for(i=0;i<a;i++)
for(j=0;j<110;j++);
}
/*向DS1302写入一字节数据*/
void ds1302_write_byte( uchar addr, uchar d) {
unsigned char i;
RST_SET; /*启动DS1302总线*/
/*写入目标地址:addr*/
addr = addr & 0xFE;/*最低位置零*/
for (i = 0; i < 8; i ++) {
if (addr & 0x01) {
IO_SET;
}
else {
IO_CLR;
}
SCK_SET;
SCK_CLR;
addr = addr >> 1;
}
/*写入数据:d*/
for (i = 0; i < 8; i ++) {
if (d & 0x01) {
IO_SET;
}
else {
IO_CLR;
}
SCK_SET;
SCK_CLR;
d = d >> 1;
}
RST_CLR; /*停止DS1302总线*/
}
/*从DS1302读出一字节数据*/
uchar ds1302_read_byte(uchar addr) {
unsigned char i;
unsigned char temp;
RST_SET; /*启动DS1302总线*/
/*写入目标地址:addr*/
addr = addr | 0x01;/*最低位置高*/
for (i = 0; i < 8; i ++) {
if (addr & 0x01) {
IO_SET;
}
else {
IO_CLR;
}
SCK_SET;
SCK_CLR;
addr = addr >> 1;
}
/*输出数据:temp*/
for (i = 0; i < 8; i ++) {
temp = temp >> 1;
if (IO_R) {
temp |= 0x80;
}
else {
temp &= 0x7F;
}
SCK_SET;
SCK_CLR;
}
RST_CLR; /*停止DS1302总线*/
return temp;
}
/*向DS302写入时钟数据*/
void ds1302_write_time(void) {
unsigned char i,tmp;
for(i=0;i<8;i++){ //BCD处理
tmp=time_buf1[i]/10;
time_buf[i]=time_buf1[i]%10;
time_buf[i]=time_buf[i]+tmp*16;
}
ds1302_write_byte(ds1302_control_add,0x00); //关闭写保护
ds1302_write_byte(ds1302_sec_add,0x80); //暂停
//ds1302_write_byte(ds1302_charger_add,0xa9); //涓流充电
ds1302_write_byte(ds1302_year_add,time_buf[1]); //年
ds1302_write_byte(ds1302_month_add,time_buf[2]); //月
ds1302_write_byte(ds1302_date_add,time_buf[3]); //日
ds1302_write_byte(ds1302_day_add,time_buf[7]); //周
ds1302_write_byte(ds1302_hr_add,time_buf[4]); //时
ds1302_write_byte(ds1302_min_add,time_buf[5]); //分
ds1302_write_byte(ds1302_sec_add,time_buf[6]); //秒
ds1302_write_byte(ds1302_day_add,time_buf[7]); //周
ds1302_write_byte(ds1302_control_add,0x80); //打开写保护
}
/*从DS302读出时钟数据*/
void ds1302_read_time()
{
unsigned char i,tmp;
time_buf[1]=ds1302_read_byte(ds1302_year_add); //年
time_buf[2]=ds1302_read_byte(ds1302_month_add); //月
time_buf[3]=ds1302_read_byte(ds1302_date_add); //日
time_buf[4]=ds1302_read_byte(ds1302_hr_add); //时
time_buf[5]=ds1302_read_byte(ds1302_min_add); //分
time_buf[6]=(ds1302_read_byte(ds1302_sec_add))&0x7F;//秒
time_buf[7]=ds1302_read_byte(ds1302_day_add); //周
for(i=0;i<8;i++)
{ //BCD处理
tmp=time_buf[i]/16;
time_buf1[i]=time_buf[i]%16;
time_buf1[i]=time_buf1[i]+tmp*10;
}
}
/*DS302初始化函数*/
void ds1302_init(void) {
RST_CLR; /*RST脚置低*/
SCK_CLR; /*SCK脚置低*/
ds1302_write_byte(ds1302_sec_add,0x00);
}
/************************************
AD转换程序
***************************************/
void adc0809(void)
{
uchar code LEDData[]=
{
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f
};
sbit OE = P1^4;
sbit EOC = P1^5;
sbit ST = P1^6;
sbit CLK = P1^3;
uchar AD_DATA[5];
int i;
TMOD = 0x02;
TH0 = 0x14;
TL0 = 0x00;
IE = 0x82;
TR0 = 1;
while(1)
{
P1 = 0x7c;
ST = 0;
ST = 1;
ST = 0;
while(EOC == 0);
OE = 1;
AD_DATA[0]=P3;
Display_Result(AD_DATA[0]);
OE = 0;
DelayMS(500);
P1 = 0x7b;
ST = 0;
ST = 1;
ST = 0;
while(EOC == 0);
OE = 1;
AD_DATA[1]=P3;
Display_Result(AD_DATA[1]);
OE = 0;
DelayMS(500);
P1 = 0x7a;
ST = 0;
ST = 1;
ST = 0;
while(EOC == 0);
OE = 1;
AD_DATA[2]=P3;
Display_Result(AD_DATA[2]);
OE = 0;
DelayMS(500);
P1 = 0x79;
ST = 0;
ST = 1;
ST = 0;
while(EOC == 0);
OE = 1;
AD_DATA[3]=P3;
Display_Result(AD_DATA[3]);
OE = 0;
DelayMS(500);
P1 = 0x78;
ST = 0;
ST = 1;
ST = 0;
while(EOC == 0);
OE = 1;
AD_DATA[4]=P3;
Display_Result(AD_DATA[4]);
OE = 0;
DelayMS(500);
}
}
void Timer0_INT() interrupt 1
{
CLK = !CLK;
}
void DelayMS(uint ms)
{
uchar i;
while(ms--)
{
for(i=0;i<120;i++);
}
}
/***************************************
数码管函数
**************************************/
void Display_Result(uchar d)
{
for(i=0;i<100;i++)
{
P2 = 0xf7;
P0 = LEDData[d%10];
DelayMS(5);
P2 = 0xfb;
P0 = LEDData[d%100/10];
DelayMS(5);
P2 = 0xfd;
P0 = LEDData[d/100];
DelayMS(5);
}
}
void motor_foreward_forever()
{
time=1;
TMOD=0x01;
CW1=1;
CLK1=0;
CW2=1;
CLK2=0;
delay();
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
EA=1;
ET0=1;
TR0=1;
do {}
while (1);
}
void motor_reversal_forever()
{
time=1;
TMOD=0x01;
CW1=0;
CLK1=0;
CW2=0;
CLK2=0;
delay();
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
EA=1;
ET0=1;
TR0=1;
do {}
while (1);
}
/***************************************
视日跟踪系统电机控制
**************************************/
void motor_forever()
{
void timer0(void) interrupt 1 using 1
{
TR0=0;
CLK1=!CLK1;
CLK2=!CLK2;
for(m=0;m<30;m++)
{
;
}
CLK1=!CLK1;
CLK2=!CLK2;
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
TR0=1;
}
if(6<time_buf[4]<12)
{
motor_foreward_forever();
}
else
{
motor_reversal_forever();
}
}
void motor1_forward_appoint()
{
TMOD=0x01;
CW1=0;
CLK1=0;
i=1;
delay();
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
EA=1;
ET0=1;
TR0=1;
do {}
while (1);
}
void motor1_reversal_appoint()
{
TMOD=0x01;
CW1=1;
CLK1=0;
i=1;
delay();
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
EA=1;
ET0=1;
TR0=1;
do {}
while (1);
}
void motor2_forward_appoint()
{
TMOD=0x01;
CW2=0;
CLK2=0;
i=1;
delay();
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
EA=1;
ET0=1;
TR0=1;
do {}
while (1);
}
void motor2_reversal_appoint()
{
TMOD=0x01;
CW2=1;
CLK2=0;
i=1;
delay();
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
EA=1;
ET0=1;
TR0=1;
do {}
while (1);
}
/***************************************
光电跟踪系统电机控制
**************************************/
void motor_appoint()
{
if((AD_DATA[1]>AD_DATA[3])&&((AD_DATA[1]-AD_DATA[3])>=e1))
{ n=AD_DATA[1]-AD_DATA[3];
void timer0(void) interrupt 1 using 1
{
if (i<=1080*n) /*脉冲数*/
{
TR0=0;
CLK1=!CLK1;
for(m=0;m<30;m++)
{
;
}
CLK1=!CLK1;
i++;
}
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
TR0=1;
}
motor1_forward_appoint();
}
else if((AD_DATA[1]<AD_DATA[3])&&((AD_DATA[3]-AD_DATA[1])>=e1))
{ n=AD_DATA[3]-AD_DATA[1];
void timer0(void) interrupt 1 using 1
{
if (i<=1080*n) /*脉冲数*/
{
TR0=0;
CLK1=!CLK1;
for(m=0;m<30;m++)
{
;
}
CLK1=!CLK1;
i++;
}
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
TR0=1;
}
motor1_reversal_appoint();
}
if((AD_DATA[2]>AD_DATA[4])&&((AD_DATA[2]-AD_DATA[4])>=e2))
{ n=AD_DATA[2]-AD_DATA[4];
void timer0(void) interrupt 1 using 1
{
if (i<=1080*n) /*脉冲数*/
{
TR0=0;
CLK2=!CLK2;
for(m=0;m<30;m++)
{
;
}
CLK2=!CLK2;
i++;
}
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
TR0=1;
}
motor2_forward_appoint();
}
else if((AD_DATA[2]<AD_DATA[4])&&((AD_DATA[4]-AD_DATA[2])>=e2))
{ n=AD_DATA[4]-AD_DATA[2];
void timer0(void) interrupt 1 using 1
{
if (i<=1080*n) /*脉冲数*/
{
TR0=0;
CLK=!CLK;
for(m=0;m<30;m++)
{
;
}
CLK2=!CLK2;
i++;
}
TH0=(65536-1000)/256;
TL0=(65536-1000)%256;
TR0=1;
}
motor2_reversal_appoint();
}
}
main(void)
{ e=150;
e1=5;
e2=5;
ds1302();
if(6<time_buf1[4]<18)
{adc0809();
if(AD_DATA[0]<e)
motor_forever();
else motor_appoint();
}
else end
}
一周热门 更多>