• 模块化编程+LCD1602调试工具——“51单片机”


    各位CSDN的uu们你们好呀,小雅兰又来啦,刚刚学完静态数码管显示和动态数码管显示,感觉真不错呢,下面,小雅兰就要开始学习模块化编程以及LCD1602调试工具的知识了,让我们进入51单片机的世界吧!!!


    模块化编程

    LCD1602调试工具


    模块化编程

     

     

    右键点击Open document ,就会显示出此头文件的内容,这句代码的意思也就是把这部分内容拷贝到此文件下

    /*--------------------------------------------------------------------------
    AT89X52.H

    Header file for the low voltage Flash Atmel AT89C52 and AT89LV52.
    Copyright (c) 1988-2002 Keil Elektronik GmbH and Keil Software, Inc.
    All rights reserved.
    --------------------------------------------------------------------------*/

    #ifndef __AT89X52_H__
    #define __AT89X52_H__

    /*------------------------------------------------
    Byte Registers
    ------------------------------------------------*/
    sfr P0      = 0x80;
    sfr SP      = 0x81;
    sfr DPL     = 0x82;
    sfr DPH     = 0x83;
    sfr PCON    = 0x87;
    sfr TCON    = 0x88;
    sfr TMOD    = 0x89;
    sfr TL0     = 0x8A;
    sfr TL1     = 0x8B;
    sfr TH0     = 0x8C;
    sfr TH1     = 0x8D;
    sfr P1      = 0x90;
    sfr SCON    = 0x98;
    sfr SBUF    = 0x99;
    sfr P2      = 0xA0;
    sfr IE      = 0xA8;
    sfr P3      = 0xB0;
    sfr IP      = 0xB8;
    sfr T2CON   = 0xC8;
    sfr T2MOD   = 0xC9;
    sfr RCAP2L  = 0xCA;
    sfr RCAP2H  = 0xCB;
    sfr TL2     = 0xCC;
    sfr TH2     = 0xCD;
    sfr PSW     = 0xD0;
    sfr ACC     = 0xE0;
    sfr B       = 0xF0;

    /*------------------------------------------------
    P0 Bit Registers
    ------------------------------------------------*/
    sbit P0_0 = 0x80;
    sbit P0_1 = 0x81;
    sbit P0_2 = 0x82;
    sbit P0_3 = 0x83;
    sbit P0_4 = 0x84;
    sbit P0_5 = 0x85;
    sbit P0_6 = 0x86;
    sbit P0_7 = 0x87;

    /*------------------------------------------------
    PCON Bit Values
    ------------------------------------------------*/
    #define IDL_    0x01

    #define STOP_   0x02
    #define PD_     0x02    /* Alternate definition */

    #define GF0_    0x04
    #define GF1_    0x08
    #define SMOD_   0x80

    /*------------------------------------------------
    TCON Bit Registers
    ------------------------------------------------*/
    sbit IT0  = 0x88;
    sbit IE0  = 0x89;
    sbit IT1  = 0x8A;
    sbit IE1  = 0x8B;
    sbit TR0  = 0x8C;
    sbit TF0  = 0x8D;
    sbit TR1  = 0x8E;
    sbit TF1  = 0x8F;

    /*------------------------------------------------
    TMOD Bit Values
    ------------------------------------------------*/
    #define T0_M0_   0x01
    #define T0_M1_   0x02
    #define T0_CT_   0x04
    #define T0_GATE_ 0x08
    #define T1_M0_   0x10
    #define T1_M1_   0x20
    #define T1_CT_   0x40
    #define T1_GATE_ 0x80

    #define T1_MASK_ 0xF0
    #define T0_MASK_ 0x0F

    /*------------------------------------------------
    P1 Bit Registers
    ------------------------------------------------*/
    sbit P1_0 = 0x90;
    sbit P1_1 = 0x91;
    sbit P1_2 = 0x92;
    sbit P1_3 = 0x93;
    sbit P1_4 = 0x94;
    sbit P1_5 = 0x95;
    sbit P1_6 = 0x96;
    sbit P1_7 = 0x97;

    sbit T2   = 0x90;       /* External input to Timer/Counter 2, clock out */
    sbit T2EX = 0x91;       /* Timer/Counter 2 capture/reload trigger & dir ctl */

    /*------------------------------------------------
    SCON Bit Registers
    ------------------------------------------------*/
    sbit RI   = 0x98;
    sbit TI   = 0x99;
    sbit RB8  = 0x9A;
    sbit TB8  = 0x9B;
    sbit REN  = 0x9C;
    sbit SM2  = 0x9D;
    sbit SM1  = 0x9E;
    sbit SM0  = 0x9F;

    /*------------------------------------------------
    P2 Bit Registers
    ------------------------------------------------*/
    sbit P2_0 = 0xA0;
    sbit P2_1 = 0xA1;
    sbit P2_2 = 0xA2;
    sbit P2_3 = 0xA3;
    sbit P2_4 = 0xA4;
    sbit P2_5 = 0xA5;
    sbit P2_6 = 0xA6;
    sbit P2_7 = 0xA7;

    /*------------------------------------------------
    IE Bit Registers
    ------------------------------------------------*/
    sbit EX0  = 0xA8;       /* 1=Enable External interrupt 0 */
    sbit ET0  = 0xA9;       /* 1=Enable Timer 0 interrupt */
    sbit EX1  = 0xAA;       /* 1=Enable External interrupt 1 */
    sbit ET1  = 0xAB;       /* 1=Enable Timer 1 interrupt */
    sbit ES   = 0xAC;       /* 1=Enable Serial port interrupt */
    sbit ET2  = 0xAD;       /* 1=Enable Timer 2 interrupt */

    sbit EA   = 0xAF;       /* 0=Disable all interrupts */

    /*------------------------------------------------
    P3 Bit Registers (Mnemonics & Ports)
    ------------------------------------------------*/
    sbit P3_0 = 0xB0;
    sbit P3_1 = 0xB1;
    sbit P3_2 = 0xB2;
    sbit P3_3 = 0xB3;
    sbit P3_4 = 0xB4;
    sbit P3_5 = 0xB5;
    sbit P3_6 = 0xB6;
    sbit P3_7 = 0xB7;

    sbit RXD  = 0xB0;       /* Serial data input */
    sbit TXD  = 0xB1;       /* Serial data output */
    sbit INT0 = 0xB2;       /* External interrupt 0 */
    sbit INT1 = 0xB3;       /* External interrupt 1 */
    sbit T0   = 0xB4;       /* Timer 0 external input */
    sbit T1   = 0xB5;       /* Timer 1 external input */
    sbit WR   = 0xB6;       /* External data memory write strobe */
    sbit RD   = 0xB7;       /* External data memory read strobe */

    /*------------------------------------------------
    IP Bit Registers
    ------------------------------------------------*/
    sbit PX0  = 0xB8;
    sbit PT0  = 0xB9;
    sbit PX1  = 0xBA;
    sbit PT1  = 0xBB;
    sbit PS   = 0xBC;
    sbit PT2  = 0xBD;

    /*------------------------------------------------
    T2CON Bit Registers
    ------------------------------------------------*/
    sbit CP_RL2= 0xC8;      /* 0=Reload, 1=Capture select */
    sbit C_T2 = 0xC9;       /* 0=Timer, 1=Counter */
    sbit TR2  = 0xCA;       /* 0=Stop timer, 1=Start timer */
    sbit EXEN2= 0xCB;       /* Timer 2 external enable */
    sbit TCLK = 0xCC;       /* 0=Serial clock uses Timer 1 overflow, 1=Timer 2 */
    sbit RCLK = 0xCD;       /* 0=Serial clock uses Timer 1 overflow, 1=Timer 2 */
    sbit EXF2 = 0xCE;       /* Timer 2 external flag */
    sbit TF2  = 0xCF;       /* Timer 2 overflow flag */

    /*------------------------------------------------
    T2MOD Bit Values
    ------------------------------------------------*/
    #define DCEN_   0x01    /* 1=Timer 2 can be configured as up/down counter */
    #define T2OE_   0x02    /* Timer 2 output enable */

    /*------------------------------------------------
    PSW Bit Registers
    ------------------------------------------------*/
    sbit P    = 0xD0;
    sbit F1   = 0xD1;
    sbit OV   = 0xD2;
    sbit RS0  = 0xD3;
    sbit RS1  = 0xD4;
    sbit F0   = 0xD5;
    sbit AC   = 0xD6;
    sbit CY   = 0xD7;

    /*------------------------------------------------
    Interrupt Vectors:
    Interrupt Address = (Number * 8) + 3
    ------------------------------------------------*/
    #define IE0_VECTOR    0  /* 0x03 External Interrupt 0 */
    #define TF0_VECTOR    1  /* 0x0B Timer 0 */
    #define IE1_VECTOR    2  /* 0x13 External Interrupt 1 */
    #define TF1_VECTOR    3  /* 0x1B Timer 1 */
    #define SIO_VECTOR    4  /* 0x23 Serial port */

    #define TF2_VECTOR    5  /* 0x2B Timer 2 */
    #define EX2_VECTOR    5  /* 0x2B External Interrupt 2 */

    #endif



     

    下面,我们来把之前的动态数码管显示的代码模块化一下~~

    小雅兰一共是把这些代码封装成了五个文件:

    Delay.h的内容:

    1. #ifndef __DELAY_H__
    2. #define __DELAY_H__
    3. void Delay(unsigned char xms);
    4. #endif

    Delay.c的内容:

    1. #include
    2. void Delay(unsigned char xms) //@12.000MHz
    3. {
    4. unsigned char data i, j;
    5. while(xms)
    6. {
    7. i = 2;
    8. j = 239;
    9. do
    10. {
    11. while (--j);
    12. } while (--i);
    13. xms--;
    14. }
    15. }

    Nixie.h的内容:

    1. #ifndef __NIXIE_H__
    2. #define __NIXIE_H__
    3. void Nixie(unsigned char Location,unsigned char Number);
    4. #endif

    Nixie.c的内容:

    1. #include
    2. #include "Delay.h"
    3. unsigned char NixieTable[]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F};
    4. void Nixie(unsigned char Location,unsigned char Number)
    5. {
    6. switch(Location)
    7. {
    8. case 1:
    9. P2_4=1;
    10. P2_3=1;
    11. P2_2=1;
    12. break;
    13. case 2:
    14. P2_4=1;
    15. P2_3=1;
    16. P2_2=0;
    17. break;
    18. case 3:
    19. P2_4=1;
    20. P2_3=0;
    21. P2_2=1;
    22. break;
    23. case 4:
    24. P2_4=1;
    25. P2_3=0;
    26. P2_2=0;
    27. break;
    28. case 5:
    29. P2_4=0;
    30. P2_3=1;
    31. P2_2=1;
    32. break;
    33. case 6:
    34. P2_4=0;
    35. P2_3=1;
    36. P2_2=0;
    37. break;
    38. case 7:
    39. P2_4=0;
    40. P2_3=0;
    41. P2_2=1;
    42. break;
    43. case 8:
    44. P2_4=0;
    45. P2_3=0;
    46. P2_2=0;
    47. break;
    48. }
    49. P0=NixieTable[Number];
    50. Delay(1);
    51. P0=0x00;
    52. }

    main.c的内容:

    1. #include
    2. #include "Nixie.h"
    3. void main()
    4. {
    5. while(1)
    6. {
    7. Nixie(1,1);
    8. Nixie(2,2);
    9. Nixie(3,3);
    10. Nixie(4,4);
    11. Nixie(5,5);
    12. Nixie(6,6);
    13. }
    14. }


     LCD1602调试工具

     LCD1602.c的内容:

    1. #include
    2. //引脚配置:
    3. sbit LCD_RS=P2^6;
    4. sbit LCD_RW=P2^5;
    5. sbit LCD_EN=P2^7;
    6. #define LCD_DataPort P0
    7. //函数定义:
    8. /**
    9. * @brief LCD1602延时函数,12MHz调用可延时1ms
    10. * @param 无
    11. * @retval 无
    12. */
    13. void LCD_Delay()
    14. {
    15. unsigned char i, j;
    16. i = 2;
    17. j = 239;
    18. do
    19. {
    20. while (--j);
    21. } while (--i);
    22. }
    23. /**
    24. * @brief LCD1602写命令
    25. * @param Command 要写入的命令
    26. * @retval 无
    27. */
    28. void LCD_WriteCommand(unsigned char Command)
    29. {
    30. LCD_RS=0;
    31. LCD_RW=0;
    32. LCD_DataPort=Command;
    33. LCD_EN=1;
    34. LCD_Delay();
    35. LCD_EN=0;
    36. LCD_Delay();
    37. }
    38. /**
    39. * @brief LCD1602写数据
    40. * @param Data 要写入的数据
    41. * @retval 无
    42. */
    43. void LCD_WriteData(unsigned char Data)
    44. {
    45. LCD_RS=1;
    46. LCD_RW=0;
    47. LCD_DataPort=Data;
    48. LCD_EN=1;
    49. LCD_Delay();
    50. LCD_EN=0;
    51. LCD_Delay();
    52. }
    53. /**
    54. * @brief LCD1602设置光标位置
    55. * @param Line 行位置,范围:1~2
    56. * @param Column 列位置,范围:1~16
    57. * @retval 无
    58. */
    59. void LCD_SetCursor(unsigned char Line,unsigned char Column)
    60. {
    61. if(Line==1)
    62. {
    63. LCD_WriteCommand(0x80|(Column-1));
    64. }
    65. else if(Line==2)
    66. {
    67. LCD_WriteCommand(0x80|(Column-1+0x40));
    68. }
    69. }
    70. /**
    71. * @brief LCD1602初始化函数
    72. * @param 无
    73. * @retval 无
    74. */
    75. void LCD_Init()
    76. {
    77. LCD_WriteCommand(0x38);//八位数据接口,两行显示,5*7点阵
    78. LCD_WriteCommand(0x0c);//显示开,光标关,闪烁关
    79. LCD_WriteCommand(0x06);//数据读写操作后,光标自动加一,画面不动
    80. LCD_WriteCommand(0x01);//光标复位,清屏
    81. }
    82. /**
    83. * @brief 在LCD1602指定位置上显示一个字符
    84. * @param Line 行位置,范围:1~2
    85. * @param Column 列位置,范围:1~16
    86. * @param Char 要显示的字符
    87. * @retval 无
    88. */
    89. void LCD_ShowChar(unsigned char Line,unsigned char Column,char Char)
    90. {
    91. LCD_SetCursor(Line,Column);
    92. LCD_WriteData(Char);
    93. }
    94. /**
    95. * @brief 在LCD1602指定位置开始显示所给字符串
    96. * @param Line 起始行位置,范围:1~2
    97. * @param Column 起始列位置,范围:1~16
    98. * @param String 要显示的字符串
    99. * @retval 无
    100. */
    101. void LCD_ShowString(unsigned char Line,unsigned char Column,char *String)
    102. {
    103. unsigned char i;
    104. LCD_SetCursor(Line,Column);
    105. for(i=0;String[i]!='\0';i++)
    106. {
    107. LCD_WriteData(String[i]);
    108. }
    109. }
    110. /**
    111. * @brief 返回值=X的Y次方
    112. */
    113. int LCD_Pow(int X,int Y)
    114. {
    115. unsigned char i;
    116. int Result=1;
    117. for(i=0;i
    118. {
    119. Result*=X;
    120. }
    121. return Result;
    122. }
    123. /**
    124. * @brief 在LCD1602指定位置开始显示所给数字
    125. * @param Line 起始行位置,范围:1~2
    126. * @param Column 起始列位置,范围:1~16
    127. * @param Number 要显示的数字,范围:0~65535
    128. * @param Length 要显示数字的长度,范围:1~5
    129. * @retval 无
    130. */
    131. void LCD_ShowNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)
    132. {
    133. unsigned char i;
    134. LCD_SetCursor(Line,Column);
    135. for(i=Length;i>0;i--)
    136. {
    137. LCD_WriteData(Number/LCD_Pow(10,i-1)%10+'0');
    138. }
    139. }
    140. /**
    141. * @brief 在LCD1602指定位置开始以有符号十进制显示所给数字
    142. * @param Line 起始行位置,范围:1~2
    143. * @param Column 起始列位置,范围:1~16
    144. * @param Number 要显示的数字,范围:-32768~32767
    145. * @param Length 要显示数字的长度,范围:1~5
    146. * @retval 无
    147. */
    148. void LCD_ShowSignedNum(unsigned char Line,unsigned char Column,int Number,unsigned char Length)
    149. {
    150. unsigned char i;
    151. unsigned int Number1;
    152. LCD_SetCursor(Line,Column);
    153. if(Number>=0)
    154. {
    155. LCD_WriteData('+');
    156. Number1=Number;
    157. }
    158. else
    159. {
    160. LCD_WriteData('-');
    161. Number1=-Number;
    162. }
    163. for(i=Length;i>0;i--)
    164. {
    165. LCD_WriteData(Number1/LCD_Pow(10,i-1)%10+'0');
    166. }
    167. }
    168. /**
    169. * @brief 在LCD1602指定位置开始以十六进制显示所给数字
    170. * @param Line 起始行位置,范围:1~2
    171. * @param Column 起始列位置,范围:1~16
    172. * @param Number 要显示的数字,范围:0~0xFFFF
    173. * @param Length 要显示数字的长度,范围:1~4
    174. * @retval 无
    175. */
    176. void LCD_ShowHexNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)
    177. {
    178. unsigned char i,SingleNumber;
    179. LCD_SetCursor(Line,Column);
    180. for(i=Length;i>0;i--)
    181. {
    182. SingleNumber=Number/LCD_Pow(16,i-1)%16;
    183. if(SingleNumber<10)
    184. {
    185. LCD_WriteData(SingleNumber+'0');
    186. }
    187. else
    188. {
    189. LCD_WriteData(SingleNumber-10+'A');
    190. }
    191. }
    192. }
    193. /**
    194. * @brief 在LCD1602指定位置开始以二进制显示所给数字
    195. * @param Line 起始行位置,范围:1~2
    196. * @param Column 起始列位置,范围:1~16
    197. * @param Number 要显示的数字,范围:0~1111 1111 1111 1111
    198. * @param Length 要显示数字的长度,范围:1~16
    199. * @retval 无
    200. */
    201. void LCD_ShowBinNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length)
    202. {
    203. unsigned char i;
    204. LCD_SetCursor(Line,Column);
    205. for(i=Length;i>0;i--)
    206. {
    207. LCD_WriteData(Number/LCD_Pow(2,i-1)%2+'0');
    208. }
    209. }

    LCD1602.h的内容:

    1. #ifndef __LCD1602_H__
    2. #define __LCD1602_H__
    3. //用户调用函数:
    4. void LCD_Init();
    5. void LCD_ShowChar(unsigned char Line,unsigned char Column,char Char);
    6. void LCD_ShowString(unsigned char Line,unsigned char Column,char *String);
    7. void LCD_ShowNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length);
    8. void LCD_ShowSignedNum(unsigned char Line,unsigned char Column,int Number,unsigned char Length);
    9. void LCD_ShowHexNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length);
    10. void LCD_ShowBinNum(unsigned char Line,unsigned char Column,unsigned int Number,unsigned char Length);
    11. #endif

    main.c的内容:

    1. #include
    2. #include "LCD1602.h"
    3. void main()
    4. {
    5. LCD_Init();
    6. LCD_ShowChar(1,1,'K');
    7. LCD_ShowString(1,3,"Hello");
    8. LCD_ShowNum(1,9,123,3);
    9. LCD_ShowSignedNum(1,13,-66,2);
    10. LCD_ShowHexNum(2,1,0xA8,2);
    11. LCD_ShowBinNum(2,4,0xAA,8);
    12. while(1)
    13. {}
    14. }

     这个调试工具还是非常方便的!!!


    好啦,小雅兰的模块化编程和LCD1602调试工具的内容就到这里啦,还要继续加油!!!

  • 相关阅读:
    字典服务的设计与管理
    法大大音视频双录新品,打造企业合规新体验
    【前端设计模式】之桥接模式
    Cherno C++视频课程-学习笔记
    嵌入式(驱动开发)(中断处理)
    【技能树笔记】网络篇——练习题解析(二)
    阿里云网络、数据中心和服务器技术创新优势说明
    worthington组织解离指南——上皮组织&结缔组织
    2023国赛C题解题思路代码及图表:蔬菜类商品的自动定价与补货决策
    spark相关网站
  • 原文地址:https://blog.csdn.net/weixin_74957752/article/details/133493625