完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
扫一扫,分享给好友
#include
#include #include #include #include sfr ADC_CONTR = 0xBC; //ADC control register sfr ADC_RES = 0xBD; //ADC hight 8-bit result register sfr ADC_RESL = 0xBE; //ADC low 2-bit result register sfr P1ASF = 0x9D; //P1 secondary function control register ***it key1 = P3^2; //定义 选择键 ***it key2 = P3^3; //定义 加 按键端口 ***it key3 = P3^4; //定义 减 按键端口 ***it key4 = P3^5; //定义 启动时间 按键端口 ***it key5 = P3^6; //定义 停止时间 按键端口 ***it pwm0 = P2^0; //纵向舵机驱动端口 ***it pwm1 = P2^1; //横向舵机驱动端口 #define uchar unsigned char //宏定义 unsigned char字符为uchar #define uint unsigned int //宏定义 unsigned int #define ulint unsigned long int //宏定义 /*定义相关特殊功能寄存器*/ #define ADC_POWER 0x80 //ADC power control bit #define ADC_FLAG 0x10 //ADC complete flag #define ADC_START 0x08 //ADC start control bit #define ADC_SPEEDLL 0x00 //540 clocks #define ADC_SPEEDL 0x20 //360 clocks #define ADC_SPEEDH 0x40 //180 clocks #define ADC_SPEEDHH 0x60 //90 clocks idata uint guangmin_0=0,guangmin_1=0,guangmin_2=0,guangmin_3=0; //光敏值变量 uchar idata Temp[16]="13-04-30 : G "; uchar idata Test[16]="19:14:00 : K "; uchar idata Temp1[16]="X: Y: "; uchar idata Test1[16]="guangzhao: "; uchar idata Temp2[16]=" G0 G1 "; uchar idata Test2[16]=" G2 G3 "; idata uint nian,yue,ri,shi,fen,miao=0; idata uint xiay; idata int _shi=18,_fen=30; //停止时间变量 idata int shi_=6,fen_=30; //启动时间变量 idata uchar set=0,set1=0,set2=0; //模式控制变量 idata uchar xian=0; //显示模式标志变量 idata uint GZ; //光照变量 idata uchar X,Y; //角度变量 idata uint js=0; //标志位变量 idata uint b=107,d=107; //角度控制变量 idata float high,dir;//太阳高度角,方位角 void delay(uint x) /////ms延时函数 { xdata uint i,j; for(i=0;i } void Delay1us() //@12.000MHz { _nop_(); _nop_(); _nop_(); _nop_(); } void InitADC() { P1ASF = 0xf0; //Set all P1 as analog input port 0xff=1111 1111B 即P1全部用作AD,使用时根据实际情况赋值 ADC_RES = 0; //清空转换结果存储寄存器 ADC_RESL= 0; ADC_CONTR = 0x00; Delay1us();//等待ADC_CONTR值写入 _nop_(); _nop_(); _nop_(); _nop_(); } unsigned int GetADC(unsigned char ch,unsigned char speed) { unsigned int res; ADC_CONTR =ADC_CONTR | ADC_POWER | speed | ADC_START | ch; //与 Delay1us();//确保ADC_CONTR的值写入nop; nop; nop; nop; while(!(ADC_CONTR & ADC_FLAG)); //如果AD转换未结束FLAG位为0,程序在此等待,如果为1,跳出循环 res=ADC_RES*4+ADC_RESL; //读AD转换结果,公式自己理解 ADC_RES=0; ADC_RESL=0; ADC_CONTR=0; //寄存器复位 return res; } void readtime() { nian= BCD_Decimal(read_1302(0x8d)); yue = BCD_Decimal(read_1302(0x89)); ri = BCD_Decimal(read_1302(0x87)); shi = BCD_Decimal(read_1302(0x85)); fen = BCD_Decimal(read_1302(0x83)); miao= BCD_Decimal(read_1302(0x81)); } uchar key_bcd(uchar key_decimal) //转成ds1302所需的BCD码 { uchar temp; temp=(((key_decimal/10)&0x0f)<<4)|(key_decimal%10); return temp; } void keyscan() { //-----------------------------模式键key1---------------------------- if(key1==0&&xian==0) { delay(5); if(key1==0) { delay(10); while(!key1); set++; set1=0; set2=0; if(set>6)set=0; } } //-----------------------------模式键key4---------------------------- if(key4==0&&xian==0) { delay(5); if(key4==0) { delay(10); while(!key4); set=0; set1++; set2=0; if(set1>2)set1=0; } } //-----------------------------模式键key5---------------------------- if(key5==0&&xian==0) { delay(5); if(key5==0) { delay(10); while(!key5); set=0; set1=0; set2++; if(set2>2)set2=0; } } //-----------------------------key2---------------------------- if(key2==0) { delay(2); if(key2==0) { delay(5); while(!key2); switch(set) { case 1: nian++; if(nian>99)nian=0; write_1302(0x8e,0x00); write_1302(0x8c,key_bcd(nian)| 0x80); write_1302(0x8e,0x80); break; case 2: yue++; if(yue>12)yue=1; write_1302(0x8e,0x00); write_1302(0x88,key_bcd(yue)); write_1302(0x8e,0x80); break; case 3: ri++; if(ri>31)ri=1; write_1302(0x8e,0x00); write_1302(0x86,key_bcd(ri)); write_1302(0x8e,0x80); break; case 4: shi++; if(shi>23)shi=0; write_1302(0x8e,0x00); write_1302(0x84,key_bcd(shi)); write_1302(0x8e,0x80); break; case 5: fen++; if(fen>59)fen=0; write_1302(0x8e,0x00); write_1302(0x82,key_bcd(fen)); write_1302(0x8e,0x80); break; case 6: miao++; if(miao>59)miao=0; write_1302(0x8e,0x00); write_1302(0x80,key_bcd(miao)&0x7f); write_1302(0x8e,0x80); break; } switch(set1) { case 1: _shi++; if(_shi>23)_shi=23; break; case 2: _fen++; if(_fen>59)_fen=0; break; } switch(set2) { case 1: shi_++; if(shi_>_shi)shi_=_shi; break; case 2: fen_++; if(fen_>59)fen_=0; break; } } } //-----------------------------key3---------------------------- if(key3==0) { delay(2); if(key3==0) { delay(5); while(!key3); if(set==0&&set1==0&&set2==0) { xian++; if(xian>2)xian=0; b++;d++; } switch(set) { case 1: nian--; if(nian>99)nian=99; write_1302(0x8e,0x00); write_1302(0x8c,key_bcd(nian)| 0x80); write_1302(0x8e,0x80); break; case 2: yue--; if(yue>12)yue=12; write_1302(0x8e,0x00); write_1302(0x88,key_bcd(yue)); write_1302(0x8e,0x80); break; case 3: ri--; if(ri>31)ri=31; write_1302(0x8e,0x00); write_1302(0x86,key_bcd(ri)); write_1302(0x8e,0x80); break; case 4: shi--; if(shi>23)shi=23; write_1302(0x8e,0x00); write_1302(0x84,key_bcd(shi)); write_1302(0x8e,0x80); break; case 5: fen--; if(fen>59)fen=59; write_1302(0x8e,0x00); write_1302(0x82,key_bcd(fen)); write_1302(0x8e,0x80); break; case 6: miao--; if(miao>59)miao=59; write_1302(0x8e,0x00); write_1302(0x80,key_bcd(miao)&0x7f); write_1302(0x8e,0x80); break; } switch(set1) { case 1: _shi--; if(_shi case 2: _fen--; if(_fen<0)_fen=59; break; } switch(set2) { case 1: shi_--; if(shi_<0)shi_=0; break; case 2: fen_--; if(fen_<0)fen_=59; break; } } } } void suntime() { xdata float JD=112; xdata float JF=32.82; xdata float R=37.958; xdata float pi=3.1416;//π值 xdata float K,N0,j,c; xdata float G,L,H,N,U;///定义日角,误差时间,北京时间,积日,角度 xdata float Q,V,Et,Sd,D,O;//赤纬角,时差系数,时差,地方时,真太阳时,真太阳时角 K=2*pi/365.2422; N0=79.6764+0.2422*(nian-1985)-(int)((nian-1985)/4); j=nian/4-(int)(nian/4); //判断是否闰年 if(yue<=2) c=30.6; if((j==0)&&(yue>2)) c=31.8; else c=32.8; G=(int)(30.6*yue-c+0.5)+ri;//计算当天积日、日角 L=(JD+JF/60)/15; //当地与北京时间的误差 H=shi-8+fen/60; //北京准确时间 N=G+(H-L)/24; //得到积日 U=(N-N0)*K; //经度订正,时刻订正 Q=0.3723+23.2567*sin(U)+0.1149*sin(2*U)-0.1712*sin(3*U)-0.758*cos(U)+0.3656*cos(2*U)+0.0201*cos(3*U);//太阳赤纬角 V=2*pi*(N+1096)/365.25; Et=0.012522+0.557214*cos(V)-7.3314*sin(V)-3.13548*cos(2*V)-9.4188*sin(2*V)-0.078462*cos(3*V)-0.309612*sin(3*V)-0.131202*cos(4*V)-0.178938*sin(4*V)-0.00906*cos(5*V)-0.0140778*sin(5*V);//时差 Sd=shi+(fen-(120-(JD+JF/60))*4)/60; //地方时 D=Sd+Et/60; //真太阳时 O=(D-12)*15; //真太阳时角 high=asin(sin(Q)*sin(R)+cos(Q)*cos(R)*cos(O)); dir=acos((sin(high)*sin(O)-sin(Q))/(cos(high)*cos(R))); //太阳高度角、方位角计算 } uchar ttt; void display() { if(xian==0) //显示模式0 { ttt++; if(ttt>5)ttt=0; if(set==1) { if(ttt>2) { Temp[0]=nian/10+'0'; Temp[1]=nian%10+'0'; } else { Temp[0]=' '; Temp[1]=' '; } } else { Temp[0]=nian/10+'0'; Temp[1]=nian%10+'0'; } if(set==2) { if(ttt>2) { Temp[3]=yue/10+'0'; Temp[4]=yue%10+'0'; } else { Temp[3]=' '; Temp[4]=' '; } } else { Temp[3]=yue/10+'0'; Temp[4]=yue%10+'0'; } if(set==3) { if(ttt>2) { Temp[6]=ri/10+'0'; Temp[7]=ri%10+'0'; } else { Temp[6]=' '; Temp[7]=' '; } } else { Temp[6]=ri/10+'0'; Temp[7]=ri%10+'0'; } if(set1==1) { if(ttt>2) { Temp[9]=_shi/10+'0'; Temp[10]=_shi%10+'0'; } else { Temp[9]=' '; Temp[10]=' '; } } else { Temp[9]=_shi/10+'0'; Temp[10]=_shi%10+'0'; } if(set1==2) { if(ttt>2) { Temp[12]=_fen/10+'0'; Temp[13]=_fen%10+'0'; } else { Temp[12]=' '; Temp[13]=' '; } } else { Temp[12]=_fen/10+'0'; Temp[13]=_fen%10+'0'; } ShowString(0,Temp); if(set==4) { if(ttt>2) { Test[0]=shi/10+'0'; Test[1]=shi%10+'0'; } else { Test[0]=' '; Test[1]=' '; } } else { Test[0]=shi/10+'0'; Test[1]=shi%10+'0'; } if(set==5) { if(ttt>2) { Test[3]=fen/10+'0'; Test[4]=fen%10+'0'; } else { Test[3]=' '; Test[4]=' '; } } else { Test[3]=fen/10+'0'; Test[4]=fen%10+'0'; } if(set==6) { if(ttt>2) { Test[6]=miao/10+'0'; Test[7]=miao%10+'0'; } else { Test[6]=' '; Test[7]=' '; } } else { Test[6]=miao/10+'0'; Test[7]=miao%10+'0'; } if(set2==1) { if(ttt>2) { Test[9]=shi_/10+'0'; Test[10]=shi_%10+'0'; } else { Test[9]=' '; Test[10]=' '; } } else { Test[9]=shi_/10+'0'; Test[10]=shi_%10+'0'; } if(set2==2) { if(ttt>2) { Test[12]=fen_/10+'0'; Test[13]=fen_%10+'0'; } else { Test[12]=' '; Test[13]=' '; } } else { Test[12]=fen_/10+'0'; Test[13]=fen_%10+'0'; } ShowString(1,Test); } if(xian==1) //显示模式1 { Y=b; X=d; GZ=(guangmin_0+guangmin_1+guangmin_2+guangmin_3)/4; if(X<=107) { X=107-X; Temp1[3]='-'; //角度数值转换显示 Temp1[4]=X/100%10+'0'; Temp1[5]=X/10%10+'0'; Temp1[6]=X%10+'0'; } if(X>=107) { X=X-107; Temp1[3]='+'; //角度数值转换显示 Temp1[4]=X/100%10+'0'; Temp1[5]=X/10%10+'0'; Temp1[6]=X%10+'0'; } if(Y<=107) { Y=107-Y; Temp1[11]='-'; //角度数值转换显示 Temp1[12]=Y/100%10+'0'; Temp1[13]=Y/10%10+'0'; Temp1[14]=Y%10+'0'; } if(Y>=107) { Y=Y-107; Temp1[11]='+'; //角度数值转换显示 Temp1[12]=Y/100%10+'0'; Temp1[13]=Y/10%10+'0'; Temp1[14]=Y%10+'0'; } Test1[10]=GZ/1000+'0'; //光敏数值转换显示 Test1[11]=GZ/100%10+'0'; Test1[12]=GZ/10%10+'0'; Test1[13]=GZ%10+'0'; ShowString(0,Temp1); ShowString(1,Test1); } if(xian==2) //显示模式2 { Temp2[4]=guangmin_0/1000+'0'; //光敏数值转换显示 Temp2[5]=guangmin_0/100%10+'0'; Temp2[6]=guangmin_0/10%10+'0'; Temp2[7]=guangmin_0%10+'0'; Temp2[11]=guangmin_1/1000+'0'; //光敏数值转换显示 Temp2[12]=guangmin_1/100%10+'0'; Temp2[13]=guangmin_1/10%10+'0'; Temp2[14]=guangmin_1%10+'0'; Test2[4]=guangmin_2/1000+'0'; //光敏数值转换显示 Test2[5]=guangmin_2/100%10+'0'; Test2[6]=guangmin_2/10%10+'0'; Test2[7]=guangmin_2%10+'0'; Test2[11]=guangmin_3/1000+'0'; //光敏数值转换显示 Test2[12]=guangmin_3/100%10+'0'; Test2[13]=guangmin_3/10%10+'0'; Test2[14]=guangmin_3%10+'0'; ShowString(0,Temp2); ShowString(1,Test2); } } void timer0_irpt()interrupt 1 //中断1服务程序 { // TH0 = (65536-10)/256; //重装计数值 // TL0 = (65536-10)%256; //重装计数值 js++; //js自加1 if(js==2000) //如果js等于2000 { pwm0=1; pwm1=1; //纵向舵机驱动端口取反 js=0; //把js置0 } else //否则判断js是否等于b { if(js==b) {pwm0=0;} //如果等于就把纵向舵机驱动端口取反 if(js==d) {pwm1=0;} } } void chushihua() //内部中断初始化函数 { TMOD = 0x01; //将计数器设置为工作方式1 TH0 = (65536-10)/256; //给计数器高八位赋值 TL0 = (65536-10)%256; //给计数器低八位赋值 EA = 1; //开总中断 ET0 = 1; //开中断1 TR0 = 1; } void pianzhuankongzhi(void) { // if(xiay==1) // { // GZ=(guangmin_0+guangmin_1+guangmin_2+guangmin_3)/4; // if(GZ>50) // { // if(((guangmin_0+guangmin_1)-10)<(guangmin_2+guangmin_3)) // { // b++; // if(b>140){b=140;} // } // if(((guangmin_2+guangmin_3)-10)<(guangmin_0+guangmin_1)) // { // b--; // if(b<45){b=45;} // } // Delay1us(); // if(((guangmin_1+guangmin_2)-10)<(guangmin_3+guangmin_0)) // { // d--; // if(d<45){d=45;} // } // if(((guangmin_3+guangmin_0)-10)<(guangmin_1+guangmin_2)) // { // d++; // if(d>170){d=170;} // } // } // else // { suntime(); if(dir d--; if(d<45){d=45;} } if(d d++; if(d>170){d=170;} } Delay1us(); if(b b++; if(b>140){b=140;} } if(high { b--; if(b<45){b=45;} } // if(b>140){b=140;} // if(b<45){b=45;} // if(d<45){d=45;} // if(d>170){d=170;} // } // } // else // { // b=107; // d=107; // // } } void main(void) { InitADC(); //ADC初始化 InitLcd_LCD1602(); //液晶初始化 ds1302_init(); //DS1302初始化 chushihua(); //PWM中断初始化 xiay=1; while(1) { readtime(); // if(((shi*60+fen)>=(shi_*60+fen_))&&((shi*60+fen)<=(_shi*60+_fen-1))) //判断时间段开跟踪 { // guangmin_0=GetADC(2,ADC_SPEEDLL); // guangmin_3=GetADC(1,ADC_SPEEDLL); // // guangmin_2=GetADC(0,ADC_SPEEDLL); // guangmin_1=GetADC(3,ADC_SPEEDLL); pianzhuankongzhi(); //舵机控制 EA=1; } // else // { // EA=0; // } display(); //显示控制 keyscan(); //按键控制 } } |
|
相关推荐
1个回答
|
|
怎么仿真的?在什么地方仿真的
|
|
|
|
你正在撰写答案
如果你是对答案或其他答案精选点评或询问,请使用“评论”功能。
726 浏览 0 评论
777 浏览 1 评论
基于瑞萨FPB-RA4E2智能床头灯项目——1编译环境搭建与点亮驱动ws2812全彩LED
680 浏览 0 评论
嵌入式学习-飞凌嵌入式ElfBoard ELF 1板卡-LCD显示图片编程示例之介绍mmap
1149 浏览 0 评论
《DNESP32S3使用指南-IDF版_V1.6》第二章 常用的C语言知识点
1120 浏览 0 评论
【youyeetoo X1 windows 开发板体验】少儿AI智能STEAM积木平台
11800 浏览 31 评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-27 06:51 , Processed in 0.543601 second(s), Total 73, Slave 55 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号