/* 时钟芯片驱动程序 */
/*******************************************************************/
#include "msp430x41x.h"
#include "delay.h"
#include "iodefine.h"
#include "all_variable.h"
/*****************************************************************/
/* 内部函数声明 */
/*****************************************************************/
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接收一个字节数据
void S35390A_SEND_BYTE(unsigned char senddata); //向S35390发送一个字节数据
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写数据
unsigned char S35390A_READ(unsigned char opt,unsigned char count); //从S35390A读数据
unsigned char S35390A_SWAP_BYTE(unsigned char swdata); //字节首尾交换
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接收一个字节数据
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发送一个字节数据
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通讯启动标志
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通讯结束标志
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写数据
Input: opt:装置命今,count:写数据的字节个数
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); //发送装置命今字节
s_temp=S35390A_GETACK(); //接收应答信号
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(); //接收应答信号
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读数据
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); //发送装置命今字节
s_temp=S35390A_GETACK(); //接收应答信号
if((s_temp & S35390A_SDA))
{
S35390A_STOP();
return(0);
}
for(ws=0;ws
s35390a[ws] = S35390A_RECEIVE_BYTE();
if(ws==(count-1))
{
S35390A_SETNCK(); //不发回应
}
else
{
S35390A_SETACK(); //发回应
}
}
S35390A_STOP(); //停止总线
return(1);
}
/**********************************************************
Function: S35390A_SWAP_BYTE(unsigned char swdata)
Description: 字节首尾位交换
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; //设置为24小时制
S35390A_WRITE(0x60,0x01);
Delay_Nms(5);
s35390a[0]=0x20; //设置报警1中断使能
S35390A_WRITE(0x62,0x01); //设置报警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; //下降沿产生中断
}
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; //分
}
上一篇:中断函数写法的比较
下一篇:430单片机烧录问题和接线方法
推荐阅读最新更新时间:2024-03-16 15:07