PIC Vietnam

PIC Vietnam (http://www.picvietnam.com/forum/index.php)
-   Cơ bản về vi điều khiển và PIC (http://www.picvietnam.com/forum/forumdisplay.php?f=8)
-   -   Giup ve Interrupts (http://www.picvietnam.com/forum/showthread.php?t=2286)

nguyen.geo 21-04-2008 10:45 PM

Giup ve Interrupts
 
Code:

#include <16F877a.h>
#fuses HS,NOWDT,PROTECT,NOLVP,NOBROWNOUT
#use delay(clock=20000000)

#BYTE ADCON1        = 0x9F
#byte TMR2    = 0x11
#bit  RS            = 0x07.4
#bit  EN            = 0x07.5
#bit  SP            = 0x05.4
#bit  Up            = 0x09.0
#bit  Down          = 0x09.1
#bit  Enter        = 0x09.2
#bit  Menu          = 0x07.3
#bit  T1ON                = 0x10.0
#bit  TMR2ON          = 0x12.2
#bit        TMR2IE        = 0x8C.1
#bit        TMR2IF  = 0x0C.1
#bit  INTE                = 0x0B.4
#bit  INTIF                = 0x0B.1
#byte        PIE1                = 0x8C
int8 fstart,fcounter,timeover;

void  Int_io(void)
{  set_tris_a(0x1f);  set_tris_b(0xc1);
  set_tris_c(0x08);  set_tris_d(0x00);
  set_tris_e(0xff);  return;
}

void        Interrupts(void)
{  setup_timer_0(RTCC_INTERNAL|RTCC_DIV_256);       
        enable_interrupts(int_timer0);        set_timer0(224);
       
        setup_timer_2(T2_DIV_BY_1,255,16);
        enable_interrupts(int_timer2);       
        TMR2ON = 0;                set_timer2(0);
       
        enable_interrupts(INT_EXT);       
        ext_int_edge( H_TO_L );                                INTE = 0;
  enable_interrupts(global);                return;
}

#int_timer0
void interrupt_timer0()
{  timeover++; 
        if(timeover==79)
        {        timeover = 0;       
                set_timer0(230);        fstart = 1;
        }       
        else        set_timer0(5);               
        return;
}

#int_EXT
void        EXT_isr()
{        if(INTIF)
        {        if(TMR2ON)
                {                TMR2ON = 0;                INTE = 0;        fstart = 0;        }
                else
                {                set_timer2(0);                fcounter = 0;                TMR2ON = 1;                }
                INTIF = 0;               
        }
        return;
}

#int_timer2
void interrupt_timer2()
{        if(TMR2IF)
        {        fcounter++;                TMR2IF = 0;        }       
        return;       
}

void    main(void)
{  Int_io();    Interrupts();                fstart = 1;
        while(1)       
                {        if(fstart==1)
                        {        INTE = 1;        fstart = 2;        }
                        if(fstart==0)
                        {        fstart = 2;                output_d(fcounter);        }
                }       
        return;
}

Đoạn code em viết dùng để thực hiện công việc sau :
- Timer 0 dùng để định thời 1s và sẽ liên tục hoạt động.
- Timer 2 dùng để đếm thời gian trong 1 chu kỳ trên chân INT-RB0 ( ngắt lần 1 khỡi động TIMER2- Ngắt lần 2 thì ngừng đọc dữ liệu timer2 )
Không hiểu là đoạn code này có vấn đề hay chương trình dịch của em có vấn đề mà khi chạy mô phỏng cũng như là chạy thật đều không đúng nó cứ chạy loạn lên. Khi chạy mô phỏng phát tần số 50HZ vào chân INT thì trên PORTD nó cứ chạy loạn lên.
Các bác giúp em với.


Múi giờ GMT. Hiện tại là 04:30 AM.

Tên diễn đàn: vBulletin Version 3.8.11
Được sáng lập bởi Đoàn Hiệp.
Copyright © PIC Vietnam