• Tin mới

    [PIC TUTORIAL] Bài 1 Tạo xung tần số 1KHz, duty 50% tại RD0 và xung tần số 1Hz, duty 50% tại RD1.

    Sơ đồ mạch điện 




    Chương trình mẫu
    #include "16f887.h"
    #include "def_16f887.h"
    #fuses   HS,PUT,NOWDT,NOPROTECT,NOLVP 
    #use     delay(Clock = 8000000)        //Thach anh 8MHz
    int16 temp = 0;
    #int_TIMER1
    void  TIMER1_isr(void) 
    {  
       temp++;
       RD0 = !RD0;                    //Tao xung 1KHz.
       if(temp == 1000)               //T = 2x1000x0,5 = 1000ms = 1s => f = 1Hz.      
       {
        RD1 = !RD1;                   //Tao xung 1Hz   
        temp = 0;
       }
       set_timer1(64535);
    }
    
    void main()
    {
    //Khoi tao T1 va ngat.
       setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);  //T1 dem xung noi, ti le chia 1
       enable_interrupts(INT_TIMER1);
       enable_interrupts(GLOBAL);
       set_timer1(64535);         //f = 1KHz => CK xung = 1ms, he so duty 50% => muc cao = muc thap = 0,5ms
                                  //Thach anh 8 MHz => CK may = 4/8 = 0,5us
                                  //0,5ms = 500us = 1000xung = 65535 - 64535
    //Thiet lap cac pin xuat nhap
       set_tris_b(0xff);          //PORTB nhan du lieu.
       port_b_pullups(0xff);      //Kich hoat dien tro treo PORTB.
       set_tris_c(0x00);          //PORTC xuat du lieu.
       set_tris_d(0x00);          //PORTD xuat du lieu. 
    //----------------------------
       while(1)
       {
        PORTC = PORTB;
        delay_ms(20);
       }
    }
    
    DOWNLOAD FILE MÔ PHỎNG + CODE


    No comments