[ บทความ : ตัวอย่างสำหรับ AVR ตอนที่ 12 ] ทดสอบ RTC ของ CP-jrAVR

ตัวอย่างซอร์สโค้ดสำหรับติดต่อกับ RTC เบอร์ DS1307 ของบอร์ด CP-jrAVR เป็นดังนี้

;***************************************************************************
;*                  APPLICATION  NOTE  FOR  THE  CP-JRAVR                  *
;*                                                                         *
;* Control Board        :CP-JRAVR                                          *
;* File Name            :"DS1307.ASM"                                     *
;* Title                :RTC(DS1307) TO RS232                              *
;*                      :9600,N,8,1                                        *
;* Date                 :25/02/43                                          *
;* Version              :1.0                                               *
;* Support telephone    :712 1120-1 (ETT)                                  *
;* Support fax          :3917216 (ETT)                                     *
;* Support www          :www.etteam.com,www.ett.co.th                      *
;* Target MCU           :AT90S2313/AT90LS2313                              *
;***************************************************************************

.include "2313def.inc"

                  .ORG    $0000 
                   RJMP   RESET_VEC                ;Reset Handle

.EQU         PORT_EE   =  PORTD
.EQU         DDR_EE    =  DDRD
.EQU         PIN_EE    =  PIND
.EQU         BIT_SCL   =  5
.EQU         BIT_SDA   =  6

.DEF         TEMP      =  R19
.DEF         TEMP1     =  R20
.DEF         TEMP2     =  R25 
       
RESET_VEC:        LDI     TEMP,LOW(RAMEND)
                  OUT     SPL,TEMP            ;init Stack Pointer Low     
                  LDI     TEMP,HIGH(RAMEND)
                  OUT     SPL+1,TEMP          ;init Stack Pointer High

                  CLI                         ;clear golbal interrupt
                  CBI     UCR,RXCIE           ;clesr rx interrupt
                  CBI     UCR,TXCIE           ;clesr tx interrupt
                  CBI     UCR,UDRIE           ;clesr data empty interrupt
                  CBI     UCR,CHR9            ;send 8 data 

                  LDI     R17,51
                  OUT     UBRR,r17            ;baud 9600 at 8MHz Xtal 
  
                  SBI     UCR,TXEN            ;set pin tx as serial TX 
                  SBI     UCR,RXEN            ;set pin rx as serial RX

                  RCALL   DELAY               ;delay for start

                  SBI     DDR_EE,BIT_SDA
                  SBI     DDR_EE,BIT_SCL 
                  SBI     PORT_EE,BIT_SDA
                  SBI     PORT_EE,BIT_SCL

                  LDI     ZH,high(TEXT_TAB*2)
                  LDI     ZL,low(TEXT_TAB*2)
XX:               LPM
                  LDI     R16,0X00
                  CP      R0,R16
                  BREQ    YY 
                  MOV     TEMP,R0
                  RCALL   TX232
                  ADIW    ZL,1
                  RJMP    XX            

YY:               RCALL   WRITE_BYTE

WAIT:             LDI     TEMP2,2
WAIT1:            RCALL   RANDOM_READ
                  PUSH    TEMP
                  SWAP    TEMP
                  ANDI    TEMP,0X0F
                  RCALL   HEXASC
                  RCALL   TX232

                  POP     TEMP
                  ANDI    TEMP,0X0F
                  RCALL   HEXASC
                  RCALL   TX232
                  LDI     TEMP,0X3A
                  RCALL   TX232
                  DEC     TEMP2
                  BRNE    WAIT1

                  RCALL   RANDOM_READ
                  PUSH    TEMP
                  SWAP    TEMP
                  ANDI    TEMP,0X0F
                  RCALL   HEXASC
                  RCALL   TX232 
                  POP     TEMP
                  ANDI    TEMP,0X0F
                  RCALL   HEXASC
                  RCALL   TX232
                  LDI     TEMP,0X0D
                  RCALL   TX232
                  RJMP    WAIT

;****************************************************
;*                  WRITE BYTE                      *
;****************************************************
WRITE_BYTE:       CBI      PORT_EE,BIT_SDA      ;start condition
                  CBI      PORT_EE,BIT_SCL
                  LDI      TEMP,0XD0            ;write command
                  RCALL    TX_BYTE
                  RCALL    ACKNOW               ;wait acknowledge
                  BRCS     WRITE_BYTE 

                  LDI      TEMP,0X00            ;address high
                  RCALL    TX_BYTE
                  RCALL    ACKNOW               ;wait acknowledge
                  BRCS     WRITE_BYTE

                  LDI      TEMP,00              ;data
                  RCALL    TX_BYTE
                  RCALL    ACKNOW               ;wait acknowledge
                  BRCS     WRITE_BYTE

                  CBI      PORT_EE,BIT_SDA

                  SBI      PORT_EE,BIT_SCL      ;stop condition
                  SBI      PORT_EE,BIT_SDA
                  RET
