공부/8051 2012. 11. 10. 16:06

 

SOURCE 공개

 

1. I2C용 RTC S3530A 루틴

 

//-------- S3530A STATUS REG -----------

#define    S3530_REG_INT2FE_BIT    0x01        //INT2 level interrupt set

#define    S3530_REG_INT1FE_BIT    0x02        //INT1 level interrupt set

#define    S3530_REG_INT2ME_BIT    0x04        //INT2 edge interrupt

#define    S3530_REG_INT1ME_BIT    0x08        //INT2 edge interrupt

#define    S3530_REG_INT2AE_BIT    0x10 //INT2 interrupt output enable(1), disabled(0).

#define    S3530_REG_INT1AE_BIT    0x20 //INT1 interrupt output enable(1), disabled(0).

#define    S3530_REG_1224_BIT    0x40 //12(0), 24(1) time mode set

#define    S3530_REG_POWER_BIT    0x80 //Power Status, Normal(1), Fail(0)

 

typedef    struct{    

        _uchar    your;

        _uchar    mon;

        _uchar    day;

        _uchar    day_week;

        _uchar    time;

        _uchar    min;

        _uchar    sec;

        _uchar    reg;

        

}_rtc;

 

 

xdata _rtc rtc_temp;    //data를 읽은 원본,data를 저장할때 쓰는 버퍼

xdata _rtc rtc;

 

void rtc_time_read()

{

    i2c_start();

    i2c_write(0x65);      //access 1 mode

    rtc_temp.your = i2c_read(YES);

    rtc_temp.mon = i2c_read(YES);

    rtc_temp.day = i2c_read(YES);

    rtc_temp.day_week = i2c_read(YES);

    rtc_temp.time = i2c_read(YES);

    rtc_temp.min = i2c_read(YES);

    rtc_temp.sec = i2c_read(NO);

    i2c_stop();

 

    //data 가공

    rtc_temp.your = by_byte(rtc_temp.your);

    rtc_temp.mon = by_byte(rtc_temp.mon);

    rtc_temp.day = by_byte(rtc_temp.day);

    rtc_temp.day_week = by_byte(rtc_temp.day_week);

    rtc_temp.time = by_byte(rtc_temp.time);

    if(rtc_temp.time & 0x80) //상위 bit 1이면 12시간 제의 오후

    {

        rtc_temp.time = rtc_temp.time & 0x7F;

        //rtc_temp.time = rtc_temp.time + 11; //24시간 형태로 변경

        

    }

 

    rtc_temp.min = by_byte(rtc_temp.min); //Control register

    rtc_temp.sec = by_byte(rtc_temp.sec) & 0x7F; //Control register

 

    //data 버퍼에 복사

    rtc.your= rtc_temp.your;

    rtc.mon= rtc_temp.mon;

    rtc.day= rtc_temp.day;

    // //p_sts.rtc_time[3] = rtc.day_week = rtc_temp.day_week;

    rtc.time = rtc_temp.time;

    rtc.min = rtc_temp.min; //Control register

    rtc.sec = rtc_temp.sec; //Control register

 

 

}

 

/*

* 레지스터 내용

* bit 7 : 1(전원 들어옴) ,read only

* bit 6 : 0(12시간제) , 1(24시간제)

* bit 5 : /INT1 동작, 0(동작안함), 1(동작함)

* bit 4 : /INT2 동작, 0(동작안함), 1(동작함)

* bit 3 : 인터럽트 동작 설정 찾아서 해보길

* bit 2 :

* bit 1 :

* bit 0 :

*/

 

void rtc_reg_read()

{

    i2c_start();

    i2c_write(0x65);      //access 1 mode

     _nop_ ();

    rtc_temp.reg = i2c_read(NO);

    _nop_ ();

    i2c_stop();

    rtc.reg = by_byte(rtc_temp.reg); //Control register

 

}

 

//RTC 데이터 쓰기.....

void rtc_time_write()

{

 

    rtc_temp.your = by_byte(rtc_temp.your);

    rtc_temp.mon = by_byte(rtc_temp.mon);

    rtc_temp.day = by_byte(rtc_temp.day);

    //rtc_temp.day_week = by_byte(rtc_temp.day_week);

    rtc_temp.time = by_byte(rtc_temp.time);

    rtc_temp.min = by_byte(rtc_temp.min);

    rtc_temp.sec = by_byte(rtc_temp.sec);

 

 

    i2c_start();

    i2c_write(0x64);      //access 1 mode

     _nop_ ();

     _nop_ ();

    i2c_write(rtc_temp.your);

     _nop_ ();

     _nop_ ();

    i2c_write(rtc_temp.mon);

     _nop_ ();

     _nop_ ();

    i2c_write(rtc_temp.day);

     _nop_ ();

     _nop_ ();

    i2c_write(rtc_temp.day_week);     //주의 어떻게 될지 모름

     _nop_ ();

     _nop_ ();

    i2c_write(rtc_temp.time);

     _nop_ ();

     _nop_ ();

    i2c_write(rtc_temp.min);

     _nop_ ();

     _nop_ ();

    i2c_write(rtc_temp.sec);

     _nop_ ();

     _nop_ ();

    i2c_stop();

 

}

 

// RTC초기화 부분.. 상태register 에 쓰는부분....

void rtc_reg_write()

{

    rtc_temp.reg = by_byte(rtc_temp.reg);

 

    i2c_start();

    i2c_write(0x62);      //access 1 mode

     _nop_ ();

     _nop_ ();

    i2c_write(rtc_temp.reg);

     _nop_ ();

     _nop_ ();

    i2c_stop();

}

 

void rtc_init()

{

    rtc_temp.reg = S3530_REG_1224_BIT;    //rtc초기화

    rtc_reg_write();

}

 

//상위비트와 하위 비트를 바꾸어 준다.

unsigned char by_byte(unsigned char in_data)

{

    unsigned char i,temp_data, out_data = 0;

    bit k;

 

    for(i=0 ; i<8 ;i++)

    {

 

        k = ((in_data & 0x80) ? 1 : 0); //msb data

        in_data <<= 1;     // Shift the byte by one bit

        temp_data = 0;

        temp_data |= k;        //lsb data

        temp_data <<= i;

        out_data |= temp_data;

    }

 

    return out_data;

}

'공부 > 8051' 카테고리의 다른 글

[ADUC831] ADUC831 간단 매뉴얼 2  (0) 2012.11.12
[ADUC831] ADUC831 간단 매뉴얼 1  (0) 2012.11.12
[ADUC812] ADUC812 설명 8  (0) 2012.11.10
[ADUC812] ADUC812 설명 7  (0) 2012.11.10
[ADUC812] ADUC812 설명 6  (0) 2012.11.10
[ADUC812] ADUC812 설명 5  (0) 2012.11.10
posted by 큰파도

댓글을 달아 주세요