Hallo ich will die Breite der Pulse (Pulse Width) des Ausgandsignals
meines IR-Empfänger messen und zwar mit dem Mikrocontroler C8051F340.
Die Messung sollte wie folgt erfolgen:
Pseudocode:
Set Timer ISR
If time High
Set time low, Run
Else
Set time High, Run
Aus dieses Pseudocode habe ich folgenden COde geschrieben:
1 | #include <c8051f340.h>
|
2 | bit FLAG;
|
3 | unsigned char CAPTURE;
|
4 | unsigned char A;
|
5 | unsigned char C;
|
6 | unsigned char Pulse_Width;
|
7 |
|
8 |
|
9 |
|
10 | void port_init (void);
|
11 | void pca_init (void);
|
12 |
|
13 | void main (void)
|
14 | {
|
15 |
|
16 | port_init (); //
|
17 | pca_init ();
|
18 | PCA0MD &= ~0x40; // Disable Watchdog Timer
|
19 |
|
20 | // PCA Timer INIT:
|
21 | PCA0MD = 0x00; // Input to timer 4 1/12 X Fosc
|
22 | TH0 = 0x00; // High Byte
|
23 | TL0 = 0x00; //Low Byte
|
24 |
|
25 | IE = 1;
|
26 | //EIE1=1; // Enable External interrupt 1
|
27 | EA = 1; // Enable all interrupts
|
28 | ET0= 1; // Turn PCA timer 0 on
|
29 | CR=1;
|
30 |
|
31 |
|
32 | while (1){ } // wait of interrupt
|
33 | //
|
34 |
|
35 | }
|
36 |
|
37 | void pca_init (void)
|
38 | {
|
39 | PCA0CN = 0x40; //
|
40 | PCA0CPM0 = 0x21; //Capture positive edge first for measuring pulse width
|
41 | PCA0MD = 0x00; //
|
42 | FLAG=0;
|
43 | }
|
44 |
|
45 | void port_init (void)
|
46 | { P0SKIP = 0xff; //
|
47 | P1SKIP = 0x03; //
|
48 | P1MDOUT |= 0x04; //
|
49 | }
|
50 |
|
51 |
|
52 | void Int_0_ISR(void) interrupt 0
|
53 | {
|
54 | CCF0 =0; // Clear Module 0's event flag
|
55 | if (FLAG==0) // Check if this is the first
|
56 | {
|
57 | // capture or second:
|
58 | // FIRST CAPTURE:
|
59 | CAPTURE = PCA0CPL0; // Save 16-bit capture value
|
60 | PCA0CPH0 = CAPTURE+1;
|
61 | PCA0CPM0 = 0x11; // Change module to now capture
|
62 | // falling edges
|
63 | FLAG = 1; //1st capture complete
|
64 | }
|
65 | else // SECOND CAPTURE:
|
66 | {
|
67 | Pulse_Width = CAPTURE - PCA0CPL0;
|
68 | A = Pulse_Width+1;
|
69 | C = PCA0CPH0 - CAPTURE;
|
70 | A = C;
|
71 | PCA0CPM0 = 0x21; //Optional: needed if user wants to
|
72 | FLAG = 0; //measure next pulse width
|
73 | }
|
74 | }
|
Aber die Code manchen leider gar nicht nach der Compilierung. Können mal
gucken und mir helfen?
Danke
armando