Advertisement
Guest User

Untitled

a guest
Jun 24th, 2017
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.37 KB | None | 0 0
  1.  
  2.  
  3. /*
  4. ** lab19.c
  5. **
  6. ** Dislay a scrolling message to the LCD screen (assumed to be
  7. ** plugged in to port A (JA) and port C (JB bottom).
  8. **
  9. ** Program is incomplete - will need to be modified based
  10. ** on information learned in lab 19.
  11. */
  12.  
  13. #include <avr/io.h>
  14. #include <avr/interrupt.h>
  15. #include "lcd.h"
  16. /* Function prototypes */
  17.  
  18.  
  19. /* Print Ready on start
  20. */
  21. char message[30] = "Ready, Connor Walker s42355362";
  22.  
  23. void setLcd(void)
  24. {
  25.     uint8_t i;
  26.    
  27.     /* Wait 20ms = 20000us */
  28.     delay(20000);
  29.  
  30.     /* Make port bits connected to LCD control lines be outputs.
  31.     ** RS bit is port C, bit 4
  32.     ** R/W bit is port C, bit 5
  33.     ** E bit is port C, bit 6
  34.     */
  35.     DDRG = 0x70;    /* FIX THIS VALUE */
  36.  
  37.     /* Send "Function Set" instruction to specify the
  38.     ** LCD panel configuration: 2 lines, 8 bit 5x8 chars
  39.     */
  40.     write_lcd_instruction(0x38);    /* FIX THIS VALUE */
  41.  
  42.     /* Turn the display on */
  43.     write_lcd_instruction(0x0C);    /* FIX THIS VALUE */
  44.  
  45.     /* Clear the display */
  46.     write_lcd_instruction(0x01);    /* FIX THIS VALUE */
  47.  
  48.     /* Set data address to 0 (top left) */
  49.     write_lcd_instruction(0x80);    /* FIX THIS VALUE */
  50.  
  51.     /* Output the string - we iterate over the string
  52.     ** until we reach the null character at the end. */
  53.     for(i=0; message[i] != 0; i++) {
  54.         write_lcd_data(message[i]);
  55.     }
  56.  
  57.     /* Repeatedly scroll the message left - with a half
  58.     ** second delay between each movement. We implement
  59.     ** our half second delay as 10 x 50000us delays.
  60.     */
  61.     while(PINA == 0x00) {
  62.         for(i=0; i< 10; i++) {
  63.             delay(50000);
  64.         }
  65.         /* Issue command to scroll message left */
  66.         write_lcd_instruction(0x18);    /* FIX THIS VALUE */
  67.     }
  68. }
  69.  
  70. /* Delay for at least us microseconds.
  71. ** us is assumed to be greater than 0. max value is 65535
  72. ** We use timer/counter 3 to do this. We do not consider
  73. ** the overhead of calling the function or setting up
  74. ** the timer so if this function is called with very small
  75. ** values of us, the delay will not be that accurate.
  76. */
  77. void delay(uint16_t us)
  78. {
  79.     /* Set up timer/counter 3. We will make the timer
  80.     ** counter every microsecond (clk/8) and just set
  81.     ** the target (output compare A) to be the given
  82.     ** number of microseconds.
  83.     ** We assume the timer is off (not counting) at
  84.     ** the start of this function. We also assume
  85.     ** that TCCR3A has its default value (0).
  86.     */
  87.     /* Set target */
  88.     OCR3A = us;
  89.     /* Set current count */
  90.     TCNT3 = 0;
  91.     /* Ensure the output compare A flag is clear. To do this
  92.     ** we write a 1 to the OCF3A bit of ETIFR
  93.     */
  94.     ETIFR |= (1<<OCF3A);
  95.  
  96.     /* Set the timer going - normal mode, divide by 8 */
  97.     TCCR3B = 0x02;
  98.  
  99.     /* Wait until the OCF3A bit of ETIFR is 1 */
  100.     while((ETIFR & (1 << OCF3A)) == 0) {
  101.         /* Do nothing */
  102.     }
  103.  
  104.     /* We've reached the target - we stop the timer */
  105.     TCCR3B = 0;
  106.     return;
  107. }
  108.  
  109. void write_lcd_instruction(uint8_t instn)
  110. {
  111.     wait_if_lcd_busy();
  112.     /* Write 0 to RS, R/W and E bits */
  113.     PORTG = 0;
  114.     /* Make port A an output port */
  115.     DDRC = 0xFF;
  116.     /* Write 1 to E bit */
  117.     PORTG |= (1<<6);
  118.     /* Write instn to port A */
  119.     PORTC = instn;
  120.     /* Delay - one more clock cycle, to ensure we have
  121.     ** a write pulse width of at least 220ns. (Two clock
  122.     ** cycles are 250ns.) */
  123.     asm("nop"::);
  124.     /* Write 0 to E bit */
  125.     PORTG = 0;
  126.     /* Make port A an input port */
  127.     DDRC = 0;
  128. }
  129.  
  130. void write_lcd_data(uint8_t data)
  131. {
  132.     wait_if_lcd_busy();
  133.     /* Write 0 to E bit, 1 to RS and 0 to R/W */
  134.     PORTG = 0x10;
  135.     /* Make port A be an output port */
  136.     DDRC = 0xFF;
  137.     /* Write 1 to E bit */
  138.     PORTG |= (1<<6);
  139.     /* Write data to port A */
  140.     PORTC = data;
  141.     /* Delay one more clock cycle */
  142.     asm("nop"::);
  143.     /* Write 0 to E bit */
  144.     PORTG = 0x10;
  145.     /* Write 0 to RS bit */
  146.     PORTG = 0;
  147.     /* Make port A be an input port */
  148.     DDRC = 0;
  149. }
  150.  
  151. void writeToLcd(char *writeLcd){   
  152.     for (int index=0; writeLcd[index] !=0; index++) {
  153.         write_lcd_data(writeLcd[index]);
  154.     }
  155. }
  156.  
  157.  
  158.  
  159.  
  160.  
  161. uint8_t read_lcd_status(void)
  162. {
  163.     uint8_t data;
  164.     /* Write 0 to E bit, 1 to R/W, 0 to RS,
  165.     ** i.e. 00100000
  166.     */
  167.     PORTG = 0x20;
  168.     /* NOTE, port A is an input */
  169.     /* Write 1 to E bit */
  170.     PORTG |= (1<<6);
  171.     /* Delay two clock cycles */
  172.     asm("nop"::);
  173.     asm("nop"::);
  174.     /* Read the data */
  175.     data = PINC;
  176.     /* Write 0 to E bit */
  177.     PORTG = 0x20;
  178.     /* Write 0 to R/W bit */
  179.     PORTG = 0;
  180.  
  181.     return data;
  182. }
  183.  
  184. void wait_if_lcd_busy(void)
  185. {
  186.     /* While bit 7 of the LCD status is 1 - just wait */
  187.     while(read_lcd_status() & 0x80) {
  188.         /* Do nothing */
  189.     }
  190. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement