/*-------------------------------------------------------------------------- |-裝置碼-| -指令--|讀出/寫入位|確認(rèn)位| |0 1 1 0 |C2 C1 C0| R/W | ACK | MSB LSB ----------------------------------------------------------------------------*/ sbit S35390A_SDA=P2^4;//; sbit S35390A_SCL=P2^5;//; /*---------------- ReadCommand --------------------*/ #define ReadStatus1 0x61 #define ReadStatus2 0x63 #define ReadDate 0x65 #define ReadTime 0x67 #define ReadInt1 0x69 #define ReadInt2 0x6B #define Readcorrection 0x6D #define ReadFree 0x6f /*---------------- WriteCommand --------------------*/ #define WriteStatus1 0x60 #define WriteStatus2 0x62 #define WriteDate 0x64 #define WriteTime 0x66 #define WriteInt1 0x68 #define WriteInt2 0x6a #define Writecorrection 0x6c #define WriteFree 0x6e ///時間部分代碼 ////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////// ///********************************************************* //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 //P2M0=P2M0&0x2f; //P2M0|=0x10; //P2M0=0x35; //P2M1=0x00; S35390A_SDA=1; S35390A_SDA=1; S35390A_SDA=1; TMS12Delay(I2C_353Delay); //TMS12Delay(I2C_353Delay); 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 //P2M0=P2M0&0x2f; //P2M0|=0x10; //P2M0=0x35; //P2M1=0x00; S35390A_SDA=0; S35390A_SDA=0; S35390A_SDA=0; TMS12Delay(I2C_353Delay); //TMS12Delay(I2C_353Delay); 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 //P2M0=0x35; //P2M0=P2M0&0x1f; //P2M0&=0x1f; //P2M0|=0x20; //P2M0=0x35; //P2M1=0x00; S35390A_SCL=1; S35390A_SCL=1; S35390A_SCL=1; TMS12Delay(I2C_353Delay); //TMS12Delay(I2C_353Delay); 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 //P2M0=0x35; //P2M0=P2M0&0x1f; //P2M0&=0x1f; //P2M0|=0x20; //P2M1=0x00; S35390A_SCL=0; S35390A_SCL=0; S35390A_SCL=0; TMS12Delay(I2C_353Delay); //TMS12Delay(I2C_353Delay); return; } ////********************************************************** ///Function: S35390A_RECEIVE_BYTE(void) ///Description: 從S35390接收一個字節(jié)數(shù)據(jù) ///Input: none ///output: none ///Return: none ///**********************************************************/ unsigned char S35390A_RECEIVE_BYTE(void) { uchar i; uchar dat=0; //P2M0=0x25; // 普通P2.4 IO P2M0=0x35; // 普通P2.4 IO P2M1=0x00; P24_KEYSDA=1; TMS12Delay(I2C_353Delay); TMS12Delay(I2C_353Delay); P2M0=0x25; // 普通P2.4 IO P2M1=0x00; TMS12Delay(I2C_353Delay); delay_ms(10); for(i=0;i<8;i++) { TMS12Delay(I2C_353Delay); P25_KEYSCL=1; TMS12Delay(I2C_353Delay); if(P24_KEYSDA==1) { dat|=0x01; } P25_KEYSCL=0; TMS12Delay(I2C_353Delay); TMS12Delay(I2C_353Delay); if(i<7) { dat<<=1; } } //TMS12Delay(I2C_Delay); //P2M0=0x35; // 普通P2.4 IO //P2M1=0x00; //P24_KEYSDA=1; //TMS12Delay(I2C_Delay); //P25_KEYSCL=1; //TMS12Delay(I2C_Delay); //P25_KEYSCL=0; TMS12Delay(I2C_353Delay); return(dat); } ////********************************************************** ///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; uchar i; P2M0=0x35; //推挽P2.4 輸出 P2M1=0x00; for(w=0;w<8;w++) { if(senddata&0x80) { S35390A_SDA_HIGH(); } else { S35390A_SDA_LOW(); } //TMS12Delay(I2C_353Delay); S35390A_SCL_HIGH(); //TMS12Delay(I2C_Delay); //TMS12Delay(I2C_353Delay); senddata<<=1; S35390A_SCL_LOW(); TMS12Delay(I2C_353Delay); } //KEYI2C_WAIT_ACK(); S35390A_GETACK(); } ////********************************************************** ///Function: S35390A_START(void) ///Description: I2C通訊啟動標(biāo)志 ///Input: none ///output: none ///Return: none ///**********************************************************/ void S35390A_START(void) { S35390A_SDA_HIGH(); //高 TMS12Delay(I2C_353Delay); S35390A_SCL_HIGH(); //高 TMS12Delay(I2C_353Delay); S35390A_SDA_LOW(); //低 TMS12Delay(I2C_353Delay); S35390A_SCL_LOW(); //低 TMS12Delay(I2C_353Delay); TMS12Delay(I2C_353Delay); } /// ////********************************************************** ///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(); //高 TMS12Delay(I2C_353Delay); TMS12Delay(I2C_353Delay); S35390A_SDA_HIGH(); //高 TMS12Delay(I2C_353Delay); TMS12Delay(I2C_353Delay); //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; unsigned int z=0; S35390A_SDA_LOW(); //S35390A_SCL_LOW(); //S35390A_SDA_DIR&=~S35390A_SDA; //S35390A_SCL_OUT|=S35390A_SCL; TMS12Delay(I2C_353Delay); //P2M0=P2M0&0x2f; P2M0|=0x25; P2M1=0x00; S35390A_SCL=1; S35390A_SCL=1; TMS12Delay(I2C_353Delay); TMS12Delay(I2C_353Delay); while((S35390A_SDA==1)&&(z<2000))z++;//條件判斷, sda=1,則沒有應(yīng)答。如果沒有應(yīng)答則延時:z<50,z++;后返回 // S35390A_SDA=0; //wait: //w=((S35390A_SDA_IN&S35390A_SDA)); //w=S35390A_SDA; //delay_us(350); //if((w!=0)&&((z--)!=0)) //{ //goto wait; //} //w=S35390A_SDA; S35390A_SCL_LOW(); TMS12Delay(I2C_353Delay); TMS12Delay(I2C_353Delay); if (S35390A_SDA) return 0; else return 1; //return(w); } /// ////********************************************************** ///Function: S35390A_SETACK(void) ///Description: 輸出ACK信號 ///Input: none ///output: none ///Return: none ///**********************************************************/ void S35390A_SETACK(void) { S35390A_SCL_LOW(); TMS12Delay(I2C_353Delay); TMS12Delay(I2C_353Delay); S35390A_SDA_LOW(); TMS12Delay(I2C_353Delay); TMS12Delay(I2C_353Delay); S35390A_SCL_HIGH(); TMS12Delay(I2C_353Delay); TMS12Delay(I2C_353Delay); S35390A_SCL_LOW(); TMS12Delay(I2C_353Delay); TMS12Delay(I2C_353Delay); } /// ////********************************************************** ///Function: S35390A_SETNCK(void) ///Description: 輸出NCK信號 ///Input: none ///output: none ///Return: none ///**********************************************************/ void S35390A_SETNCK(void) { S35390A_SCL_LOW(); TMS12Delay(I2C_353Delay); TMS12Delay(I2C_353Delay); S35390A_SDA_HIGH(); TMS12Delay(I2C_353Delay); TMS12Delay(I2C_353Delay); S35390A_SCL_HIGH(); TMS12Delay(I2C_353Delay); TMS12Delay(I2C_353Delay); S35390A_SCL_LOW(); TMS12Delay(I2C_353Delay); TMS12Delay(I2C_353Delay); } /// /// /// ////********************************************************** ///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; P2M0=0x35; //推挽P2.4 輸出 P2M1=0x00; S35390A_START(); //啟動總線 //KEYI2C_WRITE_8BIT(opt); //發(fā)送裝置命今字節(jié) S35390A_SEND_BYTE(opt); //s_temp=S35390A_GETACK(); //接收應(yīng)答信號 //Send_One_Byte(s_temp); //Send_One_Byte(s_temp & S35390A_SDA); //KEYI2C_ACK(); //if((s_temp & S35390A_SDA)) //{ //S35390A_STOP(); //return(0); //} for(ws=0;ws<count;ws++) //寫多個字節(jié)數(shù)據(jù) { s_temp=s35390a_buff[ws]; //KEYI2C_WRITE_8BIT(s_temp); S35390A_SEND_BYTE(s_temp); //s_temp=S35390A_GETACK(); //接收應(yīng)答信號 //KEYI2C_ACK(); //Send_One_Byte(s_temp & S35390A_SDA); //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; P2M0=0x35; //推挽P2.4 輸出 P2M1=0x00; S35390A_START(); //啟動總線 //KEYI2C_WRITE_8BIT(opt); //發(fā)送裝置命今字節(jié) S35390A_SEND_BYTE(opt); //S35390A_SETACK(); //s_temp=S35390A_GETACK(); //接收應(yīng)答信號 //if((s_temp & S35390A_SDA)) //{ //S35390A_STOP(); //return(0); //} for(ws=0;ws<count;ws++) //讀多個字節(jié) { s35390a_buff[ws] = S35390A_RECEIVE_BYTE(); //s35390a_buff[ws] =KEYI2C_READ_8BIT2(); P2M0=0x35; //推挽P2.4 輸出 P2M1=0x00; TMS12Delay(I2C_353Delay); if(ws==(count-1)) { S35390A_SETNCK(); //不發(fā)回應(yīng) } else { S35390A_SETACK(); //發(fā)回應(yīng) } } P2M0=0x35; //推挽P2.4 輸出 P2M1=0x00; 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_INIT(void) ///Description: S35390A初始化 ///Input: none ///output: none ///Return: week ///**********************************************************/ void S35390A_INIT(void) { //S35390A_SDA_DIR |= S35390A_SDA; //輸出 //S35390A_SCL_DIR |= S35390A_SCL; uchar i; P2M0=0x35; P2M1=0x00; delay_ms(5000); //延時 delay_ms(5000); //延時 //TSM12Sleep(); P22_KEYEN=1; //S35390A_INT1_DIR &= ~S35390A_INT1; //輸入 S35390A_SDA=1; S35390A_SCL=1; TMS12Delay(I2C_353Delay); TMS12Delay(I2C_353Delay); // EA=0; s35390a_buff[0]=0x00; S35390A_WRITE(0x62, 0x01); //清除報警1中斷使能,使INT1腳輸出高電平 delay_ms(5000); //延時 delay_ms(5000); //延時 if(S35390A_READ(0x61,0x01)) { if((s35390a_buff[0]&0xc0)!=0) { Send_One_Byte(s35390a_buff[0]); s35390a_buff[0]=0xc0; S35390A_WRITE(0x60,0x01); delay_ms(5000); delay_ms(5000); delay_ms(5000); delay_ms(5000); S35390A_READ(0x61,0x01); //Send_One_Byte(s35390a_buff[0]); S35390A_READ(0x63,0x01); //Send_One_Byte(s35390a_buff[0]); S35390A_READ(0x65,0x01); //Send_One_Byte(s35390a_buff[0]); S35390A_READ(0x67,0x01); //Send_One_Byte(s35390a_buff[0]); S35390A_READ(0x69,0x01); //Send_One_Byte(s35390a_buff[0]); S35390A_READ(0x6b,0x01); //Send_One_Byte(s35390a_buff[0]); S35390A_READ(0x6d,0x01); //Send_One_Byte(s35390a_buff[0]); S35390A_READ(0x6f,0x01); //Send_One_Byte(s35390a_buff[0]); } } if(S35390A_READ(0x63,0x01)) { //Send_One_Byte(s35390a_buff[0]); if((s35390a_buff[0]&0x80)!=0) { s35390a_buff[0]=0x0c; S35390A_WRITE(0x60,0x01); } } delay_ms(500); s35390a_buff[0]=0x40; //設(shè)置為24小時制 S35390A_WRITE(0x60,0x01); delay_ms(5000); delay_ms(5000); delay_ms(5000); S35390A_READ(0x61,0x01); Send_One_Byte(s35390a_buff[0]); delay_ms(5000); delay_ms(5000); //S35390A_READ(0x6f,0x01); //Send_One_Byte(s35390a_buff[0]); //readS35390ATime(); //s35390a_buff[0]=0xff; //S35390A_WRITE(0x6e,0x1); //s35390a_buff[0]=0; delay_ms(5500); delay_ms(5500); delay_ms(5500); delay_ms(5500); //s35390a_buff[0]=S35390A_READ(0x6f,0x1); //Send_One_Byte(s35390a_buff[0]); //SetS35390ATime(); //delay_ms(5500); //delay_ms(5500); //delay_ms(5500); //delay_ms(5500); readS35390ATime(); SetS35390ATime(); delay_ms(5500); delay_ms(5500); delay_ms(5500); delay_ms(5500); readS35390ATime(); // EA=1; } void SetS35390ATime(void) { Get_Time_data(); EA=0; s35390b_buff[0] = HEX2BCD(Year); s35390b_buff[1] = HEX2BCD(Month); s35390b_buff[2] = HEX2BCD(Date);//S35390A_SWAP_BYTE(Date%64); //日 s35390b_buff[3] = 0x00; //星期不使用為00 s35390b_buff[4] = HEX2BCD(Hours);//S35390A_SWAP_BYTE(Hours%64); //時 s35390b_buff[5] = HEX2BCD(Minutes);//S35390A_SWAP_BYTE(Minutes%128); //分 s35390a_buff[0]=S35390A_SWAP_BYTE(s35390b_buff[0]); s35390a_buff[1]=S35390A_SWAP_BYTE(s35390b_buff[1]); s35390a_buff[2]=S35390A_SWAP_BYTE(s35390b_buff[2]); s35390a_buff[3] = 0x00; s35390a_buff[4]=S35390A_SWAP_BYTE(s35390b_buff[4]); //s35390a_buff[4]|=0x04; s35390a_buff[5]=S35390A_SWAP_BYTE(s35390b_buff[5]); s35390a_buff[6]=0x00; Send_One_Byte(s35390a_buff[0]); Send_One_Byte(s35390a_buff[1]); Send_One_Byte(s35390a_buff[2]); Send_One_Byte(s35390a_buff[4]); Send_One_Byte(s35390a_buff[5]); S35390A_WRITE(0x64,0x07); //寫時間和日期 s35390a_buff[0] = 0x0; s35390a_buff[1] = 0x0; s35390a_buff[2] = 0x0; s35390a_buff[3] = 0x0; //星期不使用為00 s35390a_buff[4] = 0x0; s35390a_buff[5] = 0x0; s35390a_buff[6] = 0x0; EA=1; } // 年 Y1 Y2 Y4 Y8 Y10 Y20 Y40 Y80 如果是 53 年 標(biāo)識 1100 1010 低 高 // 月 M1 M2 M4 M8 M10 0 0 0 如果是 12 月 標(biāo)識 0100 1000 低 高 // 日 D1 D2 D4 D8 D10 D20 0 0 如果是 29 日 標(biāo)識 1001 0100 低 高 // 時 H1 H2 H4 H8 H10 H20 AMPM 0 如果是 22 時 標(biāo)識 0100 0110 低 高 24小時制度 // 分 M1 M2 M4 M8 M10 M20 M40 0 如果是 55 時 標(biāo)識 1010 1010 低 高 // 秒 S1 S2 S4 S8 S10 S20 S40 0 如果是 55 秒 標(biāo)識 1010 1010 低 高 //00 01 02 0B 11 void readS35390ATime(void) { P22_KEYEN=0; S35390A_READ(0x65,0x07); //讀時間 delay_ms(50); //Year=s35390a_buff[0]; //年 //Send_One_Byte(Year); //Month=s35390a_buff[1]%32; //月 //Send_One_Byte(Month); //Date=s35390a_buff[2]%64; //日 //Send_One_Byte(Date); //Hours=s35390a_buff[4]%64; //時 //Send_One_Byte(Hours); //Minutes=s35390a_buff[5]%128; //分 //Send_One_Byte(Minutes); //Send_One_Byte(s35390a_buff[0]); //Send_One_Byte(s35390a_buff[1]); //Send_One_Byte(s35390a_buff[2]); //Send_One_Byte(s35390a_buff[4]); //Send_One_Byte(s35390a_buff[5]); s35390b_buff[0]=S35390A_SWAP_BYTE(s35390a_buff[0]); s35390b_buff[1]=S35390A_SWAP_BYTE(s35390a_buff[1]); s35390b_buff[2]=S35390A_SWAP_BYTE(s35390a_buff[2]); s35390b_buff[4]=S35390A_SWAP_BYTE(s35390a_buff[4]); s35390b_buff[4]=s35390b_buff[4]&0xbf; //Send_One_Byte(s35390b_buff[4]); s35390b_buff[5]=S35390A_SWAP_BYTE(s35390a_buff[5]); Send_One_Byte(S35390A_SWAP_BYTE(s35390a_buff[0])); Send_One_Byte(S35390A_SWAP_BYTE(s35390a_buff[1])); Send_One_Byte(S35390A_SWAP_BYTE(s35390a_buff[2])); Send_One_Byte(s35390b_buff[4]); //Send_One_Byte(S35390A_SWAP_BYTE(s35390a_buff[4])); Send_One_Byte(S35390A_SWAP_BYTE(s35390a_buff[5])); Get_data_buff[8]=((s35390b_buff[0]&0xf0)>>4)+0x30; Get_data_buff[9]=(s35390b_buff[0]&0x0f)+0x30; Get_data_buff[11]=((s35390b_buff[1]&0xf0)>>4)+0x30; Get_data_buff[12]=(s35390b_buff[1]&0x0f)+0x30; Get_data_buff[14]=((s35390b_buff[2]&0xf0)>>4)+0x30; Get_data_buff[15]=(s35390b_buff[2]&0x0f)+0x30; Get_data_buff[17]=((s35390b_buff[4]&0xf0)>>4)+0x30; Get_data_buff[18]=(s35390b_buff[4]&0x0f)+0x30; Get_data_buff[20]=((s35390b_buff[5]&0xf0)>>4)+0x30; Get_data_buff[21]=(s35390b_buff[5]&0x0f)+0x30; GetNB_Time_buff[0]=Get_data_buff[8]; GetNB_Time_buff[1]=Get_data_buff[9]; GetNB_Time_buff[2]=Get_data_buff[11]; GetNB_Time_buff[3]=Get_data_buff[12]; GetNB_Time_buff[4]=Get_data_buff[14]; GetNB_Time_buff[5]=Get_data_buff[15]; GetNB_Time_buff[6]=Get_data_buff[17]; GetNB_Time_buff[7]=Get_data_buff[18]; GetNB_Time_buff[8]=Get_data_buff[20]; GetNB_Time_buff[9]=Get_data_buff[21]; Year=((Get_data_buff[8]-0x30)*10)+(Get_data_buff[9]-0x30); if (Get_data_buff[11]==0x31) Month=10+(Get_data_buff[12]-0x30); else Month=(Get_data_buff[12]-0x30); if (Get_data_buff[14]>=0x31) Date=((Get_data_buff[14]-0x30)*10)+(Get_data_buff[15]-0x30); else Date=(Get_data_buff[15]-0x30); if (Get_data_buff[17]>=0x31) Hours=((Get_data_buff[17]-0x30)*10)+(Get_data_buff[18]-0x30); else Hours=(Get_data_buff[18]-0x30); if (Get_data_buff[20]>=0x31) Minutes=((Get_data_buff[20]-0x30)*10)+(Get_data_buff[21]-0x30); else Minutes=(Get_data_buff[21]-0x30); //Send_One_Byte(Get_data_buff[8]); //Send_One_Byte(Get_data_buff[9]); //Send_One_Byte(Get_data_buff[11]); //Send_One_Byte(Get_data_buff[12]); //Send_One_Byte(Get_data_buff[14]); //Send_One_Byte(Get_data_buff[15]); //Send_One_Byte(Get_data_buff[17]); //Send_One_Byte(Get_data_buff[18]); //Send_One_Byte(Get_data_buff[20]); //Send_One_Byte(Get_data_buff[21]); } |
|
來自: 昵稱66540557 > 《待分類》