本应用笔记提供了用于将串行外设接口(SPI)RTC与内置SPI接口模块的摩托罗拉DSP连接的硬件和软件示例。本示例使用摩托罗拉DSP演示套件作为电路的基础。
DS1306引脚配置
引脚配置
描述
DS1306实时时钟(RTC)可通过3线或SPI™接口与微控制器(μC)或数字信号处理(DSP)单元连接。本应用笔记介绍了如何将DS1306连接至内置SPI接口模块的摩托罗拉DSP。DS1305也可用于此应用。该电路使用摩托罗拉DSP56F800DEMO演示板和CodeWarrior IDE。
使用示例软件
示例软件是从空白项目开始开发的。按照摩托罗拉套件安装指南(教程:创建 CodeWarrior 项目)中的说明进行操作,了解详细信息。在main.c中添加本应用笔记中包含的代码。
操作
该程序使用GPIO端口来控制DS1306上的CE。软件初始化DSP中的SPI控制器模块,将时间和日期写入DS1306。然后,软件循环读取时间和日期。DS1305和DS1306支持SPI模式1和3。
图1所示为该电路的原理图。该电路包括连接到摩托罗拉演示板的子卡。请注意,图1中的电路包括几个带SPI接口的RTC。一次只能使用一个RTC,软件仅支持DS1306。该软件如图2所示。
图 1.DSP56F800演示板的子卡。
图2.演示代码。
/* File: main.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); unsigned char rbyte_spi(void); #define REG_BASE 0x0000 #define SPI_BASE 0x0F20 #define GPIOB_BASE 0x0FC0 #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_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 min=0x58, sec=0x59, hr=0x09, dow=0x04, date=0x23, mon=0x10, yr=0x03; reset_spi(); GPIO_B_DR = 0; // disble RTC - CS low GPIO_B_DR = 0x0008; // enable RTC - CS high wbyte_spi(0x8f); // control register write address rbyte_spi(); // dummy read wbyte_spi(0); // disable write protect rbyte_spi(); GPIO_B_DR = 0; // disble RTC - CS low GPIO_B_DR = 0x0008; // enable RTC - CS high wbyte_spi(0x80); // select seconds register write address rbyte_spi(); // dummy read 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 = 0; // disble RTC - CS low while(1) { GPIO_B_DR = 0x0008; // enable RTC - CS high wbyte_spi(0); // seconds register read address rbyte_spi(); // dummy read 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 = 0; // disable RTC - CS low } 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 = 0x0096; // 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 = 0x000c; // direction is output } void wbyte_spi( unsigned char wbyte) // ------ write one byte ------- { while (!(SPSCR & 0x0200)); // wait for transmitter empty flag SPDTR = wbyte; } unsigned char rbyte_spi(void) // -------- read one byte ---------- { while (!(SPSCR & 0x2000)); // wait for receiver full flag return(SPDRR); }
审核编辑:郭婷
全部0条评论
快来发表一下你的评论吧 !