Commit 54e2f77a authored by Petter Breedveld's avatar Petter Breedveld

update to spi

parent db5590dc
......@@ -3,12 +3,54 @@
*
*/
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdio.h>
#define F_CPU 3333333UL
#define BAUD_9600 ((4UL*F_CPU)/9600)
int uart_transmit(char data, FILE* stream){
while (!(USART0.STATUS & (1 << USART_DREIF_bp))){
//Wait for ongoing transmission to finish (if there is one)
}
USART0.TXDATAL = data; //Put new data in register
return 0;
}
void uart_init(unsigned long baud){
PORTB.OUTSET |= (1 << PIN2_bp);
PORTB.DIRSET |= (1 << PIN2_bp);
USART0.BAUD = baud; //Set baudrate
USART0.CTRLB |= (1 << USART_RXEN_bp) | (1 << USART_TXEN_bp); //Enable RX and TX
USART0.CTRLA |= (1 << USART_RXCIE_bp); //Enable interupts on RX
fdevopen(uart_transmit, NULL); //Allows the use of printf()
}
ISR(USART0_RXC_vect){
uint8_t data = USART0.RXDATAL; //We must read the data to clear the interrupt flag
}
int main(void)
{
/* Replace with your application code */
uart_init(BAUD_9600);
while (1)
{
......
......@@ -5,7 +5,6 @@
* Author: Petter
*/
#define F_CPU 3333333UL
#include <avr/io.h>
#include <avr/interrupt.h>
......
......@@ -17,13 +17,12 @@
// RTFD
//Oled is connected to alternate SPI pins
#define MOSI_BIT 2 //on port C
#define SCK_BIT 0 //on port C
#define SS_BIT 3 //on port C
#define MOSI_bm 1<<2 //on port C
#define SCK_bm 1<<0 //on port C
#define SS_bm 1<<3 //on port C
#define SPI_PORT PORTC //We can now use SPI_PORT.DIR, .OUT etc.
//We could also have defined bit masks for MOSI, SCL and SS.
//Then we wouldn't have to shift them all the time
//We could also have defined bit positions for MOSI, SCL and SS.
......@@ -34,9 +33,9 @@ void SPI_MasterInit()
PORTMUX.CTRLB |= PORTMUX_SPI0_bm; //To use alternative SPI pins
SPI_PORT.DIR |= (1 << MOSI_BIT) | (1 << SCK_BIT) | (1 << SS_BIT); //Set pins as output
SPI_PORT.DIR |= MOSI_bm | SCK_bm | SS_bm; //Set pins as output
SPI_PORT.OUTSET = (1 << SS_BIT); //Set SS high -> OLED inactive
SPI_PORT.OUTSET = SS_bm; //Set SS high -> OLED inactive
// Now enable SPI, Master and set clock rate
......@@ -53,7 +52,7 @@ void SPI_MasterInit()
void SPI_MasterTransmit(char cData)
{
// First select the correct slave by setting its slave select (SS) LOW
PORTC.OUTCLR = (1 << SS_BIT);
SPI_PORT.OUTCLR = SS_bm;
// Then start the transmission by assigning the data to the SPI data register
SPI0.DATA = cData;
......@@ -66,5 +65,5 @@ void SPI_MasterTransmit(char cData)
SPI0.DATA; //Dummy read to clear flag
// Finally set the slave select bit HIGH before leaving the function
PORTC.OUTSET = (1 << SS_BIT);
SPI_PORT.OUTSET = SS_bm;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment