1 | // config-settings siehe Help-->Topics-->PIC18 config settings
|
2 |
|
3 | #pragma config OSC = HS //clock source is high speed crystal
|
4 | #pragma config PWRT = ON //power up timer enabled
|
5 | #pragma config BOR = OFF //Brown out Reset disabled
|
6 | #pragma config WDT = OFF //watchdog timer off
|
7 | #pragma config LVP = OFF //Single-Supply ICSP disabled
|
8 | //#pragma config DEBUG = ON
|
9 | //#pragma config PBADEN = OFF //turns ADC on port B off
|
10 |
|
11 | #include <p18f458.h>
|
12 | #include <stdio.h>
|
13 | #include <delays.h>
|
14 |
|
15 | #include "RandomAccess.h"
|
16 |
|
17 |
|
18 | void main (void)
|
19 | {
|
20 | char dummy_SPI_data;
|
21 | char *aqfb_bytes;
|
22 |
|
23 | //setting status LEDs
|
24 | TRISAbits.TRISA1 = 0; //making RA1 an output pin
|
25 | TRISAbits.TRISA0 = 0; //making RA0 an output pin
|
26 |
|
27 |
|
28 | //configure all neccessary pins ('1' means input, '0' makes the pin an output pin)
|
29 | TRISCbits.TRISC1 = 1; //Reader Status bit
|
30 | TRISCbits.TRISC1 = 0; //Reader Reset bit
|
31 | TRISCbits.TRISC2 = 0; //SPI CS
|
32 | TRISCbits.TRISC3 = 0; //SPI Clock
|
33 | TRISCbits.TRISC4 = 1; //SPI Data in
|
34 | TRISCbits.TRISC5 = 0; //SPI Data out
|
35 |
|
36 | //init MSSP for SPI communication
|
37 | SSPSTATbits.SMP = 0; //input data sampled at middle of data output
|
38 | SSPSTATbits.CKE = 1; //transmission occurs from idle to active clock
|
39 | SSPCON1bits.CKP = 0; //Idle state of clock is a low level
|
40 |
|
41 | //spi clock is fosc/64
|
42 | SSPCON1bits.SSPM3 = 0;
|
43 | SSPCON1bits.SSPM2 = 0;
|
44 | SSPCON1bits.SSPM1 = 1;
|
45 | SSPCON1bits.SSPM0 = 0;
|
46 |
|
47 | SSPCON1bits.SSPEN = 1; //enables serial port function on dedicated i/o pins
|
48 |
|
49 | INTCONbits.GIEH = 0; //disable all interrupts
|
50 |
|
51 |
|
52 | rfid_reset = 1; //turning reset for reader off
|
53 | turn_RX_off();
|
54 | turn_RX_on();
|
55 | aqfb_bytes = poll_Single();
|
56 |
|
57 |
|
58 |
|
59 | while(1){}
|
60 | }
|
61 |
|
62 |
|
63 |
|
64 | void delay(int _10ms)
|
65 | {
|
66 | int i;
|
67 | for (i=0;i<_10ms;i++)
|
68 | Delay10KTCYx(10);
|
69 | }
|
70 |
|
71 | char write_SPI_data(char data)
|
72 | {
|
73 | rfid_cs = 0; //chip select for RFID Reader pulled low
|
74 | SSPBUF = data; //write to SPI buffer register. data is sent when register is completely written (8 bits)
|
75 | while (!SSPSTATbits.BF){} //wait for buffer full status bit is set
|
76 | rfid_cs = 1; //de-select RFID Reader by releasing CS signal line
|
77 | return SSPBUF; //SSPBUF must be read before next read/write operation can be started
|
78 | }
|
79 |
|
80 | char read_SPI_data(void)
|
81 | {
|
82 | TRISCbits.TRISC5 = 1; //while in "receive only mode" the SPI Data out pin should be disabled (programmed as input)
|
83 | rfid_cs = 0; //chip select for RFID Reader pulled low
|
84 | SSPBUF = 0x00; //dummy data written to SSPBUF register to trigger clock generation for reading data from slave
|
85 | while (!SSPSTATbits.BF){} //wait for buffer full status bit is set
|
86 | rfid_cs = 1; //release CS signal line
|
87 | TRISCbits.TRISC5 = 0; //reconfigure SPI data out pin
|
88 | return SSPBUF;
|
89 | }
|
90 |
|
91 |
|
92 | void turn_RX_off(void)
|
93 | {
|
94 | green_LED = 0; //clear green LED
|
95 | red_LED = 0; //clear red LED
|
96 | dummy_SPI_data = write_SPI_data(rf_off); //sending SPI Command to RFID Reader
|
97 | while(rfid_iSTAT==0){} //waiting for Reader to handshake
|
98 | if (read_SPI_data() && 0b00000001 == 1) //checking for acknowledge bit
|
99 | green_LED = 1;
|
100 | else
|
101 | red_LED = 1;
|
102 | }
|
103 |
|
104 |
|
105 | void turn_RX_on(void)
|
106 | {
|
107 | green_LED = 0; //clear green LED
|
108 | red_LED = 0; //clear red LED
|
109 | dummy_SPI_data = write_SPI_data(rf_on); //sending SPI Command to RFID Reader
|
110 | while(rfid_iSTAT==0){} //waiting for Reader to handshake
|
111 | if (read_SPI_data() && 0b00000001 == 1) //reading ACK/NACK bit
|
112 | green_LED = 1;
|
113 | else
|
114 | red_LED = 1;
|
115 | }
|
116 |
|
117 | void abort_RFID(void)
|
118 | {
|
119 | green_LED = 0; //clear green LED
|
120 | red_LED = 0; //clear red LED
|
121 | dummy_SPI_data = write_SPI_data(abort); //sending SPI Command to RFID Reader
|
122 | while(rfid_iSTAT==0){} //waiting for Reader to handshake
|
123 | if (read_SPI_data() && 0b00000001 == 1) //reading ACK/NACK bit
|
124 | green_LED = 1;
|
125 | else
|
126 | red_LED = 1;
|
127 | }
|
128 |
|
129 | char * poll_Single(void)
|
130 | {
|
131 | char i=0;
|
132 | static char poll_response[16];
|
133 |
|
134 | green_LED = 0; //clear green LED
|
135 | red_LED = 0; //clear red LED
|
136 | dummy_SPI_data=write_SPI_data(poll_Single);
|
137 | dummy_SPI_data=write_SPI_data(0x00); //AFI code for all Type B cards
|
138 | dummy_SPI_data=write_SPI_data(0x00); //wake command, using 1 slot for polling
|
139 | while(rfid_iSTAT==0){} //waiting for Reader to handshake
|
140 |
|
141 | //reading response to poll single command
|
142 | while(rfid_iSTAT)
|
143 | {
|
144 | poll_response[i] = read_SPI_data();
|
145 | i++;
|
146 | }
|
147 |
|
148 | //clearing the remaining of response data field
|
149 | while(i<16)
|
150 | {
|
151 | poll_response[i] = 0;
|
152 | i++;
|
153 | }
|
154 | return poll_response;
|
155 | }
|