วันอาทิตย์ที่ 3 ตุลาคม พ.ศ. 2553

Lab7: การใช้งาน CCP(Counter)


#include<18F4431.h>
#define  CLOCK_SP    20000000 // ความเร็วสัญญาณนาฬิกา
#fuses HS                     //   โหมดการทำงานแบบ High Speed
#fuses NOLVP,NOWDT           // No Low Voltage Program, No Watchdog timer
#fuses NOPROTECT              // Code no protection
#use delay (clock=CLOCK_SP)   // ใช้งานฟังก์ชัน delay_ms() & delay_us()
#use rs232(baud=9600,xmit= PIN_C6,rcv= PIN_C7 ) // ใช้งาน module RS232

int16 A;                                    //ประกาศตัวแปร time1,time2 เป็น floating point number
BOOLEAN hook_cpp1, HookRise;     //ประกาศตัวแปร hook_cpp1, HookRise เป็น Boolean

#int_ccp1
void capture_isr()
{
   if(HookRise)
   {
    A = A+1;
    HookRise = FALSE;
   }
}
// ฟังก์ชัน interrupt ในการตรวจสอบสัญญาณเข้า ให้นับขึ้นที่ 1 เก็บไว้ที่ตัวแปร A

void main(void){
  
   while(true){

      HookRise=TRUE;
      hook_cpp1=TRUE;
      setup_ccp1(CCP_CAPTURE_RE);           //กำหนดค่า CCP1(Capture)
      enable_interrupts(INT_CCP1);              // Enable interrupt CCP1
      enable_interrupts(GLOBAL);                // ตั้งค่า interrupt รวมทำงาน
      printf("%lu\r\n",A);              //แสดงค่าผ่าน RS232
   
   }
}