Forum: Mikrocontroller und Digitale Elektronik Manchester Decoding PIC16lf1509


von Seto (Gast)


Lesenswert?

Hallo Leute,

ich versuche eine Manchester Decodierung zu programmieren. Dabei wird
eine Binäre 1 als 01 Bits dargestellt und eine Binäre 0 als 10 Bits.
Dabei möchte ich mit 10 kHz decodieren. Ich verwende einen PIC16lf1509.
Ich habe zurzeit leider keine Möglichkeit mein Programm zu testen.
Außerdem bin ich noch nicht ganz fertig.

Für die Decodierung soll erst eine Präambel empfangen werden, damit man
weiß, dass Daten gesendet werden. Einfach 8-Bits bei denen die Flanke
wechselt 10101010. Dabei soll jeder Flankenwechsel 50 us dauern, was ich
mit einem Flankeninterrupt messe. Danach sollen 14 Datenbits empfangen
werden, wobei vor den Daten ein Startbit (1) und dahinter ein Stopbit
(0) empfangen wird.
Die Präambel und die Nutzdaten werden in einem Buffer gespeichert.
Anschließend werden die empfangenen Data encoded.

Kann man das auf diese weise machen?!

CODE:
1
/* 
2
 * File:                Init.h
3
 * Author:              Max
4
 * Comments:
5
 * Revision history: 
6
 */
7
8
// This is a guard condition so that contents of this file are not included
9
// more than once.  
10
#ifndef Init_H
11
#define  Init_H
12
13
#include <xc.h> // include processor files - each processor file is guarded.  
14
15
typedef unsigned int uint;
16
/***** CONFIGURATION *****/
17
//  ext reset, internal oscillator (no clock out), no watchdog timer
18
#pragma config MCLRE = ON, FOSC = INTOSC, CLKOUTEN = OFF, WDTE = OFF
19
//  brownout resets enabled, low brownout voltage, no low-power brownout reset
20
#pragma config BOREN = ON, BORV = LO, LPBOR = OFF
21
//  no power-up timer, no code protect, no write protection
22
#pragma config PWRTE = OFF, CP = OFF, WRT = OFF
23
//  stack resets on, high-voltage programming
24
#pragma config STVREN = ON, LVP = OFF
25
26
#define Send LATAbits.LATA1                 // Turn's LED on/off
27
#define logic_lvl LATAbits.LATA2            // current logic level of Input-Bit
28
#define IntTrigger OPTION_REGbits.INTEDG    // if 1 interrupt on rising edge else on falling edge
29
30
31
// Clockfrequency = 2 MHz / 4  = 500 kHz ---> 2 us per tick
32
// Timeperiod 10 kHz = 100 us
33
// Mid-bit time = 50 us
34
    // default values for 10 kHz frequency at 2 MHz clockfrequency
35
struct DecodeTime {
36
    uint midbit_low;    // for avarage of T= 50 us = 25 ticks +-50%
37
    uint midbit_high;
38
    uint period_low;    // for avarage of 2T = 100 us = 50 ticks +-50%
39
    uint period_high;
40
};
41
42
struct DecodeTime ManDecode;
43
44
    // variable initialisation
45
uint decode_buffer = 0b00000000;        // assuming that payload has 8 Bits
46
uint M_Time;                            // Measured Time
47
uint Preamble = 0b00000000;             // Buffer for Preamble request
48
uint Payload = 0b0000000000000000;      // Buffer for Payload
49
uint PreDetect = 0;                     // true if preamble was received
50
uint Pcounter = 0;                      // Counts Payload size
51
uint data_send = 0;                     // True if Data was received successfull
52
53
    // function declaration
54
void SetPreCounter(void);
55
void GetPayload(void);
56
57
58
#endif
1
/************************************************************************
2
*                                                                       *
3
*   Filename:      Main.c                                               *
4
*   Created on 28. Dezember 2017, 17:31                                 *
5
*   File Version:  1.0                                                 *
6
*                                                                       *
7
*   Author:        Max                                                  *
8
*                                                                       *
9
*************************************************************************
10
*                                                                       *
11
*   Architecture:  Mid-range PIC                                        *
12
*   Processor:     16LF1509                                             *
13
*   Compiler:      MPLAB XC8 v1.44 (Free mode)                          *
14
*                                                                       *
15
*************************************************************************/
16
17
#include "Init.h"
18
19
//Functions---------------------------------------------------------------------
20
// Initialisation
21
void init(void) {
22
    
23
        // default value's
24
    ManDecode.midbit_low = 13;
25
    ManDecode.midbit_high = 37;
26
    ManDecode.period_high = 38;
27
    ManDecode.period_high = 62;
28
    
29
    TRISA = 0b111101;               // configure RA1 (only) as an output (0 -> output, 1 -> input)
30
    LATA = 0;                       // set RA1 low
31
    ANSELA = 0;                     // disable analog input mode for all pins
32
                                    // --> RA2 is a digital input
33
        // configure oscillator
34
    OSCCONbits.SCS1 = 1;            // select internal clock -- OSCCON oscillator control register
35
    OSCCONbits.IRCF = 0b1100;       // internal oscillator = 2 MHz
36
        // configure Timer0
37
    OPTION_REGbits.TMR0CS = 0;      // select timer mode
38
    OPTION_REGbits.PSA = 1;         // if '0' assign's prescaler to Timer0
39
//    OPTION_REGbits.PS = 0b111;      // prescale 1:128
40
                                    // -> increment TMR0 every 4 us
41
        // configure Interrupt on changing edge
42
    OPTION_REGbits.INTEDG = 1;      // interrupt on rising edge
43
    
44
        // enable interrupts
45
    INTCONbits.INTF = 0;            // clearing external interrupt flags
46
    INTCONbits.TMR0IF = 0;          // clearing Timer0 flag
47
    INTCONbits.INTE = 1;            // enable external interrupt
48
    INTCONbits.TMR0IE = 1;          // enable Timer0 interrupt
49
    ei();                           // enable global interrupts
50
    
51
52
}
53
/***** INTERRUPT SERVICE ROUTINE *****/
54
void interrupt isr(void)
55
{
56
    static uint time;
57
    //*** Service external interrupt
58
    //
59
    //  Triggered on changing edge on INT pin
60
    // 
61
    //   
62
    
63
    if(INTCONbits.INTF && !INTCONbits.TMR0IF) {
64
        time=TMR0;
65
        M_Time = time;
66
        TMR0=0;                         // start counting
67
        if (M_Time >= ManDecode.midbit_low) {
68
            if (M_Time <= ManDecode.midbit_high) {
69
                // Count Bit in Preamble Buffer
70
                if(!PreDetect) {
71
                    SetPreCounter();  
72
                }
73
                else if(PreDetect) {
74
                // Read Start-/Stopbit and Payload
75
                    GetPayload();
76
                    
77
                }
78
            }
79
        }
80
        else {                          // reset Preamble and Payload Buffer
81
            Preamble = 0b00000000;
82
            Payload = 0b0000000000000000;
83
        }
84
        IntTrigger = ~IntTrigger;       // change interrupt to falling/rising edge        
85
    }
86
    else if (INTCONbits.TMR0IF) {           // if Timer overflows -> reset 
87
        INTCONbits.TMR0IF = 0;
88
        IntTrigger = 1;                 // change interrupt to rising edge
89
        Preamble = 0b00000000;
90
        Payload = 0b0000000000000000;
91
    }                   
92
    INTCONbits.INTF = 0;                // clear interrupt flag
93
    
94
}
95
void GetPayload(void) {
96
    Payload <<= 1;
97
    if(IntTrigger && (Pcounter != 16)) {
98
        Payload |= 1;
99
        Pcounter++;
100
    }
101
    // Test if Payload is True
102
    if(Pcounter == 16) {
103
            // Test if Startbit and Stopbit are sent
104
        uint check = 0b10000000000000001;
105
        uint data_complete = 0b10000000000000000;
106
        if((Payload&check)== data_complete) {
107
            // wait for guardtime and decode
108
            data_send = 1;
109
//            Man_Decode(); /// Not yet implemented
110
        }
111
    }
112
}
113
void SetPreCounter(void) {
114
    Preamble <<= 1;
115
    if(IntTrigger) {
116
        Preamble |= 1;
117
        if(Preamble == 170) {
118
            PreDetect = 1;
119
        }
120
    }
121
}
122
123
void main(){
124
    init();
125
    if(data_send) {
126
//        Man_Encode();     Not yet implemented -- decodes received Data
127
    }
128
}

Bitte melde dich an um einen Beitrag zu schreiben. Anmeldung ist kostenlos und dauert nur eine Minute.
Bestehender Account
Schon ein Account bei Google/GoogleMail? Keine Anmeldung erforderlich!
Mit Google-Account einloggen
Noch kein Account? Hier anmelden.