APPLICATION NOTE 2361 Interfacing an SPI-Interface RTC with a PIC Microcontroller

Similar documents
Chapter 10 Sections 1,2,9,10 Dr. Iyad Jafar

APPLICATION NOTE Wire Communication with a Microchip PICmicro Microcontroller

Embedded Systems. PIC16F84A Sample Programs. Eng. Anis Nazer First Semester

PIC 16F84A programming (II)

Embedded Systems Programming and Architectures

Lecture (04) PIC16F84A (3)

Hi Hsiao-Lung Chan Dept Electrical Engineering Chang Gung University, Taiwan

Experiment 7:The USART

Assembly Language Instructions

Chapter 4 Sections 1 4, 10 Dr. Iyad Jafar

16.317: Microprocessor Systems Design I Fall 2013 Exam 3 Solution

Outlines. PIC Programming in C and Assembly. Krerk Piromsopa, Ph.D. Department of Computer Engineering Chulalongkorn University

CENG 336 INT. TO EMBEDDED SYSTEMS DEVELOPMENT. Spring 2006

DERTS Design Requirements (1): Microcontroller Architecture & Programming

SOLUTIONS!! DO NOT DISTRIBUTE!!

TOPIC 3 INTRODUCTION TO PIC ASSEMBLY LANGUAGE. E4160 Microprocessor & Microcontroller System. Prepared by : Puziah Yahaya JKE, POLISAS / DEC 2010

Flow Charts and Assembler Programs

LAB WORK 2. 1) Debugger-Select Tool-MPLAB SIM View-Program Memory Trace the program by F7 button. Lab Work

Application Note - PIC Source Code v1.1.doc

Lesson 14. Title of the Experiment: Introduction to Microcontroller (Activity number of the GCE Advanced Level practical Guide 27)

Weekly Report: Interactive Wheel of Fortune Week 4 02/014/07-02/22/07 Written by: Yadverinder Singh

Example of Asyncronous Serial Comms on a PIC16F877

16.317: Microprocessor-Based Systems I Spring 2012

UNIVERSITY OF ULSTER UNIVERSITY EXAMINATIONS : 2001/2002 RESIT. Year 2 MICROCONTROLLER SYSTEMS. Module Code: EEE305J1. Time allowed: 3 Hours

More (up a level)... Connecting the Nokia 3510i LCD to a Microchip PIC16F84 microcontroller

EET203 MICROCONTROLLER SYSTEMS DESIGN Serial Port Interfacing

ECE 354 Introduction to Lab 1. February 5 th, 2003

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING. EE6008 Microcontroller based system design

Dept. of Computer Engineering Final Exam, First Semester: 2016/2017

UNIVERSITY OF ULSTER UNIVERSITY EXAMINATIONS : 2001/2002. Semester 2. Year 2 MICROCONTROLLER SYSTEMS. Module Code: EEE305J2. Time allowed: 3 Hours

Instuction set

ELCT 912: Advanced Embedded Systems

16.317: Microprocessor-Based Systems I Summer 2012

Chapter 13. PIC Family Microcontroller

Chapter 5 Sections 1 6 Dr. Iyad Jafar

Week1. EEE305 Microcontroller Key Points

DHANALAKSHMI COLLEGE OF ENGINEERING, CHENNAI DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING. EE Microcontroller Based System Design

EECE.3170: Microprocessor Systems Design I Summer 2017 Homework 5 Solution

ECE 354 Computer Systems Lab II. Interrupts, Strings, and Busses

EEE111A/B Microprocessors

D:\PICstuff\PartCounter\PartCounter.asm

Embedded System Design

ELE492 Embedded System Design

When JP1 is cut, baud rate is Otherwise, baud rate is Factory default is that JP1 is shorted. (JP1 is jumper type in some model)

movwf prevcod ; a new button is pressed - rcnt=3 movwf (mtx_buffer+1) movlw 3 movwf rcnt

Interfacing PIC Microcontrollers. ADC8BIT2 Schematic. This application demonstrates analogue input sampling

