Advertisement
Guest User

Untitled

a guest
Jan 17th, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.54 KB | None | 0 0
  1. bool mcp2515_init(void)
  2. {
  3. char str[80];
  4. unsigned int test_value;
  5.  
  6. SET(MCP2515_CS);
  7. SET_OUTPUT(MCP2515_CS);
  8.  
  9. SET(USB_CS);
  10. //SET_OUTPUT(USB_CS);
  11.  
  12. SET(SD_CS);
  13. //SET_OUTPUT(SD_CS);
  14.  
  15. RESET(P_SCK);
  16. RESET(P_MOSI);
  17. RESET(P_MISO);
  18.  
  19. SET_OUTPUT(P_SCK);
  20. SET_OUTPUT(P_MOSI);
  21. SET_INPUT(P_MISO);
  22.  
  23. SET_INPUT(MCP2515_INT);
  24. SET(MCP2515_INT);
  25.  
  26. // active SPI master interface
  27. SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0);
  28. SPSR = 0;
  29.  
  30. // reset MCP2515 by software reset.
  31. // After this he is in configuration mode.
  32. RESET(MCP2515_CS);
  33. spi_putc(SPI_RESET);
  34. SET(MCP2515_CS);
  35.  
  36. // wait a little bit until the MCP2515 has restarted
  37. _delay_us(15);
  38.  
  39. // load CNF1..3 Register
  40. RESET(MCP2515_CS);
  41.  
  42. spi_putc(SPI_WRITE);
  43. spi_putc(CNF3);
  44.  
  45. spi_putc((1<<PHSEG21)); // Bitrate 125 kbps at 16 MHz
  46. spi_putc((1<<BTLMODE)|(1<<PHSEG11));
  47. spi_putc((1<<BRP2)|(1<<BRP1)|(1<<BRP0));
  48.  
  49.  
  50.  
  51. // activate interrupts
  52. spi_putc((1<<RX1IE)|(1<<RX0IE));
  53. SET(MCP2515_CS);
  54.  
  55.  
  56. test_value = mcp2515_read_register(CANCTRL);
  57. sprintf(str, "test value is [%d]nn",test_value);
  58. UART1_TxString(str);
  59.  
  60. // test if we could read back the value => is the chip accessible?
  61. if (mcp2515_read_register(CNF1) != ((1<<BRP2)|(1<<BRP1)|(1<<BRP0))) {
  62.  
  63. return false;
  64. }
  65.  
  66. // deaktivate the RXnBF Pins (High Impedance State)
  67. mcp2515_write_register(BFPCTRL, 0);
  68.  
  69. // set TXnRTS as inputs
  70. mcp2515_write_register(TXRTSCTRL, 0);
  71.  
  72. // turn off filters => receive any message
  73. mcp2515_write_register(RXB0CTRL, (1<<RXM1)|(1<<RXM0));
  74. mcp2515_write_register(RXB1CTRL, (1<<RXM1)|(1<<RXM0));
  75.  
  76. // reset device to normal mode
  77. mcp2515_write_register(CANCTRL, 0);
  78.  
  79. return true;
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement