单片机应用——IO控制(一)

原理图

P2

功能简介:芯片上店3s内输出为零。A组逻辑与BCD一致。

A拉低1秒,k输出14s的高后输出低电平并且保持低,A拉低3s(开脚)高1.5s,低2s,高1.5s,一直低;A拉低6s后,A1输出高,高1.5s,低2s,跳转高电平1.5s,一直低;如果A拉低后一直低,K就一直低,(开脚)也一直低 ,A1保持低电平;如果A释放1s后,K端输出14s高跳转为低电平;A释放3s后,(关脚)高1.5s,低2s,又高1.5s后一直保持低状态;A释放6s后,A1输出两次高电平,A1保持低电平。

简单来说:A每次拉低,K先输出14s高,接下来(开脚)闪两次,再A1闪两次,之后K保持低电平;

A释放,K输出14s高,(关脚)闪两次,再A1闪两次,之后全部输出保持低。

8位单片机项目需求与合作:18665321219

/* =========================================================================
 * Project:       GPIO_Setting
 * File:          main.c
 * Description:   Set GPIO of PORTA/PORTB
 *                  1. PORTB I/O state
 *                      - PB7 ~ PB6 set input mode and enable pull-high resistor
 *                      - PB5 ~ PB4 set output mode 
 *                      - PB3 ~ PB2 set input mode and enable pull-low resistor
 *                      - PB1 ~ PB0 set open-drain output mode                               
 *                              
 *                  2. PORTA I/O state
 *                      - PA3 ~ PA2 set output mode 
 *                      - PA1 ~ PA0 set input mode and enable pull-low resistor                                
 * Author:        JasonLee
 * Version:       V1.0                              
 * Date:          2017/11/08
 =========================================================================*/
#include <ny8.h>
#include "ny8_constant.h"
#define UPDATE_REG(x)    __asm__("MOVR _" #x ",F")

typedef    union sFlg 
{
    unsigned char mByte;
    
    struct        //mBit
    {
        unsigned char fBit0 : 1;  // A SW/A1
        unsigned char fBit1 : 1;  // B SW/B1
        unsigned char fBit2 : 1;  // C SW/C1
        unsigned char fBit3 : 1;  // D SW/D1
        unsigned char fBit4 : 1;  // E SW/E1/OPEN
        unsigned char fBit5 : 1;  // K
        unsigned char fBit6 : 1;  // CLOSE     
        unsigned char fBit7 : 1;  //
    };
}flgType;
    
volatile flgType swState;
volatile flgType optState;

volatile unsigned char timCount = 0;        //int 
//volatile unsigned int timCount1 = 0;    //char


unsigned char swFlgA = 0;
unsigned char swFlg1A = 0;
unsigned char dlyCountA = 0;
//unsigned char dlyCount1A = 0;
//unsigned char StateA1 = 0;
unsigned char dlyCountA1 = 0;
unsigned char dlyFlgA1 = 0;

unsigned char swFlgB = 0;
unsigned char swFlg1B = 0;
unsigned char dlyCountB = 0;
//unsigned char dlyCount1B = 0;
//unsigned char StateB1 = 0;
unsigned char dlyCountB1 = 0;
unsigned char dlyFlgB1 = 0;

unsigned char swFlgC = 0;
unsigned char swFlg1C = 0;
unsigned char dlyCountC = 0;
//unsigned char dlyCount1C = 0;
//unsigned char StateC1 = 0;
unsigned char dlyCountC1 = 0;
unsigned char dlyFlgC1 = 0;

unsigned char swFlgD = 0;
unsigned char swFlg1D = 0;
unsigned char dlyCountD = 0;
//unsigned char dlyCount1D = 0;
//unsigned char StateD1 = 0;
unsigned char dlyCountD1 = 0;
unsigned char dlyFlgD1 = 0;

unsigned char dlyCountCLOSE = 0;
unsigned char dlyFlgCLOSE = 0;
//unsigned char dlyCount1E = 0;
//unsigned char StateD1 = 0;
unsigned char dlyCountOPEN = 0;
unsigned char dlyFlgOPEN = 0;

//unsigned char StateK = 0;
unsigned char dlyFlgK = 0;
unsigned char dlyCountK = 0;

void isr_hw(void) __interrupt(0)
{
    if(INTFbits.T0IF)                //SET TIM1 /CLOSE TIM1
    {      
        TMR0 = 51;        //32768/16/204  100MS  //L_IRC
        timCount++;
        //timCount1++;
          
        INTFbits.T0IF = 0;
    }
}

