I. Introduction to SSI Communication Protocol
SSI (Synchronous Serial Interface) is a synchronous serial interface. The frame format of SSI communication is shown in the figure below. Data transmission uses a synchronous method. During the idle phase when no data transmission occurs, both the clock and data are kept at a high level. The encoder is triggered to load and transmit data on the falling edge of the first clock pulse. Then, the encoder sends out data on the rising edge of each clock pulse, with the most significant bit first and the least significant bit last. After all bits have been transmitted, the clock returns to a high level, and the data also returns to the corresponding high level.
II. Incremental Encoders and Absolute Encoders
Incremental encoders convert angles into periodic electrical signals, then into pulses, using the number of pulses to represent the displacement. Absolute encoders, on the other hand, assign a unique digital code to each position on their code disk. Therefore, their measured values depend only on the starting and ending positions, not on the measurement process itself. Absolute encoders, due to their absolute uniqueness at each position, interference resistance, and lack of power-off memory, are widely used in various industrial control systems for high-precision measurement and control of angles and lengths.
III. Working Principle of Single-Turn Absolute Encoder
A single-turn absolute encoder contains a code disk with several code tracks. These tracks have through holes and dark holes (opaque). For photoelectric encoders, the code disk is located between the light source and the light receiver. When a code track is at the light source (or the measurement interface circuit), the photoelectric receiver receives a light signal through the through hole and sets it to 1; otherwise, it sets it to 0. This allows a fixed angular position to be encoded, with the decimal angle encoded as n bits (4 bits as shown in the diagram). Higher bit numbers correspond to more code tracks, resulting in a higher encoder measurement resolution. For example, a 12-bit encoder has a resolution of 360/2^12 = 360/4096 = 0.0878. Upon receiving a trigger signal (a rising edge in SSI mode), the encoded binary data is shifted into a shift register. Then, in receiver synchronous clock mode, one bit of data is sent to the data line with each rising edge, and the controller reads it.
IV. Sample Code
This test example reads the rotation angle and sends the angle information to a serial port assistant.
(https://item.taobao.com/item.htm?spm=a230r.1.14.68.1ee0c2dfGeO7eU&id=591334271759&ns=1&abbucket=19#detail)
1. Wiring:
The encoder is powered by 5V and outputs TTL level data. It transmits data to the microcontroller (STM32F10) via three wires.
2. Code:
(CSN-PA5;CLK-PA6;DO-PA7)
CLK - Analog synchronous clock, data transmission begins on the first falling edge, and data is output on each subsequent rising edge; CSN - Control signal, low level during data transmission, high level to end;
DO - Data Output Port
Port configuration:
void Gpio_Init ( void ){GPIO_InitTypeDef GPIO_InitStructure;RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); //使能PA端口时钟//GPIO_DeInit(GPIOA);//PA5-CSN -高速推挽输出GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHzGPIO_Init(GPIOA, &GPIO_InitStructure); ////PA6-CKLGPIO_InitStructure.GPIO_Pin = GPIO_Pin_6; //GPIO_Init(GPIOA, &GPIO_InitStructure); //推挽输出,IO口速度为50MHz//PA7-DOGPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; //浮空输入GPIO_Init(GPIOA, &GPIO_InitStructure);}
The 12-bit system outputs one binary bit at a time, reads it out, and stores it. When reading data, CSN needs a high-to-low level transition, then during the loop reading, CLK transitions from low to high. Finally, both CLK and CSN are high.