flitjes

Untitled

Sep 20th, 2012
464
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.23 KB | None | 0 0
  1. #include "../UART/usart_driver.h"
  2. #include "avr/io.h"
  3. #include "../UART/UART.h"
  4. #include "avr/delay.h"
  5.  
  6. //#define DEBUG             1
  7. #define UCPHA               1
  8. #define INVEN               7
  9. #define CMODE0              6
  10. #define CMODE1              7
  11. #define FERR                4
  12. #define BUFOVF              3
  13. #define PERR                2
  14. #define TXCIF               6
  15. #define USART USARTC1
  16.  
  17. void USART_CHECK_ERROR()
  18. {
  19.     if( USART.STATUS & (1 << FERR))
  20.     print("FERR");
  21.     if( USART.STATUS & (1 << PERR))
  22.     print("PERR");
  23.     if( USART.STATUS & (1 << BUFOVF))
  24.     print("BUFOVF");
  25. }
  26.  
  27. void USART_SPI_Init()
  28. {
  29.     /*
  30.     USART initialization should use the following sequence:
  31.     1. Set the TxD pin value high, and optionally set the XCK pin low.
  32.     2. Set the TxD and optionally the XCK pin as output.
  33.     3. Set the baud rate and frame format.
  34.     4. Set the mode of operation (enables XCK pin output in synchronous mode).
  35.     5. Enable the transmitter or the receiver, depending on the usage
  36.     */
  37.  
  38.     /* Set TXD0 high*/
  39.     SetPin(PORTC,PIN7_bm);
  40.     /* Set CLK high*/
  41.     SetPin(PORTC,PIN5_bm);
  42.    
  43.     /* PIN7 (TXD0) as output. */
  44.     PORTC.DIRSET = PIN7_bm;
  45.     /* PIN5 (CLK) as output. */
  46.     PORTC.DIRSET = PIN5_bm;
  47.  
  48.     /* Set baudrate
  49.     * bsel = (fper/(2 * baudrate)) – 1
  50.     * bsel = (32000000/(2 * 8000000))-1 = 1
  51.     * Baudrate = 8 000 000
  52.     * USART_Baudrate_Set(&USART, 1, 0);
  53.     *   If ScaleFactor < 0
  54.     *       BSEL = (1/(2^(ScaleFactor)*16))*(((I/O clock frequency)/Baudrate)-1)
  55.     * (1/(2^(-7)*16))*(((32000000)/16000000)-1)
  56.     * Baudrate = 16 000 000*/
  57.     //USART_Baudrate_Set(&USART, 8, -7);
  58.      USART_Baudrate_Set(&USART, 1, 0);
  59.  
  60.    
  61.     /* Set USART in SPI mode 1, (UCPHA 1, INVEN = 0) */
  62.     USART.CTRLC = (1 << CMODE0) | (1 << CMODE1) | (1 << UCPHA);
  63.    
  64.     #ifdef DEBUG
  65.     char str[50];
  66.     sprintf(str,"CTRLC %x \n\r", USART.CTRLC);
  67.     print(str);
  68.     #endif
  69.    
  70.     /*SET Port C, pin 7 -> INVEN 0*/
  71.     PORTC.PIN7CTRL &= ~(1 << INVEN);
  72.     #ifdef DEBUG
  73.     sprintf(str,"INVEN %x \n\r", PORTC.PIN7CTRL);
  74.     print(str);
  75.     #endif
  76.    
  77.     /* Enable TX. */
  78.     USART_Tx_Enable(&USART);
  79.    
  80.     #ifdef DEBUG
  81.     print("USART SPI Init\n\r");
  82.     #endif
  83. }
  84.  
  85.  
  86.  
  87. void USART_SPI_Send(uint8_t byte)
  88. {
  89.     USART_CHECK_ERROR();
  90.     while(!USART_IsTXDataRegisterEmpty(&USART));
  91.     /* SET SS Low */
  92.     ClearPin(PORTC, PIN4_bm);
  93.     USART_PutChar(&USART, byte);
  94.     /* SET SS High */
  95.     _delay_us(1);
  96.     SetPin(PORTC, PIN4_bm);
  97. }
Advertisement
Add Comment
Please, Sign In to add comment