生活随笔
收集整理的這篇文章主要介紹了
爱普生EPSON实时时钟芯片-RX8900SA
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
愛普生EPSON實時時鐘芯片-RX8900SA 一、引腳連接 二、使用方法 三、代碼 "Rx8900sa.c文件" "Rx8900sa.h文件"
一、引腳連接
近期由于項目需求,被指定使用一款實時時鐘芯片RX8900SA,經查詢后發現網絡資源很少,便在此記錄一下使用筆記,方便查詢使用,附代碼。后期發現愛普生的RX8025t芯片手冊內容相近,資源多且有中文手冊,可對比查看。 芯片采用I2C通訊方式,具體時序查看芯片應用手冊,常用的引腳如下: SDA/SCL:用于I2C通訊; /INT:用于鬧鐘報警,正常狀態時是高電平,中斷發生后,引腳電平由 高3V->低; Vbat:連接電池;
二、使用方法
寄存器配置
1.芯片寄存器一共有15個,00~06用來設置和讀取實時時間,數據格式為BCD碼,需要注意的是03WEEK寄存器數據格式是不一樣的,每個位對應周幾(不許進行BCD碼轉換); 2.08~0A是用來對定時鬧鐘的時間配置,鬧鐘配置有兩種方式具體操作在下面; 3.0D~0F是控制寄存器,用來設定中斷方式、定時方式、中斷標志位等;
1.實時時間讀取/設置
1.設備地址為64,想要寫入相應的寄存器,需要先發設備地址,然后寄存器地址,然后數值,比如WriteByteAdd(Rx8900sa_ADD,15,0x08); //使能alarm中斷信號AIE; 2.讀取相應的寄存器,需要先發設備地址,然后相應的寄存器,比如typGetAlarmTime.AF=ReadByteAdd(Rx8900sa_ADD,14);//讀取中斷標志寄存器,AF在第3位,0開始計數。
2.鬧鐘時間讀取/設置
開啟鬧鐘定時報警功能(即到點兒/INT引腳就由高變低),主要分兩步: 1.設置定時時間,鬧鐘最小定時時間為分鐘,設置不到秒,最大設置時間為日,若想要更長時間的定時,添加應用代碼即可,分鐘/小時時間設置向08/09寄存器寫入數值即可,數值80h~FFh中及代表忽略(即每一分鐘或者每小時);這里比較特殊的是day和week的定時,時間設置在0A寄存器,但需要使用0D寄存器的WADA位來控制,0代表周時間報警,1代表日時間報警,0A寄存器數據格式最高位為1的話表示每一天。 2.設置2個寄存器(0D,0F),清除1個中斷標志位;0D Extension Register寄存器中第6位WADA用來控制按照day設定的時間報警,還是用week設定的時間報警,0F Control Register寄存器的第3位AIE是用來使能鬧鐘定時中斷,因此在初始化的時候需要對該為寫1;1個中斷標志位是0E Flag Register寄存器的第3位AF,在初始化的時候需要進行清0,當定時時間到,中斷發生,該標志位置1,/INT引腳電平由3V->0V,清零該標志位后便可進行下一次定時報警,不清除不再會觸發定時報警。
三、代碼
“Rx8900sa.c文件”
其中包括一些延時函數,用于自己的開發環境中重新定義一下即可,這里的OS.h主要用來設定延時函數 代碼片.
#include
< msp430
. h
>
#include
"OS.h"
#include
"Rx8900sa.h" TIME typNowTime
, typSetTime
;
ALARM typSetAlarmTime
, typGetAlarmTime
;
uint8_t
hex_2bcd ( uint8_t s
)
{ uint8_t temp
; uint8_t t
; s
%= 100 ; temp
= s
/ 10 ; if ( ! temp
) { t
= s
; } else { t
= temp
* 6 + s
; } return t
;
}
void Rx8900sa_ReadNowTime ( void )
{ uint8_t buff_rd
[ 7 ] ; uint8_t i
; for ( i
= 0 ; i
< 7 ; i
++ ) { buff_rd
[ i
] = ReadByteAdd ( Rx8900sa_ADD
, i
) ; } typNowTime
. second
= ( ( buff_rd
[ 0 ] >> 4 ) & 0x07 ) * 10 + ( buff_rd
[ 0 ] & 0x0f ) ; typNowTime
. minute
= ( ( buff_rd
[ 1 ] >> 4 ) & 0x07 ) * 10 + ( buff_rd
[ 1 ] & 0x0f ) ; typNowTime
. hour
= ( ( buff_rd
[ 2 ] >> 4 ) & 0x03 ) * 10 + ( buff_rd
[ 2 ] & 0x0f ) ; typNowTime
. day
= ( ( buff_rd
[ 4 ] >> 4 ) & 0x03 ) * 10 + ( buff_rd
[ 4 ] & 0x0f ) ; typNowTime
. month
= ( ( buff_rd
[ 5 ] >> 4 ) & 0x01 ) * 10 + ( buff_rd
[ 5 ] & 0x0f ) ; typNowTime
. year
= ( ( buff_rd
[ 6 ] >> 4 ) & 0x0f ) * 10 + ( buff_rd
[ 6 ] & 0x0f ) ; switch ( buff_rd
[ 3 ] ) { case 0x02 : { typNowTime
. week
= 1 ; } break ; case 0x04 : { typNowTime
. week
= 2 ; } break ; case 0x08 : { typNowTime
. week
= 3 ; } break ; case 0x10 : { typNowTime
. week
= 4 ; } break ; case 0x20 : { typNowTime
. week
= 5 ; } break ; case 0x40 : { typNowTime
. week
= 6 ; } break ; case 0x01 : { typNowTime
. week
= 7 ; } break ; default : { typNowTime
. week
= 0 ; } break ; }
}
uint16_t
Rx8900sa_SetNowTime ( uint8_t second
, uint8_t minute
, uint8_t hour
, uint8_t week
, uint8_t day
, uint8_t month
, uint8_t year
)
{ uint8_t i
; uint8_t buff_wr
[ 7 ] ; static TIME set_time_old
; typSetTime
. second
= second
; typSetTime
. minute
= minute
; typSetTime
. hour
= hour
; typSetTime
. day
= day
; typSetTime
. month
= month
; typSetTime
. year
= year
; switch ( week
) { case 1 : { typSetTime
. week
= 0x02 ; } break ; case 2 : { typSetTime
. week
= 0x04 ; } break ; case 3 : { typSetTime
. week
= 0x08 ; } break ; case 4 : { typSetTime
. week
= 0x10 ; } break ; case 5 : { typSetTime
. week
= 0x20 ; } break ; case 6 : { typSetTime
. week
= 0x40 ; } break ; case 7 : { typSetTime
. week
= 0x01 ; } break ; default : { typSetTime
. week
= 0 ; } break ; } if ( typSetTime
. second
!= set_time_old
. second
|| typSetTime
. minute
!= set_time_old
. minute
|| typSetTime
. hour
!= set_time_old
. hour
|| typSetTime
. week
!= set_time_old
. week
|| typSetTime
. day
!= set_time_old
. day
|| typSetTime
. month
!= set_time_old
. month
|| typSetTime
. year
!= set_time_old
. year
) { buff_wr
[ 0 ] = hex_2bcd ( typSetTime
. second
) ; buff_wr
[ 1 ] = hex_2bcd ( typSetTime
. minute
) ; buff_wr
[ 2 ] = hex_2bcd ( typSetTime
. hour
) ; buff_wr
[ 3 ] = typSetTime
. week
; buff_wr
[ 4 ] = hex_2bcd ( typSetTime
. day
) ; buff_wr
[ 5 ] = hex_2bcd ( typSetTime
. month
) ; buff_wr
[ 6 ] = hex_2bcd ( typSetTime
. year
) ; for ( i
= 0 ; i
< 7 ; i
++ ) { WriteByteAdd ( Rx8900sa_ADD
, i
, buff_wr
[ i
] ) ; delay_ms ( 5 ) ; } set_time_old
. second
= typSetTime
. second
; set_time_old
. minute
= typSetTime
. minute
; set_time_old
. hour
= typSetTime
. hour
; set_time_old
. week
= typSetTime
. week
; set_time_old
. day
= typSetTime
. day
; set_time_old
. month
= typSetTime
. month
; set_time_old
. year
= typSetTime
. year
; } return 1 ;
}
uint16_t
Rx8900sa_SetAlarmDay ( uint8_t minute
, uint8_t hour
, uint8_t day
)
{ uint8_t i
; uint8_t buff_wr
[ 4 ] ; typSetAlarmTime
. minute
= minute
; typSetAlarmTime
. hour
= hour
; typSetAlarmTime
. day
= day
; buff_wr
[ 0 ] = hex_2bcd ( typSetAlarmTime
. minute
) ; buff_wr
[ 1 ] = hex_2bcd ( typSetAlarmTime
. hour
) ; buff_wr
[ 2 ] = hex_2bcd ( typSetAlarmTime
. day
) ; for ( i
= 8 ; i
< 11 ; i
++ ) { WriteByteAdd ( Rx8900sa_ADD
, i
, buff_wr
[ i
- 8 ] ) ; delay_ms ( 5 ) ; } WriteByteAdd ( Rx8900sa_ADD
, 15 , 0x08 ) ; delay_ms ( 5 ) ; WriteByteAdd ( Rx8900sa_ADD
, 13 , 0x40 ) ; delay_ms ( 5 ) ; rx8900sa_ClearAlarmFlag ( ) ; delay_ms ( 5 ) ; return 1 ;
}
uint16_t
Rx8900sa_SetAlarmWeek ( uint8_t minute
, uint8_t hour
, uint8_t week
)
{ uint8_t i
; uint8_t buff_wr
[ 4 ] ; typSetAlarmTime
. minute
= minute
; typSetAlarmTime
. hour
= hour
; typSetAlarmTime
. week
= week
; buff_wr
[ 0 ] = hex_2bcd ( typSetAlarmTime
. minute
) ; buff_wr
[ 1 ] = hex_2bcd ( typSetAlarmTime
. hour
) ; buff_wr
[ 2 ] = typSetAlarmTime
. week
; for ( i
= 8 ; i
< 10 ; i
++ ) { WriteByteAdd ( Rx8900sa_ADD
, i
, buff_wr
[ i
- 8 ] ) ; delay_ms ( 5 ) ; } WriteByteAdd ( Rx8900sa_ADD
, 10 , buff_wr
[ 2 ] ) ; delay_ms ( 5 ) ; WriteByteAdd ( Rx8900sa_ADD
, 15 , 0x08 ) ; delay_ms ( 5 ) ; WriteByteAdd ( Rx8900sa_ADD
, 13 , 0x00 ) ; delay_ms ( 5 ) ; rx8900sa_ClearAlarmFlag ( ) ; delay_ms ( 5 ) ; return 1 ;
}
void rx8900sa_ClearAlarmFlag ( void )
{ WriteByteAdd ( Rx8900sa_ADD
, 14 , 0x40 ) ;
}
void Rx8900sa_ReadAlarm ( void )
{ uint8_t buff_rd
[ 3 ] ; uint8_t i
; for ( i
= 8 ; i
< 11 ; i
++ ) { buff_rd
[ i
- 8 ] = ReadByteAdd ( Rx8900sa_ADD
, i
) ; } typGetAlarmTime
. minute
= ( ( buff_rd
[ 0 ] >> 4 ) & 0x07 ) * 10 + ( buff_rd
[ 0 ] & 0x0f ) ; typGetAlarmTime
. hour
= ( ( buff_rd
[ 1 ] >> 4 ) & 0x03 ) * 10 + ( buff_rd
[ 1 ] & 0x0f ) ; typGetAlarmTime
. day
= ( ( buff_rd
[ 2 ] >> 4 ) & 0x03 ) * 10 + ( buff_rd
[ 2 ] & 0x0f ) ; typGetAlarmTime
. week
= buff_rd
[ 2 ] ; typGetAlarmTime
. AF = ReadByteAdd ( Rx8900sa_ADD
, 14 ) ; typGetAlarmTime
. AIE = ReadByteAdd ( Rx8900sa_ADD
, 15 ) ; typGetAlarmTime
. WADA = ReadByteAdd ( Rx8900sa_ADD
, 13 ) ; if ( ( typGetAlarmTime
. AF & 0x08 ) != 0 ) { rx8900sa_ClearAlarmFlag ( ) ; }
} uint8_t
GetNowtimeSecond ( void )
{ return typNowTime
. second
;
}
uint8_t
GetNowtimeMinute ( void )
{ return typNowTime
. minute
;
}
uint8_t
GetNowtimeHour ( void )
{ return typNowTime
. hour
;
}
uint8_t
GetNowtimeDay ( void )
{ return typNowTime
. day
;
}
uint8_t
GetNowtimeMonth ( void )
{ return typNowTime
. month
;
}
uint8_t
GetNowtimeYear ( void )
{ return typNowTime
. year
;
}
void Set_SCL ( uint8_t x
)
{ if ( x
) { SCL_H ; } else { SCL_L ; } delay_us ( 20 ) ;
}
void Set_SDA ( uint8_t x
)
{ if ( x
) { SDA_H ; } else { SDA_L ; } delay_us ( 20 ) ;
}
void I2Cstart ( void )
{ SDA_OUTPUT ; Set_SCL ( 1 ) ; Set_SDA ( 1 ) ; Set_SDA ( 0 ) ; delay_us ( 20 ) ;
}
void I2Cstop ( void )
{ SDA_OUTPUT ; Set_SCL ( 0 ) ; Set_SDA ( 0 ) ; Set_SCL ( 1 ) ; Set_SDA ( 1 ) ;
}
void I2CResponseAck ( void )
{ uint16_t i
= 0 ; Set_SCL ( 1 ) ; SDA_INPUT ; while ( ( SDA_X ) && ( i
< 2500 ) ) { i
++ ; } Set_SCL ( 0 ) ;
} void write_byte ( uint8_t data
)
{ uint8_t i
; SDA_OUTPUT ; for ( i
= 0 ; i
< 8 ; i
++ ) { Set_SCL ( 0 ) ; if ( ( data
& 0x80 ) != 0 ) { Set_SDA ( 1 ) ; } else { Set_SDA ( 0 ) ; } Set_SCL ( 1 ) ; data
= data
<< 1 ; } Set_SCL ( 0 ) ;
}
uint8_t
read_byte ( void )
{ uint8_t i
, k
; Set_SCL ( 0 ) ; SDA_INPUT ; for ( i
= 0 ; i
< 8 ; i
++ ) { k
= k
<< 1 ; Set_SCL ( 1 ) ; if ( SDA_X ) { k
= k
| 1 ; } Set_SCL ( 0 ) ; } return k
;
}
void I2CSendAck ( uint8_t n
)
{ Set_SCL ( 0 ) ; SDA_OUTPUT ; if ( n
) { Set_SDA ( 1 ) ; } else { Set_SDA ( 0 ) ; } Set_SCL ( 1 ) ;
}
void WriteByteAdd ( uint8_t iic_add
, uint8_t byte_add
, uint8_t data
)
{ I2Cstart ( ) ; write_byte ( iic_add
) ; I2CResponseAck ( ) ; write_byte ( byte_add
) ; I2CResponseAck ( ) ; write_byte ( data
) ; I2CResponseAck ( ) ; I2Cstop ( ) ;
}
uint8_t
ReadByteAdd ( uint8_t iic_add
, uint8_t byte_add
)
{ uint8_t temp
; I2Cstart ( ) ; write_byte ( iic_add
) ; I2CResponseAck ( ) ; write_byte ( byte_add
) ; I2CResponseAck ( ) ; I2Cstart ( ) ; write_byte ( iic_add
| 1 ) ; I2CResponseAck ( ) ; temp
= read_byte ( ) ; I2CSendAck ( 1 ) ; I2Cstop ( ) ; return temp
;
}
“Rx8900sa.h文件”
代碼片.
#ifndef _RX8900SA_H
#define _RX8900SA_H#define uint8_t unsigned char
#define uint16_t unsigned int
#define uint32_t unsigned long#define Rx8900sa_ADD
0X64 typedef struct TIME_type
{ unsigned char second
; unsigned char minute
; unsigned char hour
; unsigned char week
; unsigned char day
; unsigned char month
; unsigned char year
;
} TIME ;
extern
TIME typNowTime
, typSetTime
; typedef struct ALARM_type
{ unsigned char minute
; unsigned char hour
; unsigned char week
; unsigned char day
; unsigned char
AF ; unsigned char
AIE ; unsigned char
WADA ;
} ALARM ;
extern
ALARM typSetAlarmTime
, typGetAlarmTime
;
#define
SCL BIT5
#define
SCL_OUTPUT ( P3DIR |= SCL )
#define
SCL_L ( P3OUT &= ~ SCL )
#define
SCL_H ( P3OUT |= SCL ) #define
SDA BIT7
#define
SDA_OUTPUT ( P3DIR |= SDA )
#define
SDA_INPUT ( P3DIR &= ~ SDA )
#define
SDA_L ( P3OUT &= ~ SDA )
#define
SDA_H ( P3OUT |= SDA )
#define
SDA_X ( P3IN & SDA ) extern uint16_t
Rx8900sa_SetNowTime ( uint8_t second
, uint8_t minute
, uint8_t hour
, uint8_t week
, uint8_t day
, uint8_t month
, uint8_t year
) ;
extern uint16_t
Rx8900sa_SetAlarmDay ( uint8_t minute
, uint8_t hour
, uint8_t day
) ;
extern uint16_t
Rx8900sa_SetAlarmWeek ( uint8_t minute
, uint8_t hour
, uint8_t week
) ;
extern uint8_t
ReadByteAdd ( uint8_t iic_add
, uint8_t byte_add
) ;
extern
void WriteByteAdd ( uint8_t iic_add
, uint8_t byte_add
, uint8_t data
) ;
extern
void Rx8900sa_ReadNowTime ( void ) ;
extern
void Rx8900sa_ReadAlarm ( void ) ;
extern
void rx8900sa_ClearAlarmFlag ( void ) ;
#endif
總結
以上是生活随笔 為你收集整理的爱普生EPSON实时时钟芯片-RX8900SA 的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔 網站內容還不錯,歡迎將生活随笔 推薦給好友。