S35390 Driver

Publisher:独行于世Latest update time:2016-08-25 Source: eefocusKeywords:S35390 Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
/*******************************************************************/

 /* Clock chip driver */
 /***********************************************************************/
#include "msp430x41x.h"
#include "delay.h"
#include "iodefine.h"
#include "all_variable.h"
 /*****************************************************************/
 /* Internal function declaration */
 /********************************************************************/
void S35390A_SDA_HIGH(void); //SDA pin outputs high level
void S35390A_SDA_LOW(void); //SDA pin outputs low level 
void S35390A_SCL_HIGH(void); //SCK pin outputs high level
void S35390A_SCL_LOW(void); //SDA pin outputs low level
void S35390A_START(void); //Start S35390
void S35390A_STOP(void); //Stop S35390
unsigned char S35390A_GETACK(void); //Get ACK signalvoid
S35390A_SETACK(void); //Output ACK signalvoid
S35390A_SETNCK(void); //Output NACK signalunsigned
char S35390A_RECEIVE_BYTE(void); //Receive a byte of data from S35390void 
S35390A_SEND_BYTE(unsigned char senddata); //Send a byte of data to S35390extern
void DELAYeightNOP(void);
extern void DELAYthreeNOP(void);
extern void SetS35390ATime(void);
extern void readS35390ATime(void);
unsigned char S35390A_WRITE(unsigned char opt,unsigned char char count);//write data to S35390A
unsigned char S35390A_READ(unsigned char opt,unsigned char count); //read data from S35390A
unsigned char S35390A_SWAP_BYTE(unsigned char swdata); //byte head and tail swap
unsigned char S35390A_CACULATE_WEEK(unsigned char year,unsigned char month,unsigned char date);//calculate the week by year, month and day
void S35390A_INIT(void);//S35390A initialization
/************************************************************
Function: S35390A_SDA_HIGH(void)
Description: SDA pin outputs high level  
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<   Delay_Nus(10);
  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 communication start flag  
Input: none
Output: none
Return: none
**********************************************************/
void S35390A_START(void)
{
  S35390A_SDA_HIGH(); //High
  
  S35390A_SCL_HIGH(); //High
  DELAYthreeNOP(); 
  
  S35390A_SDA_LOW(); //Low
  DELAYthreeNOP();
  
  S35390A_SCL_LOW(); //Low
  DELAYeightNOP();
}

/**********************************************************
Function: S35390A_STOP(void)
Description: I2C communication end flag  
Input: none
Output: none
Return: none
**********************************************************/
void S35390A_STOP(void)
{
  S35390A_SDA_LOW(); //Low
  
  S35390A_SCL_HIGH(); //High
  DELAYthreeNOP();
  
  S35390A_SDA_HIGH(); //High
  DELAYeightNOP();
  
  
  //S35390A_SCL_LOW(); //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: Write data to S35390A  
Input: opt: device command, count: number of bytes to write data
Output: none
Return: 0 or 1
**********************************************************/
unsigned char S35390A_WRITE(unsigned char opt,unsigned char count)
{
  unsigned char s_temp=0;
  unsigned char ws;
  S35390A_START(); //Start the bus
  S35390A_SEND_BYTE(opt); //Send device command byte
  s_temp=S35390A_GETACK(); //Receive response signal
  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(); //Receive response signalif
    ((s_temp & S35390A_SDA))
    {
      S35390A_STOP();
      return(0);
    }
  }
  
  S35390A_STOP(); //Stop the busreturn
  (1);
}

/**********************************************************
Function: S35390A_READ(unsigned char opt,unsigned char adr,unsigned char count)
Description: Read data from 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(); //Start the bus
  S35390A_SEND_BYTE(opt); //Send device command byte
  s_temp=S35390A_GETACK(); //Receive response signal
  if((s_temp & S35390A_SDA))
  {
    S35390A_STOP();
    return(0);
  }
  
  for(ws=0;ws   {
    s35390a[ws] = S35390A_RECEIVE_BYTE();
    if(ws==(count-1))
    {
      S35390A_SETNCK(); //Do not send a response
    }
    else
    {
      S35390A_SETACK(); //Send a response
    }
  }
  
  S35390A_STOP(); //Stop the busreturn
  (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 initialization
Input: none
Output: none
Return: week
**********************************************************/
void S35390A_INIT(void)
{
  S35390A_SDA_DIR |= S35390A_SDA; //Output
  S35390A_SCL_DIR |= S35390A_SCL;
  S35390A_INT1_DIR &= ~S35390A_INT1; //Input
 // S35390A_INT2_DIR &= ~(1<   S35390A_SDA_OUT |= S35390A_SDA; //Output high level
  S35390A_SCL_OUT |= S35390A_SCL;
  S35390A_INT1_OUT |=S35390A_INT1;
 // S35390A_INT2_OUT |= 1<   s35390a[0] = 0x00;            
  S35390A_WRITE(0x62, 0x01); // Clear alarm 1 interrupt enable, make INT1 pin output high level
  Delay_Nms(5); // Delay
  
  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; //Set to 24-hour system
  S35390A_WRITE(0x60,0x01);
  Delay_Nms(5);
  
  s35390a[0]=0x20; //Set alarm 1 interrupt enable
  S35390A_WRITE(0x62,0x01); //Set alarm 1 interrupt enable
  
  s35390a[0] =0;
  s35390a[1] = S35390A_SWAP_BYTE((0x08<<4)|0x00); //Time;
  s35390a[2] =0;
  S35390A_WRITE(0x68,0x03); //The interrupt time is 0 o'clock every day
  
  S35390A_INT1_DIR &= ~S35390A_INT1; //Set to input mode
  S35390A_INT1_OUT |= S35390A_INT1; //Set to high level
  P1IE|=S35390A_INT1; //Interrupt enable
  P1IES|=S35390A_INT1; //Interrupt is generated on the falling edge
}
void SetS35390ATime(void)
{
  s35390a[0] = S35390A_SWAP_BYTE(twdata[7]); //Year
  s35390a[1] = S35390A_SWAP_BYTE(twdata[6]%32); //Month
  s35390a[2] = S35390A_SWAP_BYTE(twdata[4]%64); //day
  s35390a[3] = 0x00; //week is not used and is 00
  s35390a[4] = S35390A_SWAP_BYTE(twdata[3]%64); //hour
  s35390a[5] = S35390A_SWAP_BYTE(twdata[2]%128); //minute
  s35390a[6] = 0x00; //second is 00
  S35390A_WRITE(0x64,0x07); //write time and date
}

