找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3167|回复: 0
打印 上一主题 下一主题
收起左侧

STM32学习--LCD

[复制链接]
跳转到指定楼层
楼主
ID:81272 发表于 2015-5-27 16:53 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
完整源码下载: lcd.RAR (122.01 KB, 下载次数: 16)

  1. /*******************************************************************************

  2. STM32学习日志(7)----LCD测试程序

  3. 编译环境:        EWARM V5.30
  4. 硬件环境:        南京万利 EK-STM32F
  5. 主芯片        :        STM32F103VBT6
  6. STM32 FW:   V3.0.0
  7. 作者        :        szlihongtao
  8. 时间        :          2010-06-30
  9. 说明        :   1. 在 SysTick 定时中断程序中刷新LCD的显示
  10.                         2. 实现了1个秒表计时器
  11. *******************************************************************************/
  12. /**
  13.   ******************************************************************************
  14.   * @file    Project/Template/main.c
  15.   * @author  MCD Application Team
  16.   * @version V3.0.0
  17.   * @date    04/06/2009
  18.   * @brief   Main program body
  19.   ******************************************************************************
  20.   * @copy
  21.   *
  22.   * THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  23.   * WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
  24.   * TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
  25.   * DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
  26.   * FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
  27.   * CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
  28.   *
  29.   * <h2><center>© COPYRIGHT 2009 STMicroelectronics</center></h2>
  30.   */

  31. /* Includes ------------------------------------------------------------------*/
  32. #include "stm32f10x.h"
  33. #include "stm32_m.h"
  34. #include "lcd.h"

  35. /** @addtogroup Template_Project
  36.   * @{
  37.   */

  38. /* Private typedef -----------------------------------------------------------*/
  39. /* Private define ------------------------------------------------------------*/
  40. /* Private macro -------------------------------------------------------------*/
  41. /* Private variables ---------------------------------------------------------*/
  42. /* Private function prototypes -----------------------------------------------*/
  43. /* Private functions ---------------------------------------------------------*/

  44. /**
  45.   * @brief  Main program.
  46.   * @param  None
  47.   * @retval : None
  48.   */

  49. bit f_tb;                                                // 基本定时标志
  50. static INT16U tmr_1sec,cnt_sec;
  51. //******************************************************************************
  52. static void delayms(INT16U cnt)
  53. {
  54.         INT16U i;

  55.         while(cnt--)
  56.                 for (i=0; i<7333; i++);
  57. }
  58. //******************************************************************************
  59. // 延时50ms
  60. //******************************************************************************
  61. static void delay(void)
  62. {
  63.         INT32U i;
  64.     static INT32U jjj=5240*50;

  65.         for (i=0; i<jjj; i++);
  66. }
  67. //******************************************************************************
  68. // 时钟设置初始化
  69. //******************************************************************************
  70. static void RCC_Configuration(void)
  71. {
  72.   ErrorStatus HSEStartUpStatus;
  73. /*
  74. RCC_AdjustHSICalibrationValue 调整内部高速晶振(HSI)校准值
  75. RCC_ITConfig 使能或者失能指定的RCC中断
  76. RCC_ClearFlag 清除RCC的复位标志位
  77. RCC_GetITStatus 检查指定的RCC中断发生与否
  78. RCC_ClearITPendingBit 清除RCC的中断待处理位
  79. */
  80.           /* RCC system reset(for debug purpose) */
  81.           // 时钟系统复位
  82.           RCC_DeInit();

  83.         // 使能外部的8M晶振
  84.         // 设置外部高速晶振(HSE)
  85.           /* Enable HSE */
  86.           RCC_HSEConfig(RCC_HSE_ON);

  87.         // 使能或者失能内部高速晶振(HSI)
  88.         RCC_HSICmd(DISABLE);

  89.         // 等待HSE起振
  90.         // 该函数将等待直到HSE就绪,或者在超时的情况下退出
  91.           /* Wait till HSE is ready */
  92.           HSEStartUpStatus = RCC_WaitForHSEStartUp();

  93.           if(HSEStartUpStatus == SUCCESS)
  94.           {
  95.             /* HCLK = SYSCLK */
  96.                 // 设置AHB时钟(HCLK)
  97.             RCC_HCLKConfig(RCC_SYSCLK_Div1);        // 72 MHz

  98.             /* PCLK1 = HCLK/2 */
  99.                 // 设置低速AHB时钟(PCLK1)
  100.             RCC_PCLK1Config(RCC_HCLK_Div2);        // 36 MHz

  101.             /* PCLK2 = HCLK */
  102.                 // 设置高速AHB时钟(PCLK2)
  103.             RCC_PCLK2Config(RCC_HCLK_Div1);        // 72 MHz

  104.             /* ADCCLK = PCLK2/8 */
  105.                 // 设置ADC时钟(ADCCLK)
  106.                    RCC_ADCCLKConfig(RCC_PCLK2_Div8);

  107.                 // 设置USB时钟(USBCLK)
  108.                 // USB时钟 = PLL时钟除以1.5
  109.                 RCC_USBCLKConfig(RCC_USBCLKSource_PLLCLK_1Div5);

  110.                 // 设置外部低速晶振(LSE)
  111.                 RCC_LSEConfig(RCC_LSE_OFF);

  112.                 // 使能或者失能内部低速晶振(LSI)
  113.                 // LSE晶振OFF
  114.                 RCC_LSICmd(DISABLE);

  115.                 // 设置RTC时钟(RTCCLK)
  116.                 // 选择HSE时钟频率除以128作为RTC时钟
  117.                 RCC_RTCCLKConfig(RCC_RTCCLKSource_HSE_Div128);

  118.                 // 使能或者失能RTC时钟
  119.                 // RTC时钟的新状态
  120.                 RCC_RTCCLKCmd(DISABLE);

  121.             /* Flash 2 wait state */
  122.             FLASH_SetLatency(FLASH_Latency_2);

  123.             /* Enable Prefetch Buffer */
  124.             FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);

  125.             /* PLLCLK = 8MHz * 9 = 72 MHz */
  126.                 // 设置PLL时钟源及倍频系数
  127.             RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);

  128.             /* Enable PLL */
  129.                 // 使能或者失能PLL
  130.             RCC_PLLCmd(ENABLE);

  131.             /* Wait till PLL is ready */
  132.                 // 检查指定的RCC标志位设置与否
  133.             while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
  134.             {
  135.             }

  136.             /* Select PLL as system clock source */
  137.                 // 设置系统时钟(SYSCLK)
  138.             RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);

  139.             /* Wait till PLL is used as system clock source */
  140.                 // 返回用作系统时钟的时钟源
  141.             while(RCC_GetSYSCLKSource() != 0x08)
  142.             {
  143.             }
  144.   }

  145.         // 使能或者失能AHB外设时钟
  146.         RCC_AHBPeriphClockCmd(RCC_AHBPeriph_DMA1
  147.                                                         |RCC_AHBPeriph_DMA2
  148.                                                         |RCC_AHBPeriph_SRAM
  149.                                                         |RCC_AHBPeriph_FLITF
  150.                                                         |RCC_AHBPeriph_CRC
  151.                                                         |RCC_AHBPeriph_FSMC
  152.                                                         |RCC_AHBPeriph_SDIO,DISABLE);
  153.         // 使能或者失能APB1外设时钟
  154.         RCC_APB1PeriphClockCmd(RCC_APB1Periph_ALL,DISABLE);

  155.         // 强制或者释放高速APB(APB2)外设复位
  156.         RCC_APB2PeriphResetCmd(RCC_APB2Periph_ALL,ENABLE);
  157.         // 退出复位状态
  158.         RCC_APB2PeriphResetCmd(RCC_APB2Periph_ALL,DISABLE);

  159.         // 强制或者释放低速APB(APB1)外设复位
  160.         RCC_APB1PeriphResetCmd(RCC_APB1Periph_ALL,ENABLE);

  161.         // 强制或者释放后备域复位
  162.         RCC_BackupResetCmd(ENABLE);

  163.         // 使能或者失能时钟安全系统
  164.         RCC_ClockSecuritySystemCmd(DISABLE);
  165. }
  166. //******************************************************************************
  167. // NVIC设置
  168. //******************************************************************************
  169. static void NVIC_Configuration(void)
  170. {
  171.         NVIC_InitTypeDef  NVIC_InitStructure;

  172.           /* Configure one bit for preemption priority */
  173.           NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1);

  174.     /* Enable the EXTI4 Interrupt on PD.4 */
  175.           NVIC_InitStructure.NVIC_IRQChannel = EXTI4_IRQn;
  176.           NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  177.           NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  178.           NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  179.           NVIC_Init(&NVIC_InitStructure);
  180. }
  181. //******************************************************************************
  182. // SysTick设置初始化
  183. //******************************************************************************
  184. static void SysTick_Config1(void)
  185. {
  186.         #define SystemFreq                72000000.0        // 单位为Hz
  187.         #define TB_SysTick                2000.0                        // 单位为uS

  188.         INT32U ticks;

  189.         ticks=(INT32U)((TB_SysTick/1000000.0)*SystemFreq);
  190.         ticks=ticks;
  191.         SysTick_Config(ticks);
  192. }
  193. //******************************************************************************
  194. // GPIO设置
  195. //******************************************************************************
  196. static void GPIO_Configuration(void)
  197. {
  198.         GPIO_InitTypeDef GPIO_InitStructure;

  199.         // 使能或者失能APB2外设时钟
  200.         RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD|RCC_APB2Periph_GPIOE, ENABLE);

  201.           GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7;
  202.           GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  203.           GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  204.           GPIO_Init(GPIOC, &GPIO_InitStructure);

  205.           GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3|GPIO_Pin_4; // key
  206.           GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  207.           GPIO_Init(GPIOD, &GPIO_InitStructure);

  208.           GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  209.           GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11;
  210.           GPIO_Init(GPIOC, &GPIO_InitStructure);                        // lcd_comm

  211.           GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;                // lcd_seg
  212.           GPIO_Init(GPIOE, &GPIO_InitStructure);
  213. }
  214. //******************************************************************************
  215. // LED5亮
  216. //******************************************************************************
  217. void Led_RW_ON(void)
  218. {
  219.           GPIO_SetBits(GPIOC,GPIO_Pin_4);
  220. }
  221. //******************************************************************************
  222. // LED5灭
  223. //******************************************************************************
  224. void Led_RW_OFF(void)
  225. {
  226.           GPIO_ResetBits(GPIOC,GPIO_Pin_4);
  227. }
  228. //******************************************************************************
  229. // pos=0-3
  230. //******************************************************************************
  231. static void myWrite_char(u8 pos,char car,u8 f_dot)
  232. {
  233.         write_char(&car,f_dot,pos);
  234. }
  235. //******************************************************************************
  236. static void disp_sec(INT16U cnt_sec)
  237. {
  238.         INT8U min,sec;
  239.         char ch;

  240.         cnt_sec%=3600;

  241.         min=cnt_sec/60;
  242.         sec=cnt_sec%60;

  243.         ch=0x30+min/10;
  244.         myWrite_char(0,ch,0);

  245.         ch=0x30+min%10;
  246.         myWrite_char(1,ch,1);  // 加小数点,隔开分钟与秒的显示

  247.         ch=0x30+sec/10;
  248.         myWrite_char(2,ch,0);

  249.         ch=0x30+sec%10;
  250.         myWrite_char(3,ch,0);
  251. }
  252. //******************************************************************************
  253. // 主程序
  254. //******************************************************************************
  255. int main(void)
  256. {
  257.         RCC_Configuration();
  258.           GPIO_Configuration();
  259.         NVIC_Configuration();
  260.           SysTick_Config1();
  261.         Led_RW_ON();
  262.         delay();
  263.         Led_RW_OFF();
  264. //------------------------------------------------------------------------------
  265.     write_string("STM3");                  /*STM32 LCD demo*/
  266.            delayms(1000);

  267. #if 0
  268.         for(;;)
  269.           {
  270.               write_string("STM3");          /*STM32 LCD demo*/
  271.               delayms(400);

  272.               write_string("0 01");
  273.               delayms(400);

  274.               write_string("0 02");
  275.               delayms(400);

  276.                 GPIOC->ODR ^= (1<<6);                // led3 toogle
  277.           }
  278. #endif
  279. //------------------------------------------------------------------------------
  280. #if 0
  281.         for(;;)
  282.           {
  283.                  INT8U i,ch;

  284.                 for(ch='0';ch<='9';++ch)
  285.                 {
  286.                         for(i=0;i<4;++i)
  287.                                 myWrite_char(i,ch,0);

  288.                       delayms(400);
  289.                 }

  290.                 for(i=0;i<4;++i)
  291.                         myWrite_char(i,' ',0);
  292.               delayms(800);

  293.                 for(ch='A';ch<='F';++ch)
  294.                 {
  295.                         for(i=0;i<4;++i)
  296.                                 myWrite_char(i,ch,1);

  297.                       delayms(400);
  298.                 }

  299.                 for(i=0;i<4;++i)
  300.                         myWrite_char(i,'+',0);
  301.               delayms(800);

  302.                 for(i=0;i<4;++i)
  303.                         myWrite_char(i,'-',0);
  304.               delayms(800);
  305.           }
  306. #endif
  307. //------------------------------------------------------------------------------
  308.         for(;;)
  309.         {
  310.                 if (f_tb)                // 每隔2ms设置为1
  311.                 {
  312.                         f_tb=0;
  313.                         if (++tmr_1sec>=(1000/2))
  314.                         {
  315.                                 tmr_1sec=0;
  316.                                 disp_sec(++cnt_sec);

  317.                                 GPIOC->ODR ^= GPIO_Pin_6;                // led3 toogle
  318.                         }
  319.                 }
  320.         }
  321. }
  322. //******************************************************************************
  323. #ifdef  USE_FULL_ASSERT

  324. /**
  325.   * @brief  Reports the name of the source file and the source line number
  326.   *   where the assert_param error has occurred.
  327.   * @param file: pointer to the source file name
  328.   * @param line: assert_param error line source number
  329.   * @retval : None
  330.   */
  331. void assert_failed(uint8_t* file, uint32_t line)
  332. {
  333.   /* User can add his own implementation to report the file name and line number,
  334.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  335.   /* Infinite loop */
  336.   while (1)
  337.   {
  338.   }
  339. }
  340. #endif

  341. /**
  342.   * @}
  343.   */
  344. //******************************************************************************
  345. /******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/
  346. //******************************************************************************
  347. /*
  348.         LED2---------PC7
  349.         LED3---------PC6
  350.         LED4---------PC5
  351.         LED5---------PC4

  352.         KEY2---------PD3
  353.         KEY3---------PD4
  354. */
复制代码



分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表