SPI驱动--TM1628

 

/**
  ******************************************************************************
  * @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;

/*
按键切换输出:
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[14]={0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF,0x00,0x00,0xFF,0xFF};

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

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

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

void main(void)
{
    GPIO_Init(GPIOA, (GPIO_Pin_TypeDef)GPIO_PIN_3,GPIO_MODE_IN_FL_NO_IT);
    TM1628_init();
    Delay(0XFFFF);
  while (1)
  {
        #if 1
    if(PA3)
        {
            while(PA3);
            g_flag++;
        }
        if(g_flag==1)
        {
            TM1628_display(BUFF_1);
        }
        else if(g_flag==2)
        {
            TM1628_display(BUFF_2);        }
        else if(g_flag==3)
        {
            TM1628_display(BUFF_3);        }
        else if(g_flag==4)
        {
            TM1628_display(BUFF_4);        }
        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****/
main

 

 #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))==SET           //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);
}


void Wr_TM_CMD(uchar TMcmd)
{
    Set(STB);
    delay_us(DELAY_TM);
    Reset(STB);
    WR_TM1628_Byte(TMcmd);
}


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]);

}
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<14;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);
#endif
_TM1628_H

 

posted @ 2024-07-08 17:11  不折不扣  阅读(10)  评论(0编辑  收藏  举报