移植了stm32的tm1638驱动到stm8但是缺无法获取键值

2019-07-15 13:31发布



程序如下


#include "stm8s.h"                              //#include "STM32f10x.h"

/*********************define and global variables*********************************************/

//#define GPIO_Pin_TypeDef GPIO
#define STB  GPIO_PIN_0                                          //chip-select line
#define CLK1 GPIO_PIN_1                                                 //clock line
#define DIO  GPIO_PIN_2                                                                                                                                                 //data line
#define Set(x)   GPIO_WriteHigh(GPIOB,(x))                                //Sets the selected data port bits
#define Reset(x) GPIO_WriteLow(GPIOB,(x))                        //Resets the selected data port bits
#define Get(x) GPIO_ReadInputPin(GPIOB,(x))==SET                   //Read the specified input port pin


uint16_t const tm_dat[2][14]={{'0','1','2','3','4','5',                //the char and its segment code
                        '6','7','8','9','.','-','_',' '},
                        {0x3F,0x06,0x5B,0x4F,0x66,0x6D,
                        0x7D,0x07,0x7F,0x6F,0x80,0x40,
                        0x08,0x00}};
/***********************************************************************************************
*Function Name: RCC_Config               
*Purpose      : Configration Clock
***********************************************************************************************/
void RCC_Config(){
        CLK_HSECmd(ENABLE);
}

/***********************************************************************************************
*Function Name: GPIO_Config               
*Purpose      : Configration GPIO
***********************************************************************************************/
void GPIO_Config(){
        GPIO_Init(GPIOB, GPIO_PIN_0, GPIO_MODE_OUT_PP_HIGH_FAST);
        GPIO_Init(GPIOB, GPIO_PIN_1, GPIO_MODE_OUT_PP_HIGH_FAST);
        GPIO_Init(GPIOB, GPIO_PIN_2, GPIO_MODE_OUT_PP_HIGH_FAST);
        GPIO_Init(GPIOD, GPIO_PIN_7, GPIO_MODE_OUT_PP_LOW_FAST);
}
/***********************************************************************************************
*Function Name: Write_Byte               
*Purpose      : Write one byte to the data port
*params       : byte  -------8-bits byte  
*return       : none
***********************************************************************************************/
void Write_Byte(uint8_t byte){
        uint8_t i=0;
        for(i=0;i<8;i++){
                Reset(CLK1);
                if(byte&0x01){
                        Set(DIO);
                }else{
                        Reset(DIO);
                }
                Set(CLK1);
                byte>>=1;
        }
}

/***********************************************************************************************
*Function Name: Read_Byte               
*Purpose      : Read one byte from data port
*params       : none
*return       : the 8-bits byte which is read from data port
***********************************************************************************************/
int8_t Read_Byte(){
        uint8_t i=0;
        uint8_t temp=0x00;
        Reset(STB);
        for(i=0;i<8;i++){
                Reset(CLK1);
                temp>>=1;
                if(Get(DIO)){
                        temp|=0x80;
                }
                Set(CLK1);
        }
        return temp;
}
/*unsigned char TM1638_Read(void)                                        //?áêy?Yoˉêy
{
        unsigned char i;
        unsigned char temp=0;
        Set(DIO);        //éè???aê?è?
        for(i=0;i<8;i++)
        {
                temp>>=1;
                Reset(CLK1);
                if(DIO)
                        temp|=0x80;
                Set(CLK1);
        }
        return temp;
}*/

/***********************************************************************************************
*Function Name: Write_Cmd               
*Purpose      : Write a conmand to the data port
*params       : cmd  -------8-bits byte,the conmand,check the data sheet to find the conmand
*return       : none
***********************************************************************************************/
void Write_Cmd(uint8_t cmd){
        Set(STB);
        Reset(STB);
        Write_Byte(cmd);
}
/***********************************************************************************************
*Function Name: Read_Key               
*Purpose      : Read the key number which has been pressed
*params       : none
*return       : the number of the key. 0-8.  "return 0" represents no key has been pressed.
***********************************************************************************************/
int8_t Read_Key(){

        uint8_t i=0;
        uint8_t key1=0x00;
        uint16_t key2=0x00;
        Write_Cmd(0x42);
        Set(DIO);   //this is obligatory, check the data sheet,GPIO
        for(i=0;i<4;i++){
                key1=Read_Byte();
                key2|=(key1<<i);}
                key2>>=1;
        for(i=0;i<8;i++){
                if(0x01<<i==key2)return i+1;
        }

        return 0;
        }
/*unsigned char Read_key(void)
{
        uint8_t c[4],i,key_value=0;
        Reset(STB);
        Write_Cmd(0x42);                          
        for(i=0;i<4;i++)               
        c=TM1638_Read();
        Set(STB);                                                   
        for(i=0;i<4;i++)
                key_value|=c<<i;
        for(i=0;i<8;i++)
                if((0x01<<i)==key_value)
                        break;
        return i;
}*/

/***********************************************************************************************
*Function Name: Write_Dat               
*Purpose      : Write data to the location specified
*params       : addr  ------the address,0x00 to 0x0f
                dat   ------the data,segment code
*return       : none
***********************************************************************************************/
void Write_Dat(uint8_t addr,uint8_t dat){
        Write_Cmd(0x44);
        Write_Cmd(0xc0|addr);
        Write_Byte(dat);
}

/***********************************************************************************************
*Function Name: TM1638_SendData               
*Purpose      : Write data to the location specified
*params       : i     ------the bit code of digtal tube,0 to 7
                str   ------the string,the char which was not in tm_data will be replace with "''".
*return       : none
***********************************************************************************************/
void TM1638_SendData(uint8_t i,char * str){
        int j=0,k=0;
        unsigned char chr;
        for(;i<8;i++){
                k=0;
                for(j=0;j<14;j++){
                        if(*str==tm_dat[0][j]){
                                chr=tm_dat[1][j];
                                k=1;
                                break;
                        }
                }
               
                if(k==0){
                        chr=0x00;
                }
               
                if(*(str+1)=='.'){
                        chr|=0x80;
                        Write_Dat(i*2,chr);
                        str++;
                }else{
                        Write_Dat(i*2,chr);
                }
                str++;
                if(*str=='')break;
        }
}
/***********************************************************************************************
*Function Name: TM1638_Init               
*Purpose      : the initialization of tm1638
*params       : none
*return       : none
***********************************************************************************************/
void TM1638_Init(){
        int i=0;
        RCC_Config();
        GPIO_Config();
        Write_Cmd(0x8a);
        Write_Cmd(0x40);
        Reset(STB);
        Write_Byte(0xc0);
        for(i=0;i<16;i++)
        Write_Byte(0x00);
        Set(STB);
}

友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
该问题目前已经被作者或者管理员关闭, 无法添加新回复
2条回答
暴躁的小黄瓜
1楼-- · 2019-07-15 19:13
这个程序可以驱动tm1638显示却无法获得键值,本人菜鸟还请各位大神指点
hrsoftgao
2楼-- · 2019-07-15 19:34
你是如何显示的  能否贴出代码,共同研究。

一周热门 更多>