ECE Test #1: Name

Section 21. Addressable USART

ELCT706 MicroLab Session #4 UART Usage for Bluetooth connection PC - PIC

ECE 354 Introduction to Lab 2. February 23 rd, 2003

AN587. Interfacing to an LCD Module. Interfacing to an LCD Module INTRODUCTION OPERATION CONTROL SIGNAL FUNCTIONS TABLE 2: CONDITIONAL ASSEMBLY FLAGS

Arithmetic,logic Instruction and Programs

Serial Communication with PIC16F877A

ELCT706 MicroLab Session #4 UART Usage for Bluetooth connection PC - PIC

SOLUTIONS!! DO NOT DISTRIBUTE PRIOR TO EXAM!!

Serial Communication

PIC16F87X 13.0 INSTRUCTION SET SUMMARY INSTRUCTIONS DESCRIPTIONS

Hardware Interfacing. EE25M Introduction to microprocessors. Part V. 15 Interfacing methods. original author: Feisal Mohammed

University of Jordan Faculty of Engineering and Technology Department of Computer Engineering Embedded Systems Laboratory

16.317: Microprocessor-Based Systems I Fall 2012

A Better Mouse Trap. Consumer Appliance, Widget, Gadget APPLICATION OPERATION: Ontario, Canada

PIC Discussion. By Eng. Tamar Jomaa

The University of Texas at Arlington Lecture 7

ME 6405 Introduction to Mechatronics

/* PROGRAM FOR BLINKING LEDs CONEECTED TO PORT-D */

CONNECT TO THE PIC. A Simple Development Board

Jordan University of Science and Technology Electrical Engineering Department Microcontrollers and Embedded Systems Spring 2011

Micro II and Embedded Systems

PIC PROGRAMMING START. The next stage is always the setting up of the PORTS, the symbol used to indicate this and all Processes is a Rectangle.

ALU and Arithmetic Operations

Outline. Micriprocessor vs Microcontroller Introduction to PIC MCU PIC16F877 Hardware:

Performance & Applications

Binary Outputs and Timing

/ 40 Q3: Writing PIC / 40 assembly language TOTAL SCORE / 100 EXTRA CREDIT / 10

THE UNIVERSITY OF THE WEST INDIES

Laboratory Exercise 5 - Analog to Digital Conversion

EXPERIMENT 4: Parallel Input/Output. Objectives Introduction to the Parallel Input/Output (I/O) Familiarization to Interfacing

Chapter 3: Further Microcontrollers

ECE 354 Computer Systems Lab II. Memory and Indirect Addressing

Input/Output Ports and Interfacing

16.317: Microprocessor Systems Design I Fall 2015

Done 1 NVB THF TLF T/R POL 1SHOT. Figure 1: Status register

Chapter 5. Problems All programming problems should include design pseudo code either as a separate design document on embedded comments in the code.

Laboratory: Introduction to Mechatronics. Instructor TA: Edgar Martinez Soberanes Lab 2. PIC and Programming

DISCONTINUED. SPI Communication with AMT bit Absolute Encoder

Fortune. Semiconductor Corporation 富晶半導體股份有限公司. 8-bit MCU with 1k program ROM, 64-byte RAM, 1 R2F module and 3 13 LCD driver. TD Rev. 1.

Mechatronics and Measurement. Lecturer:Dung-An Wang Lecture 6

Section 4. Architecture

CHAPTER 6 CONCLUSION AND SCOPE FOR FUTURE WORK

Q1: Multiple choice / 20 Q2: Protected mode memory accesses. / 40 Q3: Reading PIC. / 40 assembly language TOTAL SCORE / 100

PTK8756B 8 Bit Micro-controller Data Sheet

Figure 1: Pushbutton without Pull-up.

16.317: Microprocessor Systems Design I Spring 2015

Maxim > Design Support > Technical Documents > Application Notes > Microcontrollers > APP 4465

PIC16C63A/65B/73B/74B

ECE Homework #3

/ 28 HLL assembly Q4: Conditional instructions / 40 TOTAL SCORE / 100 EXTRA CREDIT / 10

The University of Texas at Arlington Lecture 5

EECE.3170: Microprocessor Systems Design I Spring 2016

Hi Hsiao-Lung Chan Dept Electrical Engineering Chang Gung University, Taiwan

Transcription:

Maxim/Dallas > App Notes > REAL-TIME CLOCKS Keywords: DS1305, SPI, PIC, real time clock, RTC, spi interface, pic microcontroller Aug 20, 2003 APPLICATION NOTE 2361 Interfacing an SPI-Interface RTC with a PIC Microcontroller This application note provides an example schematic and software for using the DS1305 real-time clock (RTC) with a PIC microcontroller. The DS1305 is connected to the PIC using the SPI interface. A serial RS-232 port is used for data input and output. Pin Configuration Description The DS1305 real-time clock (RTC) can be interfaced with a microcontroller (µc) using a 3-wire or an SPI interface. This application note shows how to connect a DS1305 to a PIC16F628µC. The DS1306 could also be used in this application. The circuit uses a serial interface for communications. A terminal program with user control of the RS232 DTR control line is required. DTR is used to reset the µc and start code execution. A DS232 is used to perform TTL/RS232 level translation.

A schematic of the circuit is shown in Figures 1 and 2. The software is shown in Figure 3. For Larger Image Figure 1. PIC16F628 interface. For Larger Image Figure 2. DS1305 daughter card. Figure 3. Code for Demo

#include <P16F628.inc> list p=16f628 config H'3F2A' ; this config gives us LVP and enables /MCLR errorlevel -302 ; don't print message for operands that are not in bank 0 ; define the baud rate for the hardware uart #define BAUD_VALUE 0x15 ; 0x15 sets the baud rate to 57600 with a 20.0MHz crystal #define SPI_CLK PORTA,1 ; spi bus clock line #define SPI_MOSI PORTA,2 ; spi master out data #define SPI_MISO PORTA,3 ; spi slave input data #define SPI_CE PORTA,4 ; chip enable for SPI device SCRATCH equ 0x40 ; 1 by general purpose scratchpad TMP equ 0x41 ; temp register TMP2 equ 0x42 ; temp register COUNT equ 0x43 YRS equ 0x44 MON equ 0x45 DOW equ 0x46 DAYS equ 0x47 HRS equ 0x48 MINS equ 0x49 SECS equ 0x4a user_bits equ 0x2C ; this is 0x0C for the 16F84 save_w equ 0x38 save_status equ 0x39 SET_BANK0 MACRO bcf STATUS, RP0 bcf STATUS, RP1 ENDM SET_BANK1 MACRO bsf STATUS, RP0 bcf STATUS, RP1 ENDM org 0x00 RESET: goto START ;--------------- start --------------- org 0x0A START: ; turn off the comparator for porta SET_BANK0 movlw 0x07 movwf CMCON ; turn off the voltage reference module SET_BANK1 movlw 0x00 movwf VRCON SET_BANK0 clrf PORTA ; initialize PORTA

movlw 0x08 ; RA3 read (high-z) SET_BANK1 movwf TRISA ; set pins for input or output bsf OPTION_REG, 7 ; turn weak pull-ups on all inputs SET_BANK0 movlw 0x07 movwf CMCON ; Initialize CMCON call uart_init CheckForCommands: movlw banner-1 ; move label address into W register ; print string starting at address of label call uart_getchar ; s character in W ; echo movwf TMP bcf TMP,5 ; convert to upper case movf TMP,W ; put back in W xorlw 'S' ; write to RTC btfss STATUS,Z goto not_ss call set_clock ; set the clock using data from user goto CheckForCommands not_ss: movf TMP,W ; retrieve character xorlw 'R' ; read from RTC btfss STATUS,Z goto not_rr call read_clock ; display time and date via serial port goto CheckForCommands not_rr: goto CheckForCommands ;--- uart routines --- ;---- send a byte through the serial port ---- uart_putchar: charwait1: btfss PIR1, TXIF goto charwait1 movwf TXREG ;---- get a byte from the serial port ---- uart_getchar: charwait2: btfss PIR1, RCIF ; is data avalible? goto charwait2 ; if not then wait movfw RCREG ;----- initialize the serial port ----- uart_init: SET_BANK1 movlw BAUD_VALUE movwf SPBRG ; set the baud rate ; mov baudreg into SPBRG, set baud rate

