IIC驱动--TM1651

 

/**
  ******************************************************************************
  * @file    GPIO_Toggle\main.c
  * @author  MCD Application Team
  * @version  V2.0.1
  * @date     18-November-2011
  * @brief   This file contains the main function for GPIO Toggle example.
  ******************************************************************************
  * @attention
  *
  * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  *
  * <h2><center>&copy; COPYRIGHT 2011 STMicroelectronics</center></h2>
  ******************************************************************************
  */ 

/* Includes ------------------------------------------------------------------*/
#include "stm8s.h"
#include "tm1628.h"
/**
  * @addtogroup GPIO_Toggle
  * @{
  */

/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Evalboard I/Os configuration */

#define LED_GPIO_PORT  (GPIOB)
#define LED_GPIO_PINS  (GPIO_PIN_5)



/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
void Delay (uint16_t nCount);

/* Private functions ---------------------------------------------------------*/
/* Public functions ----------------------------------------------------------*/

/**
  * @brief  Main program.
  * @param  None
  * @retval None
  */

#define PA3 GPIO_ReadInputPin(GPIOA, (GPIO_Pin_TypeDef)GPIO_PIN_3)  //按键切换输出


uint8_t g_flag = 0;
uint8_t g_onetime = 0;


void Init_UART1(void)
{
    UART1_DeInit();
    UART1_Init((u32)9600, UART1_WORDLENGTH_8D, UART1_STOPBITS_1, UART1_PARITY_NO, UART1_SYNCMODE_CLOCK_DISABLE, UART1_MODE_TXRX_ENABLE);
    
UART1_ITConfig(UART1_IT_RXNE_OR, ENABLE);

//    UART1_Cmd(ENABLE);
}
void Send(uint8_t dat)
{
  while(( UART1_GetFlagStatus(UART1_FLAG_TXE)==RESET));
    
        UART1_SendData8(dat);
    
}
void UART1_SendData8_Buff(uint8_t* data,uint8_t len)
{
    uint8_t i = 0;
    for(i=0;i<len;i++)
    {
        while(( UART1_GetFlagStatus(UART1_FLAG_TXE)==RESET));
      UART1_SendData8(data[i]);    
    }


}
unsigned char num[8];    
unsigned char  tab[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,
                           0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71};
void main(void)
{
    uint8_t ret=0;
    uint8_t i=0;
    GPIO_Init(GPIOA, (GPIO_Pin_TypeDef)GPIO_PIN_3,GPIO_MODE_IN_FL_NO_IT);
    TM1651_init();
    Init_UART1();
    Delay(0XFFFF);
  while (1)
  {
        ret=GetKey();
        if(ret==0xf2)
        {
            Seg_Shows(0xc0,0xee);
            Seg_Shows(0xc1,0xee);
            Seg_Shows(0xc2,0xee);
            Seg_Shows(0xc3,0xee);
        }
        else if(ret==0xf1)
        {
            Seg_Shows(0xc0,0x77);
            Seg_Shows(0xc1,0x77);
            Seg_Shows(0xc2,0x77);
            Seg_Shows(0xc3,0x77);
        }
        else if(ret==0xf3)
        {
            Seg_Shows(0xc0,0xbb);
            Seg_Shows(0xc1,0xbb);
            Seg_Shows(0xc2,0xbb);
            Seg_Shows(0xc3,0xbb);
        }
        //UART1_SendData8(ret);    
        //while(( UART1_GetFlagStatus(UART1_FLAG_TXE)==RESET));    
        
#if 1
    if(PA3==0)
        {
            while(PA3==0);
            g_flag++;
            g_onetime=1;
                
        }
        if(g_flag==1 && g_onetime==1)
        {
            Seg_Shows(0xc0,0xff);
            Seg_Shows(0xc1,0x00);
            Seg_Shows(0xc2,0xff);
            Seg_Shows(0xc3,0x00);
            g_onetime=0;
        }
        else if(g_flag==2 && g_onetime==1)
        {
            Seg_Shows(0xc0,0x00);
            Seg_Shows(0xc1,0xff);
            Seg_Shows(0xc2,0x00);
            Seg_Shows(0xc3,0xff);    
            g_onetime=0;
        }
        else if(g_flag==3 && g_onetime==1)
        {
            Seg_Shows(0xc0,0xaa);
            Seg_Shows(0xc1,0xaa);
            Seg_Shows(0xc2,0xaa);
            Seg_Shows(0xc3,0xaa);    
            g_onetime=0;
        }
        else if(g_flag==4 && g_onetime==1)
        {
            Seg_Shows(0xc0,0x55);
            Seg_Shows(0xc1,0x55);
            Seg_Shows(0xc2,0x55);
            Seg_Shows(0xc3,0x55);
            g_onetime=0;
        }
        else 
        {
        }
        if(g_flag>=4)
        {
            g_flag=0;
        }
#endif
    }

}

/**
  * @brief Delay
  * @param nCount
  * @retval None
  */
void Delay(uint16_t nCount)
{
  /* Decrement nCount value */
  while (nCount != 0)
  {
    nCount--;
  }
}

#ifdef USE_FULL_ASSERT

/**
  * @brief  Reports the name of the source file and the source line number
  *   where the assert_param error has occurred.
  * @param file: pointer to the source file name
  * @param line: assert_param error line source number
  * @retval None
  */
void assert_failed(uint8_t* file, uint32_t line)
{ 
  /* 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) */

  /* Infinite loop */
  while (1)
  {
  }
}
#endif

/**
  * @}
  */

