- /************* 功能说明 **************
- 本例程基于STC8H1K24为主控芯片的STC8H实验箱9进行编写测试
- 4位数码管显示,显示范围-40度-160度
- 下载时钟选择 6 MHZ adc为P17
- ******************************************/
- #include <STC8H.h>
- code unsigned char DuanMa[]={ //段码P27-P20分别对应a-h
- 0x03,/*0*/
- 0x9F,/*1*/
- 0x25,/*2*/
- 0x0D,/*3*/
- 0x99,/*4*/
- 0x49,/*5*/
- 0x41,/*6*/
- 0x1F,/*7*/
- 0x01,/*8*/
- 0x09,/*9*/};
- //电子温度表测量范围-40-120度
- //MF52E 10K at 25, B = 3950, ADC = 12 bits
- code unsigned int DuiZhaoBiao[]={//对照表
- 140, //;-40 0
- 149, //;-39 1
- 159, //;-38 2
- 168, //;-37 3
- 178, //;-36 4
- 188, //;-35 5
- 199, //;-34 6
- 210, //;-33 7
- 222, //;-32 8
- 233, //;-31 9
- 246, //;-30 10
- 259, //;-29 11
- 272, //;-28 12
- 286, //;-27 13
- 301, //;-26 14
- 317, //;-25 15
- 333, //;-24 16
- 349, //;-23 17
- 367, //;-22 18
- 385, //;-21 19
- 403, //;-20 20
- 423, //;-19 21
- 443, //;-18 22
- 464, //;-17 23
- 486, //;-16 24
- 509, //;-15 25
- 533, //;-14 26
- 558, //;-13 27
- 583, //;-12 28
- 610, //;-11 29
- 638, //;-10 30
- 667, //;-9 31
- 696, //;-8 32
- 727, //;-7 33
- 758, //;-6 34
- 791, //;-5 35
- 824, //;-4 36
- 858, //;-3 37
- 893, //;-2 38
- 929, //;-1 39
- 965, //;0 40
- 1003, //;1 41
- 1041, //;2 42
- 1080, //;3 43
- 1119, //;4 44
- 1160, //;5 45
- 1201, //;6 46
- 1243, //;7 47
- 1285, //;8 48
- 1328, //;9 49
- 1371, //;10 50
- 1414, //;11 51
- 1459, //;12 52
- 1503, //;13 53
- 1548, //;14 54
- 1593, //;15 55
- 1638, //;16 56
- 1684, //;17 57
- 1730, //;18 58
- 1775, //;19 59
- 1821, //;20 60
- 1867, //;21 61
- 1912, //;22 62
- 1958, //;23 63
- 2003, //;24 64
- 2048, //;25 65
- 2093, //;26 66
- 2137, //;27 67
- 2182, //;28 68
- 2225, //;29 69
- 2269, //;30 70
- 2312, //;31 71
- 2354, //;32 72
- 2397, //;33 73
- 2438, //;34 74
- 2479, //;35 75
- 2519, //;36 76
- 2559, //;37 77
- 2598, //;38 78
- 2637, //;39 79
- 2675, //;40 80
- 2712, //;41 81
- 2748, //;42 82
- 2784, //;43 83
- 2819, //;44 84
- 2853, //;45 85
- 2887, //;46 86
- 2920, //;47 87
- 2952, //;48 88
- 2984, //;49 89
- 3014, //;50 90
- 3044, //;51 91
- 3073, //;52 92
- 3102, //;53 93
- 3130, //;54 94
- 3157, //;55 95
- 3183, //;56 96
- 3209, //;57 97
- 3234, //;58 98
- 3259, //;59 99
- 3283, //;60 100
- 3306, //;61 101
- 3328, //;62 102
- 3351, //;63 103
- 3372, //;64 104
- 3393, //;65 105
- 3413, //;66 106
- 3432, //;67 107
- 3452, //;68 108
- 3470, //;69 109
- 3488, //;70 110
- 3506, //;71 111
- 3523, //;72 112
- 3539, //;73 113
- 3555, //;74 114
- 3571, //;75 115
- 3586, //;76 116
- 3601, //;77 117
- 3615, //;78 118
- 3628, //;79 119
- 3642, //;80 120
- 3655, //;81 121
- 3667, //;82 122
- 3679, //;83 123
- 3691, //;84 124
- 3702, //;85 125
- 3714, //;86 126
- 3724, //;87 127
- 3735, //;88 128
- 3745, //;89 129
- 3754, //;90 130
- 3764, //;91 131
- 3773, //;92 132
- 3782, //;93 133
- 3791, //;94 134
- 3799, //;95 135
- 3807, //;96 136
- 3815, //;97 137
- 3822, //;98 138
- 3830, //;99 139
- 3837, //;100 140
- 3844, //;101 141
- 3850, //;102 142
- 3857, //;103 143
- 3863, //;104 144
- 3869, //;105 145
- 3875, //;106 146
- 3881, //;107 147
- 3887, //;108 148
- 3892, //;109 149
- 3897, //;110 150
- 3902, //;111 151
- 3907, //;112 152
- 3912, //;113 153
- 3917, //;114 154
- 3921, //;115 155
- 3926, //;116 156
- 3930, //;117 157
- 3934, //;118 158
- 3938, //;119 159
- 3942 //;120 160
- };
- #define SMG_WeiShu 3
- idata unsigned char SMG_HuanCun[SMG_WeiShu];//数码管缓存
- void main()
- {
- P_SW2=0x80;
- P0M0 = 0x00; P0M1 = 0x00;
- P1M0 = 0x00; P1M1 = 0xc0;
- P2M0 = 0x00; P2M1 = 0x00;
- P3M0 = 0x00; P3M1 = 0x00;
- P4M0 = 0x00; P4M1 = 0x00;
- P5M0 = 0x00; P5M1 = 0x00;
- P6M0 = 0x00; P6M1 = 0x00;
- P7M0 = 0x00; P7M1 = 0x00;
- P1IE=0xF7;
- ADCCFG=0x20;
- TH0=0xF9;
- IP=0x02;
- TCON=0x50;
- IE=0xAA;
- for(;;)PCON|=0x01;//省电模式
- }
- SMG_SaoMiao()interrupt 1
- {//数码管扫描
- static idata unsigned char a;
- switch (a)
- {
- case2:P14 = 0 ;P15 = 1; P33 = 1; break; //P14 P15 P33位码
- case1:P14 = 1 ;P15 = 0; P33 = 1; break;
- case0:P14 = 1 ;P15 = 1; P33 = 0; break;
- }
- P2=SMG_HuanCun[a];
- if(++a>=SMG_WeiShu)a=0;
- }
- ShiZhong()interrupt 3
- {//时钟
- idata unsigned char HM;
- if(++HM>4)
- {//调节刷新速度
- HM=0;
- ADC_CONTR=0xC7;//开始测量ADC
- }
- }
- NTC_DuWenDu()interrupt 5
- {
- //NTC读温度
- idata unsigned char b,c;
- idata unsigned int a;
- ADC_CONTR=0x80;
- a=4096-(ADC_RES<<8|ADC_RESL);
- for(b=1;b<sizeof(DuiZhaoBiao)/2;b++)
- {
- if(a<DuiZhaoBiao[b])
- {
- idata char d;
- SMG_HuanCun[0]=DuanMa[(a-DuiZhaoBiao[b-1])*10/(DuiZhaoBiao[b]-DuiZhaoBiao[b-1])];//计算小数
- d=b-40;
- SMG_HuanCun[1]=DuanMa[d%10]&0x7F;//显示小数点
- c=d/10%10;
- if(c==0)
- {
- if(d<0)SMG_HuanCun[2]=0xBF;//负号
- else SMG_HuanCun[2]=0xFF;//高位消0
- SMG_HuanCun[3]=0xFF;//高位消0
- return;
- }
- else SMG_HuanCun[2]=DuanMa[c];
- c=d/100%10;
- if(c==0)
- {
- if(d<0)SMG_HuanCun[3]=0xBF;//负号
- else SMG_HuanCun[3]=0xFF;//高位消0
- }
- else SMG_HuanCun[3]=DuanMa[c];
- return;
- }
- }
- }
复制代码
|