打印

DS3231程序大集合,欢迎大家来分享

[复制链接]
9178|41
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
dirtwillfly|  楼主 | 2015-11-4 08:16 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 dirtwillfly 于 2015-11-4 09:45 编辑
  #include <msp430.h>
#include"config.h"
#include"1838.h"
#include"1602.h"
#include"dh11.h"
#include"ds3231.h"
#include"ta.h"
//#include"adc.h"
int main(void)
{
  extern_12m();
  hc164_init();
  LCD_init();
  h1838_init();
  init_TA();
//  I2cByteWrite(0xD0,0x0e,0);
//  I2cByteWrite(0xD0,0x0f,0);
//  ModifyTime(15,7,19,12,07,00);//初始化时钟,2007/11/20,12/59/00
                               //小时采用24小时制
  get_show_time();
  get_show_Temperature();
  dh11_read();

// adc_init();
  _EINT();
  while (1)
  {
//  LCD_disp_string(0,0,"I AM A BOY?");
//  LCD_disp_string(0,1,"YOU ARE A GIRL!");
  Red_Code();
  Key_Icode(key_code[2]);

}
}



#include"config.h"
uchar change;

void extern_12m()
{
   WDTCTL = WDTPW + WDTHOLD;                 // Stop WDT
  if (CALBC1_12MHZ==0xFF)                                        // If calibration constant erased
  {                                                                                       
    while(1);                               // do not load, trap CPU!!        
  }
  DCOCTL = 0;                               // Select lowest DCOx and MODx settings
  BCSCTL1 = CALBC1_12MHZ;
  DCOCTL = CALDCO_12MHZ;                     // Load 8MHz constants
}

void hc164_init()
{
  P1OUT &= ~(BIT6+BIT7+BIT3);                           // Clear P1.0
  P1DIR |=BIT6+BIT7+BIT3;                            // P1.0 output
  
  P2OUT &= ~(BIT0+BIT1+BIT2);                           // Clear P1.0
  P2DIR |=BIT0+BIT1+BIT2;                            // P1.0 output
}

void LCD_delay_10us(uint n) // - - 10微秒的延时子程序
{
  uint i,j;
  for(i=n;i>0;i--)
    for(j=4;j>0;j--);  
}

void sendbyte(uchar byte)
{            
uchar c,num;   
num=byte;
for(c=0;c<8;c++)        
{     
P1OUT&=~0x80;
CLK0;   
P1OUT|=num&0x80; //(0x80即十进制的128, 二进制的10000000 按位发送
CLK1;         
num<<=1;   
}
}  


// - - 向LCD1602写指令
void LCD_write_command(uchar dat)
{
LCD_delay_10us(2);
LCD_RS_LOW; // - - 指令
//LCD_RW_LOW; // - - 写入
sendbyte(dat);
LCD_delay_10us(2);
LCD_EN_HIGH; // - - 允许
LCD_delay_10us(2);
LCD_EN_LOW;
}

// - - 向LCD1602写数据
void LCD_write_data(uchar dat)
{
LCD_delay_10us(2);
LCD_RS_HIGH;// - - 数据
//LCD_RW_LOW;// - - 写入
sendbyte(dat);
LCD_delay_10us(2);
LCD_EN_HIGH;// - - 允许
LCD_delay_10us(2);
LCD_EN_LOW;
}

// - - 设置显示位置
void LCD_set_xy(uchar x,uchar y)
{
  uchar address;
  if(y==1)
  {
    address=0x80+x; // - - 第一行位置
  } else {
    address=0xc0+x; // - - 第二行位置
  }
//    LCD_delay_10us(1);
    LCD_write_command(address);
}

// - - 显示一个字符函数
void LCD_disp_char(uchar x,uchar y,uchar dat) // - - LCD_disp_char(0,1,0x38); // - - 显示8
{
    LCD_set_xy(x,y);
//    LCD_delay_10us(1);
    LCD_write_data(dat);
}



// - - 显示一个字符串函数
void LCD_disp_string(uchar x,uchar y,char *s)
{
  LCD_set_xy(x,y);
// LCD_delay_10us(1);
  while(*s!='\0')
  {
    LCD_write_data(*s);
    s++;
  }
}

void LCD_init(void)
{
LCD_delay_10us(20);
LCD_write_command(0x38); // - - 设置8位格式,2行,5x7
LCD_delay_10us(5);
LCD_write_command(0x0c); // - - 整体显示,关光标,不闪烁
LCD_delay_10us(5);
LCD_write_command(0x06); // - - 设定输入方式,增量不移位
LCD_delay_10us(5);
LCD_write_command(0x01); // - - 清除屏幕显示
LCD_delay_10us(200);
}





#include"config.h"
#define IR_DIR_IN P2DIR&=~BIT3//红外接收头P1.0设置为输入,
#define RED_R (P2IN&BIT3)//红外接收头输入值
void h1838_init()
{
  IR_DIR_IN;
}


uchar KEY_VAL;
uchar key_code[4],code_value;
void Red_Code()
{
uchar i,j,k = 0;

for(i = 0;i < 19;i++)
{
  delay_us(400);           //延时400us
  if(RED_R)                //9ms内有高电平,则判断为干扰,退出处理程序
  {
      return;
  }
}

while(!RED_R);            //等待9ms低电平过去
for(i=0;i<5;i++)          //是否连发码
{
  delay_us(500);
  if(!RED_R)
   {
     return;
   }
}


while(RED_R);               //等待4.5ms高电平过去

for(i = 0;i < 4;i++)        //接收4个字节的数据
{
  for(j = 0;j < 8;j++)      //每个字节的数据8位
  {
   

   while(!RED_R);           //等待变高电平
   while(RED_R)             //计算高电平时间
   {
    delay_us(100);
    k++;
    if(k >22)               //高电平时间过长,则退出处理程序
    {
     return;         
    }
   
   }

   code_value>>=1;          //接受一位数据
   if(k >= 7)
   {
    code_value|=0x80;       //高电平时间大于0.56,则为数据1
   }
   k = 0;                  //计时清零
  }
  key_code[i]=code_value;  //四个字节的数据
}
//  Key_Icode();             //调用赋值函数

// Display_Key(KEY_VAL);     //显示按键值
  
//  delay_ms(50);

}
#include <msp430.h>
#define uchar unsigned char
#define uint unsigned int
#define ulong unsigned long
#define CLK0  P1OUT&=~BIT6
#define CLK1   P1OUT|=BIT6


#define LCD_EN_HIGH         P2OUT|=BIT2   //拉高
#define LCD_EN_LOW          P2OUT&=~BIT2  //拉低

#define LCD_RW_HIGH         P2OUT|=BIT1   //拉高
#define LCD_RW_LOW          P2OUT&=~BIT1   //拉低

#define LCD_RS_HIGH         P2OUT|=BIT0   //拉高
#define LCD_RS_LOW          P2OUT&=~BIT0   //拉低

#define CPU_F ((double)12000000)   //外部高频晶振8MHZ
//#define CPU_F ((double)32768)   //外部低频晶振32.768KHZ
#define delay_us(x) __delay_cycles((long)(CPU_F*(double)x/1000000.0))
#define delay_ms(x) __delay_cycles((long)(CPU_F*(double)x/1000.0))

#define KEY_0     0x16                 //键0
#define KEY_1     0x0C                 //键1
#define KEY_2     0x18                 //键2
#define KEY_3     0x5E                 //键3
#define KEY_4     0x08                 //键4
#define KEY_5     0x1C                 //键5
#define KEY_6     0x5A                 //键6
#define KEY_7     0x42                 //键7
#define KEY_8     0x52                 //键8
#define KEY_9     0x4A                 //键9
#define prex      0x44                 //电源
#define nex       0x40                 //模式
#define play_pause 0x43                 //静音
#define ch_       0x45                //模式
#define ch        0x46  
#define cha       0x47                 //模式
#define __         0x07
#define _a        0x15
#define eq        0x09
#define _100a      0x19
#define _200a      0x0d
#define power      0x12            //power
#define up      0x1b            //up
#define down      0x1a            //down
#define left      0x04            //left
#define right      0x06            //right
#define ok      0x05            //0k
#include"config.h"

