Advertisement
Guest User

Untitled

a guest
Apr 20th, 2018
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.92 KB | None | 0 0
  1. /*
  2.  * lcd_funcs.c
  3.  *
  4.  *  Created on: Apr 9, 2018
  5.  *      Author: Josh
  6.  */
  7.  
  8. #include "msp.h"
  9. #include "lcd_funcs.h"
  10. #include "delay.h"
  11.  
  12. void LCD_INIT(void) {
  13.     P3->DIR |= (BIT5 | BIT6 | BIT7);
  14.     P4->DIR = 0xFF;
  15.     P3->OUT = 0x00;
  16.     P4->OUT = 0x00;
  17.     //wait for the LCD to boot up
  18.     delay_ms(400, CURRENT_FREQ);
  19.  
  20.     LCD_CMND(FN_SET);
  21.     delay_ms(100, CURRENT_FREQ);
  22.     LCD_CMND(SET_ENTRY_MODE);
  23.     delay_ms(100, CURRENT_FREQ);
  24.     LCD_CMND(DISP_ON);
  25.     delay_ms(100, CURRENT_FREQ);
  26.  
  27.     clear_LCD();
  28.     home_LCD();
  29. }
  30.  
  31. void LCD_CMND(unsigned char cmnd) {
  32.     P3->OUT &= ~(BIT5 + BIT6 + BIT7);
  33.     P4->OUT = cmnd;
  34.     //write first nybble
  35.     P3->OUT |= EN;
  36.     delay_us(500, CURRENT_FREQ);
  37.     P3->OUT &= ~(EN);
  38.     //shift left 4
  39.     cmnd = cmnd << 4;
  40.     //write second nybble
  41.     P4->OUT = cmnd;
  42.     P3->OUT |= EN;
  43.     delay_us(500, CURRENT_FREQ);
  44.     P3->OUT &= ~(EN);
  45.     P4->OUT = 0x00;
  46. }
  47.  
  48. void clear_LCD(void) {
  49.     LCD_CMND(CLEAR_DISP);
  50.     delay_ms(100, CURRENT_FREQ);
  51. }
  52.  
  53. void home_LCD(void) {
  54.     LCD_CMND(HOME_CMND);
  55.     delay_ms(100, CURRENT_FREQ);
  56. }
  57.  
  58. //write a character to the screen at address addr with ascii value val
  59. void write_char_LCD(unsigned char addr, unsigned char val) {
  60.     addr = addr | 0x80;
  61.     LCD_CMND(addr);
  62.     delay_ms(50, CURRENT_FREQ);
  63.     P3->OUT &= ~(BIT5 + BIT6 + BIT7);
  64.     //write first nybble
  65.     P4->OUT = val;
  66.     P3->OUT |= (EN | RS);
  67.     delay_us(100, CURRENT_FREQ);
  68.     P3->OUT &= ~(EN);
  69.     //shift left 4
  70.     val = val << 4;
  71.     //write second nybble
  72.     P4->OUT = val;
  73.     P3->OUT |= (EN);
  74.     delay_us(100, CURRENT_FREQ);
  75.     P3->OUT &= ~(EN);
  76.     delay_us(100, CURRENT_FREQ);
  77.     P3->OUT &= ~(RS);
  78.     P4->OUT = 0x00;
  79.     delay_us(100, CURRENT_FREQ);
  80. }
  81.  
  82. void write_string(unsigned char* str,unsigned char addr){
  83.     while (*str != '\0'){
  84.         write_char_LCD(addr, *str);
  85.         str++;
  86.         addr++;
  87.     }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement