jruthe

MSP430 SPI Code

Mar 6th, 2014
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.29 KB | None | 0 0
  1. void configureMicrocontroller () {
  2.     //
  3.     // General Initiate statements
  4.     //
  5.     WDTCTL  = WDTPW+WDTHOLD;// Stop Watchdog timer
  6.     P2DIR   |= BIT1 | BIT2; // P2 output for LED
  7.     P2IE    |= BIT0 | BIT3; // Enable interrupts
  8.     P2IES   |= BIT0;    // Chance BIT0 to active low
  9.  
  10.     P3DIR   |= WIZ_RESET_PIN | WIZ_SS_PIN;  // Set W5200 pins
  11.     P3OUT   |= WIZ_RESET_PIN | WIZ_SS_PIN;
  12.  
  13.     //
  14.     //  Initialize SPI
  15.     //
  16.     P3SEL       |= 0x0E;
  17.     UCB0CTL0    |= UCMST+UCSYNC+UCCKPL+UCMSB;
  18.     UCB0CTL1    |= UCSSEL_2;
  19.     UCB0BR0     = 0x00;
  20.     UCB0BR1     = 0;
  21.     UCB0CTL1    &= ~UCSWRST;              
  22.  
  23.     _BIS_SR(GIE);
  24. }
  25.  
  26. void writeToRegister (u_int addr, u_char data) {
  27.     wizSelect(); // Makes SS for W5200 low
  28.     sendReceiveByte(addr >> 8);
  29.     sendReceiveByte(addr);
  30.     sendReceiveByte(0x80);
  31.     sendReceiveByte(0x01);
  32.     sendReceiveByte(data);
  33.     wizDeselect(); // makes SS for W5200 high
  34. }
  35.  
  36. u_char readFromRegister (u_int addr) {
  37.     wizSelect(); // Makes SS for W5200 low
  38.     sendReceiveByte(addr >> 8);
  39.     sendReceiveByte(addr);
  40.     sendReceiveByte(0x00);
  41.     sendReceiveByte(0x01);
  42.     u_char data = sendReceiveByte(0x00);
  43.     wizDeselect(); // makes SS for W5200 high
  44.     return data;
  45. }
  46.  
  47. void writeMultiple (u_int addr, u_int size, u_char *dest) {
  48.     u_int i;
  49.  
  50.     if (size < 1)
  51.         return;
  52.  
  53.     wizSelect(); // Makes SS for W5200 low
  54.     sendReceiveByte(addr >> 8);
  55.     sendReceiveByte(addr);
  56.     sendReceiveByte(0x80 | ((size>>8)&0x7F));
  57.     sendReceiveByte(size&0xFF);
  58.     for (i = 0; i < size; i++) {
  59.         sendReceiveByte(dest[i]);
  60.     }
  61.     wizDeselect(); // makes SS for W5200 high
  62. }
  63.  
  64. void readMultiple (u_int addr, u_int size, u_char *dest) {
  65.     int i;
  66.  
  67.     wizSelect(); // Makes SS for W5200 low
  68.     sendReceiveByte(addr >> 8);
  69.     sendReceiveByte(addr);
  70.     sendReceiveByte((size>>8)&0x7F);
  71.     sendReceiveByte(size&0xFF);
  72.     for (i = 0; i < size; i++)
  73.         dest[i] = sendReceiveByte(0x00);
  74.     wizDeselect(); // makes SS for W5200 high
  75. }
  76.  
  77. u_char sendReceiveByte (u_char data) {
  78.     UCB0TXBUF       = data;
  79.     while (!(IFG2 & UCB0TXIFG));
  80.     data            = UCB0RXBUF;
  81.  
  82.     return data;
  83. }
  84.  
  85. void main (void) {
  86.     configureMicrocontroller();
  87.  
  88.     // Write IP 192.168.1.200
  89.     writeToRegister(0x0009,0xC0)
  90.     writeToRegister(0x000A,0xA8)
  91.     writeToRegister(0x000B,0x01)
  92.     writeToRegister(0x000C,0xC8)
  93.     printf("IP Address is: %d.%d.%d.%d\n",readFromRegister(0x9),readFromRegister(0xA),
  94.         readFromRegister(0xB),readFromRegister(0xC));
  95. }
Advertisement
Add Comment
Please, Sign In to add comment