bcf TXSTA, SYNC ; clear SYNC bit, asynchronous mode bsf TXSTA, BRGH ; BRGH=1, high speed SP mode. bsf TXSTA, TXEN ; enable transmission bcf PIE1, RCIE ; disable serial port interrupt SET_BANK0 bsf RCSTA, SPEN ; set SPEN bit, serial port enable bsf RCSTA, CREN ; set CREN bit, serial port receive enable ; ;---------------------------------------- ;-- text strings for user interface -- ;---------------------------------------- banner: dt "\n\rds1305 SPI DEMO\n\rR)ead time S)et time\n\r",0h year: dt "\n\ryear (0-99): ",0h month: dt "Month (1-12): ",0h dow: dt "Day of Week (1-7): ",0h date: dt "Date (1-28,29,30,31): ",0h hour: dt "Hour (0-23): ",0h minute: dt "Minute (0-59): ",0h second: dt "Second (0-59): ",0h ;-- character conversion routines -- ;--------- ascii to bcd ---------- readbcd: clrf TMP ; clear temp reg gobcd: call uart_getchar ; s character in W ; echo to screen xorlw 0dh ; if cr, Z will be set btfss STATUS,Z ; skip if clear goto bcd ; go to bcd if Z=0 movf TMP,W ; done, move final value to W ; and bcd: xorlw 0dh ; restore value addlw -30h ; subtract ascii offset btfsc W,4 ; jump if not A-F addlw -7 ; if A-F, subtract 7 digit: andlw 0x0f ; clear upper nibble bcf TMP,4 ; clear upper nibble of temp reg bcf TMP,5 bcf TMP,6 bcf TMP,7 movwf SCRATCH ; save W movf TMP,W ; copy TMP to W movwf TMP2 ; save TMP movf SCRATCH,W ; restore W movwf TMP ; TMP now has org W value movf TMP2,W ; W now has org TMP value

swapf TMP2,W ; swap nibbles iorwf TMP,W ; insert bits 0-3 of TMP to W movwf TMP ; move W into temp reg goto gobcd ; continue until CR is encountered ;-- convert bcd to ascii -- ;-- entry: W=bcd value exit: W=last ascii -- writebcd: movwf TMP ; save W swapf TMP,W ; swap nibbles andlw 0x0f ; clear bits 4-7 addlw 0x06 ; add 6 btfss STATUS,DC ; if a-f, DC=1 goto lessnine ; if DC=0, < 9, so goto lessnine addlw 0x31 ; add 31h to make ascii goto digit1 ; skip to output lessnine: addlw 0x2a ; add offset for 0-9 to make ascii digit1: ; print char movf TMP,W ; restore W andlw 0x0f ; clear bits 4-7 addlw 0x06 ; add 6 btfss STATUS,DC ; if a-f, DC=1 goto lessnine2 ; if DC=0, < 9, so goto lessnine addlw 0x31 ; add 31h to make ascii goto digit2 ; skip to output lessnine2: addlw 0x2a ; add offset for 0-9 to make ascii digit2: ; print char ---- ;-- display RTC data -- ---- read_clock: call RTC_brst_rd ; get the data from the RTC read_regs: movf YRS,W bcd movlw '/' movf MON,W bcd movlw '/' movf DAYS,W bcd movlw ' ' movf DOW,W bcd movlw ' ' movf HRS,W bcd movlw ':' movf MINS,W bcd movlw ':'

