標(biāo)題: TM1637 STM32F103配置 [打印本頁]

作者: 曹義朋    時(shí)間: 2020-4-16 13:34
標(biāo)題: TM1637 STM32F103配置
#include "TM1637.h"

uint8_t  dpFlag = 0;//主函數(shù)有說明
unsigned char  Data[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
unsigned char  DataDp[]={0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef};
struct {
        char shi;
        char ge;
}Hour;

struct {
        char shi;
        char ge;
}Min;


/*
  *@breif GPIO_INIT  
  *@note  no
*/
void SysTick_Delay_Us(unsigned int i)
{
   for(;i>0;i--);
}


void GPIO_Config(void)
{
        GPIO_InitTypeDef  GPIO_InitStructure;
       
        TM1637_RCC_Cmd(TM_DIO_CLK|TM_CLK_CLK,ENABLE);
       
        GPIO_InitStructure.GPIO_Pin = TM_DIO_PIN | TM_CLK_PIN;
        GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;//éè???aí?íìê?3?
        GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
       
        GPIO_Init(TM_DIO_PORT,&GPIO_InitStructure);
       
}


/*
時(shí)鐘線為高時(shí),數(shù)據(jù)線由高變低
*/
void I2C_Start(void)
{
        CLK_1;
        DIO_1;
        SysTick_Delay_Us(140);
       
        DIO_0;
        SysTick_Delay_Us(140);
        CLK_0;
        SysTick_Delay_Us(140);
}


/*
*時(shí)鐘線為高時(shí),數(shù)據(jù)線由低變高
*/
void I2C_stop(void)
{
       
        CLK_0;
        SysTick_Delay_Us(140);
  DIO_0;
        SysTick_Delay_Us(140);
       
  CLK_1;
        SysTick_Delay_Us(140);       
        DIO_1;
        SysTick_Delay_Us(140);         
}


   //寫一個(gè)比特位
    void TM1637_WriteBit(unsigned char mBit)
    {
             CLK_0;
             SysTick_Delay_Us(140);
       
         if(mBit)
                   DIO_1;
         else
                   DIO_0;
         
         SysTick_Delay_Us(140);
         CLK_1;
         SysTick_Delay_Us(140);       
}


//寫一個(gè)字節(jié)   
    void TM1637_WriteByte(unsigned char Byte)
    {
              char loop = 0;
              for(loop = 0;loop<8;loop++)
              {
           TM1637_WriteBit((Byte>>loop) & 0x01);//先寫低位       
              }
                    CLK_0;
                    SysTick_Delay_Us(140);
        DIO_1;       
        SysTick_Delay_Us(140);
        CLK_1;               
            SysTick_Delay_Us(140);
        while(GPIO_ReadInputDataBit(TM_DIO_PORT ,TM_DIO_PIN) == 0x01);
                    //等待應(yīng)答信號(hào)
    }

/*
*寫指令函數(shù)
*B7:B6 =01  數(shù)據(jù)命令控制
*            11地址命令控制
*            10顯示命令控制
*/

void TM1637_WriteCommand(unsigned char mData)
{
        I2C_Start();
        TM1637_WriteByte(mData);
        I2C_stop();
}


/*
*@note  寫入地址和數(shù)據(jù)
*/

void TM1637_WriteData(unsigned char addr,unsigned char mData)
{
        I2C_Start();
        TM1637_WriteByte(addr);
        TM1637_WriteByte(mData);
        I2C_stop();
}




  /*
   控制顯示,配置好數(shù)據(jù)結(jié)構(gòu)體之后,直接調(diào)用該函數(shù)就可以顯示
    */
    void Time_Display(void)
    {
            TM1637_WriteCommand(0x40);
           
            if(dpFlag)
        TM1637_WriteData(0xc1,DataDp[Hour.ge]);

            else
        TM1637_WriteData(0xc1,Data[Hour.ge]);


        TM1637_WriteData(0xc0,Data[Hour.shi]);
        TM1637_WriteData(0xc2, Data[Min.shi]);
        //TM1637_WriteData(0xc1, Data[Min.ge]);
        TM1637_WriteData(0xc3, Data[Min.ge]);
        TM1637_WriteCommand(0x8a);
     }







歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1