/******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
mian

 

 #include "tm1628.h"


 #define DELAY_TM  50
 void Delay (uint16_t nCount);
 /**
  * @brief Delay
  * @param nCount
  * @retval None
  */
void delay_us(uint16_t nCount)
{
  /* Decrement nCount value */
  while (nCount != 0)
  {
    nCount--;
  }
}

/*********************define and global variables*********************************************/
//#define STB GPIO_PIN_6                          //chip-select line
#define MCLK GPIO_PIN_5                                         //clock line
#define DIO GPIO_PIN_4    
#define Set(x) GPIO_WriteHigh(GPIOC,(GPIO_Pin_TypeDef)(x))                //Sets the selected data port bits
#define Reset(x) GPIO_WriteLow(GPIOC,(GPIO_Pin_TypeDef)(x))            //Resets the selected data port bits
#define Get(x) GPIO_ReadInputPin(GPIOC,(GPIO_Pin_TypeDef)(x))==DIO           //Read the specified input port pin
#define SDA_OUT  GPIO_Init(GPIOC, (GPIO_Pin_TypeDef)DIO, GPIO_MODE_OUT_PP_LOW_FAST)
#define SDA_IN  GPIO_Init(GPIOC, (GPIO_Pin_TypeDef)DIO, GPIO_MODE_IN_PU_NO_IT);

void I2CStart(void) // 1651 开始
{
    SDA_OUT;
    Set(MCLK);
    Set(DIO);
    delay_us(DELAY_TM);
    Reset(DIO);        
    Reset(MCLK);    
  delay_us(DELAY_TM);    
}

//=============================================
void I2Cask(void) //1651 应答
{
    Reset(MCLK);
    delay_us(DELAY_TM);
    SDA_IN;
    while(Get(DIO));
    delay_us(DELAY_TM);
    Set(MCLK);
    delay_us(DELAY_TM);
}

//========================================
void I2CStop(void) // 1651 停止
{
    SDA_OUT;
    Reset(MCLK);
    Reset(DIO);
    delay_us(DELAY_TM);
    Set(MCLK);
    delay_us(DELAY_TM);
    Set(DIO);
    delay_us(DELAY_TM);
    Reset(MCLK);
    Reset(DIO);
}

//=========================================
void I2CWrByte(uint8_t oneByte) //写一个字节
{
        unsigned char i;
        SDA_OUT;
    for(i=0; i<8; i++) {
            Reset(MCLK);
            if(oneByte&0x01) {
                    Set(DIO);
            } else {
                    Reset(DIO);
            }
            oneByte=oneByte>>1;
            delay_us(DELAY_TM);
            Set(MCLK);
            delay_us(DELAY_TM);
    }
//8位数据传送完
    I2Cask();
}

//-------------------------------------------------
uint8_t GetKey(void) //读按键
{
        uint8_t rekey=0, i=0;

        I2CStart();
        I2CWrByte(0x46); //读按键命令
        //I2Cask();
    SDA_OUT;
        Set(DIO);// 在读按键前拉高数据线
        for(i=0;i<8;i++) //从低位开始读
        {
            Reset(MCLK);
            rekey = rekey>>1;
            delay_us(DELAY_TM);
            delay_us(DELAY_TM);
            Set(MCLK);
            SDA_IN;
            if(Get(DIO))
            {
                rekey = rekey|0x80;
            }
            else
            {
                rekey = rekey|0x00;
            }
            delay_us(DELAY_TM);
            delay_us(DELAY_TM);
        }
        I2Cask();
        I2CStop();
        return (rekey);
}
void GPIO_Config(void){
    GPIO_Init(GPIOC, (GPIO_Pin_TypeDef)MCLK|DIO, GPIO_MODE_OUT_PP_LOW_FAST);    
} 
void TM1651_init(void)
{
    GPIO_Config();
}


/************显示函数,地址自加一************/
void disp0(unsigned char *p) {
    unsigned char i;
    I2CStart();
    I2CWrByte(0x40);  //数据命令设置:地址自动加1,写数据到显示寄存器
    I2CStop();
    I2CStart();
    I2CWrByte(0xc0);  //地址命令设置:初始地址为00H
    for(i=0; i<4; i++) { //发送4字节数据到显存
        I2CWrByte(*p);
        p++;
    }
    I2CStop();
    I2CStart();
    I2CWrByte(0x8C); //显示控制命令:开显示,脉冲宽度为11/16.
    I2CStop();
}
unsigned char  CODE[10]= {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,};
/************显示函数,固定地址写数据************/
void Seg_Shows(unsigned char add, unsigned char value) {
    I2CStart();
    I2CWrByte(0x44); //数据命令设置:固定地址,写数据到显示寄存器
    I2CStop();
    I2CStart();
    I2CWrByte(add);//地址命令设置:写入add对应地址
    I2CWrByte(value);//给add地址写数据
    I2CStop();
    I2CStart();
    I2CWrByte(0x8C);//显示控制命令:开显示,脉冲宽度为11/16.
    I2CStop();
}
tm1651

 

#ifndef __TM1628_H
#define __TM1628_H     
#include "stm8s.h"
/*******************************************************************************
Typedef definitions
*******************************************************************************/
#define uchar unsigned char
#define uint  unsigned int


void TM1651_init(void);
uint8_t GetKey(void);
void Seg_Shows(unsigned char add, unsigned char value);
void disp0(unsigned char *p);
#endif
__TM1651_H

 

posted @ 2024-07-08 15:32  不折不扣  阅读(24)  评论(0编辑  收藏  举报