微控制器應用——IO控制(一)
阿新 • • 發佈:2021-10-29
原理圖
功能簡介:晶片上店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; } } } }