Two SPI modules connected in a master-slave configuration
$D 8
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 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 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> /* 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 } } }