movf SECS,W bcd movlw 0x0d ; cr ---- ;-- write to the RTC with user-entered data -- ---- set_clock: movlw year-1 ; prompt user for data (year) ; get the data movwf YRS ; save it movlw month-1 movwf MON movlw date-1 movwf DAYS movlw dow-1 movwf DOW movlw hour-1 movwf HRS movlw minute-1 movwf MINS movlw second-1 movwf SECS call RTC_brst_wr ; now write data to RTC ;-- RTC routines -- RTC_brst_rd: bsf SPI_CLK ; assert SCLK for CPOL=1 bsf SPI_CE ; assert CE movlw 0h ; seconds register read address ; send the address call read_rtc ; read the seconds data movwf SECS ; save it call read_rtc ; and so on movwf MINS call read_rtc movwf HRS call read_rtc

movwf DOW call read_rtc movwf DAYS call read_rtc movwf MON call read_rtc movwf YRS bcf SPI_CE ; de-assert CE RTC_brst_wr: bsf SPI_CLK ; assert SCLK for CPOL=1 bsf SPI_CE ; assert CE movlw 08fh ; control register write address movlw 0 ; clear write protect bcf SPI_CE ; de-assert CE bsf SPI_CLK ; assert SCLK for CPOL=1 bsf SPI_CE ; assert CE movlw 08fh ; control register write address movlw 0 ; enable osc, disable interrupts bcf SPI_CE ; de-assert CE bsf SPI_CLK ; assert SCLK for CPOL=1 bsf SPI_CE ; assert CE movlw 80h ; send seconds register write address movf SECS, W movf MINS, W movf HRS, W movf DOW, W movf DAYS, W movf MON, W movf YRS, W bcf SPI_CE ; de-assert CE ;---- Read RTC into W (assume address already sent) ---- ;---- assumes CE is asserted ---- read_rtc: movlw 08h ;Send 8 bits movwf COUNT SPI_read_loop: rlf TMP, 1 bcf SPI_CLK ; clock data out bcf TMP, 0 ; assume data out is low btfsc SPI_MISO

bsf TMP, 0 ; if data out=1, set bit bsf SPI_CLK decfsz COUNT, 1 goto SPI_read_loop movf TMP, W ;--- Write the byte in W to RTC --- ;---- assumes CE is asserted ---- write_rtc: movwf TMP ;Save the data ; ;--- Do a SPI bus write of byte in 'TMP' --- ; SPI_write: movlw 08h ;Send 8 bits movwf COUNT SPI_w_loop: bcf SPI_CLK bcf SPI_MOSI ; assume data out is low btfsc TMP, 7 bsf SPI_MOSI ; if data out=1, set bit SPI_w_cont: rlf TMP, 1 bsf SPI_CLK ; clock it in decfsz COUNT, 1 goto SPI_w_loop ;-- pclsub used for indirect addressing -- pclsub: incf SCRATCH,F ; advance table pointer movf SCRATCH,W ; move table pointer to W movwf PCL ; jump to address pointed by PCLATH,W ;---------------------------------------- ;-- write a string to USART -- ;---------------------------------------- write: movwf SCRATCH ; FSR = string address GoWrite: call pclsub ; advance pointer and read pointed byte addlw 0h ; if contents are zero, Z will be set btfsc STATUS,Z ; skip if clear ; current character is null: end of string ; print one character goto GoWrite ; loop END Application Note 2361: http://www.maxim-ic.com/an2361 More Information

For technical questions and support: http://www.maxim-ic.com/support For samples: http://www.maxim-ic.com/samples Other questions and comments: http://www.maxim-ic.com/contact Related Parts DS1305: QuickView -- Full (PDF) Data Sheet -- Free Samples DS1306: QuickView -- Full (PDF) Data Sheet -- Free Samples DS1390: QuickView -- Full (PDF) Data Sheet -- Free Samples DS1391: QuickView -- Full (PDF) Data Sheet -- Free Samples AN2361, AN 2361, APP2361, Appnote2361, Appnote 2361 Copyright 2005 by Maxim Integrated Products Additional legal notices: http://www.maxim-ic.com/legal