您当前的位置:五五电子网电子知识单片机-工控设备AVR单片机AVR单片机万年历程序 正文
AVR单片机万年历程序

AVR单片机万年历程序

点击数:7784 次   录入时间:03-04 11:39:06   整理:http://www.55dianzi.com   AVR单片机
                (*Flag)|=0x80;         //当前位加上小数点显示
                (*(Flag+1))&=0x7f;     //下一位去掉小数点显示
                break;
          case 0x40:Enter_Flag=1;break;//"确定"键处理
        //  case 0x80:Enter_Flag=1;break;//测试用
          default:break;}
        
       }
     Enter_Flag=0;
     temp3[0]=((temp1[11]<<4)|temp1[12])&0x7f; //分数据处理,去掉小数点
     temp3[1]=((temp1[9]<<4)|temp1[10])&0x7f;  //时数据处理,去掉小数点
     if((temp3[0]<0x60)&&(temp3[1]<0x24))      //判断是否为合法时间
       {Write_1byte(SD2303,0x0e,0b10110000);   //开启闹铃使能
        Write_1byte(SD2303,0x0f,0b00101000);   //开始中断使能
        Write_Nbyte(SD2303,0x08,temp3,3);}     //初始化闹铃设置
     else  
        Write_1byte(SD2303,0x0e,0b00110000);   //取消闹铃功能
    }
  }
}
 
 
//***************************DS1722函数开始************************//

void Convert(uchar p1,uchar *p2)
{uchar temp;
 if(p1&0x80)   
   {p2[13]=10;               //零下温度用10表示                    
    p1=0xff-p1;}             //温度绝对值
 else
   p2[13]=11;                //零上温度用11表示
   p2[14]=p1/10;             //温度十位
   p2[15]=p1-p2[14]*10;      //温度个位
}

//***************************通过SPI接口写数据函数*****************//
//函数参数说明:
//Address:DS1722寄存器地址
//Data:写入的数据
void DSWrite_Byte(uchar Address,uchar Data)
{uchar clear;
 PORTB=PORTB|0x10;           //使能SPI器件
 SPDR=Address;
 while(!(SPSR&0x80)) {;}
 clear=SPSR;
 clear=SPDR;
 SPDR=Data;
 while(!(SPSR&0x80)) {;}
 clear=SPSR;
 clear=SPDR;
 PORTB=PORTB&0xef;
}


//***************************通过SPI接口读数据函数*****************//
//函数参数说明:
//Address:DS1722寄存器地址
//返回值:读取的温度数据
uchar DSRead_Byte(uchar Address)
{uchar clear;
 uchar Data;
 PORTB=PORTB|0x10;
 SPDR=Address;
 while(!(SPSR&0x80)) {;}
 clear=SPSR;
 clear=SPDR;
 SPDR=Address;               //发空数据
 while(!(SPSR&0x80)) {;}
 clear=SPSR;
 Data=SPDR;
 PORTB=PORTB&0xef;
 return(Data);
}
 
void main(void)
{uchar i;
 uchar time[16]={2,0,0,4,1,2,2,7,1,1,7,3,0,0,0,0};
 uchar Set_Time[7]={0x50,0x30,0x20,0x03,0x05,0x01,0x05};
     //初始化时间   2005-01-05 Wed 20-30-50
 uchar SD,DS; 
 uchar SD2303_Controller1=0x00;              //禁止中断
 uchar SD2303_Controller2=0x20;              //时间格式:24小时制
 DDRA=0xff;                                  //A口输出
 DDRC=0xff;                                  //C口输出
 DDRD=0x80;                                  //D口最高位输出,其他带上拉输入
 PORTD=0x7F;
 DDRB=0xF3;                                  //B口带第2、3位带上拉输入,其他输出
 PORTB=0xF3;
 Write_1byte(SD2303,0x0e,SD2303_Controller1);//写控制字
 Write_1byte(SD2303,0x0f,SD2303_Controller2);//写控制字
 Write_Nbyte(SD2303,0x00,Set_Time,7);        //初始化时间
 SPCR=0b01011100;
 DSWrite_Byte(0x80,0xf0);                      //DS1722 8位分辨率自由转换模式
 while(1)
    {Key_Process();                          //按键处理        
     Read_Nbyte(SD2303,0x00,Set_Time,7);     //读取时间
     time[2]=Set_Time[6]>>4;                 //年高位;
     time[3]=Set_Time[6]&0x0f;               //年低位;
     time[4]=Set_Time[5]>>4;                 //月高位;
     time[5]=Set_Time[5]&0x0f;               //月低位;
     time[6]=Set_Time[4]>>4;                 //日高位;
     time[7]=Set_Time[4]&0x0f;               //日低位;
     time[8]=Set_Time[3];                    //星期;

上一页  [1] [2] [3] [4] [5]  下一页


本文关键字:单片机  程序  万年历  AVR单片机单片机-工控设备 - AVR单片机