SHARE
TWEET

cs120 lab11 part 2

a guest May 23rd, 2019 79 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <avr/io.h>
  2. #include <avr/interrupt.h>
  3. #include <stdio.h>
  4. #include "io.h"
  5.  
  6. #define SET_BIT(p,i) ((p) |= (1 << (i)))
  7. #define CLR_BIT(p,i) ((p) &= ~(1 << (i)))
  8. #define GET_BIT(p,i) ((p) & (1 << (i)))
  9.          
  10. /*-------------------------------------------------------------------------*/
  11.  
  12. #define DATA_BUS PORTD      // port connected to pins 7-14 of LCD display
  13. #define CONTROL_BUS PORTA   // port connected to pins 4 and 6 of LCD disp.
  14. #define RS 0            // pin number of uC connected to pin 4 of LCD disp.
  15. #define E 1         // pin number of uC connected to pin 6 of LCD disp.
  16.  
  17. /*-------------------------------------------------------------------------*/
  18.  
  19. void LCD_ClearScreen(void) {
  20.    LCD_WriteCommand(0x01);
  21. }
  22.  
  23. void LCD_init(void) {
  24.  
  25.     //wait for 100 ms.
  26.     delay_ms(100);
  27.     LCD_WriteCommand(0x38);
  28.     LCD_WriteCommand(0x06);
  29.     LCD_WriteCommand(0x0f);
  30.     LCD_WriteCommand(0x01);
  31.     delay_ms(10);                        
  32. }
  33.  
  34. void LCD_WriteCommand (unsigned char Command) {
  35.    CLR_BIT(CONTROL_BUS,RS);
  36.    DATA_BUS = Command;
  37.    SET_BIT(CONTROL_BUS,E);
  38.    asm("nop");
  39.    CLR_BIT(CONTROL_BUS,E);
  40.    delay_ms(2); // ClearScreen requires 1.52ms to execute
  41. }
  42.  
  43. void LCD_WriteData(unsigned char Data) {
  44.    SET_BIT(CONTROL_BUS,RS);
  45.    DATA_BUS = Data;
  46.    SET_BIT(CONTROL_BUS,E);
  47.    asm("nop");
  48.    CLR_BIT(CONTROL_BUS,E);
  49.    delay_ms(1);
  50. }
  51.  
  52. void LCD_DisplayString( unsigned char column, const unsigned char* string) {
  53.    LCD_ClearScreen();
  54.    unsigned char c = column;
  55.    while(*string) {
  56.       LCD_Cursor(c++);
  57.       LCD_WriteData(*string++);
  58.    }
  59. }
  60.  
  61. void LCD_Cursor(unsigned char column) {
  62.    if ( column < 17 ) { // 16x1 LCD: column < 9
  63.                         // 16x2 LCD: column < 17
  64.       LCD_WriteCommand(0x80 + column - 1);
  65.    } else {
  66.       LCD_WriteCommand(0xB8 + column - 9);  // 16x1 LCD: column - 1
  67.                                             // 16x2 LCD: column - 9
  68.    }
  69. }
  70.  
  71. void delay_ms(int miliSec) //for 8 Mhz crystal
  72.  
  73. {
  74.     int i,j;
  75.     for(i=0;i<miliSec;i++)
  76.     for(j=0;j<775;j++)
  77.   {
  78.    asm("nop");
  79.   }
  80. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top