zl程序教程

您现在的位置是:首页 >  其它

当前栏目

基于STC8H1K28的人机键盘界面

基于 界面 键盘 人机
2023-09-11 14:15:31 时间

 

01设计背景


在很多基于STC单片机的调试版都具有统一的调试端口。为了制作简便的现场调试扫之,设计以基于STC8H1K25的人机接口板,针对不同的调试对象基于从简单的按键,到复杂的模拟量的调试接口。

▲ STC单片机的统一调试端口

▲ STC单片机的统一调试端口

 

02电路设计


1.原理图和PCB1

▲ 原理图

▲ 原理图

▲ PCB设计图

▲ PCB设计图

▲ 快速制版后的实验电路板

▲ 快速制版后的实验电路板

2.单片机软件2

/*
**==============================================================================
** MAIN.C:             -- by Dr. ZhuoQing, 2020-04-23
**
**==============================================================================
*/
#include <stdio.h>

#include "C51BASIC.H"
#include "STC8H.H"
//------------------------------------------------------------------------------
#define MAIN_GLOBALS        1              // Define the global variables
#include "MAIN.H"
#include "SERIALTXT.H"
#include "STRING.H"

//------------------------------------------------------------------------------
#define LED             1, 7
#define BEEP_PIN        1, 2 

#define BEEP_ON         OFF(BEEP_PIN)
#define BEEP_OFF        ON(BEEP_PIN)

//------------------------------------------------------------------------------

void main(void) {
    unsigned int nCount;
    unsigned int nShowCount;
    unsigned char ucKey;
    unsigned char szString[16], i;
    
    //----------------------------------------------------------------------    
    STC8HInit();
    //----------------------------------------------------------------------    
    MainInit();
    
    WaitTime(200);
    //----------------------------------------------------------------------
    printf("STC8H8K    -- by Dr. ZhuoQing, %s,%s\r\n", __DATE__, __TIME__);
           
    //--------------------------------------------------------------------------
    
    //--------------------------------------------------------------------------
    nCount = nShowCount = 0;    
    for(;;) {
        if(++nCount & 0x100) ON(LED);
        else OFF(LED);
        
        WaitTime(1);
        
        //----------------------------------------------------------------------
        if(++nShowCount >= 500) {
            nShowCount = 0;
            
        }
        
        //--------------------------------------------------------------        
        
        ucKey = ReadKey();
        if(ucKey != KEY_NULL) {
            sprintf(szString, "key%bd\r", ucKey);
            printf("%s\n", szString);
            ucKey = strlen(szString);
            for(i = 0; i < ucKey; i ++) {
                UART2SendChar(szString[i]);
            }
        } 
        
        //----------------------------------------------------------------------
            
#if SERIAL_DEBUG_EN
        ConsoleDebug();
#endif // SERIAL_DEBUG_EN

    }

}

//------------------------------------------------------------------------------
void MainInit(void) {
    PM_BIDIR(LED);

#if SERIAL_DEBUG_EN
    SerialTxtInit();
#endif // SERIAL_DEBUG_EN

    ON(BT1_PIN);
    ON(BT2_PIN);
    ON(BT3_PIN);
    ON(BT4_PIN);
    ON(BEEP_PIN);

    PM_BIDIR(BT1_PIN);
    PM_BIDIR(BT2_PIN);
    PM_BIDIR(BT3_PIN);
    PM_BIDIR(BT4_PIN);
    
    PM_PP(BEEP_PIN);
    
}

//------------------------------------------------------------------------------
unsigned char ReadKeyCode(void) {
    if(VAL(BT1_PIN) == 0) return KEY_1;
    if(VAL(BT2_PIN) == 0) return KEY_2;
    if(VAL(BT3_PIN) == 0) return KEY_3;
    if(VAL(BT4_PIN) == 0) return KEY_4;
    return KEY_NULL;
}

unsigned char ReadKey(void) {
    unsigned char ucKey;
    
    ucKey = ReadKeyCode();
    if(ucKey == KEY_NULL)
        return KEY_NULL;
    
    BEEP_ON;
    WaitTime(25);
    if(ReadKeyCode() == KEY_NULL) {
        BEEP_OFF;
        return KEY_NULL;
    }
    
    while(ReadKeyCode() != KEY_NULL);
    
    BEEP_OFF;
    
//    printf("%bd\r\n", ucKey);
    return ucKey;
        
}

//==============================================================================
//                END OF THE FILE : MAIN.C
//------------------------------------------------------------------------------

  1. 电路设计AD工程文件:AD\Test\2020\STCTest\STCHMISTC8H1K.SchDoc * ↩︎

  2. 单片机程序工程文件:C51\STC\Test\2020\STCHMISTC8H1K\STCHMISTC8H1K.uvproj ↩︎