[DemoCode下载] 单片机C语言教程之函数封装

[复制链接]
1565|2
 楼主| ideafor 发表于 2016-5-8 20:39 | 显示全部楼层 |阅读模式
结构体可以把不同类型的变量放在一起,从而能实现对相互联系的变量进行打包。那么结构体能不能把函数打包呢?事实上在单片机编程中,通常我们采用模块化的编程。自然这里面会涉及到很多不同函数的定义和调用,那么如何将这些函数打包呢?函数不能作为结构体的成员,但是函数指针可以作为结构体成员。这里我们通过函数指针在结构体中的应用来介绍如何对不同但是相关的函数打包。

/*目的:练习使用结构体对函数封装同时使用指针来操作
1. 定义结构体
2. 定义三个函数
3. 定义结构体变量
4. 定义结构体指针
*/
  1. #include "stdio.h"
  2. typedef unsigned char uchar;
  3. struct mystruct{
  4.     void(*myfun1)(void);
  5.     void(*myfun2)(void);
  6.     void(*myfun3)(void);
  7. };
  8. struct mystruct test;
  9. struct mystruct *testp;
  10. void myfun1(void);
  11. void myfun2(void);
  12. void myfun3(void);
  13. void main(void)
  14. {
  15.     struct mystruct test = {
  16.         myfun1,
  17.         myfun2,
  18.         myfun3,
  19.     };
  20.     testp = &test;
  21.     testp->myfun1();
  22.     testp->myfun2();
  23.     testp->myfun3();
  24.     system("pause");
  25. }

  26. void myfun1(void)
  27. {
  28.     printf("myfun1\n");
  29. }

  30. void myfun2(void)
  31. {
  32.     printf("myfun2\n");
  33. }

  34. void myfun3(void)
  35. {
  36.     printf("myfun3\n");
  37. }
  38. 在以上代码中,我们定义了一个mystruct结构体,其中有三个函数指针作为结构体成员,然后定义了一个结构体指针,通过指针来调用各个函数。
  39. 现在我们把这种方法应用到单片机编程中,实现对LCD1602各个功能函数的打包。

  40. /*目的:练习使用结构体封装函数,然后使用结构体指针来控制LCD
  41. 1. 定义一个结构体
  42. 2. 定义一个结构体变量
  43. 3. 定义几个函数
  44. 4. 定义结构体指针
  45. 5. LCD控制引脚,初始化,在哪里显示,显示什么
  46. */
  47. #include "reg52.h"
  48. typedef unsigned char uchar;
  49. sbit RS=P2^7;
  50. sbit EN=P2^6;
  51. struct mystruct {
  52.     void (*LCD_init)(void);
  53.     void (*write_data)(uchar);
  54.     void (*write_string)(uchar *);
  55.     void (*write_com)(uchar);
  56.     void (*delay)(uchar);
  57. };

  58. void LCD_init(void);
  59. void delayUs(uchar t);
  60. void delayMs(uchar t);
  61. void write_com(uchar mycmd);
  62. void write_data(uchar mydata);
  63. void write_string(uchar *p);
  64. struct mystruct LCD_struct;
  65. struct mystruct *LCD;
  66. void main(void)
  67. {
  68.     struct mystruct LCD_struct={
  69.         LCD_init,
  70.         write_data,
  71.         write_string,
  72.         write_com,
  73.         delayMs,
  74.     };
  75.     while(1)
  76.     {
  77.             LCD=&LCD_struct;
  78.             LCD->LCD_init();
  79.             LCD->write_com(0x80);
  80.             LCD->write_string("This is struct");
  81.             while(1);
  82.     }
  83. }
  84. void LCD_init(void)
  85. {
  86.     delayMs(15);
  87.     write_com(0x38);
  88.     delayMs(5);
  89.     write_com(0x38);
  90.     write_com(0x08);
  91.     write_com(0x01);
  92.     write_com(0x06);
  93.     write_com(0x0c);
  94. }

  95. void write_data(uchar mydata)
  96. {
  97.   delayMs(5);//注意这里需要延时5ms比较保险
  98.   P0=mydata;
  99.   RS=1;
  100.   EN=1;
  101.   delayUs(5);
  102.   EN=0;
  103. }

  104. void write_string(uchar *p) //如何写一个字符串
  105. {
  106.     while(*p)
  107.     {
  108.         write_data(*p);
  109.         p++;
  110.     }
  111. }
  112. void write_com(uchar mycmd)
  113. {
  114.   delayMs(5);//注意这里需要延时5ms比较保险 代替判断忙信号
  115.   P0=mycmd;        //准备好指令
  116.   RS=0;         //告诉LCD1602,P0中放的是指令不是数据
  117.   EN=1;
  118.   delayUs(5); //根据时序图,脉冲要有一定宽度
  119.   EN=0;           //使指令有效,开始执行
  120. }  
  121. void delayUs(uchar t)
  122. {
  123.         while(--t);
  124. }

  125. void delayMs(uchar t)
  126. {
  127.         while(--t)
  128.         {
  129.                 delayUs(245);
  130.                 delayUs(245);
  131.         }
  132. }


zhuotuzi 发表于 2016-5-8 23:10 | 显示全部楼层
函数也可以作为结构体的成员吗?
Jessicakjdsl 发表于 2016-5-10 21:24 | 显示全部楼层
还真没用过结构体把函数打包的例子,只用过把相同类型的参数打包的
您需要登录后才可以回帖 登录 | 注册

本版积分规则

30

主题

149

帖子

1

粉丝
快速回复 在线客服 返回列表 返回顶部