void readS35390ATime(void)
{
  S35390A_READ(0x65,0x07); //Read time
  Delay_Nms(5);  
  trdata[6]=s35390a[0]; //Year
  trdata[5]=s35390a[1]%32; //Month
  months=trdata[5];
  trdata[3]=s35390a[2]%64; //Day
  trdata[2]=s35390a[4]%64; //Hour
  trdata[1]=s35390a[5]%128; //Minute
}

Keywords:S35390 Reference address:S35390 Driver

Previous article:Comparison of interrupt function writing methods
Next article:430 MCU programming problems and wiring methods

Recommended ReadingLatest update time:2024-11-16 20:45

Chinese scientists develop implantable flexible battery "driven" by saline solution
  In the near future, people will no longer have to worry about battery electrolyte leakage in implanted electronic devices. Chinese researchers have recently developed a flexible battery whose electrolyte is actually saline solution, which is commonly used in hospitals. Let's learn more about it with the power manage
[Power Management]
51 single chip microcomputer drives dot matrix 16*16 intermittent display C program
#include reg52.h #define uchar unsigned char #define uint unsigned int #define LINE P0 //define line IO port, i.e. 74HC154 control terminal #define shudu 20 //word moving speed sbit DATA=P1^4; //74HC595 data terminal sbit CLK=P1^5; //74HC595 shift pulse terminal sbit CLKR=P1^6; //74HC595 data latch terminal
[Microcontroller]
51 single chip microcomputer drives dot matrix 16*16 intermittent display C program
Detailed explanation of S3C2440 touch screen driver
The touch screen conversion interface of 2440 is installed on the ADC interface, and has more functions than the ADC interface. First, the touch screen interface has several conversion modes. 1. Normal conversion mode Single conversion mode is the most suitable general-purpose ADC conversion. This mode can be initiali
[Microcontroller]
Detailed explanation of S3C2440 touch screen driver
"Driver separation" design idea of ​​microcontroller firmware
Today I have found with you a common design method of separating applications and drivers. It is still worth using for some current high-performance MCUs. However, it is not recommended for MCUs whose original main frequency is not high enough and whose performance is not strong enough. After all, this The design stil
[Microcontroller]
Flash LED Driver for Mobile Phones
LEDs have become the standard solution for movie lighting and camera flash in mobile phones. The demand for higher picture quality and higher resolution requires brighter flash LED solutions. The challenge is to squeeze the best light flux from the battery by achieving the most efficient solution. As a result, operat
[Power Management]
Flash LED Driver for Mobile Phones
AVR microcontroller drives 12864 LCD program
/*LCD12864 display subroutine*, #include util/delay.h #define uchar unsigned char #define uint unsigned int #define SETLCD12864RS PORTA|=(1 PA4) #define SETLCD12864RW PORTA|=(1 PA5) #define SETLCD12864EN PORTA|=(1 PA6) #define CLLCD12864RS PORTA&=~(1 PA4) #define CLLCD12864RW PORTA&=~(1 PA5) #define CLTLCD12864EN
[Microcontroller]
Brief Introduction of LCD Backlight LED Driving Solution for Handheld Devices
Entering the 21st century, energy consumption has increasingly become the focus of the entire human society. Due to the basic demand for lighting, how to more effectively use various energy sources to produce more lighting has become a huge driving force for exploring new lighting technologies. From primitive fuel l
[Power Management]
Brief Introduction of LCD Backlight LED Driving Solution for Handheld Devices
A brief analysis of Wei Dongshan's driver video based on ok6410--USB driver
Note: The mouse driver described in this article can only realize the button-like functions of the left and right mouse buttons and the scroll wheel. Pressing the left button will type "l", the right button will type "s", and the scroll wheel will type "enter". If you want to realize a normal mouse driver, refer to
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号