1434 views|0 replies

6609

Posts

0

Resources
The OP
 

MSP430G2553 connects to SD30 series C program (for reference) [Copy link]

This post was last edited by Jacktang on 2018-1-23 22:34 /*********************************************************************** * MSP430G2553 connects to SD30 series C program (for reference) * DCO: 1Mhz * Occupies 2 IO: P1.4 P1.5 * Special thanks to Mr.Ray for providing this program * This program is modified based on the program provided by Mr.Ray * 2014.07.01 ***************************************************************************/ #include
#include "dco_1m_delay.h" //delay program, you can write it yourself #include "uart.h" //serial debugging, you can remove #define uchar unsigned char #define uint unsigned int /******************************************************************* ***Function definition***************************************************************/ uchar I2CStart(void); uchar I2CWaitAck(void); uchar I2CReceiveByte(void); void I2CStop(void); void I2CAck(void); void I2CNoAck(void); void I2CSendByte(uchar Byte); void I2CReadDate(void); void WriteTimeOn(void); void WriteTimeOff(void); void I2CWriteDate(void); /*********************************************************************** ***P1.5 SDA **************************************************************************/ #define SDA_IN P1DIR &=~ BIT5 //Input #define SDA_OUT P1DIR |= BIT5 //Output #define SDA_H P1OUT |= BIT5 //SDA = 1; #define SDA_L P1OUT &= ~BIT5 //SDA = 0; #define SDA_Read (P1IN & BIT5) //Data read/*********************************************************************** ***P1.4 SCL ***********************************************************************/ #define SCL_IN P1DIR&=~BIT4 //Input #define SCL_OUT P1DIR |= BIT4 //Output #define SCL_H P1OUT |= BIT4 //SCL = 1; #define SCL_L P1OUT &= ~BIT4 //SCL = 0; #define SCL_Read (P1IN & BIT4) //Clock reading/************************************************************************** *Return value definition*******************************************************************/ #define true 1 #define false 0 /********************************************************************** *Initialization time InitTime is: year, month, day, week, hour, minute, second. *Default: 23:59:50, December 31, 2013 ******************************************************************/ uchar ReadTime[7]; //Used to store the read value uchar InitTime[7]={0x13,0x12,0x31,0x02,0x23,0x59,0x50}; uchar bat1,bat2;//Battery power uint bat; //Battery power value/************************************************************************** *Start the I2C bus of SD30 series *************************************************************************/ uchar I2CStart(void) { SDA_OUT;SCL_OUT; _NOP(); SDA_H;SCL_H; _NOP();_NOP(); SDA_IN; _NOP();_NOP(); if(!_NOP();_NOP(); SDA_IN; _NOP();_NOP(); while(SDA_Read)return false; //Bus busy-->>Exit SDA_OUT; _NOP(); SDA_L; _NOP();_NOP(); SDA_IN; _NOP();_NOP(); while(SDA_Read)return false; //Bus error-->>Exit SCL_L; _NOP();_NOP(); return true; } /************************************************************************** *Turn off the I2C bus of the SD30 series***********************************************************************/ void I2CStop(void) { SCL_OUT;SDA_OUT; _NOP(); SDA_L; _NOP();_NOP(); SCL_L; _NOP();_NOP(); SCL_H; _NOP();_NOP(); SDA_H; } /************************************************************************** *Send ACK ***********************************************************************/ void I2CAck(void) { SCL_OUT;SDA_OUT; _NOP();_NOP(); SDA_L; _NOP();_NOP(); SCL_L; _NOP();_NOP(); SCL_H; _NOP();_NOP(); SCL_L; } /********************************************************************** *Send NO ACK ***********************************************************************/ void I2CNoAck(void) { SCL_OUT;SDA_OUT; _NOP(); SDA_H; _NOP(); SCL_L; _NOP();_NOP(); SCL_H; _NOP();_NOP(); /********************************************************************** *Read ACK signal--------Return value: 1 --> ACK; 0 --> No ACK **********************************************************************/ uchar I2CWaitAck(void) //Return value: 1=ACK, 0=no ACK { uchar Wait=255; SCL_OUT; SDA_IN; _NOP(); SCL_L; _NOP();_NOP(); SCL_H; _NOP(); SDA_IN; _NOP(); while(SDA_Read) { Wait--; _NOP(); if(!Wait) {_NOP();_NOP(); SCL_L; return false; } } SCL_L; return true; } /******************************************************************* *MCU sends a byte to SD30 series------data from high to low*******************************************************************/ void I2CSendByte(uchar Byte) { uchar bd=8; SDA_OUT; SCL_OUT; while(bd--) { SCL_L; _NOP(); if(Byte&0x80) {SDA_H;} else {SDA_L;} Byte<<=1; _NOP(); SCL_H; _NOP(); } SCL_L; } /************************************************************************** *MCU Read a byte from SD30 series------data from high to low **********************************************************************/ uchar I2CReceiveByte(void) { uchar i=8; uchar Byte=0; SCL_OUT; _NOP(); _NOP(); SDA_IN; //SDA=1 in c51; port switched to input state!! _NOP(); while(i--) { Byte<<=1; //Data read from high position_NOP(); SCL_L; _NOP();_NOP(); SCL_H; _NOP();_NOP(); if(SDA_Read) { Byte|=0x01; //Non-zero write 1, otherwise shift, default is 0 } } SCL_L; return Byte; } /****************************************************************************** *Read SD30 series real-time data register (that is, read time) ***********************************************************************/ void I2CReadDate(void) { uchar n; I2CStart(); I2CSendByte(0x65); I2CWaitAck(); for(n=0;n<7;n++) { ReadTime[n]=I2CReceiveByte(); if (n!=6) //The last data is not responded to{ I2CAck(); } } I2CNoAck(); _NOP(); I2CStop(); } /****************************************************************** *Allow writing to SD30 ***********************************************************************/ void WriteTimeOn(void) { I2CStart(); I2CSendByte(0x64); I2CWaitAck(); I2CSendByte(0x10); //Set write address to 10H I2CWaitAck(); I2CSendByte(0x80); //Set WRTC1=1 I2CWaitAck(); I2CStop(); I2CStart(); I2CSendByte(0x64); I2CWaitAck(); I2CSendByte(0x0F); //Set write address to 0FH I2CWaitAck(); I2CSendByte(0xFF); //Set WRTC2, WRTC3=1 I2CWaitAck(); I2CStop(); } /******************************************************************* *Prohibit writing to SD30 ***********************************************************************/ void WriteTimeOff(void) { I2CStart(); I2CSendByte(0x64); I2CWaitAck(); I2CSendByte(0x0F); //Set write address 0FH I2CWaitAck(); I2CSendByte(0x7B); //Set WRTC2,WRTC3=0 I2CWaitAck(); I2CSendByte(0x0); //Set WRTC1=0(10H address) I2CWaitAck(); I2CStop(); } /********************************************************************** *Write SD30 series real-time data register (initialization value and modification available) ***********************************************************************/ void I2CWriteDate(void) { WriteTimeOn(); //First allow writing I2CStart(); I2CSendByte(0x64); I2CWaitAck(); I2CSendByte(0x00); //Set write start address I2CWaitAck(); I2CSendByte(InitTime[6]); //seconds I2CWaitAck(); I2CSendByte(InitTime[5]); //minutes I2CWaitAck(); I2CSendByte(InitTime[4]|0x80); //Time (note the relationship between 12/24 hexadecimal system, if you choose 24-hour system, you need to set the highest bit to 1) I2CWaitAck(); I2CSendByte(InitTime[3]); //Week I2CWaitAck(); I2CSendByte(InitTime[2]); //Day I2CWaitAck(); I2CSendByte(InitTime[1]); //Month I2CWaitAck(); I2CSendByte(InitTime[0]); //Year I2CWaitAck(); I2CStop(); WriteTimeOff(); //Remember to turn off writing after writing} /******Write SD30 series single byte program******/ void WriteOneByte(uchar add, uchar date) { WriteTimeOn(); I2CStart(); I2CSendByte(0x64); I2CWaitAck(); I2CSendByte(add); //Set the write address I2CWaitAck(); I2CSendByte(date); //Write data I2CWaitAck(); I2CStop(); WriteTimeOff(); } /******Read SD30 series single byte program******/ uchar ReadOneByte(uchar add) { uchar buf; I2CStart(); I2CSendByte(0x64); I2CWaitAck(); I2CSendByte(add); //Set the address to read I2CWaitAck(); I2CStart(); I2CSendByte(0x65); //Write data I2CWaitAck(); buf=I2CReceiveByte(); //Read data I2CNoAck(); I2CStop(); return buf; } /******Set SD30 series alarm interrupt demonstration program******/ void WriteALARM(void) //Set alarm time: February 14, 2012 8:00 { WriteOneByte(0x09,0x08); //8 o'clock WriteOneByte(0x0b,0x14); //14th WriteOneByte(0x0c,0x02); //02nd month WriteOneByte(0x0d,0x12); //12th year WriteOneByte(0x0e,0x74); //Set alarm enable (enable year, month, day, hour alarm) WriteOneByte(0x10,0x92); //Set INT interrupt strobe (INTS1, INTS0), and alarm interrupt total enable bit (INTAE) } /******Turn off SD30XX alarm interrupt program******/ void ClrALARM(void) //Turn off alarm interrupt { WriteOneByte(0x10,0x90); } /******Set SD30XX countdown interrupt******/ void SetDjs(void) //Set countdown interrupt { WriteOneByte(0x10,0x0f); //Clear countdown interrupt total enable bit (INTDE) first WriteOneByte(0x10,0xf4); //Set periodic interrupt (IM=1) INT interrupt select (INTS1, INTS0), configure countdown interrupt total enable bit (INTDE) WriteOneByte(0x11,0x30); //Select timer frequency source (TDS1, TDS0) as 1/60HZ WriteOneByte(0x13,5); //Countdown initial value register, set 24-bit countdown count initial value (5min) WriteOneByte(0x14,0); //Countdown initial value register, set 24-bit countdown count initial value (5min) WriteOneByte(0x15,0); //Countdown initial value register, set 24-bit countdown count initial value (5min) } /******Close SD30XX countdown interrupt program******/ void ClrDjs(void) { WriteOneByte(0x10,0xf0); } /******Set SD30XX frequency interrupt******/ void SetFrq(void) { WriteOneByte(0x10,0xa1); //Select frequency interrupt (INTS1, INTS0), set frequency interrupt enable bit (INTFE) WriteOneByte(0x11,0x09); //Set 2Hz frequency interrupt } /******Turn off SD30XX frequency interrupt******/ void ClrFrq(void) { WriteOneByte(0x10,0xa0); } /*********Delay subroutine************/ void Delay(uint nn) { while(nn--); } /********************************************************************** *Main function demonstration*******************************************************************/ void main(void) { /****************************************************************************** *Initialize MSP430G2553 ***********************************************************************/ WDTCTL = WDTPW + WDTHOLD; //Turn off watchdog BCSCTL2 = SELM_0 + DIVM_0 + DIVS_0; if (CALBC1_1MHZ != 0xFF) { DCOCTL = 0x00; BCSCTL1 = CALBC1_1MHZ; DCOCTL = CALDCO_1MHZ; } BCSCTL1 |= XT2OFF + DIVA_0; TACTL |= TASSEL_2; UartInit(); //Initialize the serial port to view the program running progress I2CWriteDate(); //Demonstration for setting time // WriteALARM(); //Demonstration for setting alarm interrupt time // SetDjs(); //Demonstration for setting countdown interrupt SetFrq(); //Demonstration for setting frequency interrupt bat1=ReadOneByte(0x1a);//Demonstration for reading the highest bit of battery power of SD30 series bat2=ReadOneByte(0x1b);//Demonstration for reading the low 8 bits of battery power of SD30 series bat=(bat1>>7)*255+bat2;//Demonstration for calculating battery power value while(1) { I2CReadDate(); //Read real-time clock printf("%02x--",ReadTime[6]); } }} }} }

This post is from Microcontroller MCU
 

Guess Your Favourite
Just looking around
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list