Abstract: A fast SPI port can be bit-banged through GPIO pins and serve as a lower cost alternaTIve to 8051-compaTIble microcontrollers with SPI ports. The code shown in this applicaTIon note takes advantage of 8051-specific features to create a fast SPI port using minimal extra code.

Although 8051-compaTIble microcontrollers with SPI ports are available, a lower cost device with an SPI port bit-banged through GPIO pins often suffices for many applications. The code shown here takes advantage of features specific to the 8051 core to create a fast SPI port with minimal effort. The CPHA, CPOL, and CS_TOGGLE_BETWEEN_BYTES constants in the #define statements initialize macros that tailor the code to the type of SPI port being implemented. The preprocessor performs this code tailoring at compile time rather than at runtime, saving precious clock cycles that would be wasted if decision structures (ie, regular if-else statements) were used.

The code below includes 8051-specific C commands required to take advantage of the 8051 core's features. Although these commands are compiler specific (Keil µVision v2 Development Tools for 8051 in this case), all "good" C compilers for 8051-compatible devices incorporate similar commands.

Examining the code, PORT_0 is defined as type sfr, which alerts the compiler that this label is an 8051 Special Function Register (SFR). Because this SFR is bit-addressable, the sbit type defines identifiers that reference specific SFR bits to act as SPI port pins. The bdata type used in the spiTmp declaration allows this variable to be placed in special bit-addressable memory within the 8051 core's directly addressable RAM. Again, the sbit type defines identifiers that will reference specific bits in the spiTmp variable.

The bytes to be sent through the SPI port are loaded into the global byte array, spiData. Declaring this variable as global allows the SPI transmit / receive function to access spiData without needing to pass it as a parameter. Declaring it with the data identifier forces the compiler to store the array in the fastest accessible memory (directly addressable memory) inside the 8051 core.

The spiReadWriteBlock function contains the code for the bit-banged SPI port. It efficiently transmits every byte in the spiData array using this SPI port, starting from the last element in the array to the first. Accessing the array using this reverse order allows a comparison with zero (see code), which translates into faster assembly due to the 8051 instruction set. When the spiReadWriteBlock function is complete, bytes read with the SPI port will have replaced the original data in the spiData array, again starting from the last element in the array to the first.

Note that the code is optimized to send and receive blocks of data larger than one byte. For single-byte transfers, the looping structure and local variable inside spiReadWriteBlock should be removed. (This can be done using the preprocessor.)

When compiled for the Maxim DS89C430 / 450 family of 8051-compatible microcontrollers running at 32MHz, this bit-banged SPI port runs at just over 2Mbps, as shown in Figure 1. Also, the code requires only two bytes of directly addressable RAM and 139 bytes of flash memory for code space (including SPI port initialization and the main program loop).

Figure 1. These waveforms represent the output from the bit-banged SPI port when the CPHA, CPOL, and CS_TOGGLE_BETWEEN_BYTES constants are set to 1. This firmware uses bit-addressable memory in the 8051 core to increase the speed of the SPI port.
Figure 1. These waveforms represent the output from the bit-banged SPI port when the CPHA, CPOL, and CS_TOGGLE_BETWEEN_BYTES constants are set to 1. This firmware uses bit-addressable memory in the 8051 core to increase the speed of the SPI port.

/ * ************************************************ ********** * 8051 Bit-Banged SPI * * MAXIM INTEGRATED PRODUCTS * **************************** ****************************** * / // CONSTANTS --------------- ------------------------------- #define CPOL 1 // Set CPOL to 1 or 0 #define CPHA 1 // Set CPHA to 1 or 0 #define CS_TOGGLE_BETWEEN_BYTES 1 // Set CS toggle // 0 = false 1 = true #define N_OF_SPI_BYTES 3 // MACROS --------------------- ---------------------------- #if CPHA #define SCK_POST #if CPOL #define SCK_INIT 1 #define SCK_PRE SCK = 0 #define SCK_MID SCK = 1 #else #define SCK_INIT 0 #define SCK_PRE SCK = 1 #define SCK_MID SCK = 0 #endif #else #define SCK_PRE #if CPOL #define SCK_INIT 1 #define SCK_MID SCK = 0 #define SCK_POST SCK = 1 #else # define SCK_INIT 0 #define SCK_MID SCK = 1 #define SCK_POST SCK = 0 #endif #endif #if CS_TOGGLE_BETWEEN_BYTES #define CS_TOGGLE CS = 1; CS = 0 #else #define CS_TOGGLE #endif // PIN DEFINITIONS ------- --------------------------------- sfr PORT_0 = 0x80; sbit CS = PORT_ 0 ^ 1; sbit SCK = PORT_0 ^ 2; sbit MOSI = PORT_0 ^ 3; sbit MISO = PORT_0 ^ 4; // GLOBAL VARIABLES --------------------- ------------------ unsigned char data spiData [N_OF_SPI_BYTES]; // BIT-ADDRESSABLE GLOBAL VARIABLES ----------------- ------ unsigned char bdata spiTmp; sbit spiTmp7 = spiTmp ^ 7; sbit spiTmp6 = spiTmp ^ 6; sbit spiTmp5 = spiTmp ^ 5; sbit spiTmp4 = spiTmp ^ 4; sbit mpimp3 ^ 2; sbit spiTmp1 = spiTmp ^ 1; sbit spiTmp0 = spiTmp ^ 0; // FUNCTION PROTOTYPES ----------------------------- ------- void spiReadWriteBlock (void); // FUNCTION spiReadWriteByte ------------------------------ // / / Data is transfered starting from spiData [N_OF_SPI_BYTES-1] // to spiData [0] MSb first. The received data replaces the // existing data from spiData [N_OF_SPI_BYTES-1] to spiData [0]. // // NOTE: this function assumes that // SCK = SCK_INIT and CS = 1 void spiReadWriteBlock (void) {unsigned char data i = N_OF_SPI_BYTES-1; CS = 0; while (1) {spiTmp = spiData [i]; S CK_PRE; MOSI = spiTmp7; SCK_MID; spiTmp7 = MISO; SCK_POST; // bit 7 SCK_PRE; MOSI = spiTmp6; SCK_MID; spiTmp6 = MISO; SCK_POST; // bit 6 SCK_PRE; MOSI = spiTmp5; SCK_Spi; MPS; // bit 5 SCK_PRE; MOSI = spiTmp4; SCK_MID; spiTmp4 = MISO; SCK_POST; // bit 4 SCK_PRE; MOSI = spiTmp3; SCK_MID; spiTmp3 = MISO; SCK_POST; // bit 3 SCK_PRE; MOSI = spiTmp; SPITmp2; MISO; SCK_POST; // bit 2 SCK_PRE; MOSI = spiTmp1; SCK_MID; spiTmp1 = MISO; SCK_POST; // bit 1 SCK_PRE; MOSI = spiTmp0; SCK_MID; spiTmp0 = MISO; SCK_POST; // bit 0 = spiData [] ; if (i == 0) break; i--; CS_TOGGLE;} CS = 1;} // MAIN ------------------------- -------------------------- void main (void) {// 0. Init SPI Pins CS = 1; SCK = SCK_INIT; // 1 . Program Loop ... while (1) {spiData [2] = 0x40; spiData [1] = 0x41; spiData [0] = 0x42; spiReadWriteBlock ();}} A similar version of this article appeared in the May 2, 2005 issue of EE Times magazine.

Led Panel Light is a high-end interior lighting, the outer frame made of aluminum alloy by anodic oxidation, the light source is LED, lighting design simple and beautiful, luxurious atmosphere, both good lighting effects, but also brings beautiful feeling. Unique design LED panel 18w light, the light guide plate after a high light transmittance to form a homogeneous plane glow effect, illuminance uniformity is good, light, soft, comfortable yet bright, can relieve eye fatigue. LED indoor panel lights can also radiation, will not stimulate the pregnant women, the elderly, children's skin.

LED Panel light  LED Panel light

LED Panel light Installation form:
1, can be mounted on the ceiling, walls and installation of surface;

2, LED Panel light may be suspended under the ceiling or mounting body. When mounted on a white ceiling, the entire ceiling was the same color, very beautiful, clean and coordinated;

3, LED panel light using broadband voltage design (AC85-240V / 50-60Hz), can be used in all countries of the world; high-power LED with isolated power, constant current or constant voltage drive, high power efficiency, power clean, stable performance ,Safe and reliable;

4, LED panel light are similar LCD TV backlight technology, a new type of surface light source, light and soft, beautiful appearance, has been widely favored by European and American businessmen, a large number of foreign investors are Buying good quality, good service, good price led Panel Light and suppliers;

Application:

LED Panel light

Packing with paper boxes:

LED Panel light

Mingxue Optoelectronics Co.,Ltd. has apply the I S O 9 0 0 1: 2 0 0 8 international quality management system certificate, for led panel light we apply the CE, RoHS and SAA certificate for our led lighting product.

led panel light

LED Panel light

Led Panel Light

Led Panel Light,Led Ceiling Panel Light,Square Led Panel Light,Outdoor Led Panel Light

Shenzhen Mingxue Optoelectronics CO.,Ltd , https://www.led-lamp-china.com