-
-
Save ft232r/d9dde7202607cd5e88640058eebfe5ed to your computer and use it in GitHub Desktop.
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
#include <avr/io.h> | |
#include <stdio.h> | |
#include <stdarg.h> | |
#include <util/delay.h> | |
#include "printk.h" | |
#include "uart.h" | |
void printk(const char *fmt, ...) { | |
char str[64]; | |
uint8_t pos = 0; | |
va_list args; | |
va_start(args, fmt); | |
vsprintf(str, fmt, args); | |
while(str[pos] && pos < 64) { | |
uart_transmit(str[pos]); | |
pos++; | |
} | |
va_end(args); | |
} |
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
#include <avr/io.h> | |
#include <util/delay.h> | |
#include "uart.h" | |
#include "printk.h" | |
uint8_t spi_transceive(uint8_t data) { | |
SPDR = data; | |
while(!(SPSR & (1<<SPIF))); | |
return SPDR; | |
} | |
int main() { | |
uart_init(); | |
DDRB=(1<<PB5)|(1<<PB3)|(1<<PB2); | |
PORTB |= (1<<PB2); | |
/* Enable SPI, Master, set clock rate fck/128 */ | |
SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR1)|(1<<SPR0); | |
_delay_ms(2000); | |
printk("Start master program\r\n"); | |
uint8_t index = 0; | |
uint8_t data = 0; | |
while (1) { | |
PORTB &= ~(1<<PB2); | |
data = spi_transceive(index); | |
_delay_ms(1); | |
PORTB |= (1<<PB2); | |
printk("data %d\r\n", data); | |
index = (index + 1) % 10; | |
_delay_ms(1000); | |
} | |
} |
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
#include <avr/io.h> | |
#include <util/delay.h> | |
#include "uart.h" | |
#include "printk.h" | |
uint8_t spi_transceive(uint8_t data) { | |
SPDR = data; | |
while(!(SPSR & (1<<SPIF))); | |
return SPDR; | |
} | |
int main() { | |
uart_init(); | |
DDRB = (1<<PB4); //MISO as OUTPUT | |
SPCR = (1<<SPE); //Enable SPI | |
printk("Start slave program\r\n"); | |
uint8_t index = 0; | |
uint8_t data = 0; | |
while (1) { | |
data = spi_transceive(index); | |
printk("data %d\r\n", data); | |
index = (index + 1) % 10; | |
} | |
} |
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
#include <avr/io.h> | |
#define BAUD 38400 | |
#define BAUDRATE ((F_CPU)/(BAUD*16UL)-1) | |
void uart_init(); | |
void uart_transmit(uint8_t data); | |
uint8_t uart_recieve(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment