Microprocessors and Programming Department of Mechatronics Engineering Lecture 1 1 Microprocessors and Programming Dr. Kadir ERKAN Department of Mechatronics Engineering Fall : 2015 10/3/2019
Master Synchronous Serial Port Module 10/3/2019
Master Synchronous Serial Port Module MSSP module (Master Synchronous Serial Port) is a very useful, but at the same time one of the most complex circuit within the microcontroller. It enables high speed communication between a microcontroller and other peripherals or microcontroller devices by using few input/output lines (maximum two or three). Therefore, it is commonly used to connect the microcontroller to LCD displays, A/D converters, serial EEPROMs, shift registers etc. The main feature of this type of communication is that it is synchronous and suitable for use in systems with a single master and one or more slaves. A master device contains the necessary circuitry for baud rate generation and supplies the clock for all devices in the system. Slave devices may in that way eliminate the internal clock generation circuitry. The MSSP module can operate in one of two modes: I²C mode (Inter-Integrated Circuit) SPI mode (Serial Peripheral Interface) 10/3/2019
SPI = Serial Peripheral Interface Bus This communication technique allows 8 bits of data to be synchronously fetched or sent at the same time !!! SPI mode communication is basically provided using 3 pins !!! SDO (Serial Data Out) -- RC5/SDO SDI (Serial Data In) -- RC4/SDI/SDA SCK (Serial Clock) -- RC3/SCK/SCL/LVDIN In addition to the above, there is a 4. pin to operate in slave mode; SS’ (Slave Select) -- RA5/SS’/AN4 10/3/2019
SPI = Serial Peripheral Interface Bus In SPI mode, 4 registers of the MSSP module are used: SSPSTAT -- status register SSPCON -- control register SSPBUF -- buffer register SSPSR -- shift register (Bu yazmaca doğrudan erişilmez…) SPI Modunda kullanılırlar 10/3/2019
SPI Initialization Prior to the SPI initialization, it is necessary to specify several options: Master mode TRISC.3=0 (the SCK pin is the clock output); Slave mode TRISC.3=1 (the SCK pin is the clock input); Data input phase- middle or end of data output time (the SMP bit of the SSPSTAT register); Clock edge (the CKE bit of the SSPSTAT register); Baud Rate, bits SSPM3-SSPM0 of the SSPCON register (only in Master mode); Slave select mode, bits SSPM3-SSPM0 of the SSPCON register (Slave mode only). 10/3/2019
Microprocessors and Programming SPI’nın Çalışma Adımları Data to be transmitted should be written to the buffer register SSPBUF. If the SPI module operates in master mode, the microcontroller will automatically perform the following steps 2, 3 and 4. If the SPI module operates as Slave, the microcontroller will not perform these steps until the SCK pin detects clock signal. Step1 The data is now moved to the SSPSR register and the SSPBUF register is not cleared. Step 2 This data is then shifted to the output pin (MSB bit first) while the register is simultaneously being filled with bits through the input pin. In Master mode, the microcontroller itself generates clock, while the Slave mode uses external clock (the SCK pin). Step 3 03.10.2019 Microprocessors and Programming
Microprocessors and Programming SPI’nın Çalışma Adımları The SSPSR register is full once 8 bits of data have been received. It is indicated by setting the BF bit of the SSPSTAT register and the SSPIF bit of the PIR1 regis-ter. The received data (one byte) is automatically moved from the SSPSR register to the SSPBUF register. Since serial data transmission is performed automatically, the rest of the program is normally executed while the data transmission is in progress. In this case, the function of the SSPIF bit is to generate an interrupt when one byte transmission is completed. Step 4 Finally, the data stored in the SSPBUF register is ready for use and should be moved to a desired register. Step 5 03.10.2019 Microprocessors and Programming
Microprocessors and Programming SPI’nın Çalışma Adımları 03.10.2019 Microprocessors and Programming
Microprocessors and Programming Lecture 1 Çoklu Slave Mod Çalışma 1 Çoklu Slave Mod çalışma genel olarak 2 tarzda yapılır; Her Slave cihaz için ayrı CS pin kullanımı DataIN DataOUT pinleri iler Slavelerin birlikte bağlanması (Daisy Chain) Herbir Slave ayrı eleman olarak düşünülür… Master tarafından gönderilen veri bütün slave cihazlara kaydırılarak verilir. Bu yapının çalışabilmesi için saat işareti kullanımı bütün cihazlarda aynı olmalı ve slave cihazlar tarafından alınan bitlerin sayısı doğru olmalıdır. Yazılım maliyeti artmaktadır. 03.10.2019 Microprocessors and Programming
Microprocessors and Programming SETUP_SPI(mod) Fonksiyonu 03.10.2019 Microprocessors and Programming
Microprocessors and Programming 03.10.2019 Microprocessors and Programming
Microprocessors and Programming 03.10.2019 Microprocessors and Programming
Example The test system has a slave transmitter that reads a binary-coded decimal input from a thumbwheel switch and sends it to the master controller. This resends the code to the slave receiver, which outputs to a BCD display (0–9). Each of three devices needs its own test program to make the system work. 10/3/2019
Microprocessors and Programming 03.10.2019 Microprocessors and Programming
Microprocessors and Programming 03.10.2019 Microprocessors and Programming
I²C mode (Inter-Integrated Circuit) I²C mode (Inter IC Bus) is especially suitable when the microcontroller and integrated circuit, which the microcontroller should exchange data with, are within the same device. It is commonly about another microcontrollers or specialized, cheap integrated circuits belonging to the new generation of so called "smart peripheral components" (memories, temperature sensors, real-time clocks etc.) Data transfer in I²C mode is synchronous and bidirectional. This time only two pins are used for data transfer. These are the SDA (Serial Data) and SCL (Serial Clock) pins. The user must configure these pins as inputs or outputs through the TRISC bits. By observing particular rules (protocols), this mode enables up to 122 different components to be simultaneously connected in a simple way by using only two valuable I/O pins. Clock, necessary to synchronize the operation of both devices, is always generated by a master device (a microcontroller) and its frequency directly affects the baud rate. 10/3/2019
I²C mode (Inter-Integrated Circuit) When master and slave components are synchronized by the clock, every data exchange is always initialized by master. All slave devices share the same transmission line and all will simultaneously receive the first byte, but only one of them has the address to match. 10/3/2019
Data Transmission in I²C Master Mode Given steps are followed Start condition (bit) is satisfied. (START BIT = logic zero) Adress of the slave is defined and feedforwarded. (7-bit for 122 devices and 10-bit for 1024 devices. Command bit is sent. (data write (0) or read (1) to the device) An acknowledge bit is received. (If the slave device sends acknowledge data bit (1), data transfer will be continued until the master device (microcontroller) sends the Stop bit.) 10/3/2019
I2C Adresleme Çeşitleri X R/W ACK veri STR MSB LSB 7 bit slave adresi S 1 X R/W A1 A2 veri str msb lsb 10 bit slave adresinin sabit yüksek kısmı 9 8 7 6 5 4 3 2 1 10 bit slave adresi
I2C Elektriksel Bağlantı Open drain yada open collector bağlantı için pull-up dirençlerinin eklenmesi Pul-up dirençleri 2kΩ ile 10kΩ arasında seçilebilir… The line capacity load should not exceed 400μF. Therefore the line length and even the number of elements to be connected must be considered during design! If the capacitive load of the line is high, it is possible to reduce it by using MUX. I2C repeater and buffer integrations should be used for long distances. This also increases the amount of capacitive load that can be connected to the line. (400μF 800μF)
Data Transmission in I²C Master Mode In some cases, the inverse of the acknowledgment bit (NACK) is sent instead of the acknowledgment bit (ACK). It means that the 9th bit in the data line is not pulled to logic-0 but remains in logic-1. When the NACK state occurs, the master element must provide the stop condition and the communication should start again by providing the start condition again. One of the conditions below corresponds to the NACK condition; there is no such a device to answer, the device is not ready to answer the receiving device does not recognize the data or the address ttransmitted by the master device, the receiving device can not receive more data from the master element he slave sends NACK information to the master to inform the slave that the data reception transfer has ended. 10/3/2019
#use i2c CCS offers support for the hardware-based I2CTM and a software based master I2C device.(For more information on the hardware based I2C module, please consult the datasheet for your target device; not all PICs support I2C.
#use i2c
Embedded Functions 10/3/2019
#use i2c 10/3/2019
Microprocessors and Programming #USE i2c(sabit, sabit, sabit, …) 03.10.2019 Microprocessors and Programming
10/3/2019
10/3/2019
Example Code 10/3/2019
Example Code /****************************************************** PIC16F877 ile CCS C Dosyalarını Kullanarak 24C02 Harici EEPROM Uygulaması *******************************************************/ #include <16f877.h> // Kullanılacak denetleyicinin başlık dosyası tanıtılıyor. #fuses XT,NOWDT,NOPROTECT,NOBROWNOUT,NOLVP,NOPUT,NOWRT,NODEBUG,NOCPD // Denetleyici konfigürasyon ayarları #use delay(clock=4000000) // Gecikme fonksiyonu için kullanılan osilatör frekansı belirtiliyor #use rs232 (baud=9600, xmit=pin_C6, rcv=pin_C7, parity=N, stop=1) // RS232 ayarları belirtiliyor #use i2c(master,sda=pin_c4,scl=pin_c3,slow=100000) // I2C Fonksiyonu,SDA ve SCL pinleri ve iletişim hızı belirleniyor #include <input.c> // input.c dosyası programa ekleniyor #include <2402.c> // 2402.c dosyası programa ekleniyor byte veri,adres; // byte tipinde değişken tanımlanıyor char islem; // char tipinde değişken tanımlanıyor #int_ssp // I2C iletişiminde yazma veya okuma yapıldığında meydana gelen kesme void I2C_kesmesi() { output_high(pin_C5); // RC5'e bağlı LED yansın delay_ms(250); // Gecikme veriliyor output_low(pin_C5); // RC5'e bağlı LED sönsün } 10/3/2019
Example Code 10/3/2019 void main () { enable_interrupts(int_ssp); // SSP kesmesine izin ver enable_interrupts(GLOBAL); // Tüm izin verilen kesmeler printf("\n\r***********************************************************"); printf("\n\r 24C02C Harici EEPROM Bilgi Okuma Yazma "); printf("\n\r"); printf("\n\r Bir islem seciniz>"); while(1) do // do-while döngüsü tanımlanıyor printf("\n\rOkuma (O) veya Yazma (Y) >"); // RS232 portuna veri gönderiliyor islem=getc(); // RS232 portundan gelen veri alınıyor islem=toupper(islem); // toupper fonksiyonu tüm karakterleri büyük karaktere çevirir a>A putc(islem); // "islem" değişkeni içeriği RS232 portuna gönderiliyor } while ( (islem!='O') && (islem!='Y') ); // Girilen karakter "0" veya "Y" dışında ise döngü başına dön if (islem=='O') // Eğer okunan karakter "O" ise printf("\n\rOkumak istediginiz adresi giriniz> "); adres=gethex(); // RS232 portundan gelen veriyi hex formatında al printf("\n\rDeger= %X",read_ext_eeprom(adres) ); } if (islem=='Y') // Eğer okunan karakter "Y" ise printf("\n\rBilgi yazmak istediginiz adresi giriniz> "); printf("\n\rBilgiyi giriniz> "); veri=gethex(); // RS232 portundan gelen veriyi hex formatında al write_ext_eeprom(adres,veri); // Harici EEPROM'da belirtilen adrese "veri" değerini yaz 10/3/2019