| |||||||
Опубликовано 2010-05-19 14:51:13 автором Admin Data transfer via the SPI AVR. Lesson 10 AVRInterface SPI is widely used to share data in microcontroller technology, and probably the most popular to date from the serial interfaces . Main characteristics SPI is the ease of use , a small length of transmission wires , and high speed data transmission. In microcontroller atmega8 spi implemented in hardware , that is what we use to write firmware in microns. SPI feature is that there is one master (Master) and one or more slaves (Slave) Here is an example of data transmission between two microcontrollers , the first will lead to the second slave . here:
Wiring in such proteuse : Here we will have to write two firmware for one master for another slave . In CodeWizard AVR SPI go to the tab and expose all as shown in Figure Masterremove unused devices initialize and update the code for data transmission# include <mega8.h > // SPI functions # include <spi.h > # include <delay.h > void main (void) { PORTB = 0x00; DDRB = 0x2C; // SPI initialization // SPI Type: Master // SPI Clock Rate: 125,000 kHz // SPI Clock Phase: Cycle Half // SPI Clock Polarity: Low // SPI Data Order: MSB First SPCR = 0x52; SPSR = 0x00; while ( 1 ) { delay_ms ( 100 ); spi ('1 '); // Send 1 to turn the LEDs driven delay_ms ( 100); // Do a delay of 1 second spi ('0 '); // Send 0 to slave turned LED }; } Slave .For slave need to enable interrupts when the parish data well and accordingly choose the type of SPI# include <mega8.h > // SPI interrupt service routine interrupt [SPI_STC] void spi_isr (void) { unsigned char data; data = SPDR; if (data == '1 ') PORTC = 0x01; // If 1 then came to include LED else if (data == '0 ') PORTC = 0x00; // If 0, then came to include LED } void main (void) { // Port B initialization PORTB = 0x00; DDRB = 0x10; // Port C initialization PORTC = 0x00; DDRC = 0x01; // SPI initialization // SPI Type: Slave // SPI Clock Rate: 125,000 kHz // SPI Clock Phase: Cycle Half // SPI Clock Polarity: Low // SPI Data Order: MSB First SPCR = 0xC2; SPSR = 0x00; // Clear the SPI interrupt flag # asm in r30, spsr in r30, spdr # endasm // Global enable interrupts # asm ("sei") while ( 1 ) { }; } Here's what happened Did I mention that one master can connect many Slaves , let's implement this task. Problem : Master transmits the first number first slave , slave , depending on the number of transmitted zero or one LED turns on or off , then switched to the slave master 2 (to enable the desired slave, master on his leg ss establishes a logic 0 . Thus slave knows that communication goes with it. Upon completion of the session master data sets on the line ss logic 1 ) . scheme in this case is as follows Firmwarefor the master we will have to slightly modify the firmware from the first example. In the firmware you want to add switching Slaves .# include <mega8.h > // SPI functions # include <spi.h > # include <delay.h > void main (void) { PORTB = 0x06; DDRB = 0x2E; // SPI initialization // SPI Type: Master // SPI Clock Rate: 125,000 kHz // SPI Clock Phase: Cycle Half // SPI Clock Polarity: Low // SPI Data Order: MSB First SPCR = 0x52; SPSR = 0x00; while ( 1 ) { PORTB.1 = 0 ; // Switch to slave 1 spi ('1 '); // Dispatch him one , he switched to LED delay_ms ( 100); // Wait for 0.1 seconds spi ('0 '); // Dispatch it a 0 , so he turned off the LED delay_ms ( 100); // Wait for 0.1 seconds PORTB.1 = 1 ; // Do not slave 1 active PORTB.2 = 0 ; // Switch to slave 2 // on all similar spi ('1 '); delay_ms ( 100 ); spi ('0 '); delay_ms ( 100 ); PORTB.2 = 1 ; }; } Run and look Do you like homework to do slave output carries the data on lcd Комментарии - (0) Добавить комментарийДля отправки комментария вы должны авторизоваться. |