[Cortex-M0技术交流] 新手学习笔记2——18b20+4位总线1602

[复制链接]
 楼主| jxc827 发表于 2012-6-23 16:33 | 显示全部楼层 |阅读模式
本帖最后由 jxc827 于 2012-6-23 16:40 编辑

18b20程序参考本版例程,1602使用4位总线操作,只是在8位基础上稍微修改。
程序功能将18b20采集的温度显示到1602上。
具体接线如下:
RS   ->GPB8;
R/W ->GPC6;
E     ->GPC7;
DB4 ->GPA4;
DB5 ->GPA5;
DB6 ->GPA6;
DB7 ->GPA7;

main.c
  1. #include "includes.h"
  2. uint8_t table[]="Tempratrue:";
  3. uint16_t tmp = 0;
  4. uint8_t digit_hundred, digit_ten, digit_unit;
  5. int main (void)
  6. {
  7. UNLOCKREG();
  8. DrvSYS_SetOscCtrl(E_SYS_XTL12M, 1); //使用外部12M晶振
  9. delay_ms(100); //等待外部晶振就绪
  10. LOCKREG();
  11. LCD_GPIO_initial(); //1602 相关GPIO初始化
  12. LCD_initial(); //1602 模式初始化
  13. LCD_WriteString(2,0,table); //显示第一行文字
  14. while(1)
  15. {
  16. tmp = read_temp(); //读18B20温度
  17. digit_hundred = tmp/100;
  18. digit_ten = tmp%100/10;
  19. digit_unit = tmp%10;
  20. LCD_WriteChar(0,1,digit_hundred + 0x30); //显示十位
  21. LCD_WriteChar(1,1,digit_ten + 0x30); //显示个位
  22. LCD_WriteChar(2,1,'.'); //显示小数点
  23. LCD_WriteChar(3,1,digit_unit + 0x30); //显示小数点后一位
  24. LCD_WriteChar(4,1,0xdf); //显示摄氏度单位
  25. LCD_WriteChar(5,1,'C');
  26. delay_ms(10);
  27. }
  28. }



LCD1602.c
  1. /**************************************************
  2. ** 文件名称:lcd1602.c
  3. ** 文件说明:lcd1602液晶显示,4位数据总线
  4. ** 管脚分配:
  5. ** GPA4:DB4
  6. ** GPA5:DB5
  7. ** GPA6:DB6
  8. ** GPA7:DB7
  9. ** GPB8:RS
  10. ** GPC6:RW
  11. ** GPC7:EN
  12. **************************************************/
  13. #include "lcd1602.h"
  14. void LCD_GPIO_initial(void)
  15. {
  16. DrvGPIO_Open(LCD_DB4,E_IO_OUTPUT);
  17. DrvGPIO_Open(LCD_DB5,E_IO_OUTPUT);
  18. DrvGPIO_Open(LCD_DB6,E_IO_OUTPUT);
  19. DrvGPIO_Open(LCD_DB7,E_IO_OUTPUT);
  20. DrvGPIO_Open(LCD_RS,E_IO_OUTPUT);
  21. DrvGPIO_Open(LCD_RW,E_IO_OUTPUT);
  22. DrvGPIO_Open(LCD_EN,E_IO_OUTPUT);
  23. }
  24. void delay_us(uint32_t t) //延时
  25. {
  26. uint32_t i=0;
  27. while(t--)
  28. {
  29. for (i=0;i<1;i++);
  30. }
  31. }
  32. void delay_ms(uint32_t a)
  33. {
  34. uint32_t j=0;
  35. while(a--)
  36. {
  37. for (j=0;j<1000;j++);
  38. }
  39. }
  40. /******************************************************************
  41. ** 函数名称:LCD_command_write
  42. ** 函数说明:液晶写指令子程序
  43. ** 输入参数:com
  44. ** 输出参数:无
  45. ** 返回值: 无
  46. ** 其他说明:EN=1,RS=0,RW=0,分两次将DB0-DB7的指令代码写入指令寄存器中
  47. *******************************************************************/
  48. void LCD_command_write(uint8_t com)
  49. {
  50. CLR_EN;
  51. CLR_RS;
  52. CLR_RW;
  53. delay_us(5);
  54. SET_EN;
  55. delay_us(5);
  56. GPIOA -> DOUT = com & 0xf0; //写入高4位
  57. delay_us(5);
  58. CLR_EN;
  59. delay_us(5);
  60. SET_EN;
  61. GPIOA -> DOUT = (com << 4) & 0xf0; //写入低4位
  62. delay_us(5);
  63. CLR_EN;
  64. delay_us(5);
  65. }
  66. /************************************************************
  67. ** 函数名称:LCD_data_write
  68. ** 函数说明:液晶写数据子程序
  69. ** 输入参数:dat
  70. ** 输出参数:无
  71. ** 返回值: 无
  72. ** 其他说明:EN=0,RS=1,RW=0,将DB0-DB7数据写入数据寄存器中
  73. *************************************************************/
  74. void LCD_data_write(uint8_t dat)
  75. {
  76. CLR_EN;
  77. delay_us(5);
  78. SET_RS;
  79. CLR_RW;
  80. delay_us(5);
  81. SET_EN;
  82. GPIOA -> DOUT = dat & 0xf0; //写入高4位
  83. delay_us(5);
  84. CLR_EN;
  85. delay_us(5);
  86. SET_EN;
  87. GPIOA -> DOUT = (dat << 4)&0xf0; //写入低4位
  88. delay_us(5);
  89. CLR_EN;
  90. delay_us(5);
  91. }
  92. void LCD_initial(void) //液晶初始化
  93. {
  94. LCD_command_write(0x28); //4位数据总线,2行显示模式,5*7点阵+光标显示模式
  95. delay_us(5);
  96. LCD_command_write(0x0c); //开显示,显示光标,不闪烁
  97. LCD_command_write(0x01); //清显示
  98. }
  99. void LCD_WriteString(uint8_t x,uint8_t y,uint8_t *String)
  100. {
  101. LCD_SetXY(x,y);
  102. delay_ms(10); //加等待
  103. while(*String) //判断是否已经写完
  104. {
  105. LCD_data_write(*String++); // 写入当前指向的字符,并将指针指向下一个字符
  106. delay_ms(10);
  107. }
  108. }
  109. void LCD_SetXY(uint8_t x,uint8_t y)
  110. {
  111. if(y == 0)
  112. {
  113. LCD_command_write(0x80 + x); // 第一行
  114. }
  115. else
  116. {
  117. LCD_command_write(0xc0 + x); // 第二行
  118. }
  119. }
  120. void LCD_WriteChar(uint8_t x,uint8_t y,uint8_t data)
  121. {
  122. LCD_SetXY(x,y);
  123. delay_ms(10); //加等待
  124. LCD_data_write(data);
  125. }



18b20.c代码就不贴了,参考版上例程。

常温测试效果:
开发板放在电脑风扇出口:

完整的工程包:
祝大家端午节快乐!
完。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?注册

×
 楼主| jxc827 发表于 2012-7-6 22:45 | 显示全部楼层
没人看,自己顶
abin0415 发表于 2012-7-7 21:46 | 显示全部楼层
帮顶
您需要登录后才可以回帖 登录 | 注册

本版积分规则

6

主题

53

帖子

1

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