SPI驱动--TM1638

 

/**
  ******************************************************************************
  * @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)  //按键切换输出

#define PB4_H          GPIO_WriteHigh(GPIOC, (GPIO_Pin_TypeDef)GPIO_PIN_7)
#define PB4_L          GPIO_WriteLow(GPIOC, (GPIO_Pin_TypeDef)GPIO_PIN_7)
#define PB5_H                GPIO_WriteHigh(GPIOC, (GPIO_Pin_TypeDef)GPIO_PIN_6)
#define PB5_L                GPIO_WriteLow(GPIOC, (GPIO_Pin_TypeDef)GPIO_PIN_6)
#define PA1_H                GPIO_WriteHigh(GPIOC, (GPIO_Pin_TypeDef)GPIO_PIN_5)
#define PA1_L                GPIO_WriteLow(GPIOC, (GPIO_Pin_TypeDef)GPIO_PIN_5)
#define PA2_H                GPIO_WriteHigh(GPIOC, (GPIO_Pin_TypeDef)GPIO_PIN_4)
#define PA2_L                GPIO_WriteLow(GPIOC, (GPIO_Pin_TypeDef)GPIO_PIN_4)
uint8_t g_flag = 0;
uint8_t g_onetime = 0;
/*
按键切换输出:
1)111111  红灯亮

2)222222  绿灯亮

3)333333

4)444444

5)555555

6)8.8.8.8.8.8
*/
//共阴的数码管表格0-f
//uchar TAB_duan[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};

uchar BUFF_1[16]={0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00};

uchar BUFF_2[16]={0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF};

uchar BUFF_3[16]={0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA,0xAA};

uchar BUFF_4[16]={0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55,0x55};

//uchar BUFF_5[16]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};

uchar BUFF_5[16]={0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00};
uchar BUFF_6[16]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00};
uchar BUFF_7[16]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0xFF,0xFF,0xFF,0xFF,0x00,0x00,0xFF,0xFF};
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);
    TM1628_init();
    Init_UART1();
    Delay(0XFFFF);
  while (1)
  {
            ret=Read_key();    
        if(ret>0)
        {
            while(Read_key()==ret);        
            UART1_SendData8(ret);    
          while(( UART1_GetFlagStatus(UART1_FLAG_TXE)==RESET));            
            if(ret==03)
            {
                TM1628_display(BUFF_5);
            }
            else if(ret==02)
            {
                TM1628_display(BUFF_6);
            }
            else if(ret==01)
            {
                TM1628_display(BUFF_7);
            }
            
        }        

        
#if 1

    if(PA3==0)
        {
            while(PA3==0);
            g_flag++;
            g_onetime=1;
                
        }
        if(g_flag==1 && g_onetime==1)
        {
            TM1628_display(BUFF_1);
            g_onetime=0;
        }
        else if(g_flag==2 && g_onetime==1)
        {
            TM1628_display(BUFF_2);    
            g_onetime=0;
        }
        else if(g_flag==3 && g_onetime==1)
        {
            TM1628_display(BUFF_3);    
            g_onetime=0;
        }
        else if(g_flag==4 && g_onetime==1)
        {
            TM1628_display(BUFF_4);        
            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
 





void WR_TM1628_Byte(uchar TM_dat)
{
       uchar i;
       GPIO_Init(GPIOC, (GPIO_Pin_TypeDef)DIO, GPIO_MODE_OUT_PP_LOW_FAST);
 
       for(i=0;i<8;i++){
        Reset(MCLK);
        delay_us(DELAY_TM);
        if(TM_dat&0x01){
            Set(DIO);
        }else{
            Reset(DIO);
        }
        Set(MCLK);
        delay_us(DELAY_TM);
        TM_dat>>=1;
    }
    GPIO_Init(GPIOC, (GPIO_Pin_TypeDef)DIO, GPIO_MODE_IN_FL_NO_IT);
}
uint8_t Read_Byte(void)
{
    uint8_t i=0;
    uint8_t temp=0x00;
        GPIO_Init(GPIOC, (GPIO_Pin_TypeDef)DIO, GPIO_MODE_IN_FL_NO_IT);
    for(i=0; i<8; i++)
    {
                Reset(MCLK);
                delay_us(DELAY_TM);
                Set(MCLK);
                delay_us(DELAY_TM);
        temp>>=1;
        if(Get(DIO))
        {
            temp|=0x80;
        }
    }
        GPIO_Init(GPIOC, (GPIO_Pin_TypeDef)DIO, GPIO_MODE_OUT_PP_LOW_FAST);
    return temp;
}
unsigned char TM1638_Read(void)                    
{
    unsigned char i;
    unsigned char temp=0;
    GPIO_Init(GPIOC, (GPIO_Pin_TypeDef)DIO, GPIO_MODE_IN_FL_NO_IT);
    for(i=0;i<8;i++)
    {
        temp>>=1;
        Reset(MCLK);
        delay_us(DELAY_TM);
        if(Get(DIO))
            temp|=0x80;
        Set(MCLK);
    }
    return temp;
}
void Wr_TM_CMD(uchar TMcmd)
{
    Set(STB);
    delay_us(DELAY_TM);
    Reset(STB);
    WR_TM1628_Byte(TMcmd);
}
unsigned char Read_key(void)
{
    unsigned char c[4],i,key_value=0;
    Reset(STB);
    delay_us(DELAY_TM);
    Wr_TM_CMD(0x42);                 
    for(i=0;i<4;i++)    
{    
        c[i]=Read_Byte();
    }
    Set(STB);                           
    if(c[0]==0x04)key_value=1;
    if(c[0]==0x02)key_value=2;
    if(c[0]==0x01)key_value=3;

    if(c[1]==0x04)key_value=4;
    if(c[1]==0x02)key_value=5;
    if(c[1]==0x01)key_value=6;
    
    if(c[2]==0x04)key_value=7;
    if(c[2]==0x02)key_value=8;
    if(c[2]==0x01)key_value=9;
    
    if(c[3]==0x04)key_value=10;
    if(c[3]==0x02)key_value=11;
    if(c[3]==0x01)key_value=12;
    
    return key_value;
}
void Write_DATA(unsigned char add,unsigned char DATA)        
{
    Wr_TM_CMD(0x44);
    Reset(STB);
    delay_us(DELAY_TM);
    WR_TM1628_Byte(0xc0|add);
    WR_TM1628_Byte(DATA);
    Set(STB);    
}
void Write_allLED(unsigned char LED_flag)                    
{
    unsigned char i;
    for(i=0;i<8;i++)
        {
            if(LED_flag&(1<<i))
                Write_DATA(2*i+1,1);
            else
                Write_DATA(2*i+1,0);
        }
}


void TM1628_display(uchar* dat)
{
        Wr_TM_CMD(0x8A); //亮度调节      Write_Cmd(0x8a);
      Wr_TM_CMD(0x40);
        WR_TM1628_Byte(dat[0]);
    WR_TM1628_Byte(dat[1]);
    WR_TM1628_Byte(dat[2]);
    WR_TM1628_Byte(dat[3]);
    WR_TM1628_Byte(dat[4]);
    WR_TM1628_Byte(dat[5]);
    WR_TM1628_Byte(dat[6]);
    WR_TM1628_Byte(dat[7]);
    WR_TM1628_Byte(dat[8]);
    WR_TM1628_Byte(dat[9]);
    WR_TM1628_Byte(dat[10]);
    WR_TM1628_Byte(dat[11]);
    WR_TM1628_Byte(dat[12]);
    WR_TM1628_Byte(dat[13]);
        WR_TM1628_Byte(dat[14]);
    WR_TM1628_Byte(dat[15]);


}
void GPIO_Config(void){
    GPIO_Init(GPIOC, (GPIO_Pin_TypeDef)STB|MCLK|DIO, GPIO_MODE_OUT_PP_LOW_FAST);    
} 
void TM1628_init(void)
{
      int i=0;
    GPIO_Config();
  Wr_TM_CMD(0x88); //亮度调节      Write_Cmd(0x8a);
    Wr_TM_CMD(0x40);
    for(i=0;i<16;i++){
        WR_TM1628_Byte(0x00);
    }
}
tm1628.c

 

#ifndef __TM1628_H
#define __TM1628_H     
#include "stm8s.h"
/*******************************************************************************
Typedef definitions
*******************************************************************************/
#define uchar unsigned char
#define uint  unsigned int
//void TM1628_display(uchar dat1,uchar dat2,uchar dat3,uchar dat4,uchar dat5,uchar dat6,uchar dat7);
void TM1628_display(uchar* dat);
void TM1628_init(void);
void Wr_TM_CMD(uchar TMcmd);
void WR_TM1628_Byte(uchar TM_dat);
unsigned char Read_key(void);
void Write_allLED(unsigned char LED_flag);
void Write_DATA(unsigned char add,unsigned char DATA);
#endif
View Code

 

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