View Single Post
Old 29-06-2010, 06:01 PM   #27
huuducel
Nhập môn đệ tử
 
Tham gia ngày: Mar 2010
Bài gửi: 7
:
Question Dùng Capture đọc tín hiệu Remote

Mấy pro ơi, mình đang làm đề tài dùng input capture của con Pic6f887 để đọc tín hiệu của cái remote. Mày mò lập trình mãi mà nó không chạy. Hôm nay post lên để anh em cùng mổ xẻ xem nó bị sai chỗ nào.
Đoạn code của em như sau:

#include<stdlib.h>
#include<htc.h>
__CONFIG(INTIO & WDTDIS & PWRTEN & MCLREN & UNPROTECT & DUNPROTECT & BORDIS & IESODIS & FCMDIS & LVPDIS);
__CONFIG(BORV21);
#define _XTAL_FREQ 4000000

bit Overflow;
unsigned char Pulse_cycle;
bit CaptureRise;
bit CaptureFall;
bit CaptureComplete;
unsigned int PulseCycle;
unsigned int temp;
unsigned int val;
const char ErrorCycle = 8;
char i;
char buf[10];
void Interrupts_isr() // handler for TMR1 and CCP1
{
if(CCP1IF = 1) CCP1IF = 0;
if(CaptureRise = 1)
{
TMR1ON = 1;
CCP1IE = 0;
CCP1CON = 0x04; // Capture mode Falling
CCP1IF = 0;
CCP1IE = 1;
CaptureRise = 0;
CaptureFall = 1;
}
else if(CaptureFall = 1)
{
TMR1ON = 0;
CaptureComplete = 1;
PulseCycle = CCPR1L;
PulseCycle = ((PulseCycle|(CCPR1H<<8)) - ErrorCycle);
}

else if(TMR1IF = 1)
{
TMR1IF = 0;
Overflow = 1;
}
}
void Timer1_init() // Cau hinh Timer1
{
TMR1ON = 0;
TMR1CS = 0; // Clock internal Fosc/4
T1CKPS1 = 0;
T1CKPS0 = 0;
TMR1IE = 1;
}
void CapturePulse()
{
CaptureRise = 1;
CaptureFall = 0;
CaptureComplete = 0;
Overflow = 0;
CCP1IE = 0;// Cam ngat khi chuyen doi
CCP1CON = 0x05; // Captute mode rising
TMR1H = 0;
TMR1L = 0;
TMR1IE = 1;
CCP1IE = 1;
}
void PulseFinish()
{
CCP1IE = 0;
CCP1IF = 1;
TMR1ON = 0;
TMR1IE = 0;
TMR1IF = 0;
}

void Uart_init()
{
BRG16 = 0;
BAUDCTL = BAUDCTL & 0xF7;
TXSTA = 0x24; //Che do high-speed, cho phep phat du lieu
RCSTA = 0x90; //Nhan du lieu lien tuc, dung cac chan TX/RX
//baud rate = Fosc / (16*(n+1))
//n = Fosc/((baud rate)*16) - 1
SPBRG = 25; //Gia tri ung voi baud rate = 9600, xtal 4 MHz
//SPBRGH khong can dung den
while (CaptureComplete = 1);
val = PulseCycle;
for (i = 0; i < 10; i++) buf[i] = 0;
//Chuyen doi tu unsigned char sang ky tu ASCII
utoa(buf, val, 10);

i = 0;
while (buf[i] != 0) {
TXREG = buf[i];
i++;
while (TRMT == 0);
}
TXREG = '\n';
while (TRMT == 0);
TXREG = '\r';
for (i = 0; i < 10; i++) __delay_ms(100);
}

void main (void)
{
Uart_init();
Timer1_init();
TRISC2 = 1; // Input CCP1
CapturePulse();
while(CaptureComplete ==0 & Overflow ==0)
PulseFinish ();
if(Overflow == 1) // If co tran len 1 thi xuat gia tri ra man hinh
Uart_init();
}
huuducel vẫn chưa có mặt trong diễn đàn   Trả Lời Với Trích Dẫn