Monday, February 12, 2024

PIC16F887 SPI SN74HC165 LCD XC8 Example

Overview

The SN74HC165 is an 8-bit parallel-load shift registers chip. It uses the SPI interface to shift data out from the register. This chip commonly use in keys reading in video system, printer/scanner, microwave oven, etc.

PIC16F887 SPI SN74HC165 LCD XC8 Example
Simulating Program in Proteus

This chip is very easy to use since it's a standard logic IC. It does not require any configuration like the MCP23S17 does.

PIC16F887 SPI SN74HC165 LCD XC8 Example
Pin Configuration and Functions

It has a Dual In Line Package (DIP) easing the electronics hobbyist prototyping. I don't have a physical chip here because it is hard to find at local store. But it has a model in Proteus VSM allowing us to simulate the behavior of this chip.

PIC16F887 SPI SN74HC165 LCD XC8 Example
Logic Diagram

The SN74HC165 contain standard logic gates and flip flop as shown in the diagram above. It can be cascaded to make a larger registers more than 8-bit inputs.

PIC16F887 SPI SN74HC165 LCD XC8 Example
Load Shift Sequence

To use the chip the microprocessor must activate the LOAD (LD) pin from high to low transition first. It will latch the 9-bit input data into the register. Then the 8 clock cycle input via CLK (clock) pin will shift the data out at SO (serial out) pin.

The CLK INH (clock inhabit) must be pulled low to enable this chip.

PIC16F887 SPI Programming

We can use the build-in SPI module of PIC16F887 to read data from this chip. The SPI module has the following pins,

  1. nCS - Chip Select (RC2 or other pins)
  2. SCK - Serial Clock (RC3)
  3. SDI - Serial Data In (RC4)
  4. SDO - Serial Data Out (RC5)

I use the nCS pin to load data into the SN74C165 first before the data shifts out. In this example, the PIC16F887 read data from the SN74HC165. The 8-bit data will show on a 16x2 character LCD in decimal, hexadecimal and binary value.

  1. /*
  2.  * File: main.c
  3.  * Author: Admin
  4.  *
  5.  * Created on February 12, 2024, 7:19 PM
  6.  */
  7.  
  8. #include <xc.h>
  9. #include <stdio.h>
  10. #include "config.h"
  11. #include "LCD4Bits.h"
  12. #include "spi.h"
  13.  
  14. #define _XTAL_FREQ 8000000UL
  15.  
  16. void putch(char data){
  17. lcdData(data);
  18. }
  19.  
  20. void dec_to_bin(uint8_t data){
  21. for(int8_t i=7;i>=0;i--){
  22. if((data&(1<<i))==0) putch('0');
  23. else putch('1');
  24. }
  25. }
  26.  
  27. void main(void) {
  28. uint8_t rc_data;
  29. OSCCONbits.IRCF=7;
  30. lcdInit();
  31. spi_init();
  32. lcdCommand(0x0C);
  33. nCS=1;
  34. while(1){
  35. nCS=0;
  36. __delay_us(10);
  37. nCS=1;
  38. rc_data=spi_receive();
  39. lcdXY(1,1);
  40. printf("DEC:%3d",rc_data);
  41. printf(" HEX:0x%02X",rc_data);
  42. lcdXY(1,2);
  43. printf("BIN: ");
  44. dec_to_bin(rc_data);
  45. __delay_ms(100);
  46. }
  47. return;
  48. }
  49.  

I putted the SPI driver in one package.

The spi.h header file:

  1. /*
  2.  * File: spi.h
  3.  * Author: Admin
  4.  *
  5.  * Created on February 9, 2024, 9:13 AM
  6.  */
  7.  
  8. #include <xc.h>
  9.  
  10. #define WRITE_ADDR 0x40
  11. #define READ_ADDR 0x41
  12.  
  13. #define nCS RC2
  14.  
  15. void spi_init(void);
  16. void spi_send(uint8_t data);
  17. uint8_t spi_receive(void);
  18.  
  19.  

The spi.c source file:

  1.  
  2. #include "spi.h"
  3.  
  4. void spi_init(void){
  5. /*SPI Mode Clock Low To High*/
  6. SSPCONbits.CKP=0;
  7. SSPSTATbits.CKE=1;
  8. SSPSTATbits.SMP=0;
  9. /*SPI Master Mode Clock = Fosc/64*/
  10. SSPCONbits.SSPM=2;
  11. /*Turn On The Module*/
  12. SSPCONbits.SSPEN=1;
  13. SSPSTATbits.BF=1;
  14. PORTC=0;
  15. TRISC=0;
  16. /*SPI SDI Pin Input*/
  17. TRISC4=1;
  18. }
  19.  
  20. void spi_send(uint8_t data){
  21. SSPSTATbits.BF==1;
  22. SSPBUF=data;
  23. while(SSPSTATbits.BF==0);
  24. SSPSTATbits.BF==1;
  25. }
  26.  
  27. uint8_t spi_receive(void){
  28. uint8_t data;
  29. spi_send(0x00);
  30. data=SSPBUF;
  31. return data;
  32. }

Click here to download this example.

PIC16F887 SPI SN74HC165 LCD XC8 Example
Simulating Program in Proteus


We can use the PIC16F84A with SPI bit-banging to read the data from this chip. A larger AVR chip, the ATMega32 could use a software SPI to interface to this chip.

You can see the software SPI version of this example.



No comments:

Post a Comment

Search This Blog

Labels

25AA010A (1) 8051 (7) 93AA46B (1) ADC (30) Analog Comparator (1) Arduino (15) ARM (6) AT89C52 (7) ATMega32 (54) AVR (57) CCS PICC (28) DAC (1) DHT11 (2) Display (105) Distance Sensor (3) DS18B20 (3) dsPIC (2) dsPIC30F1010 (2) EEPROM (5) Environment Sensor (4) esp8266 (1) I2C (29) Input/Output (67) Interrupt (19) Keil (5) Keypad (10) LCD (46) Master/Slave (1) MAX7221 (1) MCP23017 (5) MCP23S17 (4) Meter (3) MikroC (2) Motor (15) MPLABX (66) Nokia 5110 LCD (3) OLED (2) One-Wire (6) Oscillator (8) PCB (6) PCD8544 (3) PCF8574 (5) PIC (107) PIC12F (2) PIC16F628A (2) PIC16F630 (1) PIC16F716 (3) PIC16F818 (10) PIC16F818/819 (2) PIC16F84A (15) PIC16F876A (1) PIC16F877A (9) PIC16F88 (1) PIC16F887 (60) PIC18 (19) PIC18F1220 (4) PIC18F2550 (3) PIC18F4550 (12) PWM (11) RTC (8) Sensor (10) SH1106 (1) Shift Register (11) Shift Registers (2) SPI (24) STM32 (6) STM32 Blue Pill (6) STM32CubeIDE (6) STM32F103C8T6 (6) SysTick (3) temperature sensor (11) Thermometer (21) Timer/Counter (30) TM1637 (2) UART (7) Ultrasonic (4) Voltmeter (7) WDT (1) XC16 (2) XC8 (94)