-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
73 lines (62 loc) · 1.93 KB
/
main.c
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
#include <stdio.h>
#include "config.c"
#include "def_pinos.h"
// RAM module chip-select
#define CS P2_3
// RAM addresses
#define RAM_SIZE 1<<13
#define RAM_WRITE_INSTRUCTION 0x02
#define RAM_READ_INSTRUCTION 0x03
unsigned char read_ram(unsigned short address) {
unsigned char address_low = address;
unsigned char address_high = address >> 8;
// execute byte read sequence (specified in the datasheet)
CS = 0; // enable RAM
SPI0DAT = RAM_READ_INSTRUCTION; // send read instruction
while (!TXBMT); // wait SPI0 buffer to get empty
SPI0DAT = address_high;
while (!TXBMT);
SPI0DAT = address_low;
while (!TXBMT);
SPI0DAT = 0x00; // write 8 bits (either 1s or 0s) during which data will be received
while (!TXBMT);
SPIF = 0; // reset SPI0 interrupt flag
while (!SPIF); // wait for end of transmission interrupt
SPIF = 0; // reset it again
CS = 1; // disable ram
return SPI0DAT; // return the data sent by the RAM
}
void write_ram(unsigned int address, unsigned short data) {
unsigned char address_low = address;
unsigned char address_high = address >> 8;
// execute byte write sequence (specified in the datasheet)
CS = 0; // enable RAM
SPI0DAT = RAM_WRITE_INSTRUCTION; // send write instruction
while (!TXBMT); // wait SPI0 buffer to get empty
SPI0DAT = address_high;
while (!TXBMT);
SPI0DAT = address_low;
while (!TXBMT);
SPI0DAT = data;
while (!TXBMT);
SPIF = 0; // reset SPI0 interrupt flag
while (!SPIF); // wait for end of transmission interrupt
SPIF = 0; // reset it again
CS = 1; // disable ram
}
// handles printf
void putchar (unsigned char c) {
SBUF0 = c;
while (TI0 == 0);
TI0 = 0;
}
void main (void) {
unsigned int i = 0;
Init_Device();
SFRPAGE = LEGACY_PAGE;
for (i = 0; i < RAM_SIZE; ++i)
{
write_ram(i, (unsigned char) i);
printf_fast_f("[%d] = %d\n", i, read_ram(i));
}
}