Example 13 The Serial Peripheral Interface SPI Lecture

  • Slides: 17
Download presentation
Example 13 The Serial Peripheral Interface (SPI) Lecture L 9. 1

Example 13 The Serial Peripheral Interface (SPI) Lecture L 9. 1

PIM_9 DP 256 Block Diagram 3 SPI Ports

PIM_9 DP 256 Block Diagram 3 SPI Ports

SPI 2 Pins PP 4 – PP 7 Pins 112, 111, 110, 109 SPI

SPI 2 Pins PP 4 – PP 7 Pins 112, 111, 110, 109 SPI 1 Pins PP 0 – PP 3 Pins 4, 3, 2, 1 SPI 0 Pins PS 4 – PS 7 Pins 93, 94, 95, 96

SPI 2 Pins PP 4 – PP 7 Pins 112, 111, 110, 109 SPI

SPI 2 Pins PP 4 – PP 7 Pins 112, 111, 110, 109 SPI 1 Pins PP 0 – PP 3 Pins 4, 3, 2, 1 SPI 0 Pins PS 4 – PS 7 Pins 93, 94, 95, 96

Two SPI modules connected in a master-slave configuration

Two SPI modules connected in a master-slave configuration

$D 8

$D 8

Connecting a 16 x 1 hex keypad to two 74165 shift registers

Connecting a 16 x 1 hex keypad to two 74165 shift registers

int read_16 shift(void){ int data; char c; SS 0_LO(); SS 0_HI(); c = send_SPI

int read_16 shift(void){ int data; char c; SS 0_LO(); SS 0_HI(); c = send_SPI 0(0); data = c; data = data << 8; c = send_SPI 0(0); data = data | c; return data; } // latch data // get 1 st byte by sending dummy data // get 2 nd byte by sending dummy data

char get_key(){ const char keytbl[] = { 0 x 3, 0 x 2, 0

char get_key(){ const char keytbl[] = { 0 x 3, 0 x 2, 0 x 1, 0 x 0, 0 x 8, 0 x 9, 0 x. A, 0 x. B, 0 x 7, 0 x 6, 0 x 5, 0 x 4, 0 x. C, 0 x. D, 0 x. E, 0 x. F }; int mask; int data; int i; char found; char key; data = read_16 shift(); mask = 0 x 8000; found = 0; i = 0; key = 16; // not found if key = 16 while((i < 16) && (found == 0)){ if((data & mask) == 0){ found = 1; key = keytbl[i]; } else { mask >>= 1; i++; } } return key; }

// Example 13: SPI Keypad Interfacing with 74165 Shift Registers #include <hidef. h> /*

// Example 13: SPI Keypad Interfacing with 74165 Shift Registers #include <hidef. h> /* common defines and macros */ #include <mc 9 s 12 dp 256. h> /* derivative information */ #include "main_asm. h" /* interface to the assembly module */ #pragma LINK_INFO DERIVATIVE "mc 9 s 12 dp 256 b" int read_16 shift(void); char get_key(void); void main(void) { char key; PLL_init(); // set system clock frequency to 24 MHz lcd_init(); // enable lcd SPI 0_init(); // enable SPI 0 set_lcd_addr(0 x 40); while(1) { key = get_key(); if(key < 16){ key = hex 2 asc(key); // convert to ascii data 8(key); // display on lcd } } }