1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
| /* PIC MMC Interface Test
[url]http://www.captain.at/electronics/[/url]
Writes ASCII characters to the MMC, reads them back and sends the characters
to the linux machine via the serial port. Use "serterm" for testing:
# ./serterm
baud=9600
baud=9600
listening...
PIC online MMC online 0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcde
fghijklmno0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmno01
23456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmno0123456789:;<=
>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnoCCCCCCCCCCCCCCCCCCCCCCCCCC
CCCCCCCCCCC [more C's follow]
exiting...
Furthermore the PIC returns any character sent to it via the interrupt
service routine. Use "ser" for testing:
# ./ser
baud=9600
baud=9600
written:ABC
readport=ABC
#
Compile with CC5x: [url]http://www.bknd.com/cc5x/[/url]
c:\picc\cc5x mmc.c -Ic:\picc\ -ammc.ASM -u
CC5x runs nicely on Linux with "dosemu" [url]http://www.dosemu.org/[/url]
Credit:
SPI functions made by Michael Dworkin
[I]**broken link removed**[/I]
*/
#include <16F876.h>
#include "int16CXX.H" // interrupt stuff
#define CP_off |= 0x3F30
#define LVP |= 128
#pragma config CP_off,PWRTE=on,WDTE=off,FOSC=HS,BODEN=on,LVP
#pragma bit CS @ PORTC.2 // output pin for chip select (MMC)
#pragma rambank 0
char sector1[64];
int currentrambank;
int counter;
#pragma rambank 1
char sector2[64];
#pragma rambank 2
char sector3[64];
#pragma rambank 3
char sector4[64];
#pragma origin 4 // force interrupt service routine at address 4
interrupt myInterrupt(void) // ISR
{
int_save_registers
char rec;
if (RCIF) // USART interrupt ?
{
while(!TXIF) { // wait for serial register to be sent, if there is still something in there
nop();
}
rec = RCREG; // Get the received character
TXREG = rec; // write to serial register -> start transmission
RCIF = 0; // clear USART int flag
}
int_restore_registers
}
void InitUSART()
{
PORTA = 0;
PORTB = 0;
PORTC = 0;
BRGH = 1; // high speed serial mode
SPBRG = 25; // Set 9600 baud for 4 MHz oscillator
SYNC = 0; // Clear SYNC bit -> Set ASYNC Mode
SPEN = 1; // Set serial port enable
TX9 = 0; // 8-bit transmissions
TXEN = 1; // Enable transmission
RCIE = 1; // Rx interrupts are desired
RX9 = 0; // 8-bit receptions
CREN = 1; // Enable reception
}
void initint() // init interrupts
{
GIE = 1; // Set Global Interrupt Enable
PEIE = 1; // Set Peripheral Interrupt Enable
}
void fillram() // fill RAM (sector1..4) with ASCII characters
{
int i;
int x;
for (i=0;i<=63;i++) {
x = i + 48;
sector1[i] = x;
sector2[i] = x;
sector3[i] = x;
sector4[i] = x;
}
}
char SPI(char d) // send character over SPI
{
SSPBUF = d; // send character
while (!BF); // wait until sent
return SSPBUF; // and return the received character
}
char Command(char befF,uns16 AdrH,uns16 AdrL,char befH )
{ // sends a command to the MMC
char a;
SPI(0xFF);
SPI(befF);
SPI(AdrH.high8);
SPI(AdrH.low8);
SPI(AdrL.high8);
SPI(AdrL.low8);
SPI(befH);
SPI(0xFF);
return SPI(0xFF); // return the last received character
}
bit MMC_Init() // init SPI
{
SMP = 0; // input is valid in the middle of clock
CKE = 0; // rising edge is data capture
CKP = 1; // high value is passive state
SSPM1 = 1; // speed f/64(312kHz), Master
SSPEN = 1; // enable SPI
CS = 1; // disable MMC
char i;
// start MMC in SPI mode
for(i=0; i < 10; i++)SPI(0xFF); // send 10*8=80 clock pulses
CS=0; // MMC-Enabled
if (Command(0x40,0,0,0x95) !=1) goto mmcerror; // Reset MMC
st: // if there is no MMC, prg. loops here
if (Command(0x41,0,0,0xFF) !=0) goto st;
return 1;
mmcerror:
return 0;
}
void serialterminate() { // terminate sent string!!!
while(!TXIF);
TXREG = 0x0d;
while(!TXIF);
TXREG = 0x0a;
}
void SerString(const char *str) // send string via RS232
{
char ps;
ps = *str; // pointer of start of string into ps
while(ps>0) // test if end of string is reached
{
str++; // set pointer to next char
if (ps== 0) break; // test if end of string is reached
while(!TXIF); // test if TXREG empty
TXREG = ps ; // send character
ps = *str; // content of pointer into ps
}
serialterminate();
}
bit writeramtommc() // write RAM (sector1..4) to MMC
{
// 512 byte-write-mode
if (Command(0x58,0,512,0xFF) !=0) {
SerString("MMC: write error 1 ");
return 1;
}
SPI(0xFF);
SPI(0xFF);
SPI(0xFE);
uns16 i;
// write ram sectors to MMC
for (i=0;i<=63;i++) {
SPI(sector1[i]);
}
for (i=0;i<=63;i++) {
SPI(sector2[i]);
}
for (i=0;i<=63;i++) {
SPI(sector3[i]);
}
for (i=0;i<=63;i++) {
SPI(sector4[i]);
}
for (i=0;i<256;i++) { // fill rest with 'C'
SPI('C');
}
SPI(255); // at the end, send 2 dummy bytes
SPI(255);
i = SPI(0xFF);
i &= 0b.0001.1111;
if (i != 0b.0000.0101) {
SerString("MMC: write error 2 ");
return 1;
}
while(SPI(0xFF) != 0xFF); // wait until MMC is not busy anymore
return 0;
}
bit sendmmc() // send 512 bytes from the MMC via the serial port
{
uns16 i;
// 512 byte-read-mode
if (Command(0x51,0,512,0xFF) !=0) {
SerString("MMC: read error 1 ");
return 1;
}
while(SPI(0xFF) != 0xFE); // wait for 0xFE - start of any transmission
for(i=0; i < 512; i++)
{
while(!TXIF); // wait until TXREG is empty
TXREG = SPI(0xFF); // get MMC byte and send via RS232
}
serialterminate();
SPI(0xFF); // at the end, send 2 dummy bytes
SPI(0xFF);
return 0;
}
void main(void)
{
InitUSART(); // initialize serial port
initint(); // initialize interrupts
ADCON1=0x6; // PortA Digital
// set ports for I/O
TRISC = 0b.1101.0011; // sck rc3-0, sdo rc5-0, CS rc2-0.
TRISB = 0b.0000.0010; // RB2>TX, RB1>RX
SerString("PIC online "); // start message
// SerString(0x00); // start message
// init MMC and send message if ok
if (MMC_Init()) SerString("MMC online ");
fillram(); // fill the RAM with ASCII characters
writeramtommc(); // write RAM to MMC
sendmmc(); // send 512 bytes of the MMC via serial port
while(1) {
nop();
nop();
}
} |