Friday, August 21, 2020

ATMega32 SPI Interfaces With SN74HC595 For Character LCD Controlling

Serial Peripheral Interface (SPI) offers a high speed communication between controller and its peripheral devices using three wires. The SN74HC595 is a SPI slave device that expand digital outputs up to 8 pin per chip/register. It commonly use 3 wires interfacing. It commonly found in relay output board, LED output, LED matrix display, 7-Segment display, character LCD, etc.

ATMega32 SPI And Character LCD Interfacing

Some Graphical and Character LCD

 

ATMega32 SPI And Character LCD Interfacing

Simulating Program

I use a 20x4 character LCD base on the HD44780 controller. The SPI clock is Fosc/4. The LCD operates in 4-bit data transfer mode because the shift registers have only 8 usable output pins.

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

 Click here to download the zip file.


 



No comments:

Post a Comment