1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
| #include "reg51.h" #include "absacc.h" #include "intrins.h"
#define uchar unsigned char #define uint unsigned int sbit TX = P1^0; sbit RX = P1^1;
sbit R1=P3^0; sbit R2=P3^1; sbit R3=P3^2; sbit R4=P3^3;
sbit C1=P4^4; sbit C2=P4^2; sbit C3=P3^5; sbit C4=P3^4;
sbit Buzz=P0^6;
unsigned int distance = 0; uchar mode; uchar mode_k; uint high=50; uint low=0; uchar count_b; uchar mode_led;
unsigned char code shuzi[]={0XC0,0XF9,0XA4,0XB0,0X99,0X92,0X82,0XF8,0X80,0X90,0xbf,0xff,0x7f};
unsigned char code zimu[]={0x88,0x83,0xc6,0xa1,0x86,0x8e,0x89,0xc7,0x8c,0xc1,0xc8};
unsigned char code weizhi[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80,0x00,0x00,0x00,0x00,0x00,0x00};
unsigned char ledweizhi[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,0xff};
void delay_ms(int ms) { int q,w; for(q=0;q<ms;q++) { for(w=845;w>0;w--); } } void shumaguan_shuzi(uchar a,uchar b) { delay_ms(1); P2=(P2&0X1f)|0xC0;P0=weizhi[a]; P2=(P2&0X1f)|0xE0;P0=shuzi[b]; delay_ms(1); P2 = (P2 & 0x1f) | 0xc0; P0 = 0x00; P2 = P2 & 0x1f; }
void shumaguan_zimu(uchar a,uchar b) { delay_ms(1); P2=(P2&0X1f)|0xC0;P0=weizhi[a]; P2=(P2&0X1f)|0xE0;P0=zimu[b]; delay_ms(1); P2 = (P2 & 0x1f) | 0xc0; P0 = 0x00; P2 = P2 & 0x1f; } void ledlight(uchar x) { P2=(P2&0X1f)|0x80; P0=ledweizhi[x]; } void Init_Keys() { R1=R2=R3=R4=1; C1=C2=C3=C4=1; } void Init_timer0() { TMOD = 0x01; TH0 = (65535 - 50000) / 256; TL0 = (65535 - 50000) % 256; EA = 1; ET0 = 1; TR0 = 1; } void Service_timer0() interrupt 1 { count_b++; if(count_b==4) { count_b=0; if(mode_led==0) { mode_led=1; } else if(mode_led==1) { mode_led=0; } } } void L3_blink() { if(mode_led==1) { ledlight(2); } if(mode_led==0) { ledlight(8); } } void display_d() { if(distance == 999) { shumaguan_zimu(0,5);
} else { shumaguan_shuzi(5, distance / 100); shumaguan_shuzi(5,12); shumaguan_shuzi(6, (distance % 100) / 10);
shumaguan_shuzi(7, distance % 10);
} P2=0x80; P0=0x00; } void Delay12us() { unsigned char i; _nop_(); _nop_(); i = 33; while (--i); } void Send_Wave() { unsigned char i; for(i = 0; i < 8; i++) { TX = 1; Delay12us(); TX = 0; Delay12us(); } } void Measure_Distance() { unsigned int time = 0; TMOD &= 0x0f; TL1 = 0x00; TH1 = 0x00; Send_Wave(); TR1 = 1; while((RX == 1) && (TF1 == 0)); TR1 = 0; if(TF1 == 0) { time = TH1; time = (time << 8) | TL1; distance = ((time / 10) * 17) / 100 + 3; } else { TF1 = 0; distance = 999; } } void display_h() { mode_k=0; shumaguan_zimu(0,9); shumaguan_shuzi(6,high/10); shumaguan_shuzi(7,high%10); ledlight(0); } void display_l() { mode_k=1; shumaguan_zimu(0,10); shumaguan_shuzi(6,low/10); shumaguan_shuzi(7,low%10); ledlight(1); } void select_mode() { switch(mode) { case(0):display_d();break; case(1):display_h();break; case(2):display_l();break; } } void alarm() { if(distance<=low||distance>=high) { P2 = ((P2 & 0x1F) | 0xA0); Buzz=1; L3_blink(); } else { P2 = ((P2 & 0x1F) | 0xA0); Buzz=0; } } void Scan_S4() { R1=1;R2=1;R3=1;R4=0; C2=1;C3=1;C4=1; if(C1==0) { while(C1==0); mode++; if(mode==3) mode=0; } } void Scan_S8() { R1=1;R2=1;R3=1;R4=0; C3=1;C4=1;C1=1; if(C2==0) { while(C2==0); if(mode_k==0) { high=high+10; } if(mode_k==1) { low=low+10; } } } void Scan_S12() { R1=1;R2=1;R3=1;R4=0; C4=1;C1=1;C2=1; if(C3==0) { while(C3==0); if(mode_k==0) { high=high-10; } if(mode_k==1) { low=low-10; } } } void main() { Init_Keys(); Init_timer0(); while(1) { Measure_Distance(); select_mode(); Scan_S12(); Scan_S8(); Scan_S4(); alarm(); } }
|