S35390驅(qū)動程序
/* 時鐘芯片驅(qū)動程序 */
/*******************************************************************/
#include "msp430x41x.h"
#include "delay.h"
#include "iodefine.h"
#include "all_variable.h"
/*****************************************************************/
/* 內(nèi)部函數(shù)聲明 */
/*****************************************************************/
void S35390A_SDA_HIGH(void); //SDA腳輸出高電平
void S35390A_SDA_LOW(void); //SDA腳輸出低電平
void S35390A_SCL_HIGH(void); //SCK腳輸出高電平
void S35390A_SCL_LOW(void); //SDA腳輸出低電平
void S35390A_START(void); //啟動S35390
void S35390A_STOP(void); //停止S35390
unsigned char S35390A_GETACK(void); //獲得ACK信號
void S35390A_SETACK(void); //輸出ACK信號
void S35390A_SETNCK(void); //輸出NACK信號
unsigned char S35390A_RECEIVE_BYTE(void); //從S35390接收一個字節(jié)數(shù)據(jù)
void S35390A_SEND_BYTE(unsigned char senddata); //向S35390發(fā)送一個字節(jié)數(shù)據(jù)
extern void DELAYeightNOP(void);
extern void DELAYthreeNOP(void);
extern void SetS35390ATime(void);
extern void readS35390ATime(void);
unsigned char S35390A_WRITE(unsigned char opt,unsigned char count);//向S35390A寫數(shù)據(jù)
unsigned char S35390A_READ(unsigned char opt,unsigned char count); //從S35390A讀數(shù)據(jù)
unsigned char S35390A_SWAP_BYTE(unsigned char swdata); //字節(jié)首尾交換
unsigned char S35390A_CACULATE_WEEK(unsigned char year,unsigned char month,unsigned char date);//通過年月日計算星期
void S35390A_INIT(void);//S35390A初始化
/*********************************************************
Function: S35390A_SDA_HIGH(void)
Description: SDA腳輸出高電平
Input: none
output: none
Return: none
**********************************************************/
void S35390A_SDA_HIGH(void)
{
S35390A_SDA_DIR|=S35390A_SDA; // set SDA as output pin
S35390A_SDA_OUT|=S35390A_SDA; //set SDA pin high
_NOP();
return;
}
/**********************************************************
Function: S35390A_SDA_low(void)
Description: SDA腳輸出低電平
Input: none
output: none
Return: none
**********************************************************/
void S35390A_SDA_LOW(void)
{
S35390A_SDA_DIR|=S35390A_SDA; //set SDA as output pin
S35390A_SDA_OUT&=~S35390A_SDA; //set SDA pin low
_NOP();
return;
}
/**********************************************************
Function: S35390A_SCL_HIGH(void)
Description: SCK腳輸出高電平
Input: none
output: none
Return: none
**********************************************************/
void S35390A_SCL_HIGH(void)
{
S35390A_SCL_DIR|=S35390A_SCL; // set SCK as output pin
S35390A_SCL_OUT|=S35390A_SCL; //set SCK pin high
_NOP();
return;
}
/**********************************************************
Function: S35390A_SCL_low(void)
Description: SDA腳輸出低電平
Input: none
output: none
Return: none
**********************************************************/
void S35390A_SCL_LOW(void)
{
S35390A_SCL_DIR|=S35390A_SCL; //set SCK as output pin
S35390A_SCL_OUT&=~S35390A_SCL; //set SCK pin low
_NOP();
return;
}
/**********************************************************
Function: S35390A_RECEIVE_BYTE(void)
Description: 從S35390接收一個字節(jié)數(shù)據(jù)
Input: none
output: none
Return: none
**********************************************************/
unsigned char S35390A_RECEIVE_BYTE(void)
{
unsigned char S35390A_buf=0;
unsigned char w;
S35390A_SDA_DIR &= ~S35390A_SDA; //輸入方式
//S35390A_SDA_OUT|=1<
for(w=0; w<8; w++)
{
S35390A_SCL_HIGH();
if((S35390A_SDA_IN & S35390A_SDA))
{
S35390A_buf |= (1<
S35390A_SCL_LOW();
DELAYeightNOP();
}
return(S35390A_buf);
}
/**********************************************************
Function: S35390A_SEND_BYTE(void)
Description: 向S35390發(fā)送一個字節(jié)數(shù)據(jù)
Input: none
output: none
Return: none
**********************************************************/
void S35390A_SEND_BYTE(unsigned char senddata)
{
unsigned char w;
for(w=0;w<8;w++)
{
if(senddata&0x80)
{
S35390A_SDA_HIGH();
}
else
{
S35390A_SDA_LOW();
}
S35390A_SCL_HIGH();
DELAYthreeNOP();
senddata<<=1;
S35390A_SCL_LOW();
DELAYeightNOP();
}
}
/**********************************************************
Function: S35390A_START(void)
Description: I2C通訊啟動標(biāo)志
Input: none
output: none
Return: none
**********************************************************/
void S35390A_START(void)
{
S35390A_SDA_HIGH(); //高
S35390A_SCL_HIGH(); //高
DELAYthreeNOP();
S35390A_SDA_LOW(); //低
DELAYthreeNOP();
S35390A_SCL_LOW(); //低
DELAYeightNOP();
}
/**********************************************************
Function: S35390A_STOP(void)
Description: I2C通訊結(jié)束標(biāo)志
Input: none
output: none
Return: none
**********************************************************/
void S35390A_STOP(void)
{
S35390A_SDA_LOW(); //低
S35390A_SCL_HIGH(); //高
DELAYthreeNOP();
S35390A_SDA_HIGH(); //高
DELAYeightNOP();
//S35390A_SCL_LOW(); //低
//DELAYeightNOP();
}
/**********************************************************
Function: S35390A_GETACK(void)
Description: 獲得ACK信號
Input: none
output: none
Return: temp
**********************************************************/
unsigned char S35390A_GETACK(void)
{
unsigned char w=0;
unsigned char z=100;
_NOP();
_NOP();
S35390A_SCL_LOW();
S35390A_SDA_DIR&=~S35390A_SDA; //SDA input
S35390A_SCL_OUT|=S35390A_SCL;
_NOP();
wait:
w=((S35390A_SDA_IN&S35390A_SDA));
if((w!=0)&&((z--)!=0))
{
goto wait;
}
S35390A_SCL_LOW();
DELAYeightNOP();
return(w);
}
/**********************************************************
Function: S35390A_SETACK(void)
Description: 輸出ACK信號
Input: none
output: none
Return: none
**********************************************************/
void S35390A_SETACK(void)
{
S35390A_SCL_LOW();
DELAYeightNOP();
S35390A_SDA_LOW();
DELAYthreeNOP();
S35390A_SCL_HIGH();
DELAYthreeNOP();
S35390A_SCL_LOW();
DELAYeightNOP();
}
/**********************************************************
Function: S35390A_SETNCK(void)
Description: 輸出NCK信號
Input: none
output: none
Return: none
**********************************************************/
void S35390A_SETNCK(void)
{
S35390A_SCL_LOW();
DELAYeightNOP();
S35390A_SDA_HIGH();
DELAYthreeNOP();
S35390A_SCL_HIGH();
DELAYthreeNOP();
S35390A_SCL_LOW();
DELAYeightNOP();
}
/**********************************************************
Function: S35390A_WRITE(unsigned char opt,unsigned char adr,unsigned char count)
Description: 向S35390A寫數(shù)據(jù)
Input: opt:裝置命今,count:寫數(shù)據(jù)的字節(jié)個數(shù)
output: none
Return: 0或1
**********************************************************/
unsigned char S35390A_WRITE(unsigned char opt,unsigned char count)
{
unsigned char s_temp=0;
unsigned char ws;
S35390A_START(); //啟動總線
S35390A_SEND_BYTE(opt); //發(fā)送裝置命今字節(jié)
s_temp=S35390A_GETACK(); //接收應(yīng)答信號
if((s_temp & S35390A_SDA))
{
S35390A_STOP();
return(0);
}
for(ws=0;ws
s_temp=s35390a[ws];
S35390A_SEND_BYTE(s_temp);
s_temp=S35390A_GETACK(); //接收應(yīng)答信號
if((s_temp & S35390A_SDA))
{
S35390A_STOP();
return(0);
}
}
S35390A_STOP(); //停止總線
return(1);
}
/**********************************************************
Function: S35390A_READ(unsigned char opt,unsigned char adr,unsigned char count)
Description: 從S35390A讀數(shù)據(jù)
Input: none
output: none
Return: none
**********************************************************/
unsigned char S35390A_READ(unsigned char opt,unsigned char count)
{
unsigned char s_temp=0;
unsigned char ws;
S35390A_START(); //啟動總線
S35390A_SEND_BYTE(opt); //發(fā)送裝置命今字節(jié)
s_temp=S35390A_GETACK(); //接收應(yīng)答信號
if((s_temp & S35390A_SDA))
{
S35390A_STOP();
return(0);
}
for(ws=0;ws
s35390a[ws] = S35390A_RECEIVE_BYTE();
if(ws==(count-1))
{
S35390A_SETNCK(); //不發(fā)回應(yīng)
}
else
{
S35390A_SETACK(); //發(fā)回應(yīng)
}
}
S35390A_STOP(); //停止總線
return(1);
}
/**********************************************************
Function: S35390A_SWAP_BYTE(unsigned char swdata)
Description: 字節(jié)首尾位交換
Input: none
output: none
Return: none
**********************************************************/
unsigned char S35390A_SWAP_BYTE(unsigned char swdata)
{
unsigned char swtemp = swdata;
swtemp = ((swtemp & 0x55) << 1) | ((swtemp & 0xaa) >> 1); //相鄰兩位對換
swtemp = ((swtemp & 0x33) << 2) | ((swtemp & 0xcc) >> 2); //
swtemp = ((swtemp & 0x0f) << 4) | ((swtemp & 0xf0) >> 4);
return(swtemp);
}
/**********************************************************
Function: S35390A_CACULATE_WEEK(unsigned char year,unsigned char month,unsigned char data)
Description: 通過年月日計算星期
Input: year,month,date
output: none
Return: week
**********************************************************/
unsigned char S35390A_CACULATE_WEEK(unsigned char year,unsigned char month,unsigned char date)
{
unsigned char wtemp;
if(month==1)
{
year-=1;
month=13;
}
else
{
if(month==2)
{
year-=1;
month=14;
}
}
wtemp=year+year/4+13*((month+1)/5)+date-36;
wtemp%=7;
return(wtemp);
}
/**********************************************************
Function: S35390A_INIT(void)
Description: S35390A初始化
Input: none
output: none
Return: week
**********************************************************/
void S35390A_INIT(void)
{
S35390A_SDA_DIR |= S35390A_SDA; //輸出
S35390A_SCL_DIR |= S35390A_SCL;
S35390A_INT1_DIR &= ~S35390A_INT1; //輸入
// S35390A_INT2_DIR &= ~(1<
S35390A_SCL_OUT |= S35390A_SCL;
S35390A_INT1_OUT |=S35390A_INT1;
// S35390A_INT2_OUT |= 1<
S35390A_WRITE(0x62, 0x01); //清除報警1中斷使能,使INT1腳輸出高電平
Delay_Nms(5); //延時
if(S35390A_READ(0x61,0x01))
{
if((s35390a[0]&0xc0)!=0)
{
s35390a[0]=0xc0;
S35390A_WRITE(0x60,0x01);
}
}
if(S35390A_READ(0x63,0x01))
{
if((s35390a[0]&0x80)!=0)
{
s35390a[0]=0x0c;
S35390A_WRITE(0x60,0x01);
}
}
Delay_Nms(5);
s35390a[0]=0x40; //設(shè)置為24小時制
S35390A_WRITE(0x60,0x01);
Delay_Nms(5);
s35390a[0]=0x20; //設(shè)置報警1中斷使能
S35390A_WRITE(0x62,0x01); //設(shè)置報警1中斷使能
s35390a[0] =0;
s35390a[1] = S35390A_SWAP_BYTE((0x08<<4)|0x00); //時;
s35390a[2] =0;
S35390A_WRITE(0x68,0x03); //中斷時間為每天的0時
S35390A_INT1_DIR &= ~S35390A_INT1; //置為輸入方式
S35390A_INT1_OUT |= S35390A_INT1; //置為高電平
P1IE|=S35390A_INT1; //中斷使能
P1IES|=S35390A_INT1; //下降沿產(chǎn)生中斷
}
void SetS35390ATime(void)
{
s35390a[0] = S35390A_SWAP_BYTE(twdata[7]); //年
s35390a[1] = S35390A_SWAP_BYTE(twdata[6]%32); //月
s35390a[2] = S35390A_SWAP_BYTE(twdata[4]%64); //日
s35390a[3] = 0x00; //星期不使用為00
s35390a[4] = S35390A_SWAP_BYTE(twdata[3]%64); //時
s35390a[5] = S35390A_SWAP_BYTE(twdata[2]%128); //分
s35390a[6] = 0x00; //秒為00
S35390A_WRITE(0x64,0x07); //寫時間和日期
}
void readS35390ATime(void)
{
S35390A_READ(0x65,0x07); //讀時間
Delay_Nms(5);
trdata[6]=s35390a[0]; //年
trdata[5]=s35390a[1]%32; //月
months=trdata[5];
trdata[3]=s35390a[2]%64; //日
trdata[2]=s35390a[4]%64; //時
trdata[1]=s35390a[5]%128; //分
}
評論