📄 lcd_4bit.lst
字号:
###############################################################################
# #
# 10/Jun/2008 15:10:59 #
# IAR ARM ANSI C/C++ Compiler V5.11.0.20622/W32 EVALUATION #
# Copyright 1999-2007 IAR Systems. All rights reserved. #
# #
# Cpu mode = thumb #
# Endian = little #
# Source file = E:\ELE\yten\pro\LCD_4bit.c #
# Command line = E:\ELE\yten\pro\LCD_4bit.c -lcN #
# E:\ELE\yten\pro\Debug\List\ -o #
# E:\ELE\yten\pro\Debug\Obj\ --debug --endian little #
# --cpu Cortex-M3 -e --fpu None --dlib_config #
# D:\IARARM\ARM\INC\DLib_Config_Normal.h -I #
# E:\ELE\yten\pro\ -I E:\ELE\yten\pro\..\LIBRARY\INC\ -I #
# D:\IARARM\ARM\INC\ -Oh #
# List file = E:\ELE\yten\pro\Debug\List\LCD_4bit.lst #
# Object file = E:\ELE\yten\pro\Debug\Obj\LCD_4bit.o #
# #
# #
###############################################################################
E:\ELE\yten\pro\LCD_4bit.c
1 /******************************************************************************/
2 /* LCD_4BIT.C: Functions for 2 line 16 character Text LCD (4-bit interface) */
3 /* connected on MCBSTM32 evaluation board */
4 /******************************************************************************/
5 /* This file is part of the uVision/ARM development tools. */
6 /* Copyright (c) 2005-2007 Keil Software. All rights reserved. */
7 /* This software may only be used under the terms of a valid, current, */
8 /* end user licence from KEIL for a compatible version of KEIL software */
9 /* development tools. Nothing else gives you the right to use this software. */
10 /******************************************************************************/
11
12 #include <stm32f10x_lib.h> /* STM32F10x Library Definitions */
13
14 /*********************** Hardware specific configuration **********************/
15
16 /*------------------------- Speed dependant settings -------------------------*/
17
18 /* If processor works on high frequency delay has to be increased, it can be
19 increased by factor 2^N by this constant */
20 #define DELAY_2N 0
21
22 /*------------------------- Text LCD size definitions ------------------------*/
23
24 #define LineLen 16 /* Width (in characters) */
25 #define NumLines 2 /* Hight (in lines) */
26
27 /*-------------------- LCD interface hardware definitions --------------------*/
28
29 /* PINS:
30 - DB4 = PC3
31 - DB5 = PC2
32 - DB6 = PC1
33 - DB7 = PC0
34 - E = PC10
35 - RW = PC11
36 - RS = PC12 */
37
38 #define PIN_E ( 1 << 10)
39 #define PIN_RW ( 1 << 11)
40 #define PIN_RS ( 1 << 12)
41 #define PINS_CTRL (0x07 << 10)
42 #define PINS_DATA (0x0F << 0)
43 #define PINS_ALL (PINS_CTRL | PINS_DATA)
44
45 const unsigned int SWAP_DATA[16] = { 0x0, 0x8, 0x4, 0xC, 0x2, 0xA, 0x6, 0xE,
46 0x1, 0x9, 0x5, 0xD, 0x3, 0xB, 0x7, 0xF};
47
48 /* Enable Clock for peripheral driving LCD pins */
49 #define LCD_CLOCK_EN RCC_APB2PeriphClockCmd (RCC_APB2Periph_GPIOC, ENABLE);
50
51 /* pin E setting to 0 or 1 */
52 #define LCD_E(x) GPIOC->ODR = (GPIOC->ODR & ~PIN_E) | (x ? PIN_E : 0);
53
54 /* pin RW setting to 0 or 1 */
55 #define LCD_RW(x) GPIOC->ODR = (GPIOC->ODR & ~PIN_RW) | (x ? PIN_RW : 0);
56
57 /* pin RS setting to 0 or 1 */
58 #define LCD_RS(x) GPIOC->ODR = (GPIOC->ODR & ~PIN_RS) | (x ? PIN_RS : 0);
59
60 /* Reading DATA pins */
61 #define LCD_DATA_IN SWAP_DATA[(((GPIOC->IDR & PINS_DATA) >> 0) & 0x0F)]
62
63 /* Writing value to DATA pins */
64 #define LCD_DATA_OUT(x) GPIOC->ODR = (GPIOC->ODR & ~PINS_DATA) | ((SWAP_DATA[x]) << 0);
65
66 /* Setting all pins to output mode */
67 #define LCD_ALL_DIR_OUT GPIOC->CRL = (GPIOC->CRL & 0xFFFF0000) | 0x00003333; \
68 GPIOC->CRH = (GPIOC->CRH & 0xFFF000FF) | 0x00033300;
69
70 /* Setting DATA pins to input mode */
71 #define LCD_DATA_DIR_IN GPIOC->CRL = (GPIOC->CRL & 0xFFFF0000) | 0x00004444;
72
73 /* Setting DATA pins to output mode */
74 #define LCD_DATA_DIR_OUT GPIOC->CRL = (GPIOC->CRL & 0xFFFF0000) | 0x00003333;
75
76 /******************************************************************************/
77
78
79 /* 8 user defined characters to be loaded into CGRAM (used for bargraph) */
80 const char UserFont[8][8] = {
81 { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
82 { 0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10 },
83 { 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18 },
84 { 0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C },
85 { 0x1E,0x1E,0x1E,0x1E,0x1E,0x1E,0x1E,0x1E },
86 { 0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F },
87 { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
88 { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }
89 };
90
91
92 /************************ Global function definitions *************************/
93
94
95 /*******************************************************************************
96 * Delay in while loop cycles *
97 * Parameter: cnt: number of while cycles to delay *
98 * Return: *
99 *******************************************************************************/
100
101 static void delay (int cnt)
102 {
103 cnt <<= DELAY_2N;
104
105 while (cnt--);
106 }
107
108
109 /*******************************************************************************
110 * Read status of LCD controller *
111 * Parameter: none *
112 * Return: Status byte contains busy flag and address pointer *
113 *******************************************************************************/
114
115 static unsigned char lcd_read_status (void)
116 {
117 unsigned char status;
118
119 LCD_DATA_DIR_IN
120 LCD_RS(0)
121 LCD_RW(1)
122 delay(10);
123 LCD_E(1)
124 delay(10);
125 status = LCD_DATA_IN << 4;
126 LCD_E(0)
127 delay(10);
128 LCD_E(1)
129 delay(10);
130 status |= LCD_DATA_IN;
131 LCD_E(0)
132 LCD_DATA_DIR_OUT
133 return (status);
134 }
135
136
137 /*******************************************************************************
138 * Wait until LCD controller busy flag is 0 *
139 * Parameter: *
140 * Return: Status byte of LCD controller (busy + address) *
141 *******************************************************************************/
142
143 static unsigned char wait_while_busy (void)
144 {
145 unsigned char status;
146
147 do {
148 status = lcd_read_status();
149 } while (status & 0x80); /* Wait for busy flag */
150
151 return (status);
152 }
153
154
155 /*******************************************************************************
156 * Write 4-bits to LCD controller *
157 * Parameter: c: command to be written *
158 * Return: *
159 *******************************************************************************/
160
161 void lcd_write_4bit (unsigned char c)
162 {
163 LCD_RW(0)
164 LCD_E(1)
165 LCD_DATA_OUT(c&0x0F)
166 delay(10);
167 LCD_E(0)
168 delay(10);
169 }
170
171
172 /*******************************************************************************
173 * Write command to LCD controller *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -