• slider image 442
  • slider image 477
  • slider image 479
  • slider image 480
  • slider image 481
  • slider image 482
:::


Browsing this Thread:   1 Anonymous Users






Re: PIC16F1503 timer 計數中斷 與 外部中斷無法同時使用
#2
版主
版主


查看用戶資訊
因爲這個元件沒有內建除錯模組,所以無法用 PK3 等除錯工具來除錯。

這是比較辛苦的一件事情。建議在外部中斷的函式裡函式加入一個 IO 腳的 toggle 變化來監測腳位,用示波器量一下腳位的變化以判斷外部中斷是否正常?

爲了避免腳位的彈跳你可以加上一個 10000pF 電容做濾波或者用軟體彈跳的方式處理。

Attach file:



jpg  (0.00 KB)


發表於: 2020/5/25 12:42
Twitter Facebook Google Plus Linkedin Del.icio.us Digg Reddit Mr. Wong 頂部


PIC16F1503 timer 計數中斷 與 外部中斷無法同時使用
#1
新會員
新會員


查看用戶資訊
請教前輩,我原本有使用timer1設定每1ms中斷一次當做時間計算給LED1使用,後來我要增加另一個LED2(PWM控制點燈),所以打算用外部中斷功能,可以用彈跳開關做為兩各LED的切換,但加入中斷後,按彈跳開關都沒有反應,我是設定負緣觸發的時候產生中斷,示波器量測確實有負緣信號產生,但卻沒有進行切換,請問是我的設定哪邊有問題嗎?
以下是我的程式code,請不吝指教,

=========================================================================
#include<pic.h>

__CONFIG(FOSC_INTOSC&WDTE_OFF&PWRTE_ON&MCLRE_OFF&CP_OFF&BOREN_OFF);

#define LED1 RC2
#define LED2 RC1 //PWM

volatile unsigned int ms=0;
volatile unsigned char step=0;
volatile unsigned char SW=0;

void system_initial(void)
{
OSCCONbits.IRCF = 0b1110; //內部振盪器頻率設為8MHz
OSCCONbits.SCS = 0b11; //內部振盪器模塊
INTCONbits.GIE = 1; //允許所有有效中斷
INTCONbits.PEIE = 1; //允許所有有效外設中斷
INTCONbits.INTE = 1; //允許INT外部中斷
TRISA = 0b00000100; //RA2 input
TRISC = 0b00000000;
ANSELA = 0b00000000; //RA0~RA4 Digital I/O
ANSELC = 0b00000000; //RC0~RC3 Digital I/O
LED1 = 0;
LED2 = 0;
}

void PWM_Init(void)
{
PWM4CON = 0b00000000; //Clear PWM2CON
PR2 = 0b10100110; //Configure the Timer2 period - decimal 166
PWM4CON = 0b11000000; //Enable PWM Module, Module Output
PWM4DCH = 0b00000000; //Clear duty cycle registers
PWM4DCL = 0b00000000;
TMR2IF = 0; //Clear the timer 2 interrupt flag
T2CON = 0b00000001; //Set prescaler to 4
TMR2ON = 1; //Enable timer 2
PWM4DCH = 0b00010000; //Pulse Width:33.5μs, Duty = 10%
PWM4DCL = 0b00000011;
}

void INT_Init(void)
{
OPTION_REGbits.INTEDG = 0; //下降沿中斷
INTF = 0; //Clear INTF
}

void TIMER1_Init(void)
{
TMR1H=0xFF;
TMR1L=0x06;
T1CONbits.T1CKPS = 0b11;
T1CONbits.TMR1CS = 0b00;
PIE1bits.TMR1IE = 1;
T1CONbits.TMR1ON = 1;
PIR1bits.TMR1IF = 0;
}

void interrupt SystemISR(void)
{
if(TMR1IF && TMR1IE)
{
TMR1IF = 0;
TMR1H = 0xFF;
TMR1L = 0x06;
ms++;
}

if(INTF && INTE)
{
INTF = 0; //Clear INTF
SW = 1;
}
}

void main(void)
{
system_initial();
TIMER1_Init();
PWM_Init();

while(1)
{
switch(step)
{
case 0:
{
PWM4CON = 0b01000000; //disable PWM Module
LED2=0;

if(SW==1)
{
LED1=0;
step=1;
}

if(ms==60)
LED1=1;

if(ms==120)
LED1=0;

if(ms==180)
LED1=1;

if(ms==240)
LED1=0;

if(ms==1000)
ms=0;

break;
}

case 1: //LED2 PWM mode
{
if(SW==1)
{
ms=0;
step=0;
}

LED1=0;
PWM4CON = 0b11000000; //Enable PWM Module

break;
}
}
}
}

發表於: 2020/5/21 16:18
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... ]

教育訓練中心

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