Last active
May 16, 2019 11:14
-
-
Save avr-programmierung/a7b5a390db3048d1a20cbb0f38a7fe6c to your computer and use it in GitHub Desktop.
ATmega88 @ 1MHz SPI-Master - Datenübertragung zweier 8-Bit Daten (11010010 und 10000001)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* spi_send_01.c | |
* SPI-Master - Datenübertragung | |
* Übertragung zweier 8-Bit Daten (11010010 und 10000001) | |
* Controller: ATmega88 @ 1MHz | |
*/ | |
#include <avr/io.h> | |
#include <util/delay.h> | |
void SPI_Master_Init(void) | |
{ | |
SPCR &= ~(1<<DORD); // Send MSB first | |
SPCR |= (1<<MSTR); // Controller = Master | |
SPCR &= ~(1<<CPOL); // CLK positiv phase | |
SPCR |= (1<<CPHA); // Sampling data @ rising edge | |
SPCR |= (1<<SPR1); // CLK / 64 = 125kHz | |
DDRB |= (1<<PB3); // Set MOSI output | |
DDRB &= ~(1<<PB4); // Set MISO input | |
DDRB |= (1<<PB5); // Set SCK output | |
DDRB |= (1<<PB2); // Set SS output | |
SPCR |= (1<<SPE); // SPI enable | |
} | |
void SPI_Master_Transmit(char cData) | |
{ | |
PORTB &= ~(1<<PB2); // SS = low (Slave = enabled) | |
SPDR = cData; // Start transmission | |
while (!(SPSR & (1<<SPIF))); // Wait for transmission complete | |
PORTB |= (1<<PB2); // SS = high (Slave = disabled) | |
} | |
int main(void) | |
{ | |
SPI_Master_Init(); | |
while(1) | |
{ | |
SPI_Master_Transmit(0b11010010); | |
_delay_ms(500); // delaytime between next transmission | |
SPI_Master_Transmit(0b10000001); | |
_delay_ms(500); // delaytime between next transmission | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment