Advertisement
pongfactory

Example LCD By Keil

Feb 28th, 2014
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 12.47 KB | None | 0 0
  1. #include "stm32f10x_lib.h"
  2. #include "platform_config.h"
  3.  
  4. GPIO_InitTypeDef GPIO_InitStructure;
  5. ErrorStatus HSEStartUpStatus;
  6. static vu32 TimingDelay;
  7.  
  8. /* Private function prototypes -----------------------------------------------*/
  9. void RCC_Configuration(void);
  10. void NVIC_Configuration(void);
  11. void lcd_init(void);                    // Initial LCD
  12. void lcd_out_data4(unsigned char);      // Strobe 4-Bit Data to LCD
  13. void lcd_write_byte(unsigned char);     // Write 1 Byte Data to LCD
  14. void lcd_write_control(unsigned char);  // Write Instruction
  15. void lcd_write_ascii(unsigned char);    // Write LCD Display(ASCII)
  16. void goto_cursor(unsigned char);        // Set Position Cursor LCD
  17. void lcd_print(unsigned char*);         // Print Display to LCD
  18. char busy_lcd(void);                    // Read Busy LCD Status
  19. void enable_lcd(void);                  // Enable Pulse
  20. void DelayuS(vu32 nCount);              // 1uS Delay
  21. void DelaymS(vu32 nTime);               // 1mS Delay
  22. void TimingDelay_Decrement(void);
  23.  
  24. int main(void)
  25. {
  26.  
  27. #ifdef DEBUG
  28.   debug();
  29. #endif
  30.  
  31.   /* System Clocks Configuration **********************************************/
  32.   RCC_Configuration();
  33.  
  34.   /* NVIC Configuration *******************************************************/
  35.   NVIC_Configuration();
  36.  
  37.   /* SysTick end of count event each 1ms with input clock equal to 9MHz (HCLK/8, default) */
  38.   SysTick_SetReload(9000);
  39.  
  40.   /* Enable SysTick interrupt */
  41.   SysTick_ITConfig(ENABLE);
  42.  
  43.   /* Initial LCD 16x2 */
  44.   lcd_init();                                           // Initial LCD
  45.  
  46.   // Loop Print Message to LCD16 x 2 //
  47.   while(1)                                              // Loop Continue
  48.   {
  49.     goto_cursor(0x00);                                  // Set Cursor Line-1
  50.     lcd_print("ET-STM32F103RBT6");                      // Display LCD Line-1
  51.     goto_cursor(0x40);                                  // Set Cursor = Line-2
  52.     lcd_print("ST-ARM Cortex-M3");                      // Display LCD Line-2
  53.     DelaymS(2500);                                      // Display Delay
  54.  
  55.     goto_cursor(0x00);                                  // Set Cursor Line-1
  56.     lcd_print("32 Bit Processor");                      // Display LCD Line-1
  57.     goto_cursor(0x40);                                  // Set Cursor = Line-2
  58.     lcd_print("BY...ETT CO.,LTD");                      // Display LCD Line-2
  59.     DelaymS(2500);                                      // Display Delay
  60.   }
  61. }
  62.  
  63. /*******************************************************************************
  64. * Function Name  : RCC_Configuration
  65. * Description    : Configures the different system clocks.
  66. * Input          : None
  67. * Output         : None
  68. * Return         : None
  69. *******************************************************************************/
  70. void RCC_Configuration(void)
  71. {
  72.   /* RCC system reset(for debug purpose) */
  73.   RCC_DeInit();
  74.  
  75.   /* Enable HSE */
  76.   RCC_HSEConfig(RCC_HSE_ON);
  77.  
  78.   /* Wait till HSE is ready */
  79.   HSEStartUpStatus = RCC_WaitForHSEStartUp();
  80.  
  81.   if(HSEStartUpStatus == SUCCESS)
  82.   {
  83.     /* Enable Prefetch Buffer */
  84.     FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
  85.  
  86.     /* Flash 2 wait state */
  87.     FLASH_SetLatency(FLASH_Latency_2);
  88.  
  89.     /* HCLK = SYSCLK */
  90.     RCC_HCLKConfig(RCC_SYSCLK_Div1);
  91.  
  92.     /* PCLK2 = HCLK */
  93.     RCC_PCLK2Config(RCC_HCLK_Div1);
  94.  
  95.     /* PCLK1 = HCLK/2 */
  96.     RCC_PCLK1Config(RCC_HCLK_Div2);
  97.  
  98.     /* PLLCLK = 8MHz * 9 = 72 MHz */
  99.     RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
  100.  
  101.     /* Enable PLL */
  102.     RCC_PLLCmd(ENABLE);
  103.  
  104.     /* Wait till PLL is ready */
  105.     while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
  106.     {
  107.     }
  108.  
  109.     /* Select PLL as system clock source */
  110.     RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
  111.  
  112.     /* Wait till PLL is used as system clock source */
  113.     while(RCC_GetSYSCLKSource() != 0x08)
  114.     {
  115.     }
  116.   }
  117. }
  118.  
  119. /*******************************************************************************
  120. * Function Name  : NVIC_Configuration
  121. * Description    : Configures Vector Table base location.
  122. * Input          : None
  123. * Output         : None
  124. * Return         : None
  125. *******************************************************************************/
  126. void NVIC_Configuration(void)
  127. {
  128. #ifdef  VECT_TAB_RAM
  129.   /* Set the Vector Table base location at 0x20000000 */
  130.   NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
  131. #else  /* VECT_TAB_FLASH  */
  132.   /* Set the Vector Table base location at 0x08000000 */
  133.   NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
  134. #endif
  135. }
  136.  
  137. /****************************/
  138. /* Strobe 4-Bit Data to LCD */
  139. /****************************/
  140. void lcd_out_data4(unsigned char val)
  141. {
  142.   if((val&0x01)==0x01)  // Bit[0]
  143.   {
  144.     LCD_D4_HI();
  145.   }
  146.   else
  147.   {
  148.     LCD_D4_LO();
  149.   }
  150.  
  151.   if((val&0x02)==0x02)  // Bit[1]
  152.   {
  153.     LCD_D5_HI();
  154.   }
  155.   else
  156.   {
  157.     LCD_D5_LO();
  158.   }
  159.  
  160.   if((val&0x04)==0x04)  // Bit[2]
  161.   {
  162.     LCD_D6_HI();
  163.   }
  164.   else
  165.   {
  166.     LCD_D6_LO();
  167.   }
  168.  
  169.   if((val&0x08)==0x08)  // Bit[3]
  170.   {
  171.     LCD_D7_HI();
  172.   }
  173.   else
  174.   {
  175.     LCD_D7_LO();
  176.   }
  177.  
  178. }
  179.  
  180. /****************************/
  181. /* Write Data 1 Byte to LCD */
  182. /****************************/
  183. void lcd_write_byte(unsigned char val)
  184. {
  185.   lcd_out_data4((val>>4)&0x0F);                         // Strobe 4-Bit High-Nibble to LCD
  186.   enable_lcd();                                         // Enable Pulse
  187.  
  188.   lcd_out_data4(val&0x0F);                              // Strobe 4-Bit Low-Nibble to LCD
  189.   enable_lcd();                                         // Enable Pulse
  190.  
  191.   while(busy_lcd());                                    // Wait LCD Execute Complete
  192. }
  193.  
  194. /****************************/
  195. /* Write Instruction to LCD */
  196. /****************************/
  197. void lcd_write_control(unsigned char val)
  198. {
  199.   LCD_RS_LO();                                          // RS = 0 = Instruction Select
  200.   lcd_write_byte(val);                                  // Strobe Command Byte
  201. }
  202.  
  203. /****************************/
  204. /* Write Data(ASCII) to LCD */
  205. /****************************/
  206. void lcd_write_ascii(unsigned char c)
  207. {
  208.   LCD_RS_HI();                                          // RS = 1 = Data Select
  209.   lcd_write_byte(c);                                    // Strobe 1 Byte to LCD
  210. }
  211.  
  212. /*******************************/
  213. /* Initial 4-Bit LCD Interface */
  214. /*******************************/
  215. void lcd_init(void)
  216. {
  217.   /* Configure IO connected to LCD16X2 */
  218.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIO_EN, ENABLE);
  219.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  220.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  221.   GPIO_InitStructure.GPIO_Pin = LCD_EN_PIN;
  222.   GPIO_Init(LCD_EN_PORT, &GPIO_InitStructure);
  223.  
  224.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIO_RW, ENABLE);
  225.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  226.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  227.   GPIO_InitStructure.GPIO_Pin = LCD_RW_PIN;
  228.   GPIO_Init(LCD_RW_PORT, &GPIO_InitStructure);
  229.  
  230.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIO_RS, ENABLE);
  231.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  232.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  233.   GPIO_InitStructure.GPIO_Pin = LCD_RS_PIN;
  234.   GPIO_Init(LCD_RS_PORT, &GPIO_InitStructure);
  235.  
  236.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIO_DATA, ENABLE);
  237.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  238.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  239.   GPIO_InitStructure.GPIO_Pin = LCD_D4_PIN |
  240.                                 LCD_D5_PIN |
  241.                                 LCD_D6_PIN |
  242.                                 LCD_D7_PIN;
  243.   GPIO_Init(LCD_DATA_PORT, &GPIO_InitStructure);
  244.  
  245.   LCD_D4_HI();
  246.   LCD_D5_HI();
  247.   LCD_D6_LO();
  248.   LCD_D7_LO();
  249.   DelayuS(15000);                                       // Power-On Delay (15 mS)
  250.  
  251.   LCD_D4_HI();
  252.   LCD_D5_HI();
  253.   LCD_D6_LO();
  254.   LCD_D7_LO();
  255.   enable_lcd();                                         // Enable Pulse
  256.   DelayuS(4100);                                        // Delay 4.1mS
  257.  
  258.   LCD_D4_HI();
  259.   LCD_D5_HI();
  260.   LCD_D6_LO();
  261.   LCD_D7_LO();
  262.   enable_lcd();                                         // Enable Pulse
  263.   DelayuS(100);                                         // delay 100uS
  264.  
  265.   LCD_D4_HI();
  266.   LCD_D5_HI();
  267.   LCD_D6_LO();
  268.   LCD_D7_LO();
  269.   enable_lcd();                                         // Enable Pulse
  270.   while(busy_lcd());                                    // Wait LCD Execute Complete
  271.  
  272.   LCD_D4_LO();
  273.   LCD_D5_HI();
  274.   LCD_D6_LO();
  275.   LCD_D7_LO();
  276.   enable_lcd();                                         // Enable Pulse
  277.   while(busy_lcd());                                    // Wait LCD Execute Complete
  278.  
  279.   lcd_write_control(0x28);                              // Function Set (DL=0 4-Bit,N=1 2 Line,F=0 5X7)
  280.   lcd_write_control(0x0C);                              // Display on/off Control (Entry Display,Cursor off,Cursor not Blink)
  281.   lcd_write_control(0x06);                              // Entry Mode Set (I/D=1 Increment,S=0 Cursor Shift)
  282.   lcd_write_control(0x01);                              // Clear Display  (Clear Display,Set DD RAM Address=0)
  283.   DelaymS(15);                                          // Wait Command Ready
  284. }
  285.  
  286. /***************************/
  287. /* Set LCD Position Cursor */
  288. /***************************/
  289. void goto_cursor(unsigned char i)
  290. {
  291.   i |= 0x80;                                            // Set DD-RAM Address Command
  292.   lcd_write_control(i);
  293. }
  294.  
  295. /************************************/
  296. /* Print Display Data(ASCII) to LCD */
  297. /************************************/
  298. void lcd_print(unsigned char* str)
  299. {
  300.   int i;
  301.  
  302.   for (i=0;i<16 && str[i]!=0;i++)                       // 16 Character Print
  303.   {
  304.     lcd_write_ascii(str[i]);                            // Print Byte to LCD
  305.   }
  306. }
  307.  
  308. /******************/
  309. /* Wait LCD Ready */
  310. /******************/
  311.  
  312. char busy_lcd(void)
  313. {
  314.   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;     // Config D7 = Read
  315.   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  316.   GPIO_InitStructure.GPIO_Pin = LCD_D7_PIN;
  317.   GPIO_Init(LCD_DATA_PORT, &GPIO_InitStructure);
  318.  
  319.   LCD_RS_LO();                                          // Instruction Select
  320.   LCD_RW_HI();                                          // Read Direction
  321.   LCD_EN_HI();                                          // Start Read Busy
  322.  
  323.   DelayuS(100);                                         // Delay Before Read
  324.   if (GPIO_ReadInputDataBit(LCD_DATA_PORT, LCD_D7_PIN) == Bit_SET)
  325.   {
  326.     LCD_EN_LO();                                        // Disable Read
  327.     LCD_RW_LO();                                        // Default = Write Direction
  328.  
  329.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;   // Config D7 = Write
  330.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  331.     GPIO_InitStructure.GPIO_Pin = LCD_D7_PIN;
  332.     GPIO_Init(LCD_DATA_PORT, &GPIO_InitStructure);
  333.     return 1;                                           // LCD Busy Status
  334.   }
  335.   else
  336.   {
  337.     LCD_EN_LO();                                        // Disable Read
  338.     LCD_RW_LO();                                        // Default = Write Direction
  339.  
  340.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;   // Config D7 = Write
  341.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  342.     GPIO_InitStructure.GPIO_Pin = LCD_D7_PIN;
  343.     GPIO_Init(LCD_DATA_PORT, &GPIO_InitStructure);
  344.     return 0;                                           // LCD Ready Status
  345.   }
  346. }
  347.  
  348.  
  349. /***********************/
  350. /* Enable Pulse to LCD */
  351. /***********************/
  352. void enable_lcd(void)                                   // Enable Pulse
  353. {
  354.   LCD_EN_HI();                                          // Enable ON
  355.   DelayuS(50);
  356.   LCD_EN_LO();                                          // Enable OFF
  357. }
  358.  
  359. /*******************************************************************************
  360. * Function Name  : Delay
  361. * Description    : Inserts a delay time.
  362. * Input          : nCount: specifies the delay time length.
  363. * Output         : None
  364. * Return         : None
  365. *******************************************************************************/
  366. void DelayuS(vu32 nCount)
  367. {
  368.   while (nCount--);
  369. }
  370.  
  371. /*******************************************************************************
  372. * Function Name  : Delay
  373. * Description    : Inserts a delay time.
  374. * Input          : nTime: specifies the delay time length, in milliseconds.
  375. * Output         : None
  376. * Return         : None
  377. *******************************************************************************/
  378. void DelaymS(u32 nTime)
  379. {
  380.   /* Enable the SysTick Counter */
  381.   SysTick_CounterCmd(SysTick_Counter_Enable);
  382.  
  383.   TimingDelay = nTime;
  384.  
  385.   while(TimingDelay != 0);
  386.  
  387.   /* Disable SysTick Counter */
  388.   SysTick_CounterCmd(SysTick_Counter_Disable);
  389.   /* Clear SysTick Counter */
  390.   SysTick_CounterCmd(SysTick_Counter_Clear);
  391. }
  392.  
  393. /*******************************************************************************
  394. * Function Name  : TimingDelay_Decrement
  395. * Description    : Decrements the TimingDelay variable.
  396. * Input          : None
  397. * Output         : TimingDelay
  398. * Return         : None
  399. *******************************************************************************/
  400. void TimingDelay_Decrement(void)
  401. {
  402.   if (TimingDelay != 0x00)
  403.   {
  404.     TimingDelay--;
  405.   }
  406. }
  407.  
  408. #ifdef  DEBUG
  409. /*******************************************************************************
  410. * Function Name  : assert_failed
  411. * Description    : Reports the name of the source file and the source line number
  412. *                  where the assert_param error has occurred.
  413. * Input          : - file: pointer to the source file name
  414. *                  - line: assert_param error line source number
  415. * Output         : None
  416. * Return         : None
  417. *******************************************************************************/
  418. void assert_failed(u8* file, u32 line)
  419. {
  420.   /* User can add his own implementation to report the file name and line number,
  421.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  422.  
  423.   /* Infinite loop */
  424.   while (1)
  425.   {
  426.   }
  427. }
  428. #endif
  429.  
  430. /******************* (C) COPYRIGHT 2008 STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement