کد:
#ifndef __lcd4bit_H
#define __lcd4bit_H
// Define LCD-16x2 PinIO Interface Mask Bit
#define LCD_EN_PIN GPIO_Pin_2
#define LCD_EN_PORT GPIOB
#define RCC_APB2Periph_GPIO_EN RCC_APB2Periph_GPIOB
#define LCD_RW_PIN GPIO_Pin_1
#define LCD_RW_PORT GPIOB
#define RCC_APB2Periph_GPIO_RW RCC_APB2Periph_GPIOB
#define LCD_RS_PIN GPIO_Pin_0
#define LCD_RS_PORT GPIOB
#define RCC_APB2Periph_GPIO_RS RCC_APB2Periph_GPIOB
#define LCD_D4_PIN GPIO_Pin_4
#define LCD_D5_PIN GPIO_Pin_5
#define LCD_D6_PIN GPIO_Pin_6
#define LCD_D7_PIN GPIO_Pin_7
#define LCD_DATA_PORT GPIOB
#define RCC_APB2Periph_GPIO_DATA RCC_APB2Periph_GPIOB
#define LCD_EN_HI() GPIO_WriteBit(LCD_EN_PORT,LCD_EN_PIN,Bit_SET)
#define LCD_EN_LO() GPIO_WriteBit(LCD_EN_PORT,LCD_EN_PIN,Bit_RESET)
#define LCD_RW_HI() GPIO_WriteBit(LCD_RW_PORT,LCD_RW_PIN,Bit_SET)
#define LCD_RW_LO() GPIO_WriteBit(LCD_RW_PORT,LCD_RW_PIN,Bit_RESET)
#define LCD_RS_HI() GPIO_WriteBit(LCD_RS_PORT,LCD_RS_PIN,Bit_SET)
#define LCD_RS_LO() GPIO_WriteBit(LCD_RS_PORT,LCD_RS_PIN,Bit_RESET)
#define LCD_D4_HI() GPIO_WriteBit(LCD_DATA_PORT,LCD_D4_PIN,Bit_SET)
#define LCD_D4_LO() GPIO_WriteBit(LCD_DATA_PORT,LCD_D4_PIN,Bit_RESET)
#define LCD_D5_HI() GPIO_WriteBit(LCD_DATA_PORT,LCD_D5_PIN,Bit_SET)
#define LCD_D5_LO() GPIO_WriteBit(LCD_DATA_PORT,LCD_D5_PIN,Bit_RESET)
#define LCD_D6_HI() GPIO_WriteBit(LCD_DATA_PORT,LCD_D6_PIN,Bit_SET)
#define LCD_D6_LO() GPIO_WriteBit(LCD_DATA_PORT,LCD_D6_PIN,Bit_RESET)
#define LCD_D7_HI() GPIO_WriteBit(LCD_DATA_PORT,LCD_D7_PIN,Bit_SET)
#define LCD_D7_LO() GPIO_WriteBit(LCD_DATA_PORT,LCD_D7_PIN,Bit_RESET)
#define lcd_clear() lcd_write_cmd(0x01) // Clear Display
#define lcd_cursor_home() lcd_write_cmd(0x02) // Set Cursor = 0
#define lcd_display_on() lcd_write_cmd(0x0E) // LCD Display Enable
#define lcd_display_off() lcd_write_cmd(0x08) // LCD Display Disable
#define lcd_cursor_blink() lcd_write_cmd(0x0F) // Set Cursor = Blink
#define lcd_cursor_on() lcd_write_cmd(0x0E) // Enable LCD Cursor
#define lcd_cursor_off() lcd_write_cmd(0x0C) // Disable LCD Cursor
#define lcd_cursor_left() lcd_write_cmd(0x10) // Shift Left Cursor
#define lcd_cursor_right() lcd_write_cmd(0x14) // Shift Right Cursor
#define lcd_display_sleft() lcd_write_cmd(0x18) // Shift Left Display
#define lcd_display_sright() lcd_write_cmd(0x1C) // Shift Right Display
/* Exported macro ------------------------------------------------------------*/
/* Exported functions ------------------------------------------------------- */
void lcd_out_data4(unsigned char val);
void lcd_write_byte(unsigned char val);
void lcd_write_cmd(unsigned char val);
void lcd_putchar(unsigned char c);
void lcd_init(void);
void set_cursor(unsigned char column, unsigned char line);
void lcd_putsf(char* str);
void lcd_puts(int num);
char busy_lcd(void);
void enable_lcd(void);
void lcd_bargraph (int value, int size);
void lcd_bargraphXY (int pos_x, int pos_y, int value);
#endif /* __lcd4bit_H */
--------------------
کد:
#include "lcd4bit.h"
#include "stm32f10x.h"
#include "delay.h"
#define DELAY_2N 0
GPIO_InitTypeDef GPIO_InitStructure;
void inita (void)
{
delay_init(72);
}
/* 8 user defined characters to be loaded into CGRAM (used for bargraph) */
const char UserFont[8][8] =
{
{ 0x11,0x0A,0x04,0x1B,0x11,0x11,0x11,0x0E },
{ 0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10 },
{ 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18 },
{ 0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C },
{ 0x1E,0x1E,0x1E,0x1E,0x1E,0x1E,0x1E,0x1E },
{ 0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F },
{ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
{ 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }
};
/******************************************************************************/
/****************************/
/* Strobe 4-Bit Data to LCD */
/****************************/
void lcd_out_data4(unsigned char val)
{
if((val&0x01)==0x01) // Bit[0]
{
LCD_D4_HI();
}
else
{
LCD_D4_LO();
}
if((val&0x02)==0x02) // Bit[1]
{
LCD_D5_HI();
}
else
{
LCD_D5_LO();
}
if((val&0x04)==0x04) // Bit[2]
{
LCD_D6_HI();
}
else
{
LCD_D6_LO();
}
if((val&0x08)==0x08) // Bit[3]
{
LCD_D7_HI();
}
else
{
LCD_D7_LO();
}
}
/****************************/
/* Write Data 1 Byte to LCD */
/****************************/
void lcd_write_byte(unsigned char val)
{
lcd_out_data4((val>>4)&0x0F); // Strobe 4-Bit High-Nibble to LCD
enable_lcd(); // Enable Pulse
lcd_out_data4(val&0x0F); // Strobe 4-Bit Low-Nibble to LCD
enable_lcd(); // Enable Pulse
while(busy_lcd()); // Wait LCD Execute Complete
}
/****************************/
/* Write Instruction to LCD */
/****************************/
void lcd_write_cmd(unsigned char val)
{
LCD_RS_LO(); // RS = 0 = Instruction Select
lcd_write_byte(val); // Strobe Command Byte
}
/****************************/
/* Write Data(ASCII) to LCD */
/****************************/
void lcd_putchar(unsigned char c)
{
LCD_RS_HI(); // RS = 1 = Data Select
lcd_write_byte(c); // Strobe 1 Byte to LCD
}
/*******************************/
/* Initial 4-Bit LCD Interface */
/*******************************/
void lcd_init(void)
{
int i;
char const *p;
/* Configure IO connected to LCD16X2 */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIO_EN, ENABLE);
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = LCD_EN_PIN;
GPIO_Init(LCD_EN_PORT, &GPIO_InitStructure);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIO_RW, ENABLE);
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = LCD_RW_PIN;
GPIO_Init(LCD_RW_PORT, &GPIO_InitStructure);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIO_RS, ENABLE);
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = LCD_RS_PIN;
GPIO_Init(LCD_RS_PORT, &GPIO_InitStructure);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIO_DATA, ENABLE);
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = LCD_D4_PIN |
LCD_D5_PIN |
LCD_D6_PIN |
LCD_D7_PIN;
GPIO_Init(LCD_DATA_PORT, &GPIO_InitStructure);
delay_init(72);
LCD_D4_HI();
LCD_D5_HI();
LCD_D6_LO();
LCD_D7_LO();
delay_ms(15); // Power-On Delay (15 mS)
LCD_D4_HI();
LCD_D5_HI();
LCD_D6_LO();
LCD_D7_LO();
enable_lcd(); // Enable Pulse
delay_us(4100); // Delay 4.1mS
LCD_D4_HI();
LCD_D5_HI();
LCD_D6_LO();
LCD_D7_LO();
enable_lcd(); // Enable Pulse
delay_us(100); // delay 100uS
LCD_D4_HI();
LCD_D5_HI();
LCD_D6_LO();
LCD_D7_LO();
enable_lcd(); // Enable Pulse
while(busy_lcd()); // Wait LCD Execute Complete
LCD_D4_LO();
LCD_D5_HI();
LCD_D6_LO();
LCD_D7_LO();
enable_lcd(); // Enable Pulse
while(busy_lcd()); // Wait LCD Execute Complete
lcd_write_cmd(0x28); // Function Set (DL=0 4-Bit,N=1 2 Line,F=0 5X7)
lcd_write_cmd(0x0C); // Display on/off Control (Entry Display,Cursor off,Cursor not Blink)
lcd_write_cmd(0x06); // Entry Mode Set (I/D=1 Increment,S=0 Cursor Shift)
//lcd_write_control(0x01); // Clear Display (Clear Display,Set DD RAM Address=0)
//delay(15000); // Wait Command Ready
/* Load user-specific characters into CGRAM */
lcd_write_cmd(0x40); /* Set CGRAM address counter to 0 */
p = &UserFont[0][0];
for (i = 0; i < sizeof(UserFont); i++, p++)
lcd_putchar(*p);
lcd_write_cmd(0x80); /* Set DDRAM address counter to 0 */
}
/*******************************************************************************
* Set cursor position on LCD display *
* Parameter: column: column position *
* line: line position *
* Return: *
*******************************************************************************/
void set_cursor(unsigned char line, unsigned char column)
{
unsigned char address;
column--;line--;
address = (line * 40) + column;
address = 0x80 + (address & 0x7F);
lcd_write_cmd(address); /* Set DDRAM address counter to 0 */
}
/************************************/
/* Print Display Data(ASCII) to LCD */
/************************************/
void lcd_putsf(char* str)
{
int i;
for (i=0;i<16 && str[i]!=0;i++) // 16 Character Print
{
lcd_putchar(str[i]); // Print Byte to LCD
}
}
////////////////////////////////////////////////////
void lcd_puts(int num)
{
int i,j;
int p,f=0;
char ch[5];
for(i=0;i<5;i++)
{
p=1;
for(j=4-i;j>0;j--)
p = p*10;
ch[i] = (num/p);
if (num>=p && !f){
f=1;
}
num =num - ch[i] *p ;
ch[i] = ch[i] +48;
if(f) lcd_putchar(ch[i]);
}
}
/////////////////////////////////////////////////////
/******************/
/* Wait LCD Ready */
/******************/
char busy_lcd(void)
{
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // Config D7 = Read
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Pin = LCD_D7_PIN;
GPIO_Init(LCD_DATA_PORT, &GPIO_InitStructure);
LCD_RS_LO(); // Instruction Select
LCD_RW_HI(); // Read Direction
LCD_EN_HI(); // Start Read Busy
delay_us(100); // Delay Before Read
if (GPIO_ReadInputDataBit(LCD_DATA_PORT, LCD_D7_PIN) == Bit_SET)
{
LCD_EN_LO(); // Disable Read
LCD_RW_LO(); // Default = Write Direction
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // Config D7 = Write
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = LCD_D7_PIN;
GPIO_Init(LCD_DATA_PORT, &GPIO_InitStructure);
return 1; // LCD Busy Status
}
else
{
LCD_EN_LO(); // Disable Read
LCD_RW_LO(); // Default = Write Direction
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // Config D7 = Write
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Pin = LCD_D7_PIN;
GPIO_Init(LCD_DATA_PORT, &GPIO_InitStructure);
return 0; // LCD Ready Status
}
}
/***********************/
/* Enable Pulse to LCD */
/***********************/
void enable_lcd(void) // Enable Pulse
{
delay_init(72);
LCD_EN_HI(); // Enable ON
delay_us(50);
LCD_EN_LO(); // Enable OFF
}
/*******************************************************************************
* Print a bargraph to LCD display *
* Parameter: val: value 0..100 % *
* size: size of bargraph 1..16 *
* Return: *
*******************************************************************************/
void lcd_bargraph (int value, int size)
{
int i;
value = value * size / 20; /* Display matrix 5 x 8 pixels */
for (i = 0; i < size; i++)
{
if (value > 5) {
lcd_putchar (0x05);
value -= 5;
}
else {
lcd_putchar (value);
break;
}
}
}
/*******************************************************************************
* Display bargraph on LCD display *
* Parameter: pos_x: horizontal position of bargraph start *
* pos_y: vertical position of bargraph *
* value: size of bargraph active field (in pixels) *
* Return: *
*******************************************************************************/
void lcd_bargraphXY (int pos_x, int pos_y, int value)
{
int i;
set_cursor(pos_x, pos_y);
for (i = 0; i < 16; i++)
{
if (value > 5) {
lcd_putchar (0x05);
value -= 5;
} else {
lcd_putchar (value);
while (i++ < 16) lcd_putchar (0);
}
}
}
/******************************************************************************/
--------------------