Example 13 The Serial Peripheral Interface (SPI) Lecture L9.1
PIM_9DP256 Block Diagram 3 SPI Ports
SPI2 Pins PP4 – PP7 Pins 112,111,110,109 SPI1 Pins PP0 – PP3 Pins 4,3,2,1 SPI0 Pins PS4 – PS7 Pins 93,94,95,96
SPI2 Pins PP4 – PP7 Pins 112,111,110,109 SPI1 Pins PP0 – PP3 Pins 4,3,2,1 SPI0 Pins PS4 – PS7 Pins 93,94,95,96
Two SPI modules connected in a master-slave configuration
$D8
Connecting a 16 x 1 hex keypad to two 74165 shift registers
int read_16shift(void){ int data; char c; SS0_LO(); // latch data SS0_HI(); c = send_SPI0(0); // get 1st byte by sending dummy data data = c; data = data << 8; c = send_SPI0(0); // get 2nd byte by sending dummy data data = data | c; return data; }
char get_key(){ const char keytbl[] = { 0x3, 0x2, 0x1, 0x0, 0x8, 0x9, 0xA, 0xB, 0x7, 0x6, 0x5, 0x4, 0xC, 0xD, 0xE, 0xF }; int mask; int data; int i; char found; char key; data = read_16shift(); mask = 0x8000; 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> /* common defines and macros */ #include <mc9s12dp256.h> /* derivative information */ #include "main_asm.h" /* interface to the assembly module */ #pragma LINK_INFO DERIVATIVE "mc9s12dp256b" int read_16shift(void); char get_key(void); void main(void) { char key; PLL_init(); // set system clock frequency to 24 MHz lcd_init(); // enable lcd SPI0_init(); // enable SPI0 set_lcd_addr(0x40); while(1) { key = get_key(); if(key < 16){ key = hex2asc(key); // convert to ascii data8(key); // display on lcd }