本應(yīng)用筆記介紹如何將DS1390連接至內(nèi)置SPI?接口模塊的摩托羅拉DSP。該電路使用摩托羅拉DSP56F800DEMO演示板和CodeWarrior? IDE。
描述
DS1390實時時鐘(RTC)可通過SPI接口與微控制器(μC)或數(shù)字信號處理(DSP)單元連接。本應(yīng)用筆記介紹如何將DS1390連接至內(nèi)置SPI接口模塊的摩托羅拉DSP。該電路使用摩托羅拉DSP56F800DEMO演示板和CodeWarrior IDE。
使用示例軟件
示例軟件是從空白項目開始開發(fā)的。按照摩托羅拉套件安裝指南(教程:創(chuàng)建 CodeWarrior 項目)中的說明進(jìn)行操作,了解詳細(xì)信息。在main.c中添加本應(yīng)用筆記中包含的代碼。
操作
該程序使用GPIO端口來控制DS1390上的CS。軟件初始化SPI控制器模塊,DSP將時間和日期寫入DS1390。然后,軟件循環(huán)讀取時間和日期。DS1390和DS1391支持SPI模式1和3。
電路原理圖如圖1所示。該電路包括連接到摩托羅拉演示板的子卡。請注意,圖1中的電路包括幾個帶SPI接口的RTC。一次只能使用一個RTC,軟件僅支持DS1390。該軟件如圖 2 所示。
圖1.
圖 2.子卡示意圖。
圖3.代碼清單
/* File: DS1390.c */ /* This example program was developed using the Motorola 56F800 Demo Board Kit. Follow the kit instalation guide for creating a CodeWarrior Project. Use the shell of the new project for this example. Note: This program is for example only and is not supported by Dallas Semiconductor Maxim. */ #include "port.h" #include "stdio.h" #include "stdlib.h" /******************************************************* * Main program for use with Embedded SDK *******************************************************/ extern sampleASM (void); void reset_spi(void); void wbyte_spi(unsigned char); void init_sci0(Word16); tx_sci0(unsigned char); void bcd2ascii(unsigned char); unsigned char rbyte_spi(void); #define REG_BASE 0x0000 #define SCI0_BASE 0x0F00 #define SPI_BASE 0x0F20 #define GPIOA_BASE 0x0FB0 #define GPIOB_BASE 0x0FC0 #define SCI0_SCIBR *(volatile UWord16 *)(SCI0_BASE + 0) #define SCI0_SCICR *(volatile UWord16 *)(SCI0_BASE + 1) #define SCI0_SCISR *(volatile UWord16 *)(SCI0_BASE + 2) #define SCI0_SCIDR *(volatile UWord16 *)(SCI0_BASE + 3) #define SPSCR *(volatile UWord16 *)(SPI_BASE + 0) #define SPDSR *(volatile UWord16 *)(SPI_BASE + 1) #define SPDRR *(volatile UWord16 *)(SPI_BASE + 2) #define SPDTR *(volatile UWord16 *)(SPI_BASE + 3) #define GPIO_A_PUR *(volatile UWord16 *)(GPIOA_BASE + 0) #define GPIO_A_DR *(volatile UWord16 *)(GPIOA_BASE + 1) #define GPIO_A_DDR *(volatile UWord16 *)(GPIOA_BASE + 2) #define GPIO_A_PER *(volatile UWord16 *)(GPIOA_BASE + 3) #define GPIO_B_PUR *(volatile UWord16 *)(GPIOB_BASE + 0) #define GPIO_B_DR *(volatile UWord16 *)(GPIOB_BASE + 1) #define GPIO_B_DDR *(volatile UWord16 *)(GPIOB_BASE + 2) #define GPIO_B_PER *(volatile UWord16 *)(GPIOB_BASE + 3) void main (void) { unsigned char msec=0, min=0x26, sec=0x00, hr=0x17, dow=0x06, date=0x26, mon=0x12, yr=0x03, write = 0; reset_spi(); init_sci0(195); // 30MHz / 195 = 9600 baud GPIO_B_DR = 0x0008; // disable RTC - CS high GPIO_B_DR = 0; // enable RTC - CS low wbyte_spi(0x8d); // control register write address rbyte_spi(); // dummy read wbyte_spi(0x18); // enable osc, 32kHz sqw rbyte_spi(); GPIO_B_DR = 0x0008; // disable RTC - CS high if(write) { GPIO_B_DR = 0; // enable RTC - CS low wbyte_spi(0x80); // select seconds register write address rbyte_spi(); // dummy read wbyte_spi(msec); // milliseconds register data rbyte_spi(); wbyte_spi(sec); // seconds register data rbyte_spi(); wbyte_spi(min); // minutes register rbyte_spi(); wbyte_spi(hr); // hours register rbyte_spi(); wbyte_spi(dow); // day of week register rbyte_spi(); wbyte_spi(date); // date register rbyte_spi(); wbyte_spi(mon); // month register rbyte_spi(); wbyte_spi(yr); // year register rbyte_spi(); GPIO_B_DR = 0x0008; // disable RTC - CS high } while(1) { GPIO_B_DR = 0u; // enable RTC - CS low wbyte_spi(0); // seconds register read address rbyte_spi(); // dummy read wbyte_spi(0); msec = rbyte_spi(); // read milliseconds register wbyte_spi(0); sec = rbyte_spi(); // read seconds register wbyte_spi(0); min = rbyte_spi(); // ditto minutes wbyte_spi(0); hr = rbyte_spi(); // and so on wbyte_spi(0); dow = rbyte_spi(); wbyte_spi(0); date = rbyte_spi(); wbyte_spi(0); mon = rbyte_spi(); wbyte_spi(0); yr = rbyte_spi(); GPIO_B_DR = 0x0008; // disable RTC - CS high tx_sci0(0x0d); // sequence to print time & date tx_sci0(0x0a); bcd2ascii(yr); tx_sci0('/'); bcd2ascii(mon); tx_sci0('/'); bcd2ascii(date); tx_sci0(' '); bcd2ascii(hr); tx_sci0(':'); bcd2ascii(min); tx_sci0(':'); bcd2ascii(sec); } return; } //SPSCR //15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0 // r MSB SPRF ERRIE ovrf modf spte modfen spr1 spr0 sprie spmstr cpol cpha spe spite void reset_spi() { int val; SPSCR = 0x0056; // SPR0, SPMSTR, CPHA, SPE SPDSR = 0x0007; // 8-bit size SPSCR &= 0xfffd; // clear spe, resets SPI (partial) SPSCR |= 0x0002; // set spe, new values take effect GPIO_B_PER = 0x00f3; // use GPIOB3 as CS for RTC GPIO_B_DDR = 0x000d; // direction is output GPIO_A_PER = 0x00f9; // enable/disable per function (1=enable) GPIO_A_DDR = 0x0006; // direction is output (1=output) GPIO_A_DR = 0; // write bits low (0=low) } void wbyte_spi( unsigned char wbyte) // ------ write one byte ------- { while (!(SPSCR & 0x0200)); // wait for transmitter empty flag SPDTR = wbyte; } void bcd2ascii(unsigned char dat) // ----- convert bcd to ascii and send to sci ---- { tx_sci0( (dat >> 4) + 0x30); tx_sci0( (dat & 0x0f) + 0x30); } unsigned char rbyte_spi(void) // -------- read one byte ---------- { while (!(SPSCR & 0x2000)); // wait for receiver full flag return(SPDRR); } void init_sci0(Word16 baud) { GPIO_B_PER = 0x00f3; // set up GPIO_B_DDR = 0x000d; // direction is output SCI0_SCIBR = baud; // baud rate SCI0_SCICR = 0x2000; // control reg } tx_sci0(unsigned char val) { UWord16 reg; SCI0_SCICR &= 0xfffb; // turn receiver off SCI0_SCICR |= 8; // turn transmitter on do { reg = SCI0_SCISR; // clear flag by reading } while( (reg & 0x8000) == 0); // wait until RDRF is false SCI0_SCIDR = (unsigned int) (val); }
審核編輯:郭婷
-
dsp
+關(guān)注
關(guān)注
554文章
8059瀏覽量
350551 -
控制器
+關(guān)注
關(guān)注
112文章
16461瀏覽量
179547 -
GPIO
+關(guān)注
關(guān)注
16文章
1217瀏覽量
52410
發(fā)布評論請先 登錄
相關(guān)推薦
DS1390/DS1391/DS1394 pdf datas
DS1392, DS1393,pdf datasheet (
Interfacing a DS1390/DS1391 RT
Interfacing a DS1390/DS1391 RT
DS1390/DS1391 RTC與Motorola SPI
![<b class='flag-5'>DS1390</b>/<b class='flag-5'>DS1391</b> <b class='flag-5'>RTC</b>與Motorola <b class='flag-5'>SPI</b>](https://file1.elecfans.com//web2/M00/A4/C9/wKgZomUMNa-AJyTdAAAek1EF69w932.gif)
摩托羅拉Wilder正式發(fā)布
DS1390-DS1394低電壓SPI/3線接口RTC
DS1390-DS1394數(shù)據(jù)資料
![<b class='flag-5'>DS1390-DS</b>1394數(shù)據(jù)資料](https://file.elecfans.com/web2/M00/49/6B/pYYBAGKhtE6AT2trAAAOoz4w8n0897.jpg)
接口的SPI RTC與摩托羅拉DSP
![<b class='flag-5'>接口</b>的<b class='flag-5'>SPI</b> <b class='flag-5'>RTC</b>與<b class='flag-5'>摩托羅拉</b><b class='flag-5'>DSP</b>](https://file.elecfans.com/web2/M00/49/84/pYYBAGKhtFmANgu6AAAdBbmX6hY272.jpg)
評論