#include <stdio.h>#include <math.h>#include "mscbemb.h"#include "temp36.h"Functions | |
| void | publishCtlCsr (void) |
| void | publishErr (bit errbit) |
| void | publishAll () |
| void | autocalibration (float reference) |
| int | eepageAddrConvert (unsigned int index) |
| void | user_init (unsigned char init) |
| void | user_write (unsigned char index) reentrant |
| unsigned char | user_read (unsigned char index) |
| unsigned char | user_func (unsigned char *data_in, unsigned char *data_out) |
| void | user_loop (void) |
Variables | |
| unsigned char idata | _n_sub_addr = 1 |
| char code | node_name [] = "temp36" |
| char idata | svn_rev_code [] = "$Rev: 1180 $" |
| bit | EEP_CTR_Flag |
| bit | LTC2600_Flag |
| bit | REF_Flag |
| int xdata * | eep_address |
| static unsigned char | tcounter |
| static unsigned char xdata | eeprom_flag = CLEAR |
| unsigned char xdata | eeprom_wstatus |
| unsigned char xdata | eeprom_rstatus |
| unsigned int xdata | eeptemp_addr |
| unsigned char xdata * | eeptemp_source |
| unsigned char xdata | eep_request |
| unsigned char xdata | status |
| float xdata | humidity |
| float xdata | htemperature |
| unsigned int xdata | rSHTtemp1 |
| unsigned int xdata | rSHThumi1 |
| unsigned int xdata | rSHTtemp2 |
| unsigned int xdata | rSHThumi2 |
| unsigned char xdata | FCSorig1 |
| unsigned char xdata | FCSdevi1 |
| unsigned char xdata | FCSorig2 |
| unsigned char xdata | FCSdevi2 |
| unsigned char xdata | cavg = 0 |
| unsigned char idata | channel |
| unsigned char idata | avgcount |
| unsigned char idata | numavg = 0 |
| float xdata | average |
| float xdata | temperature |
| SYS_INFO | sys_info |
| MSCB_INFO_VAR code | vars [] |
| MSCB_INFO_VAR * | variables = vars |
| void autocalibration | ( | float | reference | ) |
00227 { 00228 float buffer; 00229 int offset, i; 00230 for(i=0;i<18; i++){ 00231 //offset channel 2 00232 buffer=reference-user_data.AT[2*i]; 00233 //Every increment of 16 corresponds to a quarter degree change 00234 buffer*=4; 00235 offset=(int)(buffer*16); 00236 eepage.ext2offset[i]=offset; 00237 //offset channel 1 00238 buffer=reference-user_data.AT[2*i+1]; 00239 //Every increment of 16 corresponds to a quarter degree change 00240 buffer*=4; 00241 offset=(int)(buffer*16); 00242 eepage.ext1offset[i]=offset; 00243 } 00244 return; 00245 }
| int eepageAddrConvert | ( | unsigned int | index | ) |
00250 { 00251 int add; 00252 //if index is even 00253 if(!(index%2)) 00254 add = index/2 + 18 + EEP_RW_IDX; // offset from the eepage struct 00255 //if index is odd 00256 else 00257 add = index/2 + EEP_RW_IDX; // offset from the eepage struct 00258 return add; 00259 }
| void publishAll | ( | ) |
| void publishCtlCsr | ( | void | ) |
00199 { 00200 DISABLE_INTERRUPTS; 00201 user_data.control = rCTL; 00202 user_data.status = rCSR; 00203 ENABLE_INTERRUPTS; 00204 }
| void publishErr | ( | bit | errbit | ) |
00208 { 00209 DISABLE_INTERRUPTS; 00210 errbit = SET; 00211 user_data.error = rESR; 00212 ENABLE_INTERRUPTS; 00213 }
| unsigned char user_func | ( | unsigned char * | data_in, | |
| unsigned char * | data_out | |||
| ) |
| void user_init | ( | unsigned char | init | ) |
00263 { 00264 unsigned char idata i; 00265 00266 /* Format the SVN and store this code SVN revision into the system */ 00267 for (i=0;i<4;i++) { 00268 if (svn_rev_code[6+i] < 48) { 00269 svn_rev_code[6+i] = '0'; 00270 } 00271 } 00272 00273 sys_info.svn_revision = (svn_rev_code[6]-'0')*1000+ 00274 (svn_rev_code[7]-'0')*100+ 00275 (svn_rev_code[8]-'0')*10+ 00276 (svn_rev_code[9]-'0'); 00277 00278 if(init) {} 00279 00280 // Initial setting for communication and overall ports 00281 //----------------------------------------------------------------------------- 00282 // External EEPROM chip 00283 #ifdef _ExtEEPROM_ 00284 SFRPAGE = CONFIG_PAGE; 00285 P2MDOUT |= 0x31; // Setting the RAM_CSn. SPI_MOSI, SPI_SCK to push pull 00286 P2MDOUT &= 0xFB; // Setting the RAM_WPn to open drain 00287 ExtEEPROM_Init(); 00288 00289 //Get serial number from write protected area 00290 ExtEEPROM_Read (WP_START_ADDR, (unsigned char xdata *)&eepage ,PAGE_SIZE); 00291 DISABLE_INTERRUPTS; 00292 user_data.SerialN = eepage.SerialN; 00293 ENABLE_INTERRUPTS; 00294 00295 //Refer to the first Page of the non_protected area 00296 ExtEEPROM_Read (PageAddr[0] ,(unsigned char xdata *)&eepage ,PAGE_SIZE); 00297 DISABLE_INTERRUPTS; 00298 user_data.navge = eepage.navge; 00299 ENABLE_INTERRUPTS; 00300 00301 #endif 00302 00303 // 00304 // ADT7486A Init 00305 #ifdef _ADT7486A_ 00306 SFRPAGE = CONFIG_PAGE; 00307 P0MDOUT |= 0x18; // Setting the SST_DRV (SST) to push pull 00308 SFRPAGE = CPT1_PAGE; 00309 CPT1CN |= 0x80; // Enable the Comparator 1 00310 CPT1MD = 0x03; //Comparator1 Mode Selection 00311 SFRPAGE = CPT0_PAGE; 00312 CPT0CN |= 0x80; // Enable the Comparator 0 00313 CPT0MD = 0x03; //Comparator0 Mode Selection 00314 //Use default, adequate TYP (CP1 Response Time, no edge triggered interrupt) 00315 ADT7486A_Init(SST_LINE1); //Temperature measurements related initialization 00316 ADT7486A_Init(SST_LINE2); 00317 00318 // Setting the offset for the temperature sensors 00319 for (channel=0;channel < 9; channel++){ 00320 // Set the offset for the temp01,03...17 sensors 00321 ADT7486A_Cmd(ADT7486A_addrArray[channel] 00322 , SetExt2Offset 00323 , (eepage.ext2offset[channel]>>8) 00324 , eepage.ext2offset[channel] 00325 , SST_LINE1 00326 , &temperature); 00327 delay_us(100); //wait for ADT7486A 00328 } 00329 delay_us(300); 00330 for (channel=9;channel < 18; channel++){ 00331 //Set the offset for the temp19,21...35 sensors 00332 ADT7486A_Cmd(ADT7486A_addrArray[channel-9] 00333 , SetExt2Offset 00334 , (eepage.ext2offset[channel]>>8) 00335 , eepage.ext2offset[channel] 00336 , SST_LINE2 00337 , &temperature); 00338 delay_us(100); //wait for ADT7486A 00339 } 00340 delay_us(300); 00341 for (channel=0;channel < 9; channel++){ 00342 //Set the offset for the temp02,04...18 sensors 00343 ADT7486A_Cmd(ADT7486A_addrArray[channel] 00344 , SetExt1Offset 00345 , (eepage.ext1offset[channel]>>8) 00346 , eepage.ext1offset[channel] 00347 , SST_LINE1 00348 , &temperature); 00349 delay_us(100); //wait for ADT7486A 00350 } 00351 delay_us(300); 00352 for (channel=9;channel < 18; channel++){ 00353 //Set the offset for the temp20,22...36 sensors 00354 ADT7486A_Cmd(ADT7486A_addrArray[channel-9] 00355 , SetExt1Offset 00356 , (eepage.ext1offset[channel]>>8) 00357 , eepage.ext1offset[channel] 00358 , SST_LINE2 00359 , &temperature); 00360 delay_us(100); //wait for ADT7486A 00361 } 00362 delay_us(300); 00363 #endif 00364 00365 // User variables initialization 00366 DISABLE_INTERRUPTS 00367 user_data.terror[0]=0; 00368 user_data.terror[1]=0; 00369 user_data.terror[2]=0; 00370 user_data.terror[3]=0; 00371 user_data.terror[4]=0; 00372 user_data.status=0; 00373 rCTL = user_data.control=0x4; 00374 00375 //temporary humidity sensor 00376 user_data.SHTtemp1 = 0; 00377 user_data.SHThumi1 = 0; 00378 user_data.SHTtemp2 = 0; 00379 user_data.SHThumi2 = 0; 00380 00381 for (i=0; i<36;i ++) 00382 user_data.Temp[i] = 0.0; 00383 user_data.eepage=0; 00384 user_data.navge=20; 00385 for(i=0; i<36; i++) 00386 { 00387 user_data.AT[i]=0; 00388 } 00389 00390 user_data.ref=0; 00391 00392 ENABLE_INTERRUPTS 00393 // General Variables initialization 00394 EEP_CTR_Flag = 0; 00395 rCSR=0; 00396 00397 // Humidity sensor initialization 00398 #ifdef _HUMSEN_ 00399 SFRPAGE = CONFIG_PAGE; 00400 P1MDOUT |= 0x07; 00401 SHT_VDD1=1; 00402 SHT_GND1=0; 00403 // Initializing the SHTxx communication 00404 HumiSensor_Init(humsen1); 00405 #ifdef MORETHANONEHUM 00406 P3MDOUT |= 0x07; 00407 SHT_VDD2=1; 00408 SHT_GND2=0; 00409 HumiSensor_Init(humsen2); 00410 #endif 00411 #endif 00412 00413 // DAC initialization 00414 #ifdef _LTC2600_ 00415 SFRPAGE = CONFIG_PAGE; 00416 P0MDOUT |= 0x80; // Setting the SUM_DAC_CSn to push pull 00417 P2MDOUT |= 0x30; // Setting the SPI_MOSI and SPI_SCK to push pull 00418 LTC2600_Init(); 00419 #endif 00420 }
| void user_loop | ( | void | ) |
00462 { 00463 00464 #ifdef _ADT7486A_ 00465 // Read the external temperature from each chip that is connected to SST_LINE1 00466 // Corresponds to Temp01,03,05...17 00467 for (channel=0;channel < 9; channel++){ 00468 if(!ADT7486A_Cmd(ADT7486A_addrArray[channel] 00469 , GetExt2Temp 00470 , SST_LINE1 00471 , &temperature)){ 00472 //Clear the bit if there is no error 00473 DISABLE_INTERRUPTS; 00474 user_data.Temp[channel*2] = temperature; 00475 if(channel<4) 00476 user_data.terror[0] &= Terrorclear[2*channel]; 00477 else if(channel<8) 00478 user_data.terror[1] &= Terrorclear[2*(channel-4)]; 00479 else 00480 user_data.terror[2] &= Terrorclear[2*(channel-8)]; 00481 ENABLE_INTERRUPTS; 00482 } else { 00483 //Set the bit if there is an error 00484 DISABLE_INTERRUPTS; 00485 if(channel<4) 00486 user_data.terror[0] |= Terrorset[2*channel]; 00487 else if(channel<8) 00488 user_data.terror[1] |= Terrorset[2*(channel-4)]; 00489 else 00490 user_data.terror[2] |= Terrorset[2*(channel-8)]; 00491 ENABLE_INTERRUPTS; 00492 } 00493 } 00494 00495 yield(); 00496 00497 // Read the external temperature from each chip that is connected to SST_LINE2 00498 // Corresponds to Temp19,21...35 00499 for (channel=9;channel < 18; channel++){ 00500 if(!ADT7486A_Cmd(ADT7486A_addrArray[channel-9] 00501 , GetExt2Temp 00502 , SST_LINE2 00503 , &temperature)){ 00504 00505 DISABLE_INTERRUPTS; 00506 user_data.Temp[channel*2] = temperature; 00507 if(channel<12) 00508 user_data.terror[2] &= Terrorclear[2*(channel-8)]; 00509 else if(channel<16) 00510 user_data.terror[3] &= Terrorclear[2*(channel-12)]; 00511 else 00512 user_data.terror[4] &= Terrorclear[2*(channel-16)]; 00513 ENABLE_INTERRUPTS; 00514 } 00515 else{ 00516 DISABLE_INTERRUPTS; 00517 if(channel<12) 00518 user_data.terror[2] |= Terrorset[2*(channel-8)]; 00519 else if(channel<16) 00520 user_data.terror[3] |= Terrorset[2*(channel-12)]; 00521 else 00522 user_data.terror[4] |= Terrorset[2*(channel-16)]; 00523 ENABLE_INTERRUPTS; 00524 } 00525 } 00526 00527 00528 yield(); 00529 00530 // Read the second external temperature from each chip that is connected to SST_LINE1 00531 // Corresponds to Temp02,04...18 00532 for (channel=0;channel < 9; channel++){ 00533 if(!ADT7486A_Cmd(ADT7486A_addrArray[channel] 00534 , GetExt1Temp 00535 , SST_LINE1 00536 , &temperature)){ 00537 DISABLE_INTERRUPTS; 00538 user_data.Temp[(channel*2)+1] = temperature; 00539 if(channel<4) 00540 user_data.terror[0] &= Terrorclear[2*channel+1]; 00541 else if(channel<8) 00542 user_data.terror[1] &= Terrorclear[2*(channel-4)+1]; 00543 else 00544 user_data.terror[2] &= Terrorclear[2*(channel-8)+1]; 00545 ENABLE_INTERRUPTS; 00546 } 00547 else{ 00548 DISABLE_INTERRUPTS; 00549 if(channel<4) 00550 user_data.terror[0] |= Terrorset[2*channel+1]; 00551 else if(channel<8) 00552 user_data.terror[1] |= Terrorset[2*(channel-4)+1]; 00553 else 00554 user_data.terror[2] |= Terrorset[2*(channel-8)+1]; 00555 ENABLE_INTERRUPTS; 00556 } 00557 } 00558 00559 00560 yield(); 00561 00562 // Read the second external temperature from each chip that is connected to SST_LINE2 00563 // Corresponds to Temp20,22,...36 00564 for (channel=9;channel < 18; channel++){ 00565 if(!ADT7486A_Cmd(ADT7486A_addrArray[channel-9] 00566 , GetExt1Temp 00567 , SST_LINE2 00568 , &temperature)){ 00569 DISABLE_INTERRUPTS; 00570 user_data.Temp[(channel*2)+1] = temperature; 00571 if(channel<12) 00572 user_data.terror[2] &= Terrorclear[2*(channel-8)+1]; 00573 else if(channel<16) 00574 user_data.terror[3] &= Terrorclear[2*(channel-12)+1]; 00575 else 00576 user_data.terror[4] &= Terrorclear[2*(channel-16)+1]; 00577 ENABLE_INTERRUPTS; 00578 } 00579 else{ 00580 DISABLE_INTERRUPTS; 00581 if(channel<12) 00582 user_data.terror[2] |= Terrorset[2*(channel-8)+1]; 00583 else if(channel<16) 00584 user_data.terror[3] |= Terrorset[2*(channel-12)+1]; 00585 else 00586 user_data.terror[4] |= Terrorset[2*(channel-16)+1]; 00587 ENABLE_INTERRUPTS; 00588 } 00589 } 00590 00591 00592 yield(); 00593 00594 // Calculating the rolling average 00595 if((user_data.navge <= TAVGMAX) && (user_data.navge != 0)) { 00596 if(cavg == user_data.navge) 00597 cavg=1; 00598 else { 00599 cavg++; 00600 if(numavg != user_data.navge) numavg=cavg; 00601 } 00602 00603 for(channel=0; channel<36; channel++) { 00604 Taverage[channel][(cavg-1)] = user_data.Temp[channel]; 00605 average=0; 00606 00607 for(avgcount=0; avgcount<numavg; avgcount++) 00608 average+=Taverage[channel][avgcount]; 00609 average/=numavg; 00610 DISABLE_INTERRUPTS; 00611 user_data.AT[channel]=average; 00612 ENABLE_INTERRUPTS; 00613 } 00614 } 00615 #endif 00616 00617 #ifdef _ExtEEPROM_ 00618 //----------------------------------------------------------------------------- 00619 // Checking the eeprom control flag 00620 if (EEP_CTR_Flag) { 00621 //Checking for the special instruction 00622 if (user_data.eeCtrSet & EEP_CTRL_KEY) { 00623 // Valid index range 00624 if( (int)(user_data.eeCtrSet & 0x000000ff) < TEMPOFF_LAST_INDX) { 00625 // Float area from EEP_RW_IDX, count in Float size, No upper limit specified! 00626 eep_address = (int xdata *)&eepage + eepageAddrConvert((int)(user_data.eeCtrSet & 0x000000ff)); 00627 //Checking for the write request 00628 if (user_data.eeCtrSet & EEP_CTRL_WRITE){ 00629 *eep_address = user_data.eepValue; 00630 //Checking for the read request 00631 } else if (user_data.eeCtrSet & EEP_CTRL_READ) { 00632 DISABLE_INTERRUPTS; 00633 user_data.eepValue = *eep_address; 00634 ENABLE_INTERRUPTS; 00635 } else { 00636 // Tell the user that inappropriate task has been requested 00637 DISABLE_INTERRUPTS; 00638 user_data.eeCtrSet = EEP_CTRL_INVAL_REQ; 00639 ENABLE_INTERRUPTS; 00640 } 00641 } else { 00642 DISABLE_INTERRUPTS; 00643 user_data.eeCtrSet = EEP_CTRL_OFF_RANGE; 00644 ENABLE_INTERRUPTS; 00645 } 00646 } else { 00647 // Tell the user that invalid key has been provided 00648 DISABLE_INTERRUPTS; 00649 user_data.eeCtrSet = EEP_CTRL_INVAL_KEY; 00650 ENABLE_INTERRUPTS; 00651 } 00652 EEP_CTR_Flag = CLEAR; 00653 } // if eep 00654 00655 00656 //----------------------------------------------------------------------------- 00657 // EEPROM Save procedure based on CTL bit 00658 if (CeeS) { 00659 //Check if we are here for the first time 00660 if (!eeprom_flag) { // first in 00661 rCSR = user_data.status; 00662 00663 //Temporary store the first address of page 00664 eeptemp_addr = PageAddr[(unsigned char)(user_data.eepage & 0x07)]; 00665 //Temporary store the first address of data which has to be written 00666 eeptemp_source = (unsigned char xdata *)&eepage; 00667 } 00668 00669 // EEPROM Write/Clear request 00670 if (CeeClr) eep_request = CLEAR_EEPROM; 00671 else eep_request = WRITE_EEPROM; 00672 00673 status = ExtEEPROM_Write_Clear (eeptemp_addr 00674 , &eeptemp_source 00675 , PAGE_SIZE 00676 , eep_request 00677 , &eeprom_flag); 00678 00679 if (status == DONE) { 00680 SeeS = DONE; 00681 eeprom_flag = CLEAR; 00682 //Set the active page 00683 user_data.eepage |= ((user_data.eepage & 0x07) << 5); 00684 } else { 00685 SeeS = FAILED; 00686 } 00687 00688 CeeS = CLEAR; 00689 // Publish Registers state 00690 publishCtlCsr(); 00691 } 00692 00693 //----------------------------------------------------------------------------- 00694 // EEPROM Restore procedure based on CTL bit 00695 if (CeeR) { 00696 rCSR = user_data.status; 00697 00698 //NW read to eepage(active page) instead of eepage2 00699 channel = ExtEEPROM_Read (PageAddr[(unsigned char)(user_data.eepage & 0x07)] 00700 , (unsigned char xdata *)&eepage, PAGE_SIZE); 00701 00702 if (channel == DONE) SeeR = DONE; 00703 else SeeR = FAILED; 00704 00705 CeeR = CLEAR; 00706 publishCtlCsr(); // Publish Registers state 00707 } 00708 #endif 00709 00710 //----------------------------------------------------------------------------- 00711 // Humidity sensors 00712 #ifdef _HUMSEN_ 00713 //Measuring the humidity and temperature 00714 if(CHum){ 00715 status = HumidSensor_Cmd (&rSHThumi1 00716 ,&rSHTtemp1 00717 ,&humidity 00718 ,&htemperature 00719 ,&FCSorig1 00720 ,&FCSdevi1 00721 ,humsen1); 00722 if (status == DONE) { 00723 DISABLE_INTERRUPTS; 00724 user_data.SHThumi1 = humidity; 00725 user_data.SHTtemp1 = htemperature; 00726 ENABLE_INTERRUPTS; 00727 } 00728 00729 #ifdef MORETHANONEHUM 00730 status = HumidSensor_Cmd (&rSHThumi2 00731 ,&rSHTtemp2 00732 ,&humidity 00733 ,&htemperature 00734 ,&FCSorig2 00735 ,&FCSdevi2 00736 ,humsen2); 00737 if (status == DONE) { 00738 DISABLE_INTERRUPTS; 00739 user_data.SHThumi2 = humidity; 00740 user_data.SHTtemp2 = htemperature; 00741 ENABLE_INTERRUPTS; 00742 } 00743 #endif // MORETHANONE 00744 } 00745 00746 #endif // HUMIDITY 00747 00748 #ifdef _LTC2600_ 00749 //DAC 00750 if(LTC2600_FLAG) { 00751 LTC2600_Cmd(WriteTo_Update,LTC2600_LOAD[DACIndex], user_data.DAC[DACIndex]); 00752 LTC2600_FLAG = CLEAR; 00753 } 00754 #endif 00755 00756 if(Cref) { 00757 autocalibration(user_data.ref); 00758 Sref = 1; 00759 Cref = 0; 00760 DISABLE_INTERRUPTS; 00761 user_data.status=rCSR; 00762 user_data.control=rCTL; 00763 ENABLE_INTERRUPTS; 00764 } 00765 00766 led_blink(0, 1, 150); 00767 00768 }
| void user_write | ( | unsigned char | index | ) |
00424 { 00425 if (index == IDXCTL) rCTL = user_data.control; 00426 00427 if (index == IDXEEP_CTL) EEP_CTR_Flag = SET; 00428 00429 #ifdef _LTC2600_ 00430 if ((index >= First_DACIndex) && (index <= Last_DACIndex)) { 00431 DACIndex = (index - First_DACIndex); 00432 // Update Bias Dac voltages as requested by bit5 of control register 00433 LTC2600_Flag = SET; 00434 } 00435 #endif 00436 00437 if(index==IDXNAVG) { 00438 cavg = 0; 00439 numavg = 0; 00440 } 00441 }
| unsigned char idata _n_sub_addr = 1 |
| float xdata average |
| unsigned char idata avgcount |
| unsigned char xdata cavg = 0 |
| unsigned char idata channel |
| int xdata* eep_address |
| bit EEP_CTR_Flag |
| unsigned char xdata eep_request |
unsigned char xdata eeprom_flag = CLEAR [static] |
| unsigned char xdata eeprom_rstatus |
| unsigned char xdata eeprom_wstatus |
| unsigned int xdata eeptemp_addr |
| unsigned char xdata* eeptemp_source |
| unsigned char xdata FCSdevi1 |
| unsigned char xdata FCSdevi2 |
| unsigned char xdata FCSorig1 |
| unsigned char xdata FCSorig2 |
| float xdata htemperature |
| float xdata humidity |
| bit LTC2600_Flag |
| char code node_name[] = "temp36" |
| unsigned char idata numavg = 0 |
| bit REF_Flag |
| unsigned int xdata rSHThumi1 |
| unsigned int xdata rSHThumi2 |
| unsigned int xdata rSHTtemp1 |
| unsigned int xdata rSHTtemp2 |
| unsigned char xdata status |
| char idata svn_rev_code[] = "$Rev: 1180 $" |
unsigned char tcounter [static] |
| float xdata temperature |
| MSCB_INFO_VAR code vars[] |
1.4.7