At45DB161B.c


1
//******************************************************************************
2
// MSP430F2274 Interface to AT45DB161B
3
//
4
// Uses Texas Instruments eZ430-RF2500 Development Tool with 16MBit ATMEL Flash 
5
// Memory. Wireless connection not used.
6
//
7
//******************************************************************************
8
// CONSTANTS
9
#define XTAL 16000000L
10
#define TICKSPERMS (XTAL / 1000 / 5 - 1)
11
#define TICKSPERUS (TICKSPERMS / 1000)
12
// LIBRARIES
13
#include "msp430x22x4.h"
14
// PORT DEFINITIONS
15
#define PORT_CSB_OUT P4OUT
16
#define PORT_CSB_DIR P4DIR
17
#define PORT_SPI_DIR P3DIR
18
#define PORT_SPI_SEL P3SEL
19
// REGISTER AND FLAG DEFINITIONS
20
#define TX_BUFFER UCB0TXBUF
21
#define RX_BUFFER UCB0RXBUF
22
#define IRQ_REG IFG2
23
#define RX_IFG UCB0RXIFG
24
#define TX_IFG UCB0TXIFG
25
#define SPI_CTL0 UCB0CTL0
26
#define SPI_CTL1 UCB0CTL1
27
#define SPI_BR0 UCB0BR0
28
#define SPI_BR1 UCB0BR1
29
30
// Control Register setup
31
#define STATUS_REGISTER_READ 0x57
32
#define BUFFER_1_WRITE 0x84  // buffer 1 write 
33
#define BUFFER_2_WRITE 0x87  // buffer 2 write 
34
#define BUFFER_1_READ 0x54  // buffer 1 read
35
#define BUFFER_2_READ 0x56  // buffer 2 read
36
37
// PIN DEFINITIONS
38
#define PIN_RES BIT4 //not connected yet (set to high manually)
39
#define PIN_CSB BIT3 //not connected yet (set to high manually)
40
#define PIN_MOSI BIT1
41
#define PIN_MISO BIT2
42
#define PIN_SCK BIT3
43
// FUNCTION PROTOTYPES
44
45
void wait_ms(unsigned short ms);
46
void wait_us(unsigned short us);
47
unsigned char Read_Status(void);
48
49
50
void main(void)
51
{
52
  int i=0;
53
54
  unsigned char string [20] ;
55
56
  WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer
57
  BCSCTL1 = CALBC1_16MHZ; // Set DCO to calibrated 16MHz
58
  DCOCTL = CALDCO_16MHZ;
59
  BCSCTL2 |= DIVS_3; // SMCLK to 2 MHz
60
  PORT_CSB_DIR |= PIN_CSB;
61
  PORT_CSB_OUT |= PIN_CSB; // Unselect acceleration sensor
62
  PORT_SPI_SEL |= PIN_MOSI | PIN_MISO | PIN_SCK; // P3.3,2,1 USCI_B0 option select
63
  PORT_SPI_DIR |= BIT0; // P3.0 output direction
64
// Initialize SPI interface to acceleration sensor
65
  SPI_CTL0 |= UCSYNC | UCMST | UCMSB | UCCKPH; // SPI master, 8 data bits, MSB first,
66
// clock idle low, data output on falling edge
67
  SPI_CTL1 |= UCSSEL1; // SMCLK as clock source
68
  SPI_BR0 = 0x08; // Low byte of division factor for baud rate (250 kHz)
69
  SPI_BR1 = 0x00; // High byte of division factor for baud rate
70
  SPI_CTL1 &= ~UCSWRST; // Start SPI hardware
71
  
72
  wait_us(44); // 11 * tsck
73
74
  while(i<20)
75
  {
76
    string[i++]=Read_Status();
77
    wait_us(44);
78
  }
79
}
80
81
// wait ms
82
void wait_ms(unsigned short ms)
83
{
84
  unsigned short a, b;
85
  for (a = ms; a > 0; a--) // outer loop takes 5 ck per round
86
  for (b = TICKSPERMS; b > 0; b--) // inner loop takes 5 ck per round
87
    asm("nop");
88
}
89
90
// wait us
91
void wait_us(unsigned short us)
92
{
93
  unsigned short a;
94
  us *= TICKSPERUS;
95
  for (a = us; a > 0; a--) // loop takes 5 ck per round
96
    asm("nop");
97
}
98
99
void write_SPI(unsigned char spi_data)  //SPI_Mode 3
100
{
101
102
    TX_BUFFER = spi_data; 
103
    while (!(IRQ_REG & TX_IFG));
104
105
106
107
     
108
}
109
110
unsigned char Read_Status(void)  // lesen von ADDR1 
111
{
112
  unsigned char retval;    // Rückgabewert;
113
  //CS_0;            // select EEPROM, CS = 0;
114
  write_SPI(STATUS_REGISTER_READ);
115
  write_SPI(0x00);
116
        write_SPI(0x00);
117
        write_SPI(0x00);
118
        write_SPI(0x00);
119
  //CS_1;             // deselect EEPROM, CS = 1;
120
121
  wait_ms(10);
122
  retval = RX_BUFFER;
123
  return retval;
124
125
}