;*****************************************************
;*                   RANDOM READ                     *
;*****************************************************
RANDOM_READ:      CBI      PORT_EE,BIT_SDA      ;start condition
                  CBI      PORT_EE,BIT_SCL
                  LDI      TEMP,0XD0            ;write command
                  RCALL    TX_BYTE
                  RCALL    ACKNOW               ;wait acknowledge
                  BRCS     RANDOM_READ 

                  MOV      TEMP,TEMP2            ;address high
                  RCALL    TX_BYTE
                  RCALL    ACKNOW               ;wait acknowledge
                  BRCS     RANDOM_READ

                  SBI      PORT_EE,BIT_SCL          ;stop condition
                  SBI      PORT_EE,BIT_SDA

                  CBI      PORT_EE,BIT_SDA
                  CBI      PORT_EE,BIT_SCL

                  LDI      TEMP,0XD1
                  RCALL    TX_BYTE
                  RCALL    ACKNOW
                  BRCS     RANDOM_READ

                  RCALL    RX_BYTE

                  SBI      PORT_EE,BIT_SCL
                  SBI      PORT_EE,BIT_SCL

                  CBI      PORT_EE,BIT_SDA

                  SBI      PORT_EE,BIT_SCL          ;stop condition
                  SBI      PORT_EE,BIT_SDA
                  RET
;*****************************************************
;*             CHECK BIT ACKNOWLEDGE BIT             *
;CARRY = 1: FAIL                                     *
;CARRY = 0: PASS                                     *
;*****************************************************
ACKNOW:           CBI      DDR_EE,BIT_SDA           ;BIT_SDA input port
                  SBI      PORT_EE,BIT_SDA
                  SEC
                  SBI      PORT_EE,BIT_SCL          
                  SBIS     PIN_EE,BIT_SDA
                  CLC
                  CBI      PORT_EE,BIT_SCL
                  SBI      DDR_EE,BIT_SDA          ;BIT_SDA output port
                  RET
;****************************************************
;*               TX 1 BITE TO 24256                 *
;****************************************************
TX_BYTE:          LDI      TEMP1,0X08
TX_BYTE1:         SBI      PORT_EE,BIT_SDA
                  LSL      TEMP
                  BRCS     TX_BYTE2
                  CBI      PORT_EE,BIT_SDA
TX_BYTE2:         SBI      PORT_EE,BIT_SCL
                  CBI      PORT_EE,BIT_SCL
                  DEC      TEMP1
                  BRNE     TX_BYTE1
                  RET
;****************************************************
;*               RX 1 BYTE FROM 24256               *
;****************************************************
RX_BYTE:          CBI      DDR_EE,BIT_SDA
                  SBI      PORT_EE,BIT_SDA
                  LDI      TEMP,0X00
                  LDI      TEMP1,0X08
RX_BYTE1:         SBI      PORT_EE,BIT_SCL
                  SEC
                  NOP
                  NOP
                  NOP
                  NOP
                  NOP
                  SBIS     PIN_EE,BIT_SDA
                  CLC
                  ROL      TEMP
                  CBI      PORT_EE,BIT_SCL
                  NOP
                  NOP
                  NOP
                  NOP
                  NOP
                  DEC      TEMP1
                  BRNE     RX_BYTE1
                  SBI      DDR_EE,BIT_SDA
                  RET

;***************************************************
;*                DELAY TIME FOR START             *
;***************************************************
DELAY:            PUSH     R26
                  PUSH     R27
                  LDI      R27,0XFF
DEL2:             LDI      R26,0XFF
DEL1:             DEC      R26
                  BRNE     DEL1
                  DEC      R27
                  BRNE     DEL2
                  POP      R27
                  POP      R26
                  RET

HEXASC:           LDI      ZL,LOW(ASC_TAB*2)
                  LDI      ZH,HIGH(ASC_TAB*2)
                  ADD      ZL,TEMP
                  LDI      TEMP,0
                  ADC      ZH,TEMP
                  LPM
                  MOV      TEMP,R0
                  RET

ASC_TAB:          .DB     0X30,0X31,0X32,0X33,0X34,0X35
                  .DB     0X36,0X37,0X38,0X39,0X41,0X42
                  .DB     0X43,0X44,0X45,0X46
                             
TX232:            SBIS    USR,UDRE           ;wait until UDRE = 1
                  RJMP    TX232 
                  OUT     UDR,TEMP           ;send data to serial  
                  RET 
            
TEXT_TAB:        .DB     0X0C,"CP-JRAVR V1.0",0X0A,0X0D
                 .DB     "TEST  RTC (DS1307)",0X0A,0X0D,0X00
                




สามารถ download ไฟล์ตัวอย่างของบอร์ด พร้อม assembler ได้เลยครับ


เขียนโดย : ETT
Author : ETT team
e-mail : sales@etteam.com
วันที่ทำการปรับปรุง : ๒๕ พ.ย. ๒๕๔๓