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 | }
|