• slider image 442
  • slider image 483
  • slider image 484
  • slider image 485
  • slider image 486
  • slider image 487
:::


Browsing this Thread:   1 Anonymous Users






Re: 數位輸出入阜 按鍵開關問題
#6
版主
版主


查看用戶資訊
不曉得你對按鍵的彈跳處理了不了解?

附上一段利用Timer 每 5mS 中斷來處理案件及彈跳的程式,詳細去了解一下其處理的流程,想想看為什麼要這樣做。

//********************************************************************
// Timer4 每 5mS 中斷一次,用來做按鍵掃描及按鍵彈跳處理
    //********************************************************************
    
if (TMR4IE && TMR4IF)        // Is the Timer4 interrupt ?
    
{
        
TMR4IF=0;
        
        if (
Disp_CountDisp_Count--;        // Delay Count - 1

        
if (Bounce_Count==0)    // 彈跳處理階段嗎?
        
{    // 彈跳處理已經結束,掃描按鍵
            
if (!SW5)            // SW5 被按下( Low) ?
            
{
                
Bounce_Count=8;     // 是的,設定連續檢查 8 次的彈跳時間(40mS)
                
Key_Input0x01;    // 設定 SW5 的按鍵值交由主程式處理
//                LED6=!LED6;
            
}  
             if (!
SW6)            // SW6 被按下( Low) ?
            
{
                
Bounce_Count=8
                
Key_Input0x02;
//                LED6=!LED6;
            
}  
        }  
        else                    
// 進入彈跳處理階段
        
{
            if ( !
SW5 || !SW6Bounce_Count=8;    // 檢查 SW5 及 SW6 是否都放開了,沒有的話重設彈跳檢查次數
            
else Bounce_Count--;            // 如都放開,彈跳檢查次數減一
        
}
    }

}


而主程式只要檢查是否有按鍵輸入然後執行對應的按鍵功能,而所以的按鍵掃描與彈跳處理都在背景下完成,大大減少主程式的複雜度:
:
:
if (Key_Input==0x01)
{
執行功能 1
Key_Input=0;
}
if (Key_Input==0x02)
{
執行功能 2
Key_Input=0;
}

發表於: 2009/10/26 10:56
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: 數位輸出入阜 按鍵開關問題
#5
初級會員
初級會員


查看用戶資訊
參照:

Ryang 寫道:
switch ( ) 可以中斷,哪只是代表程式可以執行到這裡,但下層的判斷式因條件不成立所以進不去。

看了一下你的程式 :
if (PORTBbits.RB3 == 1)
Sw = 1;
if (PORTBbits.RB5 == 1)
Sw = 2;

你的按鍵難不成按下去時是送 Hi 的嗎? 這跟一般的設計是不一樣的???

if (!PORTBbits.RB3) Sw = 1;
if (!PORTBbits.RB5) Sw = 2;

加個 ! 的運算子吧!


哈 按下去是Low

我現在遇到一個新的問題!!
我想要的是 按"一"下S1後 LCD 顯示 PUSH S1 接著回歸 NO PUSH
執行結果確實有顯示 PUSH S1 但 Delay 微小時間後 會變成 NO PUSH 形成 PUSH S1與NO PUSH 交互顯示
若再按"一"下S2 則變成 PUSH S2與NO PUSH 交互顯示

請問這會是甚麼原因??

主程式我改成
int main(void)
{

LCD_Delay200usX(50) ;

ADC_Initial() ;
PMP_Initial() ;
Timer1_Initial();
OC_Initial() ;
LCD_Initial() ;
IOInit() ;

while(1)
{

LCD_SetCursor(0,0);
putsLCD(LCDString);
LCD_Delay200usX(500) ;
LCD_SetCursor(0,1);
putsLCD(LCDString);
LCD_Delay200usX(500) ;

ReadSwitch();

switch (Sw)
{
case 1: //S1 Pressed
DisplayS1();
break ;

case 2: //S2 Pressed
DisplayS2();
break ;

}
PORTBbits.RB3 = 1 ;
PORTBbits.RB5 = 1 ;
}
}

發表於: 2009/10/22 20:57
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: 數位輸出入阜 按鍵開關問題
#4
版主
版主


查看用戶資訊
switch ( ) 可以中斷,哪只是代表程式可以執行到這裡,但下層的判斷式因條件不成立所以進不去。

看了一下你的程式 :
if (PORTBbits.RB3 == 1)
Sw = 1;
if (PORTBbits.RB5 == 1)
Sw = 2;

你的按鍵難不成按下去時是送 Hi 的嗎? 這跟一般的設計是不一樣的???

if (!PORTBbits.RB3) Sw = 1;
if (!PORTBbits.RB5) Sw = 2;

加個 ! 的運算子吧!

發表於: 2009/10/21 11:21
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: 數位輸出入阜 按鍵開關問題
#3
初級會員
初級會員


查看用戶資訊
nicecookie 你好
很抱歉我沒有詳細說明我的問題 Sorry

debug -> Run 中
無按下任何按鍵 LCD螢幕顯示 NO PUSH
按下 S1 LCD螢幕顯示 NO PUSH
按下 S2 LCD螢幕顯示 NO PUSH
按下任何按鍵 LCD螢幕顯示 NO PUSH

用中斷檢查發現
switch (Sw) <==若在此設為中斷點,則可以中斷 。
{
case 1: //S1 Pressed
DisplayS1(); <==若在此設為中斷點,則不能中斷 。
break ;

case 2: //S2 Pressed
DisplayS2();
break ;

}

用watch 觀看Sw的值 無論按下S1、S2等任何按鍵 Sw的值 均為0x00 並無變化

不曉得 這樣的資料夠不夠齊全?

Attach file:



jpg  (0.00 KB)


發表於: 2009/10/20 20:43
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


Re: 數位輸出入阜 按鍵開關問題
#2
資深會員
資深會員


查看用戶資訊
SORRY,其實我也看不懂那個程式
所以我不是來回答問題的

比較好奇的是

關於debug,大哥你進行了哪些?
或許可以提供想回答的人參考
這樣也節省您的寶貴時間


還是....不會動沒反應,就一股腦的上來發問呢?

發表於: 2009/10/20 13:31
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


數位輸出入阜 按鍵開關問題
#1
初級會員
初級會員


查看用戶資訊
使用PIC24FJ128GB106
按下按鍵開關SW1 ,LCD顯示 "PUSH S1"
按下按鍵開關SW2 ,LCD顯示 "PUSH S2"

編譯沒問題
但是當我按下任何按鍵開關 均無反應!!

請問有可能是哪邊出了問題??
已參考過203_PRC 的 203Lab3
==========================================
#include <p24FXXXX.h>
#include "lcd.h"
#include <timer.h>
#include <adc.h>
#include <outcompare.h>
#include <pps.h>
#include <stdio.h>

_CONFIG2(IESO_OFF & POSCMOD_HS & FNOSC_PRI& FCKSM_CSDCMD & PLLDIV_DIV2 )
_CONFIG1(JTAGEN_OFF & GCP_OFF & GWRP_OFF & ICS_PGx2 & FWDTEN_OFF & WINDIS_OFF )

void PMP_Initial(void) ;
void ADC_Initial(void) ;
void Timer1_Initial(void) ;
void OC_Initial(void) ;
void DisplayS1(void) ;
void DisplayS2(void) ;
void IOInit(void) ;
void ReadSwitch(void) ;

#define INPUT 1
#define OUTPUT 0

#define Ctrl_U1RX TRISFbits.TRISF5
#define Ctrl_U1TX TRISFbits.TRISF3

#define S1 PORTBbit.RB3
#define S2 PORTBbit.RB5

unsigned char LCDString[] = "NO PUSH";
unsigned char LCDString1[] = "PUSH S1";
unsigned char LCDString2[] = "PUSH S2";

char Sw ;

unsigned int Timer1Tick = 0,i = 0 ;
unsigned int RPM = 0 ;
unsigned UART_Timer = 0 ;

void __attribute__((interrupt, no_auto_psv)) _T1Interrupt(void)
{
IFS0bits.T1IF = 0 ;
Timer1Tick = 1 ;
}

int main(void)
{

LCD_Delay200usX(50) ;

ADC_Initial() ;
PMP_Initial() ;
Timer1_Initial();
OC_Initial() ;
LCD_Initial() ;
IOInit() ;

LCD_SetCursor(2,0);
putsLCD(LCDString);
LCD_Delay200usX(500) ;

while(1)
{

ReadSwitch();

switch (Sw)
{
case 1: //S1 Pressed
DisplayS1();
break ;

case 2: //S2 Pressed
DisplayS2();
break ;

}
}
}

void DisplayS1(void) //顯示PUSH S1
{
LCD_Delay200usX(50) ;
LCD_SetCursor(0,0);
putsLCD(LCDString1) ;
LCD_Delay200usX(1000) ;
}

void DisplayS2(void) //顯示PUSH S2
{
LCD_Delay200usX(50) ;
LCD_SetCursor(0,1);
putsLCD(LCDString2) ;
LCD_Delay200usX(1000) ;
}

void IOInit(void)
{
AD1PCFG=0xffff ;

TRISB = 0xffff ;

TRISBbits.TRISB3 = 1; // S1
TRISBbits.TRISB5 = 1; // S2

}

void ReadSwitch(void)
{
if (PORTBbits.RB3 == 1)
Sw = 1;
if (PORTBbits.RB5 == 1)
Sw = 2;

}

void OC_Initial(void)
{

PR2 = 4096 ;
T2CON = 0x8000 ;
OC1CON1 = 0x000e ;
OC1CON2 = 0x001f ;
OC1R = 0 ;
OC1RS = 1023 ;

// Set RP2 as OC1 output !!
// RP2 @ RD8
TRISDbits.TRISD8 = OUTPUT ;
iPPSOutput(OUT_PIN_PPS_RP2,OUT_FN_PPS_OC1);

}

void Timer1_Initial(void)
{
// To make a 10 ms Timer @ Fosc = 8 Mhz -> Fcy = 4Mhz
// Select 1:8 prescaler , Freq to Timer1 = 500 K -> 2 us
// 10ms = 10000 us , 10000 us / 2 us = 5000
// So, PR1 = 4999 for a 10 ms Timer

OpenTimer1( T1_ON & T1_IDLE_CON &
T1_PS_1_8 & T1_SYNC_EXT_OFF &
T1_GATE_OFF & T1_SOURCE_INT ,
4999 );

ConfigIntTimer1(T1_INT_PRIOR_4 & T1_INT_ON) ;
}

void ADC_Initial(void)
{
AD1CON1 = 0b0000000000000110 ; // Mode : Clear SAMP start conversion
// ASAM = 1 --> SAMP auto set
AD1CON2 = 0b0000000000000000 ;
AD1CON3 = 0b1000001100001111 ; // Internal RC as AD Clock
AD1CHS = 0x00 ; // Use AN0 temporary
AD1PCFG = 0b1111111111111011 ; // AN2 enabled
AD1CON1bits.ADON = 1 ;
}

void PMP_Initial(void)
{
//********************************************************************************
// STEP 1:
// Configure PMPCON: PMP on, address/data not multiplexed, PMPBE active high,
// PMPWR I/O, PMPRD I/O, 8-bit data, PMPENB and PMPRD/~PMPWR active high.
//********************************************************************************/
// PMCON = ; // Exer1

PMCON = 0b1000001100000111;
//********************************************************************************
// STEP 2:
// Configure PMPMODE: Interrupts, stall, buffers, inc/dec off, 8 bit mode,
// combined read/write with byte enable signals, and max the 3 wait delays.
//********************************************************************************/
// PMMODE = ; // Exer1
PMMODE = 0x23FF;
//********************************************************************************
// STEP 3:
// Configure PMAEN: Enable A0 function to control RS and disable all other
// PMP address pins.
//********************************************************************************/
// PMAEN = ; // Exer1
PMAEN = 0x0001;
//********************************************************************************
// STEP 4:
// Configure PMPADDR: A0 selects type of instruction, either command or data.
// This is a command so A0 should be low.
//********************************************************************************/
// PMADDR = ; // Exer1
PMADDR = 0x0000;
}

Attach file:


Link only for registered users

發表於: 2009/10/20 13:03
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部







You can view topic.
不可以 發起新主題
You cannot reply to posts.
You cannot edit your posts.
You cannot delete your posts.
You cannot add new polls.
You cannot vote in polls.
You cannot attach files to posts.
You cannot post without approval.
You cannot use topic type.
You cannot use HTML syntax.
You cannot use signature.
You cannot create PDF files.
You cannot get print page.

[進階搜尋]


:::

Microchip連結

https://www.facebook.com/microchiptechnologytaiwan/
http://www.microchip.com.tw/modules/tad_uploader/index.php?of_cat_sn=13
https://mu.microchip.com/page/tmu
http://elearning.microchip.com.tw/modules/tad_link/index.php?cate_sn=1
https://page.microchip.com/APAC-PrefCenters-TW.html
http://www.microchip.com/
http://www.microchip.com/treelink
http://www.microchipdirect.com/
http://www.microchip.com.cn/newcommunity/index.php?m=Video&a=index&id=103
http://www.microchip.com.tw/modules/tad_uploader/index.php?of_cat_sn=2
http://www.microchip.com.tw/Data_CD/eLearning/index.html
http://www.microchip.com.tw/RTC/RTC_DVD/
https://www.microchip.com/development-tools/
https://www.youtube.com/user/MicrochipTechnology
[ more... ]

教育訓練中心

!開發工具購買
辦法說明 [業界客戶] [教育單位]
----------------------------------
!校園樣品申請
辦法說明 [教師資格] [學生資格]
----------------------------------