void put2lcd(char c, char type) { char c_lo, c_hi; c_hi = (c & 0xF0) >> 2; /* Upper 4 bits of c */ c_lo = (c & 0x0F) << 2; /* Lower 4 bits of c */ if (type == DATA) LCD_DAT |= LCD_RS; /* select LCD data register */ else LCD_DAT &= (~LCD_RS); /* select LCD command register */ if (type == DATA) LCD_DAT = c_hi|LCD_E|LCD_RS; /* output upper 4 bits, E, RS high */ else LCD_DAT = c_hi|LCD_E; /* output upper 4 bits, E, RS low */ LCD_DAT |= LCD_E; /* pull E signal to high */ __asm(nop); /* Lengthen E */ __asm(nop); __asm(nop); LCD_DAT &= (~LCD_E); /* pull E to low */ if (type == DATA) LCD_DAT = c_lo|LCD_E|LCD_RS; /* output lower 4 bits, E, RS high */ else LCD_DAT = c_lo|LCD_E; /* output lower 4 bits, E, RS low */ LCD_DAT |= LCD_E; /* pull E to high */ __asm(nop); /* Lengthen E */ __asm(nop); __asm(nop); LCD_DAT &= (~LCD_E); /* pull E to low */ delay_50us(1); /* Wait for command to execute */ }
void puts2lcd (char *ptr) { while (*ptr) { /* While character to send */ put2lcd(*ptr,DATA); /* Write data to LCD */ delay_50us(1); /* Wait for data to be written */ ptr++; /* Go to next character */ } }
/* Write a byte to the LCD in 4 bit mode */ void lcd_write(unsigned char c) { if(c & 0x80) LCD_D7=1; else LCD_D7=0; if(c & 0x40) LCD_D6=1; else LCD_D6=0; if(c & 0x20) LCD_D5=1; else LCD_D5=0; if(c & 0x10) LCD_D4=1; else LCD_D4=0; LCD_STROBE(); if(c & 0x08) LCD_D7=1; else LCD_D7=0; if(c & 0x04) LCD_D6=1; else LCD_D6=0; if(c & 0x02) LCD_D5=1; else LCD_D5=0; if(c & 0x01) LCD_D4=1; else LCD_D4=0; LCD_STROBE(); delay_50us(); }
/* Initialise the LCD - put into 4 bit mode */ void lcd_init(void) { LCD_RS = 0; LCD_EN = 0; delay_100ms(); // Wait 100ms after power applied LCD_D4 = 1; LCD_D5 = 1; LCD_STROBE(); delay_2ms(); // Wait 6ms delay_2ms(); delay_2ms(); LCD_STROBE(); delay_2ms(); LCD_STROBE(); delay_2ms(); LCD_D4 = 0; // Set 4 bit mode LCD_STROBE(); delay_50us(); lcd_cmd(0b00101000); // Set interface length 4-bit, 2-line, 5x8 dots display lcd_cmd(0b00001100); // Display On, Cursor OFF, Cursor Blink OFF lcd_cmd(0b00000110); // Set entry Mode lcd_clear(); // Clear screen }
/*Delay 2ms */ void delay_2ms(void) { unsigned char cnt1; for (cnt1=0;cnt1<=34;cnt1++) delay_50us(); }
void delay_1ms(int n) { for (;n>0;n--) delay_50us(200); }
void Led30_Stop(void) //停止向后传输数据 { GPIOB->BRR = GPIO_Pin_1; delay_50us(); delay_50us(); }