void main(void)
{
    DISI();
//;Initial GPIO  
//;PORTB I/O state
//        ; PB7 ~ PB6 set input mode and enable pull-high resistor
//        ; PB5 ~ PB4 set output mode and outputs low
//        ; PB3 ~ PB2 set input mode and enable pull-low resistor
//        ; PB1 ~ PB0 set open-drain output mode   
    //ABPLCON = ~( C_PB3_PLB | C_PB2_PLB | C_PA1_PLB | C_PA0_PLB );   //Enable PB3、PB2、PA1、PA0 Pull-Low Resistor,others disable    
    PORTA = 0x00;    //PA0,PA1,PA2,PA3 OUTPUT LOW
    IOSTA = (unsigned char)(~(C_PA3_Input | C_PA2_Input | C_PA1_Input | C_PA0_Input));    //Enable PA0,PA1,PA2,PA3 input
    BPHCON = (unsigned char)(~( C_PB4_PHB | C_PB2_PHB | C_PB1_PHB | C_PB0_PHB ));      // Enable PB5,PB4,PB2,PB1,PB0 Pull-High Resistor,others disable
    PORTB = 0x00;    //PB7,PB6    OUPTPUT LOW
    IOSTB = C_PB4_Input | C_PB2_Input | C_PB1_Input | C_PB0_Input;   //Set PB4,PB2 &,PB1,PB0 to input mode,others set to output mode
    
    /*
    //initial tim1--------------------------------------------------------------------
    //      ;instrclk i_hrc 4/4M  div = 1  计数8次溢出即可
    TMR1 = 0x9c;    //到0下溢出中断,计数8 //8M/2T/256/156 = 100hz   10ms
    T1CR2 = C_TMR1_ClkSrc_Inst | C_PS1_Div256;   //指令时钟
    INTFbits.T1IF = 0;        //清除中断标志位
    INTE = INTE | C_INT_TMR1;    //开启定时器中断
    T1CR1 = C_TMR1_Reload | C_TMR1_En;   //自动重载
    */
    //initial tim0--------------------------------------------------------------------
    //定时器中断为100ms
    T0MD = C_PS0_TMR0 | C_PS0_Div16 | C_TMR0_LowClk | C_TMR0_Clk;   //使用 L_IRC
    TMR0 = 51;        //32768/16/204 = 10
    INTE = INTE | C_INT_TMR0;
    PCON1 = C_TMR0_En;
    
    ENI();
    
    //initial variable
    swState.mByte = 0;
    optState.mByte = 0;
    
    while(1)
    {
        //check A state------------------------------------------------------------
        if(PORTBbits.PB0 == 0)
        {
            swFlg1A = 0;            //清除释放标志
            swState.fBit0 = 1;
            
            if(swFlgA == 0)
            {
                swFlgA = 1;
                //dlyCount1A = timCount + 30;
                dlyCountA = timCount + 10;
            }    
            if(dlyCountA == timCount)
            {
                if(swFlgA == 1)
                {
                    swFlgA = 2;
                    optState.fBit5 = 1;
                    dlyFlgK = 0;
                    dlyCountA = timCount + 20;
                }
                else if(swFlgA == 2)
                {
                    swFlgA = 3;
                    optState.fBit4 = 1;
                    dlyFlgOPEN = 0;
                    dlyCountA = timCount + 30;
                }
                else if(swFlgA == 3)
                {
                    swFlgA = 4;
                    optState.fBit0 = 1; //A1 state
                    dlyFlgA1 = 0;        //清除A1输出标志
                }
            }
        }
        else
        {
            swFlgA = 0;        //清除按压标志
            if(swState.fBit0 == 1)
            {
                if(swFlg1A == 0)
                {
                    swFlg1A = 1;
                    //dlyCount1A = timCount + 30;
                    dlyCountA = timCount + 10;
                }
                if(dlyCountA == timCount)
                {
                    if(swFlg1A == 1)
                    {
                        swFlg1A = 2;
                        optState.fBit5 =1;
                        dlyFlgK = 0;
                        dlyCountA = timCount + 20;
                    }
                    else if(swFlg1A == 2)
                    {
                        swFlg1A = 3;
                        optState.fBit6 = 1;
                        dlyFlgCLOSE = 0;
                        dlyCountA = timCount + 30;
                    }
                    else if(swFlg1A == 3)
                    {
                        swFlg1A = 4;
                        optState.fBit0 = 1; //A1 state
                        //swState.fBit0 = 0;
                        dlyFlgA1 = 0;        //清除A1输出标志
                    }
                }
            }
        }
        
        //check B state------------------------------------------------------------
        if(PORTBbits.PB1 == 0)
        {
            swFlg1B = 0;            //清除释放标志
            swState.fBit1 = 1;
            
            if(swFlgB == 0)
            {
                swFlgB = 1;
                //dlyCount1B = timCount + 30;
                dlyCountB = timCount + 10;
            }    
            if(dlyCountB == timCount)
            {
                if(swFlgB == 1)
                {
                    swFlgB = 2;
                    optState.fBit5 =1;
                    dlyFlgK = 0;
                    dlyCountB = timCount + 20;
                }
                else if(swFlgB == 2)
                {
                    swFlgB = 3;
                    optState.fBit4 = 1;
                    dlyFlgOPEN = 0;
                    dlyCountB = timCount + 30;
                }
                else if(swFlgB == 3)
                {
                    swFlgB = 4;
                    optState.fBit1 = 1; //B1 state
                    dlyFlgB1 = 0;        //清除A1输出标志
                }
            }
        }
        else 
        {
            swFlgB = 0;        //清除按压标志
            if(swState.fBit1 == 1)
            {
                if(swFlg1B == 0)
                {
                    swFlg1B = 1;
                    //dlyCount1B = timCount + 30;
                    dlyCountB = timCount + 10;
                }
                if(dlyCountB == timCount)
                {
                    if(swFlg1B == 1)
                    {
                        swFlg1B = 2;
                        optState.fBit5 =1;
                        dlyFlgK = 0;
                        dlyCountB = timCount + 20;
                    }
                    else if(swFlg1B == 2)
                    {
                        swFlg1B = 3;
                        optState.fBit6 = 1;
                        dlyFlgCLOSE = 0;
                        dlyCountB = timCount + 30;
                    }
                    else if(swFlg1B == 3)
                    {
                        swFlg1B = 4;
                        optState.fBit1 = 1;  //B1 state
                        //swState.fBit1 = 0;
                        dlyFlgB1 = 0;        //清除A1输出标志
                    }
                }
            }
        }
        
        //check C state------------------------------------------------------------
        if(PORTBbits.PB2 == 0)
        {
            swFlg1C = 0;            //清除释放标志
            swState.fBit2 = 1;
            
            if(swFlgC == 0)
            {
                swFlgC = 1;
                //dlyCount1C = timCount + 30;
                dlyCountC = timCount + 10;
            }    
            if(dlyCountC == timCount)
            {
                if(swFlgC == 1)
                {
                    swFlgC = 2;
                    optState.fBit5 =1;
                    dlyFlgK = 0;
                    dlyCountC = timCount + 20;
                }
                else if(swFlgC == 2)
                {
                    swFlgC = 3;
                    optState.fBit4 = 1;
                    dlyFlgOPEN = 0;
                    dlyCountC = timCount + 30;
                }
                else if(swFlgC == 3)
                {
                    swFlgC = 4;
                    optState.fBit2 = 1; //C1 STATE
                    dlyFlgC1 = 0;        //清除A1输出标志
                }
            }
        }
        else 
        {
            swFlgC = 0;        //清除按压标志
            if(swState.fBit2 == 1)
            {
                if(swFlg1C == 0)
                {
                    swFlg1C = 1;
                    //dlyCount1C = timCount + 30;
                    dlyCountC = timCount + 10;
                }
                if(dlyCountC == timCount)
                {
                    if(swFlg1C == 1)
                    {
                        swFlg1C = 2;
                        optState.fBit5 =1;
                        dlyFlgK = 0;
                        dlyCountC = timCount + 20;
                    }
                    else if(swFlg1C == 2)
                    {
                        swFlg1C = 3;
                        optState.fBit6 = 1;
                        dlyFlgCLOSE = 0;
                        dlyCountC = timCount + 30;
                    }
                    else if(swFlg1C == 3)
                    {
                        swFlg1C = 4;
                        optState.fBit2 = 1;    //C1 STATE
                        //swState.fBit2 = 0;
                        dlyFlgC1 = 0;        //清除A1输出标志
                    }
                }
            }
        }
        
        //check D state------------------------------------------------------------
        if(PORTBbits.PB4 == 0)
        {
            swFlg1D = 0;            //清除释放标志
            swState.fBit3 = 1;
            
            if(swFlgD == 0)
            {
                swFlgD = 1;
                //dlyCount1D = timCount + 30;
                dlyCountD = timCount + 10;
            }    
            if(dlyCountD == timCount)
            {
                if(swFlgD == 1)
                {
                    swFlgD = 2;
                    optState.fBit5 =1;
                    dlyFlgK = 0;
                    dlyCountD = timCount + 20;
                }
                else if(swFlgD == 2)
                {
                    swFlgD = 3;
                    optState.fBit4 = 1;
                    dlyFlgOPEN = 0;
                    dlyCountD = timCount + 30;
                }
                else if(swFlgD == 3)
                {
                    swFlgD = 4;
                    optState.fBit3 = 1; //D1 STATE
                    dlyFlgD1 = 0;        //清除A1输出标志
                }
            }
        }
        else 
        {
            swFlgD = 0;        //清除按压标志
            if(swState.fBit3 == 1)
            {
                if(swFlg1D == 0)
                {
                    swFlg1D = 1;
                    //dlyCount1D = timCount + 30;
                    dlyCountD = timCount + 10;
                }
                if(dlyCountD == timCount)
                {
                    if(swFlg1D == 1)
                    {
                        swFlg1D = 2;
                        optState.fBit5 =1;
                        dlyFlgK = 0;
                        dlyCountD = timCount + 20;
                    }
                    else if(swFlg1D == 2)
                    {
                        swFlg1D = 3;
                        optState.fBit6 = 1;
                        dlyFlgCLOSE = 0;
                        dlyCountD = timCount + 30;
                    }
                    else if(swFlg1D == 3)
                    {
                        swFlg1D = 4;
                        optState.fBit3 = 1; //D1 STATE
                        //swState.fBit3 = 0;
                        dlyFlgD1 = 0;        //清除A1输出标志
                    }
                }
            }
        }
        
        //A1 output-------------------------------------------------
        if(optState.fBit0 != 0)
        {
            if(dlyFlgA1 == 0)
            {
                dlyFlgA1 = 1;
                PORTAbits.PA0 = 1; 
                dlyCountA1 = timCount + 15;
            }
            if(dlyCountA1 == timCount) 
            {
                if(dlyFlgA1 == 1)
                {
                    PORTAbits.PA0 = 0;
                    dlyCountA1 = timCount + 20;
                    dlyFlgA1 = 2;
                }
                else if(dlyFlgA1 == 2)
                {
                    PORTAbits.PA0 = 1;
                    dlyCountA1 = timCount + 15;
                    dlyFlgA1 = 3;
                }
                else if(dlyFlgA1 == 3)
                {
                    PORTAbits.PA0 = 0;
                    dlyCountA1 = timCount + 20;
                    dlyFlgA1 = 4;
                }
                else if(dlyFlgA1 == 4)
                {
                    dlyFlgA1 = 0;
                    optState.fBit0 = 0;
                }
            }
        }
        
        //B1 output-------------------------------------------------
        if(optState.fBit1 != 0)
        {
            if(dlyFlgB1 == 0)
            {
                dlyFlgB1 = 1;
                PORTAbits.PA1 = 1; 
                dlyCountB1 = timCount + 15;
            }
            if(dlyCountB1 == timCount) 
            {
                if (dlyFlgB1 == 1)
                {
                    PORTAbits.PA1 = 0;
                    dlyCountB1 = timCount + 20;
                    dlyFlgB1 = 2;
                }
                else if(dlyFlgB1 == 2)
                {
                    PORTAbits.PA1 = 1;
                    dlyCountB1 = timCount + 15;
                    dlyFlgB1 = 3;
                }
                else if(dlyFlgB1 == 3)
                {
                    PORTAbits.PA1 = 0;
                    dlyCountB1 = timCount + 20;
                    dlyFlgB1 = 4;
                }
                else if(dlyFlgB1 == 4)
                {
                    dlyFlgB1 = 0;
                    optState.fBit1 = 0;
                }
            }
        }
        
        //C1 output-------------------------------------------------
        if(optState.fBit2 != 0)
        {
            if(dlyFlgC1 == 0)
            {
                dlyFlgC1 = 1;
                PORTAbits.PA2 = 1; 
                dlyCountC1 = timCount + 15;
            }
            if(dlyCountC1 == timCount)
            {
                if(dlyFlgC1 == 1)
                {
                    PORTAbits.PA2 = 0;
                    dlyCountC1 = timCount + 20;
                    dlyFlgC1 = 2;
                }
                else if(dlyFlgC1 == 2)
                {
                    PORTAbits.PA2 = 1;
                    dlyCountC1 = timCount + 15;
                    dlyFlgC1 = 3;
                }
                else if(dlyFlgC1 == 3)
                {
                    PORTAbits.PA2 = 0;
                    dlyCountC1 = timCount + 20;
                    dlyFlgC1 = 4;
                }
                else if(dlyFlgC1 == 4)
                {
                    dlyFlgC1 = 0;
                    optState.fBit2 = 0;
                }
            }
        }
        
        
        //D1 output-------------------------------------------------
        if(optState.fBit3 != 0)
        {
            if(dlyFlgD1 == 0)
            {
                dlyFlgD1 = 1;
                PORTAbits.PA3 = 1; 
                dlyCountD1 = timCount + 15;
            }
            if(dlyCountD1 == timCount) 
            {
                if(dlyFlgD1 == 1)
                {
                    PORTAbits.PA3 = 0;
                    dlyCountD1 = timCount + 20;
                    dlyFlgD1 = 2;
                }
                else if(dlyFlgD1 == 2)
                {
                    PORTAbits.PA3 = 1;
                    dlyCountD1 = timCount + 15;
                    dlyFlgD1 = 3;
                }
                else if(dlyFlgD1 == 3)
                {
                    PORTAbits.PA3 = 0;
                    dlyCountD1 = timCount + 20;
                    dlyFlgD1 = 4;
                }
                else if(dlyFlgD1 == 4)
                {
                    dlyFlgD1 = 0;
                    optState.fBit3 = 0;
                }
            }
        }
        
        //OPEN output-------------------------------------------------
        if(optState.fBit4 != 0)
        {
            if(dlyFlgOPEN == 0)
            {
                dlyFlgOPEN = 1;
                PORTBbits.PB5 = 1; 
                dlyCountOPEN = timCount + 15;
            }
            if(dlyCountOPEN == timCount)
            {
                if(dlyFlgOPEN == 1)
                {
                    PORTBbits.PB5 = 0;
                    dlyCountOPEN = timCount + 20;
                    dlyFlgOPEN = 2;
                }
                else if(dlyFlgOPEN == 2)
                {
                    PORTBbits.PB5 = 1;
                    dlyCountOPEN = timCount + 15;
                    dlyFlgOPEN = 3;
                }
                else if(dlyFlgOPEN == 3)
                {
                    PORTBbits.PB5 = 0;
                    dlyCountOPEN = timCount + 20;
                    dlyFlgOPEN = 4;
                }
                else if(dlyFlgOPEN == 4)
                {
                    dlyFlgOPEN = 0;
                    optState.fBit4 = 0;
                }
            }
        }    
        
        //CLOSE OUTOUT-----------------------------------------------------
        if(optState.fBit6 != 0)
        {
            if(dlyFlgCLOSE == 0)
            {
                dlyFlgCLOSE = 1;
                PORTBbits.PB7 = 1; 
                dlyCountCLOSE = timCount + 15;
            }
            if(dlyCountCLOSE == timCount) 
            {
                if(dlyFlgCLOSE == 1)
                {
                    PORTBbits.PB7 = 0;
                    dlyCountCLOSE = timCount + 20;
                    dlyFlgCLOSE = 2;
                }
                else if(dlyFlgCLOSE == 2)
                {
                    PORTBbits.PB7 = 1;
                    dlyCountCLOSE = timCount + 15;
                    dlyFlgCLOSE = 3;
                }
                else if(dlyFlgCLOSE == 3)
                {
                    PORTBbits.PB7 = 0;
                    dlyCountCLOSE = timCount + 20;
                    dlyFlgCLOSE = 4;
                }
                else if(dlyFlgCLOSE == 4)
                {
                    dlyFlgCLOSE = 0;
                    optState.fBit6 = 0;
                }
            }
        }    
        
        //k output------------------------------------------------------
        if(optState.fBit5 != 0)
        {
            if(dlyFlgK == 0)
            {
                dlyCountK = timCount + 140;        //延时14s
                PORTBbits.PB6 = 1;
                dlyFlgK = 1;
            }
            if((dlyCountK == timCount) && (dlyFlgK == 1))
            {
                dlyFlgK = 2;
                PORTBbits.PB6 = 0;
                optState.fBit5 = 0;
            }
        }    
    }
}
posted @ 2021-10-29 15:15  steven_lg  阅读(304)  评论(0编辑  收藏  举报