dami的笔记 https://bbs.21ic.com/?264414 [收藏] [复制] [RSS] 技术交流你我他

日志

STM8S访问HT1381

已有 1477 次阅读2011-6-10 12:13 |个人分类:STM8|系统分类:单片机| STM8S, HT1381


很早前的一篇QQ空间的文章,转到这里来。







http//armlinux.taobao.com


http://shop66732237.taobao.com/


//搞了两三个晚上.还到处找问题,现在好了.把程序贴下来.大家少走弯路
//#include "mycommon.h" 头文件:
#define PCPORT         (GPIOC)
#define DS7_SCLK  (GPIO_PIN_3) //DS1302时钟
#define DS6_IO    (GPIO_PIN_7) // DS1302 数据
#define PGPORT         (GPIOG)
#define DS5_RST   (GPIO_PIN_0) //DS1302片选/复位

//MAIN文件:

#include "stm8_lib.h"
#include "mycommon.h"
u8 Settimebuf[7],Gettimebuf[7];
void Delay (u16 nCount);
void Delay(u16 nCount)
{
  while (nCount != 0)
  {
    nCount--;
  }
}

void init(void)
{
    GPIO_Init_TypeDef GPIO_InitStructure;
         GPIO_InitStructure.GPIO_Mode = GPIO_MODE_OUT_PP_LOW_SLOW;
    GPIO_InitStructure.GPIO_Pin = (DS7_SCLK);
         GPIO_Init(PCPORT, &GPIO_InitStructure);
    
    GPIO_InitStructure.GPIO_Mode=GPIO_MODE_OUT_OD_LOW_FAST;//GPI         O_MODE_OUT_OD_HIZ_FAST ;
    GPIO_InitStructure.GPIO_Pin = (DS6_IO);
         GPIO_Init(PCPORT, &GPIO_InitStructure);
    
    GPIO_InitStructure.GPIO_Mode = GPIO_MODE_OUT_PP_LOW_SLOW;
    GPIO_InitStructure.GPIO_Pin = (DS5_RST);
         GPIO_Init(PGPORT, &GPIO_InitStructure);
    
//----------------------------------------------------------------
//HT1381
//----------------------------------------------------------------

void W8bit_HT1381(u8 wrdata)
{
    u8 i;
  for(i=0; i<8; i++)
  {
        if(wrdata&0x01){GPIO_WriteHigh(PCPORT,DS6_IO );}
        else{GPIO_WriteLow(PCPORT,DS6_IO );}
        Delay(5);
        GPIO_WriteHigh(PCPORT,DS7_SCLK);        
      Delay(5);
        GPIO_WriteLow(PCPORT,DS7_SCLK);
        Delay(5);
        wrdata >>= 1;
  }
}
u8 R8bit_HT1381(void)
{
    u8 i,rddata;
    GPIO_WriteHigh(PCPORT,DS6_IO );
  for(i=0; i<8; i++)
  {
        rddata >>=1;
        Delay(5);
        if ((GPIO_ReadInputData(PCPORT) & DS6_IO) != (u8)0x00)
        { rddata |=0x80; }
        GPIO_WriteHigh(PCPORT,DS7_SCLK);        
      Delay(5);
        GPIO_WriteLow(PCPORT,DS7_SCLK);
        Delay(5);
        
  }
  return(rddata);
}
void Write1381(u8 Addr, u8 Wdata)
{
    GPIO_WriteLow(PGPORT,DS5_RST);
    Delay(5);
    GPIO_WriteLow(PCPORT,DS7_SCLK);
    Delay(5);
    GPIO_WriteHigh(PGPORT,DS5_RST);
    W8bit_HT1381(Addr);
    W8bit_HT1381(Wdata);
    GPIO_WriteHigh(PCPORT,DS7_SCLK);
    Delay(5);
    GPIO_WriteLow(PCPORT,DS7_SCLK);
    Delay(5);
    GPIO_WriteLow(PGPORT,DS5_RST);
}
u8 Read1381(u8 Addr)
{
    u8 rddata;
    GPIO_WriteLow(PGPORT,DS5_RST);
    Delay(5);
    GPIO_WriteLow(PCPORT,DS7_SCLK);
    Delay(5);
    GPIO_WriteHigh(PGPORT,DS5_RST);
    Delay(5);
    W8bit_HT1381(Addr);
    rddata = R8bit_HT1381();
    Delay(5);
    GPIO_WriteHigh(PCPORT,DS7_SCLK);        
    Delay(5);
    GPIO_WriteLow(PCPORT,DS7_SCLK);
    Delay(5);
    GPIO_WriteLow(PGPORT,DS5_RST);
  return(rddata);
}

void Set1381(u8 *pSecDa)
{
    u8 i;
    u8 Addr = 0x80;
    Write1381(0x8e,0x00); /* 控制命令,WP=0,写操作?*/
    for(i =7;i>0;i--)
    {
        Write1381(Addr,*pSecDa); /* 秒 分 时 日 月 星期 年 */

        pSecDa++;
        Addr +=2;
    }
    Write1381(0x8e,0x80); /* 控制命令,WP=1,写保护?*/
}
/********************************************************************
*
* 名称: v_Get1381
* 说明:
* 功能: 读取HT1381当前时间
* 调用: uc_R1381()
* 输入: ucCurtime: 保存当前时间地址。当前时间格式为: 秒 分 时 日 月 星期 年
* 7Byte (BCD码) 1B 1B 1B 1B 1B 1B 1B
* 返回值: 无
***********************************************************************/
void Get1381(void)
{
    u8 i;
    u8 Addr = 0x81;
    for (i=0;i<7;i++)
    {
        Gettimebuf = Read1381(Addr);/*格式为: 秒 分 时 日 月
        星期 年 */
        Addr += 2;
    }
}

//----------------------------------------------------------------
void main(void)
{
    u8 i;
    u8 *pText;
    init();
    
    Delay(0xFFFF);
    Delay(0xFFFF);
    Gettimebuf[6] = 0x0; //年
    Gettimebuf[5] = 0x0; //周
    Gettimebuf[4] = 0x0; //月
    Gettimebuf[3] = 0x0; //日
    Gettimebuf[2] = 0x0; //时
    Gettimebuf[1] = 0x0; //分
    Gettimebuf[0] = 0x30;//秒
    Settimebuf[6] = 0x08; //年
    Settimebuf[5] = 0x01; //周
    Settimebuf[4] = 0x12; //月
    Settimebuf[3] = 0x08; //日
    Settimebuf[2] = 0x21; //时
    Settimebuf[1] = 0x29; //分
    Settimebuf[0] = 0x30;//秒
    Set1381(Settimebuf);
    while (1)
    {
    Get1381();
    }
  
}

/**
  * @brief Reports the name of the source file and the source line number where
  * the assert error has occurred.
  * User can add his own implementation to report the file name and line number.
  * ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line)
  * @retval void None
  * @par Required preconditions:
  * None
  * @par Called s:
  * None
  */
#ifdef FULL_ASSERT
void assert_failed(u8 *file, u16 line)
#else
void assert_failed(void)
#endif
{
  /* Put enter your own code to manage an assert error */
}

/******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/

STM32,STM8SGPIO做双向IO口的方法STM32,STM8SGPIO做双向IO口的方法:
设置为开路输出,接上拉电阻,读时先输出高电平(否则有可能因先前低电平输出锁死IO口为低电平),然后读.


路过

鸡蛋

鲜花

握手

雷人

评论 (0 个评论)