Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
avrkurs
avrkurs
Commits
54e2f77a
Commit
54e2f77a
authored
Mar 15, 2017
by
Petter Breedveld
Browse files
update to spi
parent
db5590dc
Changes
3
Hide whitespace changes
Inline
Side-by-side
Debugger_Demo/Debugger_Demo/main.c
View file @
54e2f77a
...
...
@@ -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
)
{
...
...
Session3-LF/Task1_LF/Task1_LF/uart.c
View file @
54e2f77a
...
...
@@ -5,7 +5,6 @@
* Author: Petter
*/
#define F_CPU 3333333UL
#include <avr/io.h>
#include <avr/interrupt.h>
...
...
Session3-LF/Task2_LF/Task2_LF/spi.c
View file @
54e2f77a
...
...
@@ -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
PORT
C
.
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
PORT
C
.
OUTSET
=
(
1
<<
SS_BIT
)
;
SPI_
PORT
.
OUTSET
=
SS_bm
;
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment