Advertisement
Guest User

Untitled

a guest
Sep 20th, 2014
255
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.11 KB | None | 0 0
  1. #include "stm32f10x.h"
  2. #include "stm32f10x_rcc.h"
  3. #include "stm32f10x_gpio.h"
  4. #include "stm32f10x_tim.h"
  5.  
  6. //PC 0 - PC3 for the 4 data lines - create some nice defines
  7. #define LCD_D4 GPIO_Pin_0
  8. #define LCD_D5 GPIO_Pin_1
  9. #define LCD_D6 GPIO_Pin_2
  10. #define LCD_D7 GPIO_Pin_3
  11. //PC4 for the Enable strobe, PC5 for RS(H for data, L for instruction)
  12. #define LCD_EN GPIO_Pin_4
  13. #define LCD_RS GPIO_Pin_5
  14.  
  15. #define LCD_DATA 1
  16. #define LCD_INSTRUCTION 0
  17. void lcd_command(unsigned char command);
  18. void lcd_putchar(unsigned char c);
  19. void delay_microsec(register int n);
  20. void lcd_putstrings(char*p);
  21. void delay_millisec(register unsigned int n);
  22. static void set_RS(int mode); //mode is either LCD_DATA or LCD_INSTRUCTION
  23. static void pulse_EN(void);
  24. static void lcd_init_8_bit(unsigned char command); //the first few commands
  25. //of initialisation, are still
  26. //in pseudo 8bit data mode
  27. int stillBouncing = 10;
  28. int main() {
  29. RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
  30. RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE);
  31. GPIO_InitTypeDef GPIO_InitStructure;
  32. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  33. GPIO_InitStructure.GPIO_Pin = LCD_D7 | LCD_D6 | LCD_D5 | LCD_D4 |
  34. LCD_EN | LCD_RS;
  35. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  36. GPIO_Init(GPIOC, &GPIO_InitStructure);
  37. GPIO_WriteBit(GPIOC, LCD_EN, Bit_RESET);
  38. delay_microsec(15000); //delay for >15msec second after power on
  39.  
  40. lcd_init_8_bit(0x30); //we are in "8bit" mode
  41. delay_microsec(4100); //4.1msec delay
  42. lcd_init_8_bit(0x30); //- but the bottom 4 bits are ignored
  43. delay_microsec(100); //100usec delay
  44. lcd_init_8_bit(0x30);
  45. lcd_init_8_bit(0x20);
  46. lcd_command(0x28); //we are now in 4bit mode, dual line
  47. lcd_command(0x08); //display off
  48. lcd_command(0x01); //display clear
  49. delay_microsec(2000); //needs a 2msec delay !!
  50. lcd_command(0x06); //cursor increments
  51. lcd_command(0x0f); //display on, cursor(blinks) on
  52.  
  53.  
  54. lcd_putstrings("Abby is a cutie :3");
  55.  
  56.  
  57. while (1) {
  58. }
  59. }
  60. void delay_millisec(register unsigned int n) {
  61. if (n !=0){
  62. TIM7->PSC = 23999; // Set prescaler to 24,000 (PSC + 1)
  63. TIM7->ARR = n; // n = 1 gives 2msec delay rest are ok
  64. TIM7->CNT = 0;
  65. TIM7->EGR = TIM_EGR_UG; // copy values into actual registers!
  66. TIM7->CR1 |= (TIM_CR1_OPM | TIM_CR1_CEN);
  67. while (TIM7->CR1 & TIM_CR1_CEN){// wait for it to switch off
  68. delay_microsec(1000);
  69. if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0)==0){
  70. stillBouncing = stillBouncing-1;
  71. if (stillBouncing == 0){
  72. return 1;
  73. }
  74. }
  75. else {
  76. stillBouncing = 10;
  77. }
  78. }
  79. return 0;
  80. }
  81.  
  82. else{
  83. while(1){
  84. delay_microsec(1000);
  85.  
  86.  
  87. if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0)==0) {
  88. stillBouncing = stillBouncing-1;
  89. if (stillBouncing == 0){
  90. return 1;
  91. }
  92. }
  93.  
  94. else {
  95. stillBouncing = 10;
  96. }
  97. }
  98. }
  99.  
  100. void lcd_putstrings(char*p){
  101. int i=0;
  102. while (p[i]!='\0'){
  103. lcd_putchar(p[i]);
  104. i++;
  105. }
  106. }
  107.  
  108. static void set_RS(int mode) {
  109. GPIO_WriteBit(GPIOC, LCD_RS, mode);
  110. }
  111.  
  112. static void pulse_EN(void) {
  113. GPIO_WriteBit(GPIOC, LCD_EN, Bit_SET);
  114. delay_microsec(1); // enable pulse must be >450ns
  115. GPIO_WriteBit(GPIOC, LCD_EN, Bit_RESET);
  116. delay_microsec(1);
  117. }
  118.  
  119. static void lcd_init_8_bit(unsigned char command) {
  120. set_RS(LCD_INSTRUCTION);
  121. GPIO_WriteBit(GPIOC, LCD_D4, (command>>4) & 0x01); //bottom 4 bits
  122. GPIO_WriteBit(GPIOC, LCD_D5, (command>>5) & 0x01); //are ignored
  123. GPIO_WriteBit(GPIOC, LCD_D6, (command>>6) & 0x01);
  124. GPIO_WriteBit(GPIOC, LCD_D7, (command>>7) & 0x01);
  125. pulse_EN();
  126. delay_microsec(37); //let it work on the data
  127. }
  128.  
  129. void lcd_command(unsigned char command) {
  130. set_RS(LCD_INSTRUCTION);
  131. GPIO_WriteBit(GPIOC, LCD_D4, (command>>4) & 0x01); //first the top
  132. GPIO_WriteBit(GPIOC, LCD_D5, (command>>5) & 0x01); //4 bits
  133. GPIO_WriteBit(GPIOC, LCD_D6, (command>>6) & 0x01);
  134. GPIO_WriteBit(GPIOC, LCD_D7, (command>>7) & 0x01);
  135. pulse_EN();
  136. GPIO_WriteBit(GPIOC, LCD_D4, command & 0x01); //now the bottom
  137. GPIO_WriteBit(GPIOC, LCD_D5, (command>>1) & 0x01); //4 bits
  138. GPIO_WriteBit(GPIOC, LCD_D6, (command>>2) & 0x01);
  139. GPIO_WriteBit(GPIOC, LCD_D7, (command>>3) & 0x01);
  140. pulse_EN();
  141. delay_microsec(37); //let it work on the data
  142. }
  143.  
  144. void lcd_putchar(unsigned char c) {
  145. set_RS(LCD_DATA);
  146. GPIO_WriteBit(GPIOC, LCD_D4, (c>>4) & 0x01); //top 4 bits go first
  147. GPIO_WriteBit(GPIOC, LCD_D5, (c>>5) & 0x01);
  148. GPIO_WriteBit(GPIOC, LCD_D6, (c>>6) & 0x01);
  149. GPIO_WriteBit(GPIOC, LCD_D7, (c>>7) & 0x01);
  150. pulse_EN(); //this can't be too slow or it will time out
  151. GPIO_WriteBit(GPIOC, LCD_D4, c & 0x01); //bottom 4 bits last
  152. GPIO_WriteBit(GPIOC, LCD_D5, (c>>1) & 0x01);
  153. GPIO_WriteBit(GPIOC, LCD_D6, (c>>2) & 0x01);
  154. GPIO_WriteBit(GPIOC, LCD_D7, (c>>3) & 0x01);
  155. pulse_EN();
  156. delay_microsec(37); //let it work on the data
  157. }
  158.  
  159. void delay_microsec(register int n) {
  160. register int i;
  161. for (i=0; i<n; i++) {
  162. asm("nop");
  163. asm("nop");
  164. asm("nop");
  165. asm("nop");
  166. asm("nop");
  167. asm("nop");
  168. asm("nop");
  169. asm("nop");
  170. asm("nop");
  171. asm("nop");
  172. asm("nop");
  173. asm("nop");
  174. asm("nop");
  175. asm("nop");
  176. asm("nop");
  177. asm("nop");
  178. asm("nop");
  179. asm("nop");
  180. }
  181.  
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement