Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Example of Master Write SPI for Atmega328p
- // SS(CE) <=> PB0
- #include <avr/io.h>
- #define bitSet(p,m) ((p) |= (m))
- #define bitClear(p,m) ((p) &= ~(m))
- void SPI_initMaster(void);
- void SPI_transferByte(char data);
- int main(void) {
- SPI_initMaster();
- bitClear(PORTB, 0);
- SPI_transferByte(0b01010101);
- bitSet(PORTB, 1);
- }
- void SPI_initMaster() {
- DDRB = (1 << 0) | (1 << 3) | (1 << 5);
- SPCR = (1 << SPE) | (1 << MSTR) | (1 << SPR0);
- }
- void SPI_transferByte(char data) {
- SPDR = data;
- while(!(SPSR & (1 << SPIF)))
- ;
- }
Advertisement
Add Comment
Please, Sign In to add comment