//连接端口
#define DHT11_Dir      P1DIR
#define DHT11_In       P1IN
#define DHT11_Out      P1OUT
#define DHT11_Bit      BIT0

char dh11_str[8];

//8bit湿度+8bit湿度小数点
//8bit温度+8bit温度小数点
//数据位为0是高电平26~28us | 1高电平70us

uchar DHT11_Data[5] = {0}; //从DHT11中读到的数据
void read_DHT11(uchar *Data)
{
    uchar re,m,n,Time_out;
    DHT11_Dir &= (~DHT11_Bit);        //引脚为输入
    for(n = 0; n < 5; n++)
    {
        for(m = 0; m < 8; m++)
        {
            while( !(DHT11_In & DHT11_Bit) );     //每1bit以50us低电平时限开始,直到高电平到来
            delay_us(30);                     
            re = 0;            
            if((DHT11_In & DHT11_Bit))          //30us后还是高电平说明是1
            {
                re = 1;
            }
            Time_out = 2;
            while((DHT11_In & DHT11_Bit) && (Time_out++ ));
            if(Time_out == 1)                   //超时处理
            {
                break;
            }
            *Data <<= 1;
            if(re)
            {
                *Data |= 0x01;
            }
        }
        Data++;
    }
}

//DHT11的握手协议模拟和数据的读取
uchar DHT11_GetData(void)
{
    uchar o,sum;
    DHT11_Dir |= DHT11_Bit;
    DHT11_Out &= ~DHT11_Bit;    //拉低,发出开始信号
   
    delay_ms(18);               //拉低18ms
    DHT11_Out |= DHT11_Bit;     //拉高20us等待
    delay_us(20);
    DHT11_Dir &= (~DHT11_Bit);
    if( !(DHT11_In & DHT11_Bit) )       //DHT11响应
    {
        while( !(DHT11_In & DHT11_Bit) );       //80us低电平,等待DHT11的响应信号
        while( (DHT11_In & DHT11_Bit) );        //DHT11准备发送数据
        read_DHT11(DHT11_Data);                 //主机接收数据
        sum = 0;
        for(o = 0; o < 4; o++)                  //数据校验
        {
          sum += DHT11_Data[o];
        }
        if(sum != DHT11_Data[4])
        {
          return 0;
        }
        else
        {
          return 1;
        }
    }
    return 0;
}

//对温度湿度进行格式化(如果获取温度成功)
void DHT11_format(char *Des_str)
{
    Des_str[0] = DHT11_Data[0]/10 + 0x30;
    Des_str[1] = DHT11_Data[0]%10 + 0x30;
    Des_str[2] = '%';
    Des_str[3] = ' ';
    Des_str[4] = DHT11_Data[2]/10 + 0x30;
    Des_str[5] = DHT11_Data[2]%10 + 0x30;
    Des_str[6] = 'C';
    Des_str[7] = ' ';
}

void dh11_read()
{
  if(DHT11_GetData())
   DHT11_format(dh11_str);
  LCD_disp_string(0,0,dh11_str);
}

#include"config.h"

#define SDA_DIR_OUT P1DIR|=BIT4
#define SDA_DIR_IN P1DIR&=~BIT4
#define SCL_DIR_OUT P1DIR|=BIT5

#define SDA_IN P1IN&BIT4

#define SDA1 P1OUT|=BIT4
#define SDA0 P1OUT&=~BIT4
#define SCL1 P1OUT|=BIT5
#define SCL0 P1OUT&=~BIT5
uchar year,month,date,hour,minute,second;
uchar const table[]={'0','1','2','3','4','5','6','7','8','9'};
uchar const month_12[]={31,29,31,30,31,30,31,31,30,31,30,31};

uchar clockset,clockon,clockoff[5],clock_on;
uchar oclock[5][2];
uchar BCD2HEX(uchar val)    //BCD转换为Byte
{
    uchar temp;
    temp=val&0x0f;
    val>>=4;
    val&=0x0f;
    val*=10;
    temp+=val;
   
    return temp;
}


uchar HEX2BCD(uchar val)    //B码转换为BCD码
{
    uchar i,j,k;
    i=val/10;
    j=val%10;
    k=j+(i<<4);
    return k;
}



void Start_I2C()
{
    SDA_DIR_OUT;
    SCL_DIR_OUT;
    SDA1;                  //发送起始条件的数据信号
    delay_us(5);
    SCL1;
    delay_us(5);             //起始条件建立时间大于4.7us,延时
   
    SDA0;                  //发送起始信号
    delay_us(5);             // 起始条件锁定时间大于4μs
      
//    SCL0;                  //钳住I2C总线,准备发送或接收数据
//    delay_us(2);
}


void Stop_I2C()
{
    SDA_DIR_OUT;
    SCL_DIR_OUT;
    SDA0;                  //发送结束条件的数据信号
    delay_us(5);             //发送结束条件的时钟信号
    SCL1;                  //结束条件建立时间大于4us
    delay_us(5);
   
    SDA1;                  //发送I2C总线结束信号
    delay_us(1);
}


void SendByte(uchar c)
{
    uchar BitCnt;
    SDA_DIR_OUT;
    SCL_DIR_OUT;
    SCL0;
    delay_us(10);
    for(BitCnt=0;BitCnt<8;BitCnt++)         //要传送的数据长度为8位
    {
        if(c&0x80)
            SDA1;                          //判断发送位
        else
            SDA0;               
          delay_us(1);
          SCL0;                            //置时钟线为高,通知被控器开始接收数据位
          delay_us(1);                       //保证时钟高电平周期大于4μs   
          SCL1;
          delay_us(5);
          c=c<<1;
          SCL0;                            //置时钟线为高,通知被控器开始接收数据位
          delay_us(5);   
    }
    SDA1;                                  //8位发送完后释放数据线,准备接收应答位
    delay_us(1);  
    SCL0;
    delay_us(1);
                     
    SCL1;
    delay_us(1);
     SCL0;
     
}


uchar ReceiveByte(uchar b)
{
  uchar i;
  uchar temp;
  uchar Dat=0;
/*
  SDA_H;
  delay(5);
  SCL_L;
  delay(5);
*/
SDA_DIR_IN;
  for(i=0;i<8;i++)
    {
          SCL1;
      delay_us(5);
          Dat=Dat<<1;
          delay_us(1);
          temp=SDA_IN;
          if(temp&0x10)
            Dat|=0x01;
          else
            Dat|=0x00;
         delay_us(1);
         SCL0;
          delay_us(5);
    }
   SDA_DIR_OUT;
  
  if(b)//每收一个数后发送ACK或nACK
    SDA1;
  else
    SDA0;
        
  delay_us(1);
  SCL1;
  delay_us(1);
  SCL0;
  delay_us(1);
  SDA1;        //释放总线
  delay_us(1);
  return Dat;
}
/****************************************************/
/***********将一个字节写入DS3231指定的地址***********/
void I2cByteWrite(uchar device,uint addr,uchar bytedata)
{
  Start_I2C();
// delay_us(1);
  SendByte(device);
// delay_us(1);
  SendByte(addr);
// delay_us(1);
  SendByte(bytedata);
// delay_us(1);
  Stop_I2C();
}
/****************************************************/
/************从DS3231指定的地址读一个字节************/
uchar I2cByteRead(uchar device,uint addr)
{
  uchar Dat=0;
  
  Start_I2C();
  SendByte(device);
//  delay_us(1);
  SendByte(addr);
//  delay_us(1);
  Start_I2C();
  SendByte(0xd1);//读数
// delay_us(1);
  Dat=ReceiveByte(1);//只接收一个字节,所以发nACK
  Stop_I2C();
  return Dat;
}
/***************************************************/
/*********************读取时间**********************/
void Readtime(void)
{
  year=I2cByteRead(0xd0,0x06);  //年
  month=I2cByteRead(0xd0,0x05); //月
  date=I2cByteRead(0xd0,0x04);  //日
  hour=I2cByteRead(0xd0,0x02);  //时
  minute=I2cByteRead(0xd0,0x01);//分
  second=I2cByteRead(0xd0,0x00);//秒
}
/***************************************************/
/****************修改时间,BCD码输入*****************/
void ModifyTime(uchar yea,uchar mon,uchar da,uchar hou,uchar min,uchar sec)
{
  uchar temp=0;
  
  temp=HEX2BCD(yea);
  I2cByteWrite(0xD0,0x06,temp);//修改年
  
  temp=HEX2BCD(mon);
  I2cByteWrite(0xD0,0x05,temp);//修改月
   
  temp=HEX2BCD(da);
  I2cByteWrite(0xD0,0x04,temp);//修改日
  
  temp=HEX2BCD(hou);
  I2cByteWrite(0xD0,0x02,temp);//修改时
  
  temp=HEX2BCD(min);
  I2cByteWrite(0xD0,0x01,temp);//修改分
  
  temp=HEX2BCD(sec);
  I2cByteWrite(0xD0,0x00,temp);//修改秒
}
/****************************************************/
/****************读取时间 计算 并显示****************/
void get_show_time(void)
{
uchar temp1,temp2;
char a[2];
temp1=I2cByteRead(0xd0,0x06);  //年
temp2=BCD2HEX(temp1);
a[0]=table[temp2/10];
a[1]=table[temp2%10];
LCD_disp_string(0,1,a);
LCD_disp_char(2,1,'-');

temp1=I2cByteRead(0xd0,0x05);  //月
temp2=BCD2HEX(temp1);
a[0]=table[temp2/10];
a[1]=table[temp2%10];
LCD_disp_string(3,1,a);
LCD_disp_char(5,1,'-');

temp1=I2cByteRead(0xd0,0x04);  //日
temp2=BCD2HEX(temp1);
a[0]=table[temp2/10];
a[1]=table[temp2%10];
LCD_disp_string(6,1,a);
LCD_disp_char(8,1,'-');

temp1=I2cByteRead(0xd0,0x03);  //星期
temp2=BCD2HEX(temp1);
temp2=table[temp2];
LCD_disp_char(9,1,temp2);
LCD_disp_char(10,1,' ');

LCD_disp_char(8,0,' ');
if(clockset==0)
{
temp1=I2cByteRead(0xd0,0x02);  //时 24小时制
temp1&=0x3f;     
temp2=BCD2HEX(temp1);

a[0]=table[temp2/10];
a[1]=table[temp2%10];
LCD_disp_string(8,0,a);
LCD_disp_char(10,0,':');

temp1=I2cByteRead(0xd0,0x01);  //分
temp2=BCD2HEX(temp1);
a[0]=table[temp2/10];
a[1]=table[temp2%10];
LCD_disp_string(11,0,a);
LCD_disp_char(13,0,':');
  
temp1=I2cByteRead(0xd0,0x00);  //秒
temp2=BCD2HEX(temp1);
a[0]=table[temp2/10];
a[1]=table[temp2%10];
LCD_disp_string(14,0,a);

}

if(clockset!=0)
{
LCD_disp_string(8,0,"        ");
a[0]=table[oclock[clockset-1][0]/10];
a[1]=table[oclock[clockset-1][0]%10];
LCD_disp_string(8,0,a);
LCD_disp_char(10,0,'-');
a[0]=table[oclock[clockset-1][1]/10];
a[1]=table[oclock[clockset-1][1]%10];
LCD_disp_string(11,0,a);
LCD_disp_string(13,0,"CK");
LCD_disp_char(15,0,table[clockoff[clockset-1]]);
}

}

//显示温度

void get_show_Temperature(void)
{
  if(change==0)
  {
    uchar Ttemp1,Ttemp2;
   char a[2];
    Ttemp1=I2cByteRead(0xd0,0x11);     //温度 高字节
    Ttemp2=BCD2HEX(Ttemp1);
//     LCD_disp_string(9,1,"     ");
//    Ttemp3=I2cByteRead(0xd0,0x12);    //温度低字节
//    Ttemp4=BCD2HEX(Ttemp3);
    a[0]=table[Ttemp2/10];
    a[1]=table[Ttemp2%10];
    LCD_disp_string(13,1,a);
    LCD_disp_char(15,1,'c');
  }
   
  if(change!=0)
  {
   LCD_disp_string(13,1,"   ");
  LCD_disp_char(15,1,table[change-1]);
  }
  
  if((clockset!=0)&&(change==0))
{
   LCD_disp_string(13,1,"   ");
   LCD_disp_char(13,1,table[clockset]);
   LCD_disp_char(15,1,table[clockon]);
}
  
}


void change_del()
{
  uchar temp1,temp2,temp3,temp4;
  if(change>0)
  {
   temp1=I2cByteRead(0xd0,change-1);  
   temp2=BCD2HEX(temp1);
   temp2-=1;
   if(change<3)
   {
   if(temp2>59)
     temp2=59;
   }
    if(change==3)
    {
      if(temp2>23)
       temp2=23;
    }
   
   if(change==4)
   {
     if(temp2==0)
       temp2=7;
   }
    if(change==5)
    {
      if(temp2==0)
      {
        temp3=I2cByteRead(0xd0,0x05);  
        temp4=BCD2HEX(temp3);
        temp2=month_12[temp4-1];
      }
    }
   if(change==6)
   {
      if(temp2==0)
      temp2=12;  
   }
   
    temp2=HEX2BCD(temp2);
  I2cByteWrite(0xD0,change-1,temp2);//修改年
  }
}

void change_add()
{
  uchar temp1,temp2,temp3,temp4;
  if(change>0)
  {
   temp1=I2cByteRead(0xd0,change-1);  
   temp2=BCD2HEX(temp1);
   temp2+=1;
   if(change<3)
   {
   if(temp2>59)
     temp2=0;
   }
    if(change==3)
    {
      if(temp2>23)
       temp2=0;
    }
   
   if(change==4)
   {
     if(temp2==8)
       temp2=1;
   }
    if(change==5)
    {  
        temp3=I2cByteRead(0xd0,0x05);  
        temp4=BCD2HEX(temp3);
        if(temp2>month_12[temp4-1])
        temp2=1;   
    }
   if(change==6)
   {
      if(temp2==13)
      temp2=1;  
   }
   
    temp2=HEX2BCD(temp2);
  I2cByteWrite(0xD0,change-1,temp2);//修改年
  }
}

void change_set()
{
  ++change;
  if(change>=8)
    change=0;
}


void clock_set()
{
++clockset;
if(clockset>=6)
   clockset=0;
}

void clock_add()
{
  change=0;
  ++clockon;
  if( clockon==3)
    clockon=0;
}

void clock_change()
{
  if(clockon==1)
  {
    ++oclock[clockset-1][1];
    if(oclock[clockset-1][1]>59)
      oclock[clockset-1][1]=0;
  }
  
  if(clockon==2)
  {
     ++oclock[clockset-1][0];
    if(oclock[clockset-1][0]>23)
      oclock[clockset-1][0]=0;
  }
}



void Key_Icode(uchar dat)
{
   LCD_disp_char(10,1,' ');
//  LCD_disp_string(10,1,"   ");
   switch(dat)                    //第3个字节是数据,第4个字节是反码
   {                                      //为了更稳定,可以加上第4个字节数据的判断
   case KEY_0:KEY_VAL=0; LCD_disp_char(11,1,KEY_VAL+0x30);  LCD_disp_char(12,1,' '); break;
   case KEY_1:KEY_VAL=1; LCD_disp_char(11,1,KEY_VAL+0x30);  LCD_disp_char(12,1,' '); break;
   case KEY_2:KEY_VAL=2; LCD_disp_char(11,1,KEY_VAL+0x30);  LCD_disp_char(12,1,' '); break;
   case KEY_3:KEY_VAL=3; LCD_disp_char(11,1,KEY_VAL+0x30);   LCD_disp_char(12,1,' ');break;
   case KEY_4:KEY_VAL=4; LCD_disp_char(11,1,KEY_VAL+0x30);   LCD_disp_char(12,1,' ');break;
   case KEY_5:KEY_VAL=5; LCD_disp_char(11,1,KEY_VAL+0x30);   LCD_disp_char(12,1,' ');break;
   case KEY_6:KEY_VAL=6;  LCD_disp_char(11,1,KEY_VAL+0x30);  LCD_disp_char(12,1,' ');break;
   case KEY_7:KEY_VAL=7; LCD_disp_char(11,1,KEY_VAL+0x30);   LCD_disp_char(12,1,' ');break;
   case KEY_8:KEY_VAL=8; LCD_disp_char(11,1,KEY_VAL+0x30);   LCD_disp_char(12,1,' ');break;
   case KEY_9:KEY_VAL=9; LCD_disp_char(11,1,KEY_VAL+0x30);   LCD_disp_char(12,1,' ');break;
   case prex:LCD_disp_string(11,1,"PR");clock_change();break;
   case nex :LCD_disp_string(11,1,"NX");clock_add();break;
   case play_pause:LCD_disp_string(11,1,"PS");clock_set();break;
   case ch_: LCD_disp_string(11,1,"C-");change_del();break;
   case ch: LCD_disp_string(11,1,"CH");change_set();break;
   case cha:LCD_disp_string(11,1,"C+");change_add();break;
   case eq: LCD_disp_string(11,1,"EQ");
             if(clockoff[clockset-1]==0)
               clockoff[clockset-1]=1;
             else clockoff[clockset-1]=0;
               break;
   case _a:LCD_disp_string(11,1,"V+");break;
   case __:LCD_disp_string(11,1,"V-");break;
   case _100a:LCD_disp_string(11,1,"1+");break;
   case _200a:LCD_disp_string(11,1,"2+");break;
   
    case power:LCD_disp_string(11,1,"PW");break;
   case up: LCD_disp_string(11,1,"UP");break;
   case down:LCD_disp_string(11,1,"DW");break;
   case left:LCD_disp_string(11,1,"LF");break;
   case right:LCD_disp_string(11,1,"RT");break;
   case ok:LCD_disp_string(11,1,"OK");break;
   default:KEY_VAL=' ';break;
   }
  key_code[2]=' ';
   
}

void oclockon()
{
  uchar clock_flag[5];
  uchar temp1,temp2,temp3;
  temp1=I2cByteRead(0xd0,0x02);  //时 24小时制
  temp1&=0x3f;     
  temp2=BCD2HEX(temp1);
  
  temp1=I2cByteRead(0xd0,0x01);  //分
  temp3=BCD2HEX(temp1);
  uchar i;
  for(i=0;i<5;i++)
  {
    if(temp2==oclock[i][0])
    {
      if(temp3==oclock[i][1])
      {
        if(clockoff[i]==1)
          clock_flag[i]=1;
        else clock_flag[i]=0;
      }
      else clock_flag[i]=0;
    }
     else clock_flag[i]=0;
  }
  clock_on=0;
  for(i=0;i<5;i++)
  {
    if(clock_flag[i]==1)
      clock_on=1;
  }
}
#include"config.h"

uchar t;
/***********TA初始化*********/
void init_TA()
{
  TACCTL0 = CCIE;                           // TACCR0 interrupt enabled
  TACCR0 = 60000;                            //5ms中断一次
  TACTL = TASSEL_2 + MC_1;                  // SMCLK, upmode
}


#pragma vector=TIMER0_A0_VECTOR  
__interrupt void Timer_A (void)
{

  if(++t==200)
  {  
    t=0;
    dh11_read();
    get_show_Temperature();
    get_show_time();
    oclockon();
  }
  if(clock_on==1)
    P1OUT ^= 0x08;
}






相关帖子

沙发
xyz549040622| | 2015-11-4 08:26 | 只看该作者
:lol我上个51的
/***************************************************************************/
/* DEMO3231.C                                                              */
/***************************************************************************/
#include        <stdio.h>               /* Prototypes for I/O functions */
#include        <DS5000.h>              /* Register declarations for DS5000 */
/************************* bit definitions ****************************/
sbit    scl = P0^0;             /* I2C pin definitions */
sbit    sda = P0^1;
sbit    E = P1^0;               /* DCM LCD module control signal definitions */
sbit    RS = P1^1;
sbit    RW = P1^2;
sbit    CLK = P2^5;             /* DS1267 control signal definitions */
sbit    RSTb = P2^6;
sbit    DQ = P2^7;
sbit    int0 = P3^2;
/**************************** defines *******************************/
#define ADDRTC  0xd0    /* DS3231 slave address (write) */
#define ACK     0
#define NACK    1
/*********************** Function Prototypes **************************/
void    start();
void    stop();
uchar   i2cwrite(uchar d);
uchar   i2cread(char);
void    wr_dsp_dat(uchar);
void    wr_dsp_ins(uchar);
uchar   rd_dsp_ins();
void    hex2asc(uchar);
void    dsp_adj(uchar pos);
void    init_dsp();
void    writebyte();
void    initialize_DS3231();
void    disp_regs();
void    rd_temp();
void    frq_out_tog();
void    init_alrm();
void    comm_init();
/************************* Global Variables ***************************/
xdata   uchar   sec, min, hr, dy, dt, mn, yr;
/**************************** functions ******************************/
void start()            /* --------- Initiate start condition ---------- */
{
        sda = 1;  scl = 1;
        sda = 0;
}
void stop()             /* ---------- Initiate stop condition ----------- */
{
        sda = 0;  sda = 0;
        scl = 1;  scl = 1;  sda = 1;
}
uchar i2cwrite(uchar d)         /* ----------------------------- */
{
uchar i;

        scl = 0;
        for (i = 0;i < 8; i++)
        {
                if (d & 0x80)
                        sda = 1; /* Send the msbits first */
                else
                        sda = 0;
                scl = 0;
                scl = 1;
                d = d << 1;     /* do shift here to increase scl high time */
                scl = 0;
        }
        sda = 1;        /* Release the sda line */
        scl = 0;
        scl = 1;
        i = sda;
        if (i) printf("Ack bit missing  %02X\n",(unsigned int)d);
        scl = 0;
        return(i);
}
uchar i2cread(char b)   /* ----------------------------------- */
{
uchar i, d;

        d = 0;
        sda = 1;             /* Let go of sda line */
        scl = 0;
        for (i = 0; i < 8; i++) /* read the msb first */
        {
                scl = 1;
                d = d << 1;
                d = d | (unsigned char)sda;
                scl = 0;
        }
        sda = b;          /* low for ack, high for nack */
        scl = 1;
        scl = 0;

        sda = 1;          /* Release the sda line */
        return d;
}
void    wr_dsp_dat(uchar dat)   /* -------- write one byte to the display --------- */
{
        P0 = dat;
        RS = 1; /* data register */
        RW = 0; /* write */
        E = 1;
        E = 0;          /* latch data in */
}
void    wr_dsp_ins(uchar dat)   /* ---- write one byte to the display instruction ----- */
{
        P0 = dat;
        RS = 0; /* instruction register */
        RW = 0; /* write */
        E = 1;
        E = 0;          /* latch data in */
}
uchar   rd_dsp_ins()    /* ---- read one byte from the instruction registers ----- */
{
uchar   dat;
        P0 = 0xff;      /* set up for read */
        RS = 0; /* instruction register */
        RW = 1; /* read */
        E = 1;
        dat = P0;
        E = 0;
        return(dat);
}
void    init_dsp()      /* -------- initialize DMC-16207 LCD display ------- */
{
        while((rd_dsp_ins() & 0x80));   /* wait for display */

        wr_dsp_ins(0x38);       /* Set 8-bit data, 2-line display, 5X7 font */
        while((rd_dsp_ins() & 0x80));
        wr_dsp_ins(0x0c);       /* Display on, cursor off, blink off */
        while((rd_dsp_ins() & 0x80));
        wr_dsp_ins(0x06);       /* Entry mode set: increment, no display shift */
        while((rd_dsp_ins() & 0x80));
        wr_dsp_ins(0x01);       /* clear display */
        while((rd_dsp_ins() & 0x80));
        wr_dsp_ins(0x80);       /* move to start of line 1 */
        while((rd_dsp_ins() & 0x80));

        wr_dsp_ins(0x50);       /* Set CG RAM address */
        while((rd_dsp_ins() & 0x80));
        wr_dsp_dat(0x07);       /* write 1st line of custom char to CG RAM */
        while((rd_dsp_ins() & 0x80));
        wr_dsp_dat(0x05);       /* write 2nd line */
        while((rd_dsp_ins() & 0x80));
        wr_dsp_dat(0x07);       /* write 3rd line */
        while((rd_dsp_ins() & 0x80));
        wr_dsp_dat(0x00);       /* write 4th line */
        while((rd_dsp_ins() & 0x80));
        wr_dsp_dat(0x00);       /* write 5th line */
        while((rd_dsp_ins() & 0x80));
        wr_dsp_dat(0x00);       /* write 6th line */
        while((rd_dsp_ins() & 0x80));
        wr_dsp_dat(0x00);       /* write 7th line */
        while((rd_dsp_ins() & 0x80));
}
void    hex2asc(uchar hex)      /* -- convert the upper and lower nibbles to 2 ascii characters -- */
{
        if( ( (hex & 0xf0) >> 4) < 0x0a)
                wr_dsp_dat( ( (hex & 0xf0) >> 4) + 48 );        /* prints 0-9 */
        else
                wr_dsp_dat( ( (hex & 0xf0) >> 4) + 55 );        /* prints A-F */
        while((rd_dsp_ins() & 0x80));

        if( (hex & 0x0f) < 0x0a)
                wr_dsp_dat( (hex & 0x0f) + 48 );
        else
                wr_dsp_dat( (hex & 0x0f) + 55 );
        while((rd_dsp_ins() & 0x80));
}
void    dsp_adj(uchar pos)      /* ------ adjust contrast on LCD display ------- */
{
char    inc;

        RSTb = CLK = 0;                 /* initialize for 1st pass */
        RSTb = 1;                               /* enable DS1267 */
        DQ = 0;                         /* write stack select bit */
        CLK = 1;                                /* toggle clk with data valid */
        CLK = 0;

        for (inc = 7; inc >= 0; inc--)  /* write wiper 0 */
        {
                DQ = ((pos >> inc) & 0x01);     /* shift x bits left */
                CLK = 0;                        /* toggle clk with data valid */
                CLK = 1;
        }
        for (inc = 7; inc >= 0; inc--)  /* write wiper 1 */
        {
                DQ = ((pos >> inc) & 0x01);     /* shift x bits left */
                CLK = 0;                        /* toggle clk with data valid */
                CLK = 1;
        }
        RSTb = 0;                               /* reset the 1267 (/RST & CLK low) */
        CLK = 0;
}
void writebyte()        /* ----------------------------------------------- */
{
uchar Add, Data;

        printf("\nADDRESS (hex):");             /* Get Address & Data */
        scanf("%bx", &Add);
        printf("DATA (hex):");
        scanf("%bx", &Data);

        start();
        i2cwrite(ADDRTC);
        i2cwrite(Add);
        i2cwrite(Data);
        stop();
}
void    initialize_DS3231()     /* ----- set time & date; user data entry ------ */
/* Note: NO error checking is done on the user entries! */
{
        printf("\nEnter the year (0-99): ");
        scanf("%bx", &yr);
        printf("Enter the month (1-12): ");
        scanf("%bx", &mn);
        printf("Enter the date (1-31): ");
        scanf("%bx", &dt);
        printf("Enter the day (1-7): ");
        scanf("%bx", &dy);
        printf("Enter the hour (1-23): ");
        scanf("%bx", &hr);
        /* hr = hr & 0x3f;      /* force clock to 24 hour mode */
        printf("Enter the minute (0-59): ");
        scanf("%bx", &min);
        printf("Enter the second (0-59): ");
        scanf("%bx", &sec);

        start();
        i2cwrite(ADDRTC);       /* write slave address, write 1339 */
        i2cwrite(0x00); /* write register address, 1st clock register */
        i2cwrite(sec);
        i2cwrite(min);
        i2cwrite(hr);
        i2cwrite(dy);
        i2cwrite(dt);
        i2cwrite(mn);
        i2cwrite(yr);
        i2cwrite(0x10); /* enable sqw, 1hz output */
        stop();
}
void    disp_regs()     /* --- display date/time on LCD display --- */
{
uchar   age, prv_sec=99;

        while(!RI)      /* Read & Display Clock Registers */
        {
                while(int0);    /* loop until int pin goes low */
                start();
                i2cwrite(ADDRTC);
                i2cwrite(0x0f);
                i2cwrite(0);            /* clear alarm flags */
                stop();

                start();
                i2cwrite(ADDRTC);
                i2cwrite(0);
                start();
                i2cwrite(ADDRTC | 1);
                sec = i2cread(ACK);
                min = i2cread(ACK);
                hr = i2cread(ACK);
                dy = i2cread(ACK);
                dt = i2cread(ACK);
                mn = i2cread(ACK);
                yr = i2cread(NACK);
                stop();

                wr_dsp_ins(0x80);       /* move to start of line 1 */
                while((rd_dsp_ins() & 0x80));
                hex2asc(yr);
                wr_dsp_dat('/');
                while((rd_dsp_ins() & 0x80));
                hex2asc(mn);
                wr_dsp_dat('/');
                while((rd_dsp_ins() & 0x80));
                hex2asc(dt);
                wr_dsp_dat(' ');
                while((rd_dsp_ins() & 0x80));
                hex2asc(dy);
                wr_dsp_dat(' ');
                while((rd_dsp_ins() & 0x80));

                start();
                i2cwrite(ADDRTC);
                i2cwrite(0x10);
                start();
                i2cwrite(ADDRTC | 1);
                age = i2cread(NACK);
                stop();

                wr_dsp_dat(' ');
                while((rd_dsp_ins() & 0x80));
                hex2asc(age);

                wr_dsp_ins(0xc0);       /* move to start of line 2 */
                while((rd_dsp_ins() & 0x80));
                hex2asc(hr);
                wr_dsp_dat(':');
                while((rd_dsp_ins() & 0x80));
                hex2asc(min);
                wr_dsp_dat(':');
                while((rd_dsp_ins() & 0x80));
                hex2asc(sec);

                rd_temp();
        }
        RI = 0;  /* Swallow keypress to exit loop */
}
void    rd_temp()       /* -------- display temperature -------- */
{
char    str[8];
int     itemp;
float   ftemp;

        do
        {
                start();
                i2cwrite(ADDRTC);
                i2cwrite(0x0e);         /* address of control register */
                start();
                i2cwrite(ADDRTC + 1);   /* send the device address for read */
                itemp = i2cread(NACK);  /* get the control register value */
                stop();
        }       while(itemp & 0x20);            /* wait until CNVT bit goes inactive */

        start();
        i2cwrite(ADDRTC);
        i2cwrite(0x11);                 /* address of temperature MSB */
        start();
        i2cwrite(ADDRTC + 1);           /* send the device address for read */
        itemp = ( (int) i2cread(ACK) << 5 );
        itemp += ( i2cread(NACK) >> 3);
        stop();
        if(itemp & 0x1000)      itemp += 0xe000;        /* if sign bit set, make 16 bit 2's comp */

        ftemp = 0.03125 * (float) itemp;        /* convert to degrees C */
        /* ftemp = ftemp * 9 / 5 + 32;  /* skip this if you don't want degrees F */

        sprintf(str, "%5.2f", ftemp);

        wr_dsp_ins(0xc9);                       /* go to line 2, column 10 */
        while((rd_dsp_ins() & 0x80));
        wr_dsp_dat(str[0]);
        while((rd_dsp_ins() & 0x80));
        wr_dsp_dat(str[1]);
        while((rd_dsp_ins() & 0x80));
        wr_dsp_dat(str[2]);
        while((rd_dsp_ins() & 0x80));
        wr_dsp_dat(str[3]);
        while((rd_dsp_ins() & 0x80));

        wr_dsp_dat(0x02);       /* display programed 3rd char in CG RAM */
        while((rd_dsp_ins() & 0x80));
}
void    frq_out_tog()   /* --- toggle en32khz bit to enable/disable sqw --- */
{
uchar   val;

        start();
        i2cwrite(ADDRTC);
        i2cwrite(0x0f);                 /* control/status reg address */
        start();
        i2cwrite(ADDRTC + 1);           /* send the device address for read */
        val = i2cread(NACK);
        stop();
        val ^= 0x08;    /* toggle en32khz bit */
        start();
        i2cwrite(ADDRTC);
        i2cwrite(0x0f);                 /* control/status reg address */
        i2cwrite(val);
        stop();
}
void    init_alrm()     /* --- enable alarm 1 for once-per-second --- */
{
        start();
        i2cwrite(ADDRTC);
        i2cwrite(7);            /* 1st alarm 1 reg address */
        i2cwrite(0x80); /* mask alarm register */
        i2cwrite(0x80);
        i2cwrite(0x80);
        i2cwrite(0x80);
        stop();

        start();
        i2cwrite(ADDRTC);
        i2cwrite(0x0e); /* control/status reg address */
        i2cwrite(0x05); /* enable interrupts, alarm 1 output */
}
void    comm_init()     /* ------ reset DS3231 comm interface ------ */
{
        do      /* because the DS3231 I2C interface is active for both supplies */
        {       /*  after a micro reset, we must get the comm into a known state */
                sda = 1;        /* make sure master has released SDA */
                scl = 1;
                if(sda) /* if sda is high, generate a start */
                {
                        sda = 0;        /* The DS3231 will recognize a valid start */
                        sda = 1;        /*  condition anywhere in a I2C data transfer */
                }
                scl = 0;
        }       while(sda == 0);        /* if the DS3231 is holding sda low, try again */
}
void    main    (void)  /* ------------------------------------------------ */
{
uchar i, M, M1;

        dsp_adj(0x10);  /* adjust contrast on LCD display */
        init_dsp();             /* initialize LCD display */
        comm_init();
        init_alrm();            /* enable alarm */
        disp_regs();

        while(1)
        {
                printf("\nDS3231 build date: %s\n", __DATE__);
                printf("I Init DS3231 S Show temp\n");
                printf("R Read Time   W Write Byte\n");
                printf("F Frq out     D Display adj\n");
                printf("Enter Menu Selection: ");

                M = _getkey();

                switch(M)
                {
                        case 'D':
                        case 'd':       printf("val: ");        scanf("%bx", &M1);
                                        dsp_adj(M1);    break;

                        case 'F':
                        case 'f':       frq_out_tog();  break;

                        case 'I':
                        case 'i':       initialize_DS3231();    break;

                        case 'R':
                        case 'r':       disp_regs();    break;

                        case 'S':
                        case 's':       rd_temp();      break;

                        case 'W':
                        case 'w':       writebyte();    break;
                }
        }

使用特权

评论回复
评分
参与人数 1威望 +6 收起 理由
dirtwillfly + 6 很给力!
板凳
huarana| | 2015-11-4 09:33 | 只看该作者
我上个通用的。 其实是stm32下的,不过很好移植

#include "ds3231.h"

u8        dis_buf_date[]={0,0,'-',0,0,' ',0,0,':',0,0,':',0,0,'\0'};
u8        dis_buf_temp[]={0,0,'.',0,0,'\0'};
u8        txd_buf_date[];


//BCD转换为Byte
u8 BCD2_Hex(u8 val)
{
        u8 temp;
        temp = val & 0x0f;
        val>>= 4;
        val &= 0x0f;
        val *= 10;
        temp+= val;
        return temp;
}

//B转换为BCD
u8 HEX2_Bcd(u8 val)
{
        u8 i,j,k;
        i = val/10;
        j = val%10;
        k = j+(i<<4);
        return k;
}

////初始化IIC
//void IIC_Init(void)
//{                                             
//        GPIO_InitTypeDef GPIO_InitStructure;
//        //RCC->APB2ENR|=1<<4;//先使能外设IO PORTB时钟
//        RCC_APB2PeriphClockCmd(        RCC_APB2Periph_GPIOB, ENABLE );       
//          
//        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7;
//        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP ;   //推挽输出
//        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
//        GPIO_Init(GPIOB, &GPIO_InitStructure);
//
//        IIC_SCL=1;
//        IIC_SDA=1;

//}

//初始化IIC
void IIC_Init(void)
{                                             
        GPIO_InitTypeDef GPIO_InitStructure;
        //RCC->APB2ENR|=1<<4;//先使能外设IO PORTB时钟
        RCC_APB2PeriphClockCmd(        RCC_APB2Periph_GPIOA, ENABLE );       
          
        GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP ;   //推挽输出
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
        GPIO_Init(GPIOA, &GPIO_InitStructure);

        IIC_SCL=1;
        IIC_SDA=1;

}
//产生IIC起始信号
void IIC_Start(void)
{
        SDA_OUT();     //sda线输出
        IIC_SDA=1;                    
        IIC_SCL=1;
        delay_us(5);
        IIC_SDA=0;//START:when CLK is high,DATA change form high to low
        delay_us(5);
        IIC_SCL=0;//钳住I2C总线,准备发送或接收数据
}          
//产生IIC停止信号
void IIC_Stop(void)
{
        SDA_OUT();//sda线输出
        delay_us(5);
        IIC_SCL=0;
        delay_us(5);
        IIC_SDA=0;//STOP:when CLK is high DATA change form low to high
        delay_us(5);
        IIC_SCL=1;
        delay_us(5);
        IIC_SDA=1;//发送I2C总线结束信号
        delay_us(5);                                                                  
}
//等待应答信号到来
//返回值:1,接收应答失败
//        0,接收应答成功
u8 IIC_Wait_Ack(void)
{
        u8 ucErrTime=0;
        SDA_IN();      //SDA设置为输入  
        IIC_SDA=1;delay_us(2);          
        IIC_SCL=1;delay_us(2);         
        while(READ_SDA)
        {
                ucErrTime++;
                if(ucErrTime>250)
                {
                        IIC_Stop();
                        return 1;
                }
        }
        IIC_SCL=0;//时钟输出0           
        return 0;  
}
//产生ACK应答
void IIC_Ack(void)
{
        IIC_SCL=0;
        SDA_OUT();
        IIC_SDA=0;
        delay_us(5);
        IIC_SCL=1;
        delay_us(5);
        IIC_SCL=0;
}
//不产生ACK应答                    
void IIC_NAck(void)
{
        IIC_SCL=0;
        SDA_OUT();
        IIC_SDA=1;
        delay_us(5);
        IIC_SCL=1;
        delay_us(5);
        IIC_SCL=0;
}                                                                              
//IIC发送一个字节
//返回从机有无应答
//1,有应答
//0,无应答                          
void IIC_Send_Byte(u8 txd)
{                        
    u8 t;   
        SDA_OUT();             
    IIC_SCL=0;//拉低时钟开始数据传输
    for(t=0;t<8;t++)
    {              
        IIC_SDA=(txd&0x80)>>7;
        txd<<=1;           
                delay_us(5);   //对TEA5767这三个延时都是必须的
                IIC_SCL=1;
                delay_us(5);
                IIC_SCL=0;       
                delay_us(5);
    }         
}             
//读1个字节,ack=1时,发送ACK,ack=0,发送nACK   
u8 IIC_Read_Byte(unsigned char ack)
{
        unsigned char i,receive=0;
        SDA_IN();//SDA设置为输入
    for(i=0;i<8;i++ )
        {
        IIC_SCL=0;
        delay_us(5);
                IIC_SCL=1;
        receive<<=1;
        if(READ_SDA)receive++;   
                delay_us(5);
    }                                         
    if (!ack)
        IIC_NAck();//发送nACK
    else
        IIC_Ack(); //发送ACK   
    return receive;
}
//指定地址写数据/命令
void DS3231_WriteByte(u8 addr,u8 dat)
        {
         IIC_Start();
         IIC_Send_Byte(DS3231_WriteAddress);
         IIC_Wait_Ack();
         IIC_Send_Byte(addr);
         IIC_Wait_Ack();
         IIC_Send_Byte(dat);
         IIC_Wait_Ack();
         IIC_Stop();
        }
u8 DS3231_ReadCurrent(void)
        {
         u8 read_dat;
         IIC_Start();
         IIC_Send_Byte(DS3231_ReadAddress);
         IIC_Wait_Ack();
         read_dat=IIC_Read_Byte(1);
         IIC_Stop();       
         return read_dat;
        }
u8 DS3231_ReadRandom(u8 random_addr)
        {
         IIC_Start();
         IIC_Send_Byte(DS3231_WriteAddress);
         IIC_Wait_Ack();
     IIC_Send_Byte(random_addr);
     IIC_Wait_Ack();
     return (DS3231_ReadCurrent());               
        }
void DS3231_ModifyTime(u8 yea,u8 mon,u8 day,u8 hou,u8 min,u8 sec)
        {
          u8 temp=0;
                temp=HEX2_Bcd(yea);
                DS3231_WriteByte(DS3231_YEAR,temp);                //修改年
               
                temp=HEX2_Bcd(mon);
                DS3231_WriteByte(DS3231_MONTH,temp);        //修改月
               
                temp=HEX2_Bcd(day);
                DS3231_WriteByte(DS3231_DAY,temp);                //修改日
               
                temp=HEX2_Bcd(hou);
                DS3231_WriteByte(DS3231_HOUR,temp);                //修改时
               
                temp=HEX2_Bcd(min);
                DS3231_WriteByte(DS3231_MINUTE,temp);        //修改分       
               
                temp=HEX2_Bcd(sec);
                DS3231_WriteByte(DS3231_SECOND,temp);        //修改秒       
        }
void TimeDisplay(u8 Dhour,u8 Dmin,u8 Dsec)
        {
                dis_buf_date[6]=Dhour/10        +0x30;        // 时十位
                dis_buf_date[7]=Dhour%10        +0x30;        // 时个位
                dis_buf_date[9]=Dmin /10        +0x30;         // 分十位
                dis_buf_date[10]=Dmin%10        +0x30;         // 分个位
                dis_buf_date[12]=Dsec/10        +0x30;         // 秒十位
                dis_buf_date[13]=Dsec%10        +0x30;         // 秒个位
        }
void DateDisplay(u8 Dyear,u8 Dmonth,u8 Dday)
        {
//                dis_buf_date[0]=Dyear / 10        +0x30;        // 年十位
//                dis_buf_date[1]=Dyear % 10        +0x30;        // 年个位
                dis_buf_date[0]=Dmonth/10        +0x30;       // 月十位
                dis_buf_date[1]=Dmonth%10        +0x30;       // 月个位
                dis_buf_date[3]=Dday /10        +0x30;         // 天十位
                dis_buf_date[4]=Dday %10        +0x30;         // 天个位
               
//                dis_buf_date[3]=Dmonth/10        +0x30;       // 月十位
//                dis_buf_date[4]=Dmonth%10        +0x30;       // 月个位
//                dis_buf_date[0]=Dday /10        +0x30;         // 天十位
//                dis_buf_date[1]=Dday %10        +0x30;         // 天个位
        }
void TempDisplay(u8 TempH,u8 TempL)
        {
                dis_buf_temp[0]=TempH / 10        +0x30;       //
                dis_buf_temp[1]=TempH % 10        +0x30;       //
                dis_buf_temp[3]=TempL / 10        +0x30;         //
                dis_buf_temp[4]=TempL % 10        +0x30;         //
        }
void get_show_time(void)
        {
                u8 Htemp1,Htemp2,Mtemp1,Mtemp2,Stemp1,Stemp2;
               
                Stemp1=DS3231_ReadRandom(DS3231_SECOND);  //秒
                Stemp2=BCD2_Hex(Stemp1);
                Mtemp1=DS3231_ReadRandom(DS3231_MINUTE);  //分
                Mtemp2=BCD2_Hex(Mtemp1);
                Htemp1=DS3231_ReadRandom(DS3231_HOUR);    //时 24小时制
//                Htemp1&=0x3f;


                Htemp2=BCD2_Hex(Htemp1);

                TimeDisplay(Htemp2,Mtemp2,Stemp2);
                txd_buf_date[3]=Htemp2;
                txd_buf_date[4]=Mtemp2;
                txd_buf_date[5]=Stemp2;
        }
void get_show_date(void)
        {
                u8 Ytemp1,Ytemp2,Mtemp1,Mtemp2,Dtemp1,Dtemp2;
                Dtemp1=DS3231_ReadRandom(DS3231_DAY);         //日
                Dtemp2=BCD2_Hex(Dtemp1);       
                Mtemp1=DS3231_ReadRandom(DS3231_MONTH);       //月
                Mtemp2=BCD2_Hex(Mtemp1);
                Ytemp1=DS3231_ReadRandom(DS3231_YEAR);        //年
//                IIC_NAck();//发送nACK
                Ytemp2=BCD2_Hex(Ytemp1);          
                DateDisplay(Ytemp2,Mtemp2,Dtemp2);
                txd_buf_date[0]=Ytemp2;
                txd_buf_date[1]=Mtemp2;
                txd_buf_date[2]=Dtemp2;
        }
//void get_show_Temperature(void)
//        {
//                u8 Ttemp1,Ttemp2,Ttemp3,Ttemp4;

//                Ttemp1=DS3231_ReadRandom(DS3231_TEMPERATUREH);    //温度 高字节
//                Ttemp2=BCD2_Hex(Ttemp1);
//          
//                Ttemp3=DS3231_ReadRandom(DS3231_TEMPERATUREL);    //温度低字节
//                Ttemp4=BCD2_Hex(Ttemp3);
//               
//                TempDisplay(Ttemp2,Ttemp4);
//        }
void get_show_Temperature(void)                                //温度数据是以2?进制格式存储的并不需要数制转换?
        {                                                                               
//                u8 Ttemp1,Ttemp2,Ttemp3,Ttemp4;
                u8 Ttemp2,Ttemp4;
                DS3231_WriteByte(0x0e,0x20);                //0x0e寄存器的CONV位置1开启温度转换?       
               
                Ttemp2=DS3231_ReadRandom(DS3231_TEMPERATUREH);    //温度 高字节
//                Ttemp2=BCD2_Hex(Ttemp1);
          
                Ttemp4=DS3231_ReadRandom(DS3231_TEMPERATUREL);    //温度低字节
                IIC_NAck();//发送nACK
//                Ttemp4=BCD2_Hex(Ttemp3);
                Ttemp4=(Ttemp4>>6)*25;                                //将BIT7,BIT6的数据移入BIT1,BIT0位;分辨率数值扩大100倍便于整数运算       
                TempDisplay(Ttemp2,Ttemp4);
        }

#ifndef        _DS3231_H_
#define        _DS3231_H_

#include "sys.h"
#include "delay.h"

///*端口设置*/
//#define IIC_SCL         PBout(6)       //SCLK  时钟   
//#define IIC_SDA         PBout(7)       //SDA   写数据  
//#define        READ_SDA        PBin(7)                                 //SDA   读数据

////IO方向设置
//#define SDA_IN()  {GPIOB->CRL&=0X0FFFFFFF;GPIOB->CRL|=(u32)8<<28;/*GPIOB->ODR|=1<<7;*/}                        //设置SDA口为上拉输入
//#define SDA_OUT() {GPIOB->CRL&=0X0FFFFFFF;GPIOB->CRL|=(u32)3<<28;}                                                        //设置SDA口为推挽输出


/*端口设置*/
#define IIC_SCL         PAout(6)       //SCLK  时钟   
#define IIC_SDA         PAout(7)       //SDA   写数据  
#define        READ_SDA        PAin(7)                                 //SDA   读数据

//IO方向设置
#define SDA_IN()  {GPIOA->CRL&=0X0FFFFFFF;GPIOA->CRL|=(u32)8<<28;/*GPIOA->ODR|=1<<7;*/}                        //设置SDA口为上拉输入
#define SDA_OUT() {GPIOA->CRL&=0X0FFFFFFF;GPIOA->CRL|=(u32)3<<28;}                                                        //设置SDA口为推挽输出
//DS3231初始宏设置
#define DS3231_WriteAddress 0xD0    //器件写地址
#define DS3231_ReadAddress  0xD1    //器件读地址
#define DS3231_SECOND       0x00    //秒
#define DS3231_MINUTE       0x01    //分
#define DS3231_HOUR         0x02    //时
#define DS3231_WEEK         0x03    //星期
#define DS3231_DAY          0x04    //日
#define DS3231_MONTH        0x05    //月
#define DS3231_YEAR         0x06    //年
//闹铃1            
#define DS3231_SALARM1ECOND 0x07    //秒
#define DS3231_ALARM1MINUTE 0x08    //分
#define DS3231_ALARM1HOUR   0x09    //时
#define DS3231_ALARM1WEEK   0x0A    //星期/日
//闹铃2
#define DS3231_ALARM2MINUTE 0x0b    //分
#define DS3231_ALARM2HOUR   0x0c    //时
#define DS3231_ALARM2WEEK   0x0d    //星期/日
#define DS3231_CONTROL      0x0e    //控制寄存器
#define DS3231_STATUS       0x0f    //状态寄存器
#define BSY                 2       //忙
#define OSF                 7       //振荡器停止标志
#define DS3231_XTAL         0x10    //晶体老化寄存器
#define DS3231_TEMPERATUREH 0x11    //温度寄存器高字节(8位)
#define DS3231_TEMPERATUREL 0x12    //温度寄存器低字节(高2位)


//IIC所有操作函数
void         IIC_Init(void);                        //初始化IIC的IO口                                 
void         IIC_Start(void);                                //发送IIC开始信号
void         IIC_Stop(void);                                          //发送IIC停止信号
void         IIC_Send_Byte(u8 txd);                        //IIC发送一个字节
u8                 IIC_Read_Byte(u8);                                //IIC读取一个字节
u8                 IIC_Wait_Ack(void);                         //IIC等待ACK信号
void         IIC_Ack(void);                                        //IIC发送ACK信号
void         IIC_NAck(void);                                        //IIC不发送ACK信号
u8                 BCD2_Hex(u8);
u8                 HEX2_Bcd(u8);
void         DS3231_WriteByte(u8,u8);
u8                 DS3231_ReadCurrent(void);
u8                 DS3231_ReadRandom(u8);
void         DS3231_ModifyTime(u8,u8,u8,u8,u8,u8);

void TimeDisplay(u8 Dhour,u8 Dmin,u8 Dsec);
void DateDisplay(u8 Dyear,u8 Dmonth,u8 Dday);
void TempDisplay(u8 TempH,u8 TempL);
void get_show_time(void);
void get_show_date(void);
void get_show_Temperature(void);



#endif

使用特权

评论回复
评分
参与人数 1威望 +6 收起 理由
dirtwillfly + 6 很给力!
地板
songchenping| | 2015-11-4 09:56 | 只看该作者
真是烟花缭乱啊。何必不弄个下载版本啊

使用特权

评论回复
5
yxycdz| | 2015-11-7 15:04 | 只看该作者
真是看得眼花了

使用特权

评论回复
6
czq123321| | 2015-11-7 18:37 | 只看该作者
没有  压缩包?

使用特权

评论回复
7
lvyunhua| | 2015-11-7 20:07 | 只看该作者
好长啊。

使用特权

评论回复
8
dirtwillfly|  楼主 | 2015-11-7 20:09 | 只看该作者

:lol长长久久,要留住大家哦

使用特权

评论回复
9
dadangjia| | 2015-11-7 21:01 | 只看该作者
我说这个怎么这样熟悉,原来是写得呀。

使用特权

评论回复
10
dadangjia| | 2015-11-7 21:03 | 只看该作者
我写的,实现msp430g2553红外遥控1602显示温度时间湿度等信息,做好了送人了。

使用特权

评论回复
11
dadangjia| | 2015-11-7 21:03 | 只看该作者
这是原文件

2553-12M07-17.zip

251.84 KB

使用特权

评论回复
评分
参与人数 1威望 +10 收起 理由
dirtwillfly + 10 很给力!
12
dirtwillfly|  楼主 | 2015-11-7 21:33 | 只看该作者
dadangjia 发表于 2015-11-7 21:03
我写的,实现msp430g2553红外遥控1602显示温度时间湿度等信息,做好了送人了。 ...

看来不但送出了礼物,还收获了感情:lol

使用特权

评论回复
13
dadangjia| | 2015-11-8 16:38 | 只看该作者
在msp430g2553可以用,换了个msp430g2452就用不了,我觉得可能是国产货的原因,质量有问题。

使用特权

评论回复
14
dirtwillfly|  楼主 | 2015-11-8 19:42 | 只看该作者
dadangjia 发表于 2015-11-8 16:38
在msp430g2553可以用,换了个msp430g2452就用不了,我觉得可能是国产货的原因,质量有问题。 ...

:Lmsp430只要是正品,都是ti生产的,是进口货

使用特权

评论回复
15
perry_peng| | 2015-11-9 16:22 | 只看该作者
谢谢分享。

使用特权

评论回复
16
sunmeat| | 2015-11-13 16:07 | 只看该作者
dadangjia 发表于 2015-11-8 16:38
在msp430g2553可以用,换了个msp430g2452就用不了,我觉得可能是国产货的原因,质量有问题。 ...

你让国内盗版个,没法盗版。翻新倒有可能。

使用特权

评论回复
17
博哥| | 2015-11-13 18:11 | 只看该作者
谢谢楼主

使用特权

评论回复
18
dirtwillfly|  楼主 | 2015-11-13 20:24 | 只看该作者
sunmeat 发表于 2015-11-13 16:07
你让国内盗版个,没法盗版。翻新倒有可能。

是的。也有把拆机的元件丝印除掉重新印丝印的

使用特权

评论回复
19
joujoulian| | 2015-11-16 09:32 | 只看该作者
多谢楼主分享,如果可以上原理一份更好了啊 ,精华顶!

使用特权

评论回复
20
dadangjia| | 2015-11-20 20:40 | 只看该作者
ds1302是国货

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

个人签名:欢迎进入TI MCU论坛      21ic TI技术交流1群:61549143(已满),  21ic TI技术交流2群:311421422 我的博客:http://blog.timcu.com/

1179

主题

34648

帖子

1115

粉丝