Advertisement
Guest User

LCD

a guest
Jun 13th, 2010
664
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.86 KB | None | 0 0
  1. //-------------------------------------------------------------------------------------------------
  2.  
  3. // Wyświetlacz alfanumeryczny ze sterownikiem HD44780
  4.  
  5. // Sterowanie w trybie 4-bitowym bez odczytu flagi zajętości
  6.  
  7. // z dowolnym przypisaniem sygnałów sterujšcych
  8.  
  9. // Plik : HD44780.h
  10.  
  11. // Mikrokontroler : Atmel AVR
  12.  
  13. // Kompilator : avr-gcc
  14.  
  15. // Autor : Radosław Kwiecień
  16.  
  17. // Źródło : http://radzio.dxp.pl/hd44780/
  18.  
  19. // Data : 24.03.2007
  20.  
  21. //-------------------------------------------------------------------------------------------------
  22.  
  23. #define F_CPU 1000000L
  24.  
  25.  
  26. #include <avr/io.h>
  27.  
  28. #include <util/delay.h>
  29.  
  30.  
  31.  
  32. //-------------------------------------------------------------------------------------------------
  33.  
  34. //
  35.  
  36. // Konfiguracja sygnałów sterujšcych wyświetlaczem.
  37.  
  38. // Można zmienić stosownie do potrzeb.
  39.  
  40. //
  41.  
  42. //-------------------------------------------------------------------------------------------------
  43. void Test()
  44. {
  45. _delay_ms(250);
  46. _delay_ms(250);
  47. _delay_ms(250);
  48. _delay_ms(250);
  49. PORTB ^= _BV(0);
  50. _delay_ms(250);
  51. _delay_ms(250);
  52. _delay_ms(250);
  53. _delay_ms(250);
  54. PORTB ^= _BV(0);
  55. }
  56.  
  57.  
  58. #define LCD_RS_DIR      DDRD
  59.  
  60. #define LCD_RS_PORT     PORTD
  61.  
  62. #define LCD_RS_PIN      PIND
  63.  
  64. #define LCD_RS          (1 << PD0)
  65.  
  66.  
  67.  
  68. #define LCD_RW_DIR      DDRD
  69.  
  70. #define LCD_RW_PORT     PORTD
  71.  
  72. #define LCD_RW_PIN      PIND
  73.  
  74. #define LCD_RW          (1 << PD1)
  75.  
  76.  
  77.  
  78. #define LCD_E_DIR       DDRD
  79.  
  80. #define LCD_E_PORT      PORTD
  81.  
  82. #define LCD_E_PIN       PIND
  83.  
  84. #define LCD_E           (1 << PD2)
  85.  
  86.  
  87.  
  88. #define LCD_DB4_DIR     DDRD
  89.  
  90. #define LCD_DB4_PORT    PORTD
  91.  
  92. #define LCD_DB4_PIN     PIND
  93.  
  94. #define LCD_DB4         (1 << PD3)
  95.  
  96.  
  97.  
  98. #define LCD_DB5_DIR     DDRD
  99.  
  100. #define LCD_DB5_PORT    PORTD
  101.  
  102. #define LCD_DB5_PIN     PIND
  103.  
  104. #define LCD_DB5         (1 << PD4)
  105.  
  106.  
  107.  
  108. #define LCD_DB6_DIR     DDRD
  109.  
  110. #define LCD_DB6_PORT    PORTD
  111.  
  112. #define LCD_DB6_PIN     PIND
  113.  
  114. #define LCD_DB6         (1 << PD5)
  115.  
  116.  
  117.  
  118. #define LCD_DB7_DIR     DDRD
  119.  
  120. #define LCD_DB7_PORT    PORTD
  121.  
  122. #define LCD_DB7_PIN     PIND
  123.  
  124. #define LCD_DB7         (1 << PD6)
  125.  
  126.  
  127.  
  128. //-------------------------------------------------------------------------------------------------
  129.  
  130. //
  131.  
  132. // Instrukcje kontrolera Hitachi HD44780
  133.  
  134. //
  135.  
  136. //-------------------------------------------------------------------------------------------------
  137.  
  138.  
  139.  
  140. #define HD44780_CLEAR                   0x01
  141.  
  142.  
  143.  
  144. #define HD44780_HOME                    0x02
  145.  
  146.  
  147.  
  148. #define HD44780_ENTRY_MODE              0x04
  149.  
  150.     #define HD44780_EM_SHIFT_CURSOR     0
  151.  
  152.     #define HD44780_EM_SHIFT_DISPLAY    1
  153.  
  154.     #define HD44780_EM_DECREMENT        0
  155.  
  156.     #define HD44780_EM_INCREMENT        2
  157.  
  158.  
  159.  
  160. #define HD44780_DISPLAY_ONOFF           0x08
  161.  
  162.     #define HD44780_DISPLAY_OFF         0
  163.  
  164.     #define HD44780_DISPLAY_ON          4
  165.  
  166.     #define HD44780_CURSOR_OFF          0
  167.  
  168.     #define HD44780_CURSOR_ON           2
  169.  
  170.     #define HD44780_CURSOR_NOBLINK      0
  171.  
  172.     #define HD44780_CURSOR_BLINK        1
  173.  
  174.  
  175.  
  176. #define HD44780_DISPLAY_CURSOR_SHIFT    0x10
  177.  
  178.     #define HD44780_SHIFT_CURSOR        0
  179.  
  180.     #define HD44780_SHIFT_DISPLAY       8
  181.  
  182.     #define HD44780_SHIFT_LEFT          0
  183.  
  184.     #define HD44780_SHIFT_RIGHT         4
  185.  
  186.  
  187.  
  188. #define HD44780_FUNCTION_SET            0x20
  189.  
  190.     #define HD44780_FONT5x7             0
  191.  
  192.     #define HD44780_FONT5x10            4
  193.  
  194.     #define HD44780_ONE_LINE            0
  195.  
  196.     #define HD44780_TWO_LINE            8
  197.  
  198.     #define HD44780_4_BIT               0
  199.  
  200.     #define HD44780_8_BIT               16
  201.  
  202.  
  203.  
  204. #define HD44780_CGRAM_SET               0x40
  205.  
  206.  
  207.  
  208. #define HD44780_DDRAM_SET               0x80
  209.  
  210.  
  211.  
  212. //-------------------------------------------------------------------------------------------------
  213.  
  214. //
  215.  
  216. // Deklaracje funkcji
  217.  
  218. //
  219.  
  220. //-------------------------------------------------------------------------------------------------
  221.  
  222.  
  223.  
  224. void LCD_WriteCommand(unsigned char);
  225.  
  226. unsigned char LCD_ReadStatus(void);
  227.  
  228. void LCD_WriteData(unsigned char);
  229.  
  230. unsigned char LCD_ReadData(void);
  231.  
  232. void LCD_WriteText(char *);
  233.  
  234. void LCD_GoTo(unsigned char, unsigned char);
  235.  
  236. void LCD_Clear(void);
  237.  
  238. void LCD_Home(void);
  239.  
  240. void LCD_Initalize(void);
  241.  
  242.  
  243.  
  244. //-------------------------------------------------------------------------------------------------
  245.  
  246. //
  247.  
  248. // Koniec pliku HD44780.h
  249.  
  250. //
  251.  
  252. //-------------------------------------------------------------------------------------------------
  253. //-------------------------------------------------------------------------------------------------
  254.  
  255. //
  256.  
  257. // Funkcja wystawiajšca półbajt na magistralę danych
  258.  
  259. //
  260.  
  261. //-------------------------------------------------------------------------------------------------
  262.  
  263. void _LCD_OutNibble(unsigned char nibbleToWrite)
  264.  
  265. {
  266.  
  267.  
  268.  
  269. if(nibbleToWrite & 0x01)
  270.  
  271.     LCD_DB4_PORT |= LCD_DB4;
  272.  
  273. else
  274.  
  275.     LCD_DB4_PORT  &= ~LCD_DB4;
  276.  
  277.  
  278.  
  279. if(nibbleToWrite & 0x02)
  280.  
  281.     LCD_DB5_PORT |= LCD_DB5;
  282.  
  283. else
  284.  
  285.     LCD_DB5_PORT  &= ~LCD_DB5;
  286.  
  287.  
  288.  
  289. if(nibbleToWrite & 0x04)
  290.  
  291.     LCD_DB6_PORT |= LCD_DB6;
  292.  
  293. else
  294.  
  295.     LCD_DB6_PORT  &= ~LCD_DB6;
  296.  
  297.  
  298.  
  299. if(nibbleToWrite & 0x08)
  300.  
  301.     LCD_DB7_PORT |= LCD_DB7;
  302.  
  303. else
  304.  
  305.     LCD_DB7_PORT  &= ~LCD_DB7;
  306.  
  307. }
  308.  
  309. //-------------------------------------------------------------------------------------------------
  310.  
  311. //
  312.  
  313. // Funkcja wystawiajšca półbajt na magistralę danych
  314.  
  315. //
  316.  
  317. //-------------------------------------------------------------------------------------------------
  318.  
  319. unsigned char _LCD_InNibble(void)
  320.  
  321. {
  322.  
  323. unsigned char tmp = 0;
  324.  
  325.  
  326.  
  327. if(LCD_DB4_PIN & LCD_DB4)
  328.  
  329.     tmp |= (1 << 0);
  330.  
  331. if(LCD_DB5_PIN & LCD_DB5)
  332.  
  333.     tmp |= (1 << 1);
  334.  
  335. if(LCD_DB6_PIN & LCD_DB6)
  336.  
  337.     tmp |= (1 << 2);
  338.  
  339. if(LCD_DB7_PIN & LCD_DB7)
  340.  
  341.     tmp |= (1 << 3);
  342.  
  343. return tmp;
  344.  
  345. }
  346.  
  347. //-------------------------------------------------------------------------------------------------
  348.  
  349. //
  350.  
  351. // Funkcja zapisu bajtu do wyœwietacza (bez rozróżnienia instrukcja/dane).
  352.  
  353. //
  354.  
  355. //-------------------------------------------------------------------------------------------------
  356.  
  357. void _LCD_Write(unsigned char dataToWrite)
  358.  
  359. {
  360.  
  361. LCD_DB4_DIR |= LCD_DB4;
  362.  
  363. LCD_DB5_DIR |= LCD_DB5;
  364.  
  365. LCD_DB6_DIR |= LCD_DB6;
  366.  
  367. LCD_DB7_DIR |= LCD_DB7;
  368.  
  369.  
  370.  
  371. LCD_RW_PORT &= ~LCD_RW;
  372.  
  373. LCD_E_PORT |= LCD_E;
  374.  
  375. _LCD_OutNibble(dataToWrite >> 4);
  376.  
  377. LCD_E_PORT &= ~LCD_E;
  378.  
  379. LCD_E_PORT |= LCD_E;
  380.  
  381. _LCD_OutNibble(dataToWrite);
  382.  
  383. LCD_E_PORT &= ~LCD_E;
  384.  
  385. while(LCD_ReadStatus()&0x80)Test();
  386.  
  387. }
  388.  
  389. //-------------------------------------------------------------------------------------------------
  390.  
  391. //
  392.  
  393. // Funkcja odczytu bajtu z wyœwietacza (bez rozróżnienia instrukcja/dane).
  394.  
  395. //
  396.  
  397. //-------------------------------------------------------------------------------------------------
  398.  
  399.  
  400.  
  401. unsigned char _LCD_Read(void)
  402.  
  403. {
  404.  
  405. unsigned char tmp = 0;
  406.  
  407. LCD_DB4_DIR &= ~LCD_DB4;
  408.  
  409. LCD_DB5_DIR &= ~LCD_DB5;
  410.  
  411. LCD_DB6_DIR &= ~LCD_DB6;
  412.  
  413. LCD_DB7_DIR &= ~LCD_DB7;
  414.  
  415.  
  416.  
  417. LCD_RW_PORT |= LCD_RW;
  418.  
  419. LCD_E_PORT |= LCD_E;
  420.  
  421. tmp |= (_LCD_InNibble() << 4);
  422.  
  423. LCD_E_PORT &= ~LCD_E;
  424.  
  425. LCD_E_PORT |= LCD_E;
  426.  
  427. tmp |= _LCD_InNibble();
  428.  
  429. LCD_E_PORT &= ~LCD_E;
  430.  
  431. return tmp;
  432.  
  433. }
  434.  
  435.  
  436.  
  437. //-------------------------------------------------------------------------------------------------
  438.  
  439. //
  440.  
  441. // Funkcja zapisu rozkazu do wyœwietlacza
  442.  
  443. //
  444.  
  445. //-------------------------------------------------------------------------------------------------
  446.  
  447. void LCD_WriteCommand(unsigned char commandToWrite)
  448.  
  449. {
  450.  
  451. LCD_RS_PORT &= ~LCD_RS;
  452.  
  453. _LCD_Write(commandToWrite);
  454.  
  455. }
  456.  
  457.  
  458.  
  459. //-------------------------------------------------------------------------------------------------
  460.  
  461. //
  462.  
  463. // Funkcja odczytu bajtu statusowego
  464.  
  465. //
  466.  
  467. //-------------------------------------------------------------------------------------------------
  468.  
  469. unsigned char LCD_ReadStatus(void)
  470.  
  471. {
  472.  
  473. LCD_RS_PORT &= ~LCD_RS;
  474.  
  475. return _LCD_Read();
  476.  
  477. }
  478.  
  479. //-------------------------------------------------------------------------------------------------
  480.  
  481. //
  482.  
  483. // Funkcja zapisu danych do pamięci wyœwietlacza
  484.  
  485. //
  486.  
  487. //-------------------------------------------------------------------------------------------------
  488.  
  489. void LCD_WriteData(unsigned char dataToWrite)
  490.  
  491. {
  492.  
  493. LCD_RS_PORT |= LCD_RS;
  494.  
  495. _LCD_Write(dataToWrite);
  496.  
  497. }
  498.  
  499. //-------------------------------------------------------------------------------------------------
  500.  
  501. //
  502.  
  503. // Funkcja odczytu danych z pamięci wyœwietlacza
  504.  
  505. //
  506.  
  507. //-------------------------------------------------------------------------------------------------
  508.  
  509. unsigned char LCD_ReadData(void)
  510.  
  511. {
  512.  
  513. LCD_RS_PORT |= LCD_RS;
  514.  
  515. return _LCD_Read();
  516.  
  517. }
  518.  
  519. //-------------------------------------------------------------------------------------------------
  520.  
  521. //
  522.  
  523. // Funkcja wyœwietlenia napisu na wyswietlaczu.
  524.  
  525. //
  526.  
  527. //-------------------------------------------------------------------------------------------------
  528.  
  529. void LCD_WriteText(char * text)
  530.  
  531. {
  532.  
  533. while(*text)
  534.  
  535.   LCD_WriteData(*text++);
  536.  
  537. }
  538.  
  539. //-------------------------------------------------------------------------------------------------
  540.  
  541. //
  542.  
  543. // Funkcja ustawienia współrzędnych ekranowych
  544.  
  545. //
  546.  
  547. //-------------------------------------------------------------------------------------------------
  548.  
  549. void LCD_GoTo(unsigned char x, unsigned char y)
  550.  
  551. {
  552.  
  553. LCD_WriteCommand(HD44780_DDRAM_SET | (x + (0x40 * y)));
  554.  
  555. }
  556.  
  557. //-------------------------------------------------------------------------------------------------
  558.  
  559. //
  560.  
  561. // Funkcja czyszczenia ekranu wyœwietlacza.
  562.  
  563. //
  564.  
  565. //-------------------------------------------------------------------------------------------------
  566.  
  567. void LCD_Clear(void)
  568.  
  569. {
  570.  
  571. LCD_WriteCommand(HD44780_CLEAR);
  572.  
  573. _delay_ms(2);
  574.  
  575. }
  576.  
  577. //-------------------------------------------------------------------------------------------------
  578.  
  579. //
  580.  
  581. // Funkcja przywrócenia poczštkowych współrzędnych wyœwietlacza.
  582.  
  583. //
  584.  
  585. //-------------------------------------------------------------------------------------------------
  586.  
  587. void LCD_Home(void)
  588.  
  589. {
  590.  
  591. LCD_WriteCommand(HD44780_HOME);
  592.  
  593. _delay_ms(2);
  594.  
  595. }
  596.  
  597. //-------------------------------------------------------------------------------------------------
  598.  
  599. //
  600.  
  601. // Procedura inicjalizacji kontrolera HD44780.
  602.  
  603. //
  604.  
  605. //-------------------------------------------------------------------------------------------------
  606.  
  607. void LCD_Initalize(void)
  608.  
  609. {
  610.  
  611. unsigned char i;
  612.  
  613. LCD_DB4_DIR |= LCD_DB4; // Konfiguracja kierunku pracy wyprowadzeń
  614.  
  615. LCD_DB5_DIR |= LCD_DB5; //
  616.  
  617. LCD_DB6_DIR |= LCD_DB6; //
  618.  
  619. LCD_DB7_DIR |= LCD_DB7; //
  620.  
  621. LCD_E_DIR   |= LCD_E;   //
  622.  
  623. LCD_RS_DIR  |= LCD_RS;  //
  624.  
  625. LCD_RW_DIR  |= LCD_RW;  //
  626.  
  627. _delay_ms(30); // oczekiwanie na ustalibizowanie się napiecia zasilajacego
  628.  
  629. LCD_RS_PORT &= ~LCD_RS; // wyzerowanie linii RS
  630.  
  631. LCD_E_PORT &= ~LCD_E;  // wyzerowanie linii E
  632.  
  633. LCD_RW_PORT &= ~LCD_RW;
  634.  
  635. for(i = 0; i < 3; i++) // trzykrotne powtórzenie bloku instrukcji
  636.  
  637.   {
  638.  
  639.   LCD_E_PORT |= LCD_E; //  E = 1
  640.  
  641.   _LCD_OutNibble(0x03); // tryb 8-bitowy
  642.  
  643.   LCD_E_PORT &= ~LCD_E; // E = 0
  644.  
  645.   _delay_ms(5); // czekaj 5ms
  646.  
  647.   }
  648.  
  649.  
  650.  
  651. LCD_E_PORT |= LCD_E; // E = 1
  652.  
  653. _LCD_OutNibble(0x02); // tryb 4-bitowy
  654.  
  655. LCD_E_PORT &= ~LCD_E; // E = 0
  656.  
  657. _delay_ms(1); // czekaj 1ms
  658.  
  659. LCD_WriteCommand(HD44780_FUNCTION_SET | HD44780_FONT5x7 | HD44780_TWO_LINE | HD44780_4_BIT); // interfejs 4-bity, 2-linie, znak 5x7
  660.  
  661. LCD_WriteCommand(HD44780_DISPLAY_ONOFF | HD44780_DISPLAY_OFF); // wyłšczenie wyswietlacza
  662.  
  663. LCD_WriteCommand(HD44780_CLEAR); // czyszczenie zawartosći pamieci DDRAM
  664.  
  665. LCD_WriteCommand(HD44780_ENTRY_MODE | HD44780_EM_SHIFT_CURSOR | HD44780_EM_INCREMENT);// inkrementaja adresu i przesuwanie kursora
  666.  
  667. LCD_WriteCommand(HD44780_DISPLAY_ONOFF | HD44780_DISPLAY_ON | HD44780_CURSOR_OFF | HD44780_CURSOR_NOBLINK); // włšcz LCD, bez kursora i mrugania
  668.  
  669. }
  670.  
  671.  
  672.  
  673. //-------------------------------------------------------------------------------------------------
  674.  
  675. //
  676.  
  677. // Koniec pliku HD44780.c
  678.  
  679. //
  680.  
  681. //-------------------------------------------------------------------------------------------------
  682.  
  683. int main(void)
  684. {   DDRB  |= _BV(0);
  685.     PORTB |= _BV(0);
  686.     PORTB ^= _BV(0);
  687.     Test();
  688.     LCD_Initalize();
  689.     LCD_Clear();
  690.     LCD_Home();
  691.         LCD_WriteData('m');
  692.         LCD_WriteData('a');
  693.         LCD_WriteData('r');
  694.         LCD_WriteData('s');
  695.         LCD_WriteData('e');
  696.         LCD_WriteData('e');
  697.         LCD_WriteData('l');
  698.     for(;;);
  699.        
  700.     return 0;
  701. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement