Wednesday, July 8, 2020

ATMega32 SPI Interfaces To SN74HC164 And LCD Using 3 Pins

The HD44780 character LCD type uses parallel interface with controller, even it operates in 4-bit data transfer mode. Currently most electronic hobbyists use an additional shift register or an I/O expanding chip, especially the PCF8574T chip.

ATMega32 SPI Interfaces To SN74HC164 And LCD Using 3 Pins
Character LCD With PCF8574T
In this example, I use SN74HC164 shift register to interface between controller and character LCD. 

ATMega32 SPI Interfaces To SN74HC164 And LCD Using 3 Pins
Schematic Diagram
I use two wires - serial data and serial clock to interface between controller and SN74HC164. However I use one additional pin to interacts with LCD Enable pin. It's because I have difficulty during programming.

  1. /*
  2.  * SPI74164LCD.c
  3.  *
  4.  * Created: 7/7/2023 9:48:09 AM
  5.  * Author : Admin
  6.  */
  7.  
  8.  
  9. #include <avr/io.h>
  10.  
  11. #define F_CPU 4000000UL
  12. #include <util/delay.h>
  13.  
  14. void masterInit(void){
  15. /*Set MOSI, SCK and SS Output*/
  16. DDRB|=(1<<4)|(1<<5)|(1<<7);
  17. /*Enable SPI Master Set Clock Rate Fclk/4*/
  18. SPCR|=(1<<SPE)|(1<<MSTR);
  19. }
  20.  
  21. void masterTransmit(unsigned char spiData){
  22. /*Start The Transmission*/
  23. SPDR = spiData;
  24. /*Wait For Completion*/
  25. while(!(SPSR&(1<<SPIF)));
  26. }
  27.  
  28. void lcdCmd(unsigned char cmd){
  29.  
  30. masterTransmit(cmd&0xF0);
  31. PORTB|=(1<<4);
  32. _delay_us(100);
  33. PORTB&=~(1<<4);
  34. _delay_us(100);
  35.  
  36. masterTransmit(cmd<<4);
  37. PORTB|=(1<<4);
  38. _delay_us(100);
  39. PORTB&=~(1<<4);
  40. _delay_us(100);
  41.  
  42. }
  43.  
  44. void lcdDat(unsigned char dat){
  45.  
  46. masterTransmit((dat&0xF0)|1);
  47. PORTB|=(1<<4);
  48. _delay_us(100);
  49. PORTB&=~(1<<4);
  50. _delay_us(100);
  51.  
  52. masterTransmit((dat<<4)|1);
  53. PORTB|=(1<<4);
  54. _delay_us(100);
  55. PORTB&=~(1<<4);
  56. _delay_us(100);
  57. }
  58.  
  59. void lcdInit(){
  60.  
  61. masterTransmit(0x00);
  62. _delay_ms(20);
  63. lcdCmd(0x33);
  64. _delay_us(100);
  65. lcdCmd(0x32);
  66. _delay_us(100);
  67. lcdCmd(0x28);
  68. _delay_us(100);
  69.  
  70. lcdCmd(0x0F);
  71. _delay_us(100);
  72. lcdCmd(0x01);
  73. _delay_ms(2);
  74. lcdCmd(0x06);
  75. _delay_us(100);
  76. }
  77.  
  78. void lcdStr( char *str){
  79. unsigned char i=0;
  80. while(str[i]!=0){
  81. lcdDat(str[i]);
  82. i++;
  83. }
  84. }
  85.  
  86. void lcdXY(unsigned char x, unsigned char y){
  87. unsigned char tbe[]={0x80,0xC0,0x94,0xD4}; // 20x4
  88. lcdCmd(tbe[y-1]+x-1);
  89. _delay_us(100);
  90. }
  91.  
  92. void lcdClear(){
  93. lcdCmd(0x01);
  94. _delay_us(10);
  95. }
  96.  
  97.  
  98. int main(void)
  99. {
  100. masterInit();
  101.  
  102. lcdInit();
  103.  
  104. lcdXY(1,1);
  105. lcdStr("ATMEGA32 SN74HC164");
  106.  
  107. lcdXY(1,2);
  108. lcdStr("Character LCD ");
  109. lcdXY(1,3);
  110. lcdStr("Interfacing Using");
  111. lcdXY(1,4);
  112. lcdStr("Three Wires.......");
  113. lcdCmd(0x0F);
  114. _delay_ms(100);
  115.  
  116. while (1)
  117. {
  118. }
  119. }
  120.  
  121.  
  122.  

Click here to download source file.

ATMega32 SPI Interfaces To SN74HC164 And LCD Using 3 Pins
Simulating Program


No comments:

Post a Comment