Follow along with the video below to see how to install our site as a web app on your home screen.
Note: This feature may not be available in some browsers.
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 // SPI master to send data to another MCU #include<avr/io.h> #define DDR_SPI DDRB #define DD_MOSI PB3 #define DD_SCK PB5 #define DD_SS PB2 void SPI_MasterInit(void) { /* Set MOSI and SCK output and SS as output, all other input */ DDR_SPI = ( 1 << DD_MOSI) | ( 1 << DD_SCK) | ( 1 << DD_SS); /* Enable SPI, Master, set clock rate */ SPCR = ( 1 << SPE) | ( 1 << MSTR) | (1 << SPR0) | (1 << SPR1); return; } void SPI_MasterTransmit(char cData) { /* Start Transmission */ SPDR = cData; /* Wait for transmission to complete */ while(~(SPSR & (1 << SPIF))) { } return; } void msdelay(unsigned long itime) { unsigned long i,j; for(i=0; i< itime; i++) { for(j=0; j<1000; j++) {} } return; } int main() { unsigned int i; i=0; DDRC = 0xFF; DDRD = 0xFF; SPI_MasterInit(); while(1) { SPI_MasterTransmit(i); PORTD = i; msdelay(1); i++; } return 0; }
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 // SPI_test 2 #include<avr/io.h> void SPI_Init(void) { DDRB |= (1<<2) | (1<<3) | (1<<5); //SCK, MOSI and SS as output DDRB &= ~(1<<4); //MISO as input SPCR |= (1 << MSTR); //set as master SPCR |= (1 << SPR0) | (1 << SPR1); //set spi clock as fck/128 SPCR |= (1 << SPE); // enable the spi return; } void SPI_Master_Tx(unsigned char data) { SPDR = data; while(!(SPSR & (1<<SPIF))) { } return; } void msdelay(unsigned long itime) { unsigned long i,j; for(i=0; i<itime; i++) { for(j=0;j<1000; j++) { } } return; } int main() { unsigned char i=0; DDRC = 0xFF; SPI_Init(); while(1) { SPI_Master_Tx(i); PORTC = 0x55; msdelay(10); PORTC = 0xAA; msdelay(10); } return 0; }