1 /*
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 #pragma config FOSC = INTOSC
18 #pragma config WDTE = OFF
19 #pragma config PWRTE = OFF
20 #pragma config MCLRE = ON
21 #pragma config CP = OFF
22 #pragma config CPD = OFF
23 #pragma config BOREN = ON
24 #pragma config CLKOUTEN = ON
25 #pragma config IESO = OFF
26 #pragma config FCMEN = OFF
27
28
29 #pragma config WRT = OFF
30 #pragma config PLLEN = ON
31 #pragma config STVREN = ON
32 #pragma config BORV = LO
33 #pragma config LVP = OFF
34
35
36
37
38 #include <xc.h>
39
40 #include <stdio.h>
41 #include <stdlib.h>
42
43 #define TESTPULSE(aportno) PORTA = 1 << aportno; PORTA = 0
44 #define LOGMAX 9
45
46 int a;
47 char pir1[LOGMAX];
48 char ssp1add[LOGMAX];
49 char ssp1buf[LOGMAX];
50 char ssp1con1[LOGMAX];
51 char ssp1con2[LOGMAX];
52 char ssp1con3[LOGMAX];
53 char ssp1stat[LOGMAX];
54
55 int sleep;
56
57 char senddata[3] = {0xd0,0xd1,0xd2};
58 int senddata_index = 0;
59
60 void ra1onoff(int n){
61 for (int i=0; i < n;i++){
62 PORTAbits.RA1 = 1;
63 PORTAbits.RA1 = 0;
64 }
65 for (int i=0; i < n;i++){
66 PORTAbits.RA1 = 0;
67 PORTAbits.RA1 = 0;
68 }
69 }
70
71 void interrupt intservice(){
72 TESTPULSE(0);
73
74 INTCONbits.GIE = 0;
75 asm("MOVF PORTA,W");
76
77
78 pir1[a] = PIR1;
79 PIR1 = 0x00;
80 ssp1add[a] = SSP1ADD;
81 ssp1buf[a] = SSP1BUF;
82 ssp1con1[a] = SSP1CON1;
83 ssp1con2[a] = SSP1CON2;
84 ssp1con3[a] = SSP1CON3;
85 ssp1stat[a] = SSP1STAT;
86 if (++a >= LOGMAX) {
87 a = 0;
88 }
89
90 if (SSP1STATbits.P == 1 ) {
91 sleep = 1;
92 } else {
93
94 sleep = 1;
95 }
96
97 TESTPULSE(1);
98
99
100 if (SSP1STATbits.R_nW == 1) {
101 TESTPULSE(2);
102 if (SSP1STATbits.D_nA == 0) {
103 senddata_index = 0;
104 } else {
105 senddata_index++;
106 }
107 SSP1BUF = senddata[senddata_index];
108 }
109
110 TESTPULSE(2);
111
112 SSP1CON1bits.CKP = 1;
113 INTCONbits.GIE = 1;
114 }
115
116
117
118
119 int main(int argc, char** argv) {
120
121 INTCON = 0x00;
122 PIR1 = 0x00;
123 PIR2 = 0x00;
124 PIR3 = 0x00;
125 PIR4 = 0x00;
126 T1CON = 0x00;
127 T1GCON = 0x00;
128 T2CON = 0x00;
129 CPSCON0 = 0x00;
130 CPSCON1 = 0x00;
131 PIE1 = 0x00;
132 PIE2 = 0x00;
133 PIE3 = 0x00;
134 PIE4 = 0x00;
135 OPTION_REG = 0x7f;
136 WDTCON = 0x16;
137 OSCCON = 0x78;
138
139 TRISA = 0x20;
140 TRISB = 0xC0;
141 PORTA = 0x00;
142 PORTB = 0x00;
143
144 WPUA = 0x20;
145 WPUB = 0xff;
146
147 ANSELA = 0x00;
148 ANSELB = 0x00;
149
150 FVRCON = 0x00;
151 ADCON0 = 0x00;
152 ADCON1 = 0x00;
153
154 CM1CON0 = 0x00;
155 CM2CON0 = 0x00;
156
157 DACCON0 = 0x00;
158
159 a = 0;
160 sleep = 1;
161
162
163
164
165
166
167 TRISB = 0x12;
168 SSP1ADD = 0xc0;
169 SSP1CON1 = 0x3e;
170
171 INTCON = 0x00;
172 INTCONbits.GIE = 1;
173 INTCONbits.PEIE = 1;
174 INTCONbits.INTE = 0;
175
176 PIE1bits.SSP1IE = 1;
177
178 PIR1 = 0x00;
179 SSP1CON2bits.SEN = 1;
180
181 ra1onoff(3);
182 while (1) {
183 TESTPULSE(3);
184 if (sleep == 1) {
185 SLEEP();
186 }
187 NOP();
188 TESTPULSE(4);
189 }
190
191 return (EXIT_SUCCESS);
192 }