Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "../UART/usart_driver.h"
- #include "avr/io.h"
- #include "../UART/UART.h"
- #include "avr/delay.h"
- //#define DEBUG 1
- #define UCPHA 1
- #define INVEN 7
- #define CMODE0 6
- #define CMODE1 7
- #define FERR 4
- #define BUFOVF 3
- #define PERR 2
- #define TXCIF 6
- #define USART USARTC1
- void USART_CHECK_ERROR()
- {
- if( USART.STATUS & (1 << FERR))
- print("FERR");
- if( USART.STATUS & (1 << PERR))
- print("PERR");
- if( USART.STATUS & (1 << BUFOVF))
- print("BUFOVF");
- }
- void USART_SPI_Init()
- {
- /*
- USART initialization should use the following sequence:
- 1. Set the TxD pin value high, and optionally set the XCK pin low.
- 2. Set the TxD and optionally the XCK pin as output.
- 3. Set the baud rate and frame format.
- 4. Set the mode of operation (enables XCK pin output in synchronous mode).
- 5. Enable the transmitter or the receiver, depending on the usage
- */
- /* Set TXD0 high*/
- SetPin(PORTC,PIN7_bm);
- /* Set CLK high*/
- SetPin(PORTC,PIN5_bm);
- /* PIN7 (TXD0) as output. */
- PORTC.DIRSET = PIN7_bm;
- /* PIN5 (CLK) as output. */
- PORTC.DIRSET = PIN5_bm;
- /* Set baudrate
- * bsel = (fper/(2 * baudrate)) – 1
- * bsel = (32000000/(2 * 8000000))-1 = 1
- * Baudrate = 8 000 000
- * USART_Baudrate_Set(&USART, 1, 0);
- * If ScaleFactor < 0
- * BSEL = (1/(2^(ScaleFactor)*16))*(((I/O clock frequency)/Baudrate)-1)
- * (1/(2^(-7)*16))*(((32000000)/16000000)-1)
- * Baudrate = 16 000 000*/
- //USART_Baudrate_Set(&USART, 8, -7);
- USART_Baudrate_Set(&USART, 1, 0);
- /* Set USART in SPI mode 1, (UCPHA 1, INVEN = 0) */
- USART.CTRLC = (1 << CMODE0) | (1 << CMODE1) | (1 << UCPHA);
- #ifdef DEBUG
- char str[50];
- sprintf(str,"CTRLC %x \n\r", USART.CTRLC);
- print(str);
- #endif
- /*SET Port C, pin 7 -> INVEN 0*/
- PORTC.PIN7CTRL &= ~(1 << INVEN);
- #ifdef DEBUG
- sprintf(str,"INVEN %x \n\r", PORTC.PIN7CTRL);
- print(str);
- #endif
- /* Enable TX. */
- USART_Tx_Enable(&USART);
- #ifdef DEBUG
- print("USART SPI Init\n\r");
- #endif
- }
- void USART_SPI_Send(uint8_t byte)
- {
- USART_CHECK_ERROR();
- while(!USART_IsTXDataRegisterEmpty(&USART));
- /* SET SS Low */
- ClearPin(PORTC, PIN4_bm);
- USART_PutChar(&USART, byte);
- /* SET SS High */
- _delay_us(1);
- SetPin(PORTC, PIN4_bm);
- }
Advertisement
Add Comment
Please, Sign In to add comment