truong_giang2211
Junior Member level 1
- Joined
- Dec 20, 2009
- Messages
- 18
- Helped
- 1
- Reputation
- 2
- Reaction score
- 1
- Trophy points
- 1,283
- Location
- tphcm
- Activity points
- 1,455
Hi everyone,
I am newer.
As below is my code+hex file and simulate circuit in Protues7.8. I am using CCS V5.xx
I finished my project when i use PIC16f877a.
But when i copy code(i just changed PIN DATA and SCK), it can work, but the value of temperature not change(update) ? Reason why ??
Thanks for your read !
My code:
I am newer.
As below is my code+hex file and simulate circuit in Protues7.8. I am using CCS V5.xx
I finished my project when i use PIC16f877a.
But when i copy code(i just changed PIN DATA and SCK), it can work, but the value of temperature not change(update) ? Reason why ??
Thanks for your read !
My code:
Code C - [expand] 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 /////////////////////////////////////////////////////////////// #include <16F1827.h> //#DEVICE ADC=8 #include <stdio.h> #include <math.h> #FUSES NOWDT, HS, NOPUT, PROTECT, NODEBUG, NOBROWNOUT, NOLVP #use delay(clock = 4000000) #USE RS232(baud=2400, xmit=PIN_B5, rcv=PIN_B2) #define sht_data_pin PIN_B7 #define sht_clk_pin PIN_B6 #define LED PIN_B0 #define LED1 PIN_B3 void comstart (void) { output_float(sht_data_pin); //data high output_bit(sht_clk_pin, 0); //clk low delay_us(1); output_bit(sht_clk_pin, 1); //clk high delay_us(1); output_bit(sht_data_pin, 0); //data low delay_us(1); output_bit(sht_clk_pin, 0); //clk low delay_us(2); output_bit(sht_clk_pin, 1); //clk high delay_us(1); output_float(sht_data_pin); //data high delay_us(1); output_bit(sht_clk_pin, 0); //clk low } // write data to SHT75 int1 comwrite (int8 iobyte) { int8 i, mask = 0x80; int1 ack; //Shift out command delay_us(4); for(i=0; i<8; i++) { output_bit(sht_clk_pin, 0); //clk low if((iobyte & mask) > 0) output_float(sht_data_pin); //data high if MSB high else output_bit(sht_data_pin, 0); //data low if MSB low delay_us(1); output_bit(sht_clk_pin, 1); //clk high delay_us(1); mask = mask >> 1; //shift to next bit } //Shift in ack output_bit(sht_clk_pin, 0); //clk low delay_us(1); ack = input(sht_data_pin); //get ack bit output_bit(sht_clk_pin, 1); //clk high delay_us(1); output_bit(sht_clk_pin, 0); //clk low return(ack); } // read data from SHT75 int16 comread (void) { int8 i; int16 iobyte = 0; const int16 mask0 = 0x0000; const int16 mask1 = 0x0001; //shift in MSB data for(i=0; i<8; i++) { iobyte = iobyte << 1; output_bit(sht_clk_pin, 1); //clk high delay_us(1); if (input(sht_data_pin)) iobyte |= mask1; //shift in data bit else iobyte |= mask0; output_bit(sht_clk_pin, 0); //clk low delay_us(1); } //send ack 0 bit output_bit(sht_data_pin, 0); //data low delay_us(1); output_bit(sht_clk_pin, 1); //clk high delay_us(2); output_bit(sht_clk_pin, 0); //clk low delay_us(1); output_float(sht_data_pin); //data high //shift in LSB data for(i=0; i<8; i++) { iobyte = iobyte << 1; output_bit(sht_clk_pin, 1); //clk high delay_us(1); if (input(sht_data_pin)) iobyte |= mask1; //shift in data bit else iobyte |= mask0; output_bit(sht_clk_pin, 0); //clk low delay_us(1); } //send ack 1 bit output_float(sht_data_pin); //data high delay_us(1); output_bit(sht_clk_pin, 1); //clk high delay_us(2); output_bit(sht_clk_pin, 0); //clk low return(iobyte); } //***** Function to wait for SHT75 reading ***** void comwait (void) { int16 sht_delay; output_float(sht_data_pin); //data high output_bit(sht_clk_pin, 0); //clk low delay_us(1); for(sht_delay=0; sht_delay<30000; sht_delay++) // wait for max 300ms { if (!input(sht_data_pin)) break; //if sht_data_pin low, SHT75 ready delay_us(10); } } // reset SHT75 communication void comreset (void) { int8 i; output_float(sht_data_pin); //data high output_bit(sht_clk_pin, 0); //clk low delay_us(2); for(i=0; i<9; i++) { output_bit(sht_clk_pin, 1); //toggle clk 9 times delay_us(2); output_bit(sht_clk_pin, 0); delay_us(2); } comstart(); } //***** Function to soft reset SHT75 ***** void sht_soft_reset (void) { comreset(); //SHT75 communication reset comwrite(0x1e); //send SHT75 reset command delay_ms(15); //pause 15 ms } //***** Function to measure SHT75 temperature ***** int16 measuretemp (void) { int1 ack; int16 iobyte; comstart(); //alert SHT75 ack = comwrite(0x03); //send measure temp command and read ack status if(ack == 1) return; comwait(); //wait for SHT75 measurement to complete iobyte = comread(); //read SHT75 temp data return(iobyte); } //***** Function to measure SHT75 RH ***** int16 measurehumid (void) { int1 ack; int16 iobyte; comstart(); //say to SHT75 ack = comwrite(0x05); //send measure RH command and read ack status if(ack == 1) return; comwait(); //wait for SHT75 measurement to complete iobyte = comread(); //read SHT75 temp data return(iobyte); } //calculate SHT75 temp & RH void calculate_data (int16 temp, int16 humid, float & tc, float & rhlin, float & rhtrue) { float rh; //float truehumid1; //calculate temperature reading tc = ((float) temp * 0.01) - 40.0; //calculate Real RH reading rh = (float) humid; // rh linear rhlin = (rh * 0.0405) - (rh * rh * 0.0000028) - 4.0; //calculate True RH reading rhtrue = ((tc - 25.0) * (0.01 + (0.00008 * rh))) + rhlin; } //***** Function to measure & calculate SHT75 temp & RH ***** void sht_rd (float & temp, float & truehumid) { int16 restemp, reshumid; float realhumid; restemp = 0; truehumid = 0; restemp = measuretemp(); //measure temp reshumid = measurehumid(); //measure RH calculate_data (restemp, reshumid, temp, realhumid, truehumid); //calculate temp & RH } //***** Function to initialise SHT75 on power-up ***** void sht_init (void) { comreset(); //reset SHT75 delay_ms(20); //delay for power-up } //===================================== //===================================== void main(void) { float restemp, truehumid; sht_init(); delay_ms(200); long a[30]={0x00,0xff,0xff,0x00,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B}; // int so1=0,so2=0; while(TRUE) { delay_ms(100); sht_rd (restemp, truehumid); a[4]= restemp; putc(restemp); delay_ms(500); output_toggle(LED); delay_ms(200); //delay 200 ms between reading to prevent self heating of sensor } } ///////////////////////////////////////////////////////////////
Attachments
Last edited by a moderator: