Skip to content

Instantly share code, notes, and snippets.

@ft232r

ft232r/printk.c Secret

Last active July 23, 2016 06:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ft232r/d9dde7202607cd5e88640058eebfe5ed to your computer and use it in GitHub Desktop.
Save ft232r/d9dde7202607cd5e88640058eebfe5ed to your computer and use it in GitHub Desktop.
#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);
}
#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);
}
}
#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;
}
}
#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