1 | #pragma config FOSC = INTOSC // Oscillator Selection (INTOSC oscillator: I/O function on CLKIN pin)
|
2 | #pragma config WDTE = OFF // Watchdog Timer Enable (WDT enabled)
|
3 | #pragma config PWRTE = OFF // Power-up Timer Enable (PWRT disabled)
|
4 | #pragma config MCLRE = ON // MCLR Pin Function Select (MCLR/VPP pin function is MCLR)
|
5 | #pragma config CP = OFF // Flash Program Memory Code Protection (Program memory code protection is disabled)
|
6 | #pragma config CPD = OFF // Data Memory Code Protection (Data memory code protection is disabled)
|
7 | #pragma config BOREN = ON // Brown-out Reset Enable (Brown-out Reset enabled)
|
8 | #pragma config CLKOUTEN = ON // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
|
9 | #pragma config IESO = ON // Internal/External Switchover (Internal/External Switchover mode is enabled)
|
10 | #pragma config FCMEN = ON // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled)
|
11 |
|
12 | // CONFIG2
|
13 | #pragma config WRT = OFF // Flash Memory Self-Write Protection (Write protection off)
|
14 | #pragma config PLLEN = OFF // PLL Enable (4x PLL enabled)
|
15 | #pragma config STVREN = ON // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
|
16 | #pragma config BORV = LO // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
|
17 | #pragma config LVP = ON
|
18 |
|
19 |
|
20 | #include <xc.h>
|
21 | #include <pic16f1828.h>
|
22 | #include <stdint.h>
|
23 |
|
24 | #define SCK PORTBbits.RB6 //Serial Clock
|
25 | #define SDI LATCbits.LATC4 //Serial Data In
|
26 | #define SD0 PORTCbits.RC7 //Serial Data Out
|
27 | #define SS LATCbits.LATC6 //Slave Select
|
28 |
|
29 |
|
30 | #define SCK_dir TRISBbits.TRISB6
|
31 | #define SDI_dir TRISBbits.TRISB4
|
32 | #define SDO_dir TRISCbits.TRISC7
|
33 | #define SS_dir TRISCbits.TRISC6
|
34 |
|
35 | // EEPROM SPI Instructions
|
36 | #define WREN 0x06 //Write enable
|
37 | #define WRDI 0x04 //Write disable
|
38 | #define WRITE 0x02 //Write to Memory Array
|
39 | #define READ 0x03 // Read from Memory Array
|
40 | #define RDSR 0x05 // Read Status register
|
41 |
|
42 |
|
43 | void SPI_initMaster();
|
44 | void SPI_write(char data);
|
45 | char SPI_read(char dummy);
|
46 | void EEPROM_write(int address,char data);
|
47 | char EEPROM_read(int address);
|
48 | char EEPROM_status();
|
49 | char EEPROM_busy();
|
50 |
|
51 | void main(void) {
|
52 |
|
53 |
|
54 | OSCCONbits.SCS= 0b10; //internal Oscillator
|
55 | OSCCONbits.IRCF=0b1011; //1MHz
|
56 |
|
57 |
|
58 | int address = 0x0409;
|
59 | SPI_initMaster();
|
60 | EEPROM_read(address);
|
61 |
|
62 | return;
|
63 | }
|
64 |
|
65 | void SPI_initMaster()
|
66 | {
|
67 |
|
68 | SS_dir = 0;
|
69 | SCK_dir = 0;
|
70 | SDI_dir = 1;
|
71 | SDO_dir = 0;
|
72 |
|
73 |
|
74 | SSP1CON=0b00100000;
|
75 | SSP1STAT=0b01000000;
|
76 |
|
77 |
|
78 | }
|
79 |
|
80 | void SPI_write(char data)
|
81 | {
|
82 | SSP1BUF = data;
|
83 | while(SSP1STATbits.BF== 0);
|
84 |
|
85 | }
|
86 | //-----------------------------------------------------------------------------
|
87 |
|
88 | char SPI_read(char dummy)
|
89 | {
|
90 | SSP1BUF = dummy;
|
91 | while(SSP1STATbits.BF== 0);
|
92 | return SSP1BUF;
|
93 | }
|
94 |
|
95 |
|
96 | //------------------------------------------------------------------------------
|
97 | char EEPROM_read(int address)
|
98 | {
|
99 |
|
100 |
|
101 | SS=0;
|
102 | SPI_write(READ);
|
103 | SPI_write(address>>8); //Upper Byte
|
104 | SPI_write(address); //Lower Byte
|
105 | char data = SPI_read(0);
|
106 |
|
107 |
|
108 | SS=1;
|
109 | return data;
|
110 | }
|