找回密码
 立即注册

QQ登录

只需一步,快速开始

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

基于stm32f103R6 LCD12864显示程序+Proteus仿真图

  [复制链接]
跳转到指定楼层
楼主
基于stm32f103R6 lcd12864 显示 福利 加仿真



单片机源程序如下:

  1. #include "main.h"
  2. #include "stm32f1xx_hal.h"
  3. //#include "stm32f1xx_hal_gpio.h"

  4. #define uchar unsigned char
  5. #define uint unsigned int




  6. //#define         databus         P1
  7. #define     databus     GPIOB


  8. //sbit                 cs1 = P3^3;                   //左屏幕选择,低电平有效
  9. #define     cs1     GPIO_PIN_3
  10. #define    set_cs1     GPIO_SetBits(GPIOC,   cs1)
  11. #define    clr_cs1     GPIO_ResetBits(GPIOC, cs1)

  12. //sbit                 cs2 = P3^4;                   //右屏幕选择
  13. #define     cs2     GPIO_PIN_4
  14. #define    set_cs2     GPIO_SetBits(GPIOC,   cs2)
  15. #define    clr_cs2     GPIO_ResetBits(GPIOC, cs2)


  16. //sbit                 e = P3^5;                    //指令数据控制
  17. #define     e      GPIO_PIN_5
  18. #define    set_e     GPIO_SetBits(GPIOC,   e)
  19. #define    clr_e     GPIO_ResetBits(GPIOC, e)


  20. //sbit                   wr = P3^6;                    //读写控制
  21. #define           wr     GPIO_PIN_6
  22. #define    set_wr     GPIO_SetBits(GPIOC,   wr)
  23. #define    clr_wr     GPIO_ResetBits(GPIOC, wr)


  24. //sbit                 rs = P3^7;                    //指令数据选择
  25. #define     rs     GPIO_PIN_7
  26. #define    set_rs     GPIO_SetBits(GPIOC,   rs)
  27. #define    clr_rs     GPIO_ResetBits(GPIOC, rs)



  28. void LcdDelay(uint time)
  29. {
  30.         while(time--);
  31. }

  32. void _NOP_(void)
  33. {
  34.     uint i = 200;
  35.     while(i > 0)
  36.                 {
  37.         i--;
  38.     }
  39. }

  40. /**********************************
  41. 写指令
  42. **********************************/
  43. void SendCommand(uchar command)
  44. {
  45.         set_e;                 //e=1;       
  46.         clr_wr;         //wr=0;       
  47.         clr_rs;         //rs=0;       
  48.         databus->ODR = (databus->ODR & 0xff00)|command;  //databus=command;
  49.   _NOP_();
  50.         clr_e;                 //e=0;
  51.   _NOP_();
  52. }

  53. /**********************************
  54. 写数据
  55. **********************************/
  56. void WriteData(uchar dat)
  57. {
  58.         set_e;                //e=1;
  59.         clr_wr;                //wr=0;
  60.         set_rs;                //rs=1;
  61.         databus->ODR =        (databus->ODR & 0xff00)|dat;        //databus=dat;
  62.   _NOP_();
  63.         clr_e;                //e=0;
  64.   _NOP_();
  65. }

  66. /**********************************
  67. 显示开/关
  68. **********************************/
  69. void SetOnOff(uchar onoff)
  70. {
  71.         if(onoff==1)
  72.         {
  73.                 SendCommand(0x3f);       
  74.         }       
  75.         else
  76.         {
  77.                 SendCommand(0x3e);
  78.         }
  79. }


  80. /**********************************
  81. 选择页
  82. **********************************/
  83. void SetLine(uchar line)         //12864总共有8页(0~7),每页有8行
  84. {
  85.         line=line&0x07;                          //只取后三位xxxx x111  ,这3个是要改变位置的数据
  86.         line=line|0xb8;                          //页设置的固定格式
  87.         SendCommand(line);
  88. }


  89. /**********************************
  90. 选择列
  91. **********************************/
  92. void SetColum(uchar colum)        //12864每半屏有64列(0~63),分为左右2屏
  93. {
  94.         colum=colum&0x3f;         //xx11 1111,这个是要改变Y位置的数据
  95.         colum=colum|0x40;         //固定格式
  96.         SendCommand(colum);
  97. }


  98. /**********************************
  99. 选择起始行
  100. **********************************/
  101. void SetStartLine(uchar startline)
  102. {
  103.         startline=startline&0x3f;                //xx11 1111,这个是要改变x位置的数据
  104.         startline=startline|0xc0;                //11xxxxxx,是起始行设置的固定指令
  105.         SendCommand(startline);
  106. }


  107. /**********************************
  108. 选择左右屏                0:左屏,1:右屏,2:全屏
  109. **********************************/
  110. void SelectScreen(uchar screen)
  111. {
  112.         switch(screen)
  113.         {
  114.                 case 0:
  115.                                 clr_cs1;        //cs1=0;
  116.                                 LcdDelay(2);
  117.                                 set_cs2;        //cs2=1;
  118.                                 LcdDelay(2);
  119.                                 break;
  120.                 case 1:
  121.                                 set_cs1;        //cs1=1;
  122.                                 LcdDelay(2);
  123.                                 clr_cs2;        //cs2=0;
  124.                                 LcdDelay(2);
  125.                                 break;
  126.                 case 2:
  127.                                 clr_cs1;        //cs1=0;
  128.                                 LcdDelay(2);
  129.                                 clr_cs2;        //cs2=0;
  130.                                 LcdDelay(2);
  131.                                 break;
  132.         }
  133. }


  134. /**********************************
  135. 显示一个英文字符
  136. **********************************/
  137. void Show_english(uchar line,uchar column,uchar *address)
  138. {
  139.         uchar i;
  140.         SetLine(line);
  141.         SetColum(column);
  142.         for(i=0;i<8;i++)
  143.         {
  144.                 WriteData(*address);
  145.                 address++;       
  146.         }
  147.        
  148.         SetLine(line+1);
  149.         SetColum(column);
  150.         for(i=0;i<8;i++)
  151.         {
  152.                 WriteData(*address);
  153.                 address++;       
  154.         }
  155. }


  156. /**********************************
  157. 清屏
  158. **********************************/
  159. void ClearScreen(uchar screen)
  160. {
  161.         uchar i,j;
  162.         SelectScreen(screen);
  163.         for(i=0;i<8;i++)
  164.         {
  165.                 SetLine(i);
  166.                 SetColum(0);
  167.                 for(j=0;j<64;j++)
  168.                 {
  169.                         WriteData(0);                       
  170.                 }       
  171.         }
  172. }

  173. /**********************************
  174. 12864初始化
  175. **********************************/
  176. void InitLcd()
  177. {
  178.         SetOnOff(0);                //显示关
  179.         ClearScreen(2);                //清屏
  180.         SetLine(0);                        //页设置
  181.         SetColum(0);                //列设置
  182.         SetStartLine(0);        //设置起始页
  183.         SetOnOff(1);                //显示开
  184. }



  185. TIM_HandleTypeDef htim1;

  186. void SystemClock_Config(void);
  187. void Error_Handler(void);
  188. static void MX_TIM1_Init(void);


  189. static void MX_GPIO_Init(void)
  190. {
  191.   GPIO_InitTypeDef GPIO_InitStruct;

  192.   __HAL_RCC_GPIOA_CLK_ENABLE();
  193.         __HAL_RCC_GPIOB_CLK_ENABLE();
  194.   __HAL_RCC_GPIOC_CLK_ENABLE();

  195.   //HAL_GPIO_WritePin(LED_GPIO_Port, LED_Pin, GPIO_PIN_RESET);

  196.         //HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET);
  197.   GPIO_InitStruct.Pin = GPIO_PIN_0;
  198.   GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  199.   GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  200.   HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  201.        
  202.         //HAL_GPIO_WritePin(GPIOB, GPIO_PIN_1, GPIO_PIN_SET);
  203.         GPIO_InitStruct.Pin = GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;;
  204.         HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  205.        

  206.         //HAL_GPIO_WritePin(GPIOC, GPIO_PIN_13, GPIO_PIN_SET);
  207.         GPIO_InitStruct.Pin = GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;
  208.         HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  209.        
  210.        
  211. }

  212.   uchar hzk[]=
  213. {
  214. /*--  文字:  I  --*/
  215. /*--  宋体12;  此字体下对应的点阵为:宽x高=8x16   --*/
  216. 0x00,0x08,0x08,0xF8,0x08,0x08,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,

  217. /*--  文字:     --*/
  218. /*--  宋体12;  此字体下对应的点阵为:宽x高=8x16   --*/
  219. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

  220. /*--  文字:  c  --*/
  221. /*--  宋体12;  此字体下对应的点阵为:宽x高=8x16   --*/
  222. 0x00,0x00,0x00,0x80,0x80,0x80,0x00,0x00,0x00,0x0E,0x11,0x20,0x20,0x20,0x11,0x00,

  223. /*--  文字:  a  --*/
  224. /*--  宋体12;  此字体下对应的点阵为:宽x高=8x16   --*/
  225. 0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x19,0x24,0x22,0x22,0x22,0x3F,0x20,

  226. /*--  文字:  n  --*/
  227. /*--  宋体12;  此字体下对应的点阵为:宽x高=8x16   --*/
  228. 0x80,0x80,0x00,0x80,0x80,0x80,0x00,0x00,0x20,0x3F,0x21,0x00,0x00,0x20,0x3F,0x20,

  229. /*--  文字:     --*/
  230. /*--  宋体12;  此字体下对应的点阵为:宽x高=8x16   --*/
  231. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

  232. /*--  文字:  m  --*/
  233. /*--  宋体12;  此字体下对应的点阵为:宽x高=8x16   --*/
  234. 0x80,0x80,0x80,0x80,0x80,0x80,0x80,0x00,0x20,0x3F,0x20,0x00,0x3F,0x20,0x00,0x3F,

  235. /*--  文字:  a  --*/
  236. /*--  宋体12;  此字体下对应的点阵为:宽x高=8x16   --*/
  237. 0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x19,0x24,0x22,0x22,0x22,0x3F,0x20,

  238. /*--  文字:  k  --*/
  239. /*--  宋体12;  此字体下对应的点阵为:宽x高=8x16   --*/
  240. 0x08,0xF8,0x00,0x00,0x80,0x80,0x80,0x00,0x20,0x3F,0x24,0x02,0x2D,0x30,0x20,0x00,

  241. /*--  文字:  e  --*/
  242. /*--  宋体12;  此字体下对应的点阵为:宽x高=8x16   --*/
  243. 0x00,0x00,0x80,0x80,0x80,0x80,0x00,0x00,0x00,0x1F,0x22,0x22,0x22,0x22,0x13,0x00,

  244. /*--  文字:     --*/
  245. /*--  宋体12;  此字体下对应的点阵为:宽x高=8x16   --*/
  246. 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,

  247. /*--  文字:  i  --*/
  248. /*--  宋体12;  此字体下对应的点阵为:宽x高=8x16   --*/
  249. 0x00,0x80,0x98,0x98,0x00,0x00,0x00,0x00,0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00,

  250. /*--  文字:  t  --*/
  251. /*--  宋体12;  此字体下对应的点阵为:宽x高=8x16   --*/
  252. 0x00,0x80,0x80,0xE0,0x80,0x80,0x00,0x00,0x00,0x00,0x00,0x1F,0x20,0x20,0x00,0x00,
  253. };

  254. int main(void)
  255. {
  256.         uchar i,line,colum,j ;
  257.         uchar *address ;
  258.        
  259.   HAL_Init();

  260.   SystemClock_Config();

  261.   MX_GPIO_Init();
  262.   
  263.   while (1)
  264.   {
  265.                
  266.                 line=4;                               
  267.                 colum=0;                       
  268.                 address=hzk;               
  269.                 SetOnOff(1);               
  270.        
  271.                 for(i=0;i<13;i++)       
  272.                 {
  273.                         if(i<8)                               
  274.                         {
  275.                                 SelectScreen(0);                                                
  276.                                 Show_english(line,colum,address);               
  277.                                 address+=16;                                                         
  278.                                 colum+=8;                                                                 
  279.                         }       
  280.                         else                                                 
  281.                         {               
  282.                                 SelectScreen(1);
  283.                                 Show_english(line,colum,address);
  284.                                 address+=16;
  285.                                 colum+=8;                                               
  286.                         }
  287.                 }       
  288.                
  289.                 for(i = 0;i < 50;i ++)  LcdDelay(30000);        //延时
  290.   }
  291.   

  292. }


  293. void SystemClock_Config(void)
  294. {

  295.   RCC_OscInitTypeDef RCC_OscInitStruct;
  296.   RCC_ClkInitTypeDef RCC_ClkInitStruct;

  297.     /**Initializes the CPU, AHB and APB busses clocks
  298.     */
  299.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  300.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  301.   RCC_OscInitStruct.HSICalibrationValue = 16;
  302.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  303.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  304.   {
  305.     Error_Handler();
  306.   }

  307.     /**Initializes the CPU, AHB and APB busses clocks
  308.     */
  309.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  310.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  311.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  312.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  313.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  314.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

  315.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  316.   {
  317.     Error_Handler();
  318.   }

  319.     /**Configure the Systick interrupt time
  320.     */
  321.   HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);

  322.     /**Configure the Systick
  323.     */
  324.   HAL_SYSTICK_CLKSourceConfig(SYSTICK_CLKSOURCE_HCLK);

  325.   /* SysTick_IRQn interrupt configuration */
  326.   HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
  327. }

  328. /* TIM1 init function */
  329. static void MX_TIM1_Init(void)
  330. {

  331.   TIM_ClockConfigTypeDef sClockSourceConfig;
  332.   TIM_MasterConfigTypeDef sMasterConfig;

  333.   htim1.Instance = TIM1;
  334.   htim1.Init.Prescaler = 0;
  335.   htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
  336.   htim1.Init.Period = 0;
  337.   htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  338.   htim1.Init.RepetitionCounter = 0;
  339.   if (HAL_TIM_Base_Init(&htim1) != HAL_OK)
  340.   {
  341.     Error_Handler();
  342.   }

  343.   sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  344.   if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK)
  345.   {
  346.     Error_Handler();
  347.   }

  348.   sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  349.   sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  350.   if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
  351.   {
  352.     Error_Handler();
  353.   }

  354. }






  355. void Error_Handler(void)
  356. {
  357.   /* USER CODE BEGIN Error_Handler */
  358.   /* User can add his own implementation to report the HAL error return state */
  359.   while(1)
  360.   {
  361.   }
  362.   /* USER CODE END Error_Handler */
  363. }

  364. #ifdef USE_FULL_ASSERT

  365. /**
  366.    * @brief Reports the name of the source file and the source line number
  367.    * where the assert_param error has occurred.
  368.    * @param file: pointer to the source file name
  369.    * @param line: assert_param error line source number
  370.    * @retval None
  371.    */
  372. void assert_failed(uint8_t* file, uint32_t line)
  373. {
  374.   /* USER CODE BEGIN 6 */
  375.   /* User can add his own implementation to report the file name and line number,
  376.     ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  377.   /* USER CODE END 6 */

  378. }

  379. #endif
复制代码

所有资料51hei提供下载:
stm32_12864.7z (1.16 MB, 下载次数: 705)


评分

参与人数 1黑币 +100 收起 理由
admin + 100 共享资料的黑币奖励!

查看全部评分

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

使用道具 举报

沙发
ID:482998 发表于 2019-3-2 21:18 | 只看该作者
谢谢!!!!!
回复

使用道具 举报

板凳
ID:375838 发表于 2019-3-2 22:53 | 只看该作者
能用PLL作为系统时钟源吗
回复

使用道具 举报

地板
ID:527022 发表于 2019-5-4 16:09 | 只看该作者
感谢感谢
回复

使用道具 举报

5#
ID:242969 发表于 2019-5-14 10:34 | 只看该作者
仿真呢
回复

使用道具 举报

6#
ID:739190 发表于 2020-4-27 14:29 | 只看该作者
有仿真图吗
回复

使用道具 举报

7#
ID:747496 发表于 2020-5-9 12:38 | 只看该作者
谢谢楼主,找了几天,总算找到一个能够仿真的
回复

使用道具 举报

8#
ID:751363 发表于 2020-5-14 08:38 | 只看该作者
谢谢,找到了
回复

使用道具 举报

9#
ID:766129 发表于 2020-6-1 00:36 | 只看该作者
请问有仿真图吗 谢谢
回复

使用道具 举报

10#
ID:766129 发表于 2020-6-1 02:44 | 只看该作者
请问使用的是hal库还是标准库呢
回复

使用道具 举报

11#
ID:302325 发表于 2020-6-1 12:04 | 只看该作者
好资料,51黑有你更精彩!!!
回复

使用道具 举报

12#
ID:724141 发表于 2020-6-22 19:31 | 只看该作者
十分感谢,解燃眉之急
回复

使用道具 举报

13#
ID:788533 发表于 2020-6-24 15:41 | 只看该作者
hex文件在哪里的,楼主
回复

使用道具 举报

14#
ID:387687 发表于 2022-3-14 17:42 | 只看该作者
W1715967210 发表于 2020-6-24 15:41
hex文件在哪里的,楼主

没有看到仿真图。
回复

使用道具 举报

15#
ID:1031439 发表于 2022-6-1 08:40 | 只看该作者
可以自己自定义显示文字吗 在哪修改
回复

使用道具 举报

16#
ID:1031384 发表于 2022-6-2 16:13 来自手机 | 只看该作者
好资料,51黑有你更精彩!!!
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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