pic18单片机热敏电阻测温查表程序
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
//;*************************************************
1.//;* heat.c **
2.//;*************************************************
3.//;* 本程序为热敏电阻输入处理模块程序
4.//;* 将温度值在LCD特定位置显示
5.//;* 占用I/O RA1,RB5,RB4,RB3
6.//;* 使用RAM
7.//;* 程序包括:
8.//;* - tempdeal 热敏电阻输入处理子程序
9.//;* - heattab 温度值校准表
10.//;*
11.//;* 入口参数无
12.//;* 出口参数 TempH,TempL (温度值)
13.//;*************************************************
14.#include
15.#define TSRCLK TRISD5
16.#define TRCLK TRISD4
17.#define TSER TRISD3
18.#define SRCLK RD5
19.#define RCLK RD4
20.#define SER RD3
21.
22.
23.void preled(void)
24.{
25. TSRCLK=0;
26. TRCLK=0;
27. TSER=0;
28. SRCLK=0;
29. RCLK=0;
30. SER=0;
31.}
32.void moniled (unsigned char led_data)
33.{
34. //MONILED; 入口参数LED_DATA,需要为
35. //;芯片全部在时钟上升沿完成动作,故需在低电平期间完成数据的变
化。先发送高位
36. unsigned char i;
37. for(i=8;i!=0;i--)
38. {
39. if(led_data&0x80)
40. SER=1;
41. else
42. SER=0;
43. led_data<=1;
44. asm("nop");
45. SRCLK=1;
46. asm("nop");
47. SRCLK=0;
48. }
49. RCLK=0;
50. asm("nop");
51. RCLK=1;
52. RCLK=0;
53.}
54.
55.void delay(unsigned int i)
56.{
57. while(i)
58. {
59. NOP();
60. --i;
61. }
62. }
63.
64.void iniheat458()
65.{
66. TRISA1=1;//RA1输入
67. ADCON1=0;
68. ADCON0=0;
69. ADCS0=1;//设置转换时钟
70. CHS0=1;//设置通道为RA1
71. PCFG2=1;//0100,设置各通道属性及参考电压为VDD,VSS
72. ADON=1;//使能AD
73.}
74.
75.unsigned char tempdeal()
76.{
77. const char heattab[255]={
78. 0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3
,4,4,4,4,5,5,5,
79. 6,6,6,7,7,7,8,8,8,9,9,0xa,0xa,0
xb,0xb,0xc,0xc,
80. 0xd,0xd,0xe,0xe,0xf,0xf,0xf,0x1
0,0x10,0x10,
81. 0x11,0x11,0x12,0x12,0x12,0x13,0
x13,0x14,0x14,0x14,
82. 0x15,0x15,0x15,0x16,0x16,0x16,0
x17,0x17,0x18,0x18,
83. 0x19,0x19,0x1a,0x1a,0x1b,0x1b,0
x1c,0x1c,0x1c,
84. 0x1d,0x1d,0x1d,0x1e,0x1e,0x1f,0
x1f,0x20,0x20,
85. 0x21,0x21,0x22,0x22,0x23,0x23,0
x24,0x24,0x25,0x25,
86. 0x26,0x26,0x27,0x28,0x29,0x29,0
x2a,0x2a,0x2b,0x2b,
87. 0x2c,0x2d,0x2e,0x2f,0x30,0x32
88. };
89. unsigned char temp;
90. GODONE=1;
91. while(GODONE);
92. ADRESH-=0x3A;
93. temp=heattab[(ADRESH)];
94. return temp;
95.}
96.
97.void main()
98.{
99. const char ledtab[10]={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0
x07,0x7F,0x6F }; //定义依次显示"0"~"9"的数组元素
100. unsigned char temp,temph,templ;
101.
102. preled();
103. iniheat458();
104. while(1)
105. {
106. temp=tempdeal();
107. temph=temp/10;
108. templ=temp%10;
109. moniled(ledtab[templ]);
110. moniled(ledtab[temph]);
111. delay(65535);//延时1S
112. delay(65535);
113. }
114.}
115.