如果我把主程序及光
电子程序的AD_DATA[0]到AD_DATA[4]改为赋值变量,程序就可以,若不改,主程序中调到光电及视日不正确。#include<reg51.h>
#include<intrins.h>
#define uint unsigned int
#define uchar unsigned char
sbit CLK1=P0^7;
sbit CW1=P3^0;
sbit CLK2=P3^4;
sbit CW2=P1^7;
sbit OE = P1^4;
sbit EOC = P1^5;
sbit ST = P1^6;
sbit CLK = P1^3;
sbit SCK=P3^6;
sbit SDA=P3^5;
sbit RST=P3^7;
int k,i;
int m,e1,e2,e;
uint h,n;
uchar code LEDData[]=
{
0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f
};
uchar AD_DATA[5];
uchar
time_buf1[8] = {20,9,3,15,14,51,00,6};//空年月日时分秒周
uchar time_buf[8] ;//空年月日时分秒周
/***************************************
延时函数
/**************************************/
//void delayus(uchar a) //延迟
//{
// uchar i,j;
// for(i=0;i<a;i++)
// for(j=0;j<110;j++);
//}
void DelayMS(uint ms)
{
uchar i;
while(ms--)
{
for(i=0;i<120;i++);
}
}
/***************************************
时钟函数
/**************************************/
/*复位脚*/
#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
/*向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
/10;
time_buf=time_buf1%10;
time_buf=time_buf+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/16;
time_buf1=time_buf%16;
time_buf1=time_buf1+tmp*10;
}
}
/*DS302初始化函数*/
void ds1302_init(void) {
RST_CLR; /*RST脚置低*/
SCK_CLR; /*SCK脚置低*/
ds1302_write_byte(ds1302_sec_add,0x00);
}
/***************************************
数码管函数
**************************************/
void Display_Result(uchar d)
{
for(i=0;i<100;i++)
{
P3 = 0xf7;
P0 = LEDData[d%10];
DelayMS(5);
P3 = 0xfb;
P0 = LEDData[d%100/10];
DelayMS(5);
P3 = 0xfd;
P0 = LEDData[d/100];
DelayMS(5);
}
}
/************************************
AD转换程序
***************************************/
void adc0809(void)
{
TMOD = 0x02;
TH0 = 0x14;
TL0 = 0x00;
IE = 0x82;
TR0 = 1;
P1 = 0x7c;
ST = 0;
ST = 1;
ST = 0;
while(EOC == 0);
OE = 1;
AD_DATA[0]=P2;
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]=P2;
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]=P2;
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]=P2;
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]=P2;
Display_Result(AD_DATA[4]);
OE = 0;
DelayMS(500);
P1 = 0x78;
ST = 0;
ST = 1;
ST = 0;
while(EOC == 0);
OE = 1;
AD_DATA[4]=P2;
Display_Result(AD_DATA[4]);
OE = 0;
DelayMS(500);
}
void Timer0_INT() interrupt 1
{
CLK = !CLK;
}
void motor_foreward_forever()
{
CW1=1;
CLK1=0;
CW2=1;
CLK2=0;
while (1)
{ CLK1=!CLK1;
CLK2=!CLK2;
for(m=0;m<120;m++)
{
;
}
CLK1=!CLK1;
CLK2=!CLK2;
DelayMS(1);
}
}
void motor_reversal_forever()
{
CW1=0;
CLK1=0;
CW2=0;
CLK2=0;
while (1)
{ CLK1=!CLK1;
CLK2=!CLK2;
for(m=0;m<120;m++)
{
;
}
CLK1=!CLK1;
CLK2=!CLK2;
DelayMS(1);
}
}
/***************************************
视日跟踪系统电机控制
**************************************/
void motor_forever()
{
if((6<time_buf[4])&&(12>time_buf[4]))
{
motor_foreward_forever();
}
else
{
motor_reversal_forever();
}
}
void motor1_forward_appoint(uint n)
{ CW1=1;
CLK1=0;
i=1;
while (i<=1080*n)
{
CLK1=!CLK1;
DelayMS(1);
i++;
}
}
void motor1_reversal_appoint(uint n)
{
CW1=0;
CLK1=0;
i=1;
while (i<=1080*n)
{
CLK1=!CLK1;
DelayMS(1);
i++;
}
}
void motor2_forward_appoint(uint m)
{
CW2=1;
CLK2=0;
i=1;
while (i<=1080*m)
{
CLK2=!CLK2;
DelayMS(1);
i++;
}
}
void motor2_reversal_appoint(uint m)
{
CW2=0;
CLK2=0;
i=1;
while (i<=1080*m)
{
CLK2=!CLK2;
DelayMS(1);
i++;
}
}
/***************************************
光电跟踪系统电机控制
**************************************/
void motor_appoint()
{
uint n,m;
if((AD_DATA[1]>AD_DATA[3])&&((AD_DATA[1]-AD_DATA[3])>=e1))
{ n=AD_DATA[1]-AD_DATA[3];
motor1_forward_appoint(n);
}
else if((AD_DATA[1]<AD_DATA[3])&&((AD_DATA[3]-AD_DATA[1])>=e1))
{ n=AD_DATA[3]-AD_DATA[1];
motor1_reversal_appoint(n);
}
if((AD_DATA[2]>AD_DATA[4])&&((AD_DATA[2]-AD_DATA[4])>=e2))
{ m=AD_DATA[2]-AD_DATA[4];
motor2_forward_appoint(m);
}
else if((AD_DATA[2]<AD_DATA[4])&&((AD_DATA[4]-AD_DATA[2])>=e2))
{ m=AD_DATA[4]-AD_DATA[2];
motor2_reversal_appoint(m);
}
}
void main(void)
{ e=151;
e1=5;
e2=5;
ds1302_init();
DelayMS(10);
ds1302_write_time();
DelayMS(10);
while(1)
{
ds1302_read_time();
DelayMS(10);
if((6<time_buf1[4])&&(18>time_buf1[4]))
{adc0809();
if(AD_DATA[0]>e)
motor_appoint(); //光电
else motor_forever(); //视日
}
else {};
}
}
一周热门 更多>