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
|
#include <reg52.h> #include "Delay.h" sbit LED=P1^0; sbit st = P3^0; sbit eoc = P3^1; sbit oe = P3^2; sbit RS = P3^3; sbit RW = P3^4; sbit E = P3^5;
unsigned int vol = 0; unsigned char t[] = {"012346789"}; unsigned char str[] = {"VOLTAGE: "}; unsigned char i; unsigned char PWM_COUNT; unsigned int HUXI_COUNT; unsigned char PWM_VLAUE; bit direc_flag;
void writedate(unsigned int date) { RS = 1; RW = 0; E = 0; P2 = date; Delay(5); E = 1; E = 0; }
void writecom(unsigned char com) { RS = 0; RW = 0; E = 0; P2 = com; Delay(5); E = 1; E = 0; }
void initlcd() { writecom(0x38); writecom(0x0c); writecom(0x06); writecom(0x01); }
void timer0_init() { TMOD=0x02;
TH0=(65536-50000)/256; TL0=(65536-50000)%256; TR0=1; ET0=1; EA=1; PWM_COUNT =0; } void time0() interrupt 1 { PWM_COUNT++; HUXI_COUNT++; if(PWM_COUNT == PWM_VLAUE) LED = 1; if(PWM_COUNT == 40) { LED = 0; PWM_COUNT = 0; } if((HUXI_COUNT == 200) && (direc_flag == 0)) { HUXI_COUNT = 0; PWM_VLAUE++; if(PWM_VLAUE == 39) direc_flag = 1; } if((HUXI_COUNT == 200) && (direc_flag == 1)) { HUXI_COUNT = 0; PWM_VLAUE--; if(PWM_VLAUE == 1) direc_flag = 0; } }
void adc() { st = 0; st = 1; Delay(5); st = 0; while (eoc != 1); oe = 1; vol = P0; oe = 0; }
void display() { unsigned char temp0 = 0, temp1 = 0, temp2 = 0; int i = 0;
vol = vol * 100 / 51; temp0 = vol/100; temp1 = (vol%100)/10; temp2 = vol%10;
writecom(0x80); Delay(5); for(i=0; i<8; i++) { writedate(str[i]); Delay(5); } writecom(0x80 + 0x40 + 4); Delay(5); writedate(t[temp0]); Delay(5); writedate('.'); Delay(5); writedate(t[temp1]); Delay(5); writedate(t[temp2]); Delay(5); writedate('V'); Delay(5); }
void main() { HUXI_COUNT = 0; PWM_COUNT = 0; PWM_VLAUE = 20; direc_flag = 0; LED = 1; timer0_init(); initlcd();
while(1){
adc(); display();
} }
#include <intrins.h> #include <reg52.h> #include "Delay.h"
void Delay(unsigned int n){
unsigned char i, j;
while(n--) { i = 2; j = 239; do { while (--j); } while (--i); }
}
#ifndef __Delay_H__ #define __Delay_H__
void Delay(unsigned int times);
#endif
|