51 driver 1602 LCD Program

Source: Internet
Author: User
Tags clear screen
// 1602.h# include <reg52.h> // data type definition typedef unsigned char uchar; typedef unsigned int uint; // 1602 command definition // screen initialization # define screen_init 0x38 // screen setting: 16*2 rows, 5*7 lattice, 8-bit data interface // cursor and screen display settings # define show_screen 0x0c // screen display # define hide_screen 0x08 // the screen is not displayed # define show_cursor 0x02 // display the cursor # define flash_cursor 0x01 // blinking cursor // address pointer and scroll setting # define dptr_inc 0x06 // read/write a byte address pointer + 1, cursor + 1 # define dptr_dec 0x04 // read/write a byte address pointer-1, cursor-1 # define screen_scr 0x01 // write a character, full Screen left shift/right shift // ADDR settings # define addr_line1 0x80 // The first line # define addr_line2 0xc0 // The second line # define line1_end 0x10 # define line2_end 0x50 // busy status # define busy 0x80 // clear screen # define clr_scr 0x01 // clear screen // clear dptr # define clr_dptr 0x02 // Clear Data Pointer
// 1602.c# include <reg52.h> # include <intrins. h> # include "1602.h" // girls do not cry // 2012-10-25 // Ts = STS = Sw = 4 // The following is the port definition, modify sbit rs = P0 ^ 0 on your own; // select sbit RW = P0 ^ 1 for data/instructions; // read/write control sbit E = P0 ^ 2; // enable # define data P2 // data port P [0 ~ 7]-> data [0 ~ 7]/* function: microsecond delay parameter: US [uchar]: microsecond return :( none) */void delay_us (uchar US) {for (; US> 0; us --) _ NOP _ ();}/* function: 10 ms delay parameter: ms10 [uint]: 10 ms return :( none) */void delay_10ms (uint ms10) {uchar X, y, Z; while (ms10 --) {for (x = 5; x> 0; X --); For (y = 4; y> 0; y --); for (Z = 248; z> 0; Z --) ;}/ * function: Read the 1602 status parameter: (none) return :( none) Description: The highest bit is the busy status bit, 1 indicates busy */uchar read_state (void) {DATA = 0xff; RW = 1; RS = 0; E = 1; return data ;}/ * function: execute a command parameter: CMD [uchar]: Command return :( none) */void do_cmd (uchar cmd) {While (read_state () & busy) _ NOP _ (); RS = 0; RW = 0; data = cmd; E = 1; delay_us (50); E = 0; delay_us (50);}/* function: 1602 initialization parameter: (none) returned :( none) */void init_ LCD (void) {do_cmd (clr_scr); // clear screen do_cmd (screen_init); // set the display mode to do_cmd (show_screen | show_cursor/* | flash_cursor */); do_cmd (dptr_inc);}/* function: Write a byte to the memory parameter: ch [uchar]: displays the character ASCII code and returns: (none) */void write (uchar ch) {uchar ADDR; while (read _ State () & busy); // ADDR = read_state ()&~ Busy; If (ADDR = line1_end) do_cmd (addr_line2); else if (ADDR = line2_end) do_cmd (addr_line1); RW = 0; RS = 1; Data = CH; E = 1; delay_us (50); E = 0; delay_us (50);} void write_str (uchar line, uchar POs, uchar * Str) {uchar ADDR; uchar * PTR; if (line! = 1 & line! = 2) return; // only the first and second rows of IF (Pos> 16) return; // only 1st ~ 16 characters // Pos = 0 indicates that the write starts from the current position. Ignore the line if (POS) {ADDR = (line = 1? Addr_line1: addr_line2) + pos-1; do_cmd (ADDR);} For (PTR = STR; * PTR; PTR ++) {write (* PTR); delay_10ms (80 );}} # If 0 void main (void) {uchar STR [16] = {0}; uchar X; delay_10ms (10); init_ LCD (); write_str (1, 1, 012345671 "); delay_10ms (200); write_str (200, "abcdefghijk"); delay_10ms (); write_str (, "NV Hai bu Ku! "); Delay_10ms (200); do_cmd (clr_scr); do_cmd (show_screen | flash_cursor); write_str (1, 1," This is a very long string! "); For (x = 0; x <4; X ++) {do_cmd (hide_screen); delay_10ms (300); do_cmd (show_screen); delay_10ms (300 );} do_cmd (show_screen); delay_10ms (200); do_cmd (clr_scr); write_str (1,1, "screen will now scroll .... "); delay_10ms (300); do_cmd (dptr_inc | screen_scr); write_str (," screen scrolling "); do_cmd (dptr_dec | screen_scr); write_str, "screen scrolling"); do_cmd (dptr_inc); delay_10ms (200); do_cmd (clr_scr); fo R (x = 0x20; x <0xdf; X ++) {write (x); delay_10ms (80);} delay_10ms (200); do_cmd (clr_scr ); write_str (2, 1 ,"... what ?? "); For (;) {}# endif

Girl don't cry (QQ: 191035066) @ 20:02:49 @ http://www.cnblogs.com/nbsofer

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.