Saturday, February 2, 2013

What the hell is that!!!

When i looking back for the project i did for Embedded System Design one year ago. . I try gone through and refresh back all task i did. Especially all those email conversation. 

I asking my self!!!
How can i make it!!! Is that me?? And yet the project was done in very last minute... I am just like a hero in the group by surprisingly present the final product to my group in last minute!!!
Of course.... After that, i told my self.... I will never ever touch that chip any more!!!

Unfortunately.....
In my final year project. I need this chip to solve some of my problem again!!!  
When i gone through the codding i compile before!! 
I only can ask my self "What the hell is that!!!!"

Do you know what the alliance language shown below??






// Temperature monitoring and safety system
#include
#include "lcd.c"
#include "lcd.h"
void initADC( );
int read_averageADC();
int readADC( int tsensor);
void _ISR _T1Interrupt();
void convert(int no, char base);
void display(char number);
void converter();
int count=0,count2=0,x,y,key1,key2;    //declare and initialization 
int temp,temp2; //temporarry variable for temperature 
int temperature[3],i;
_CONFIG1(JTAGEN_OFF & BKBUG_OFF & FWDTEN_OFF) //setup configuration bits
_CONFIG2(FCKSM_CSDCMD & OSCIOFNC_ON & POSCMOD_NONE & FNOSC_FRC)
#define SENSOR 1
char di[10];
int main()
{
AD1PCFG = 0x1FFE; //Set all pins to analogue input
TRISB &= 0x0000; //Port B set to output for D4,D5,D6 and D7
TRISA = 0x0001; //Port A set to output for RS and E and input for AN0
_TRISB9=0;//set RB9 as input
lcd_init();
initADC();
lcd_clear();


//********************************************************************
while(1)// main loop
{
//---------------------------------------------------------------
//basic initialization
x=0;
y=0;
key1=1;
key2=0;
//----------------------------------------------------------------
/*
* DC fan speed control timer initialization and output config 
* DC fan control for temperature monitoring within temperature 0-50 
* from 0-24  40% fan speed 
* from 25-39 80%
* from 40-45 100%
* greater than 46 alarm trigled temperature monitoring fail
*/
_TRISB14=0; //port RB14 as output
_TRISB8=0;
_TRISB9=1; //port RB15 as output
AD1PCFG=0Xffff;
_T1IF=0; //Clear timer1 interrupt flag.
_T1IE=1; //Enable timer1 interrupt.
_RB14=0;
_RB15=0;
_RB8=0;
    _RB7=0;
T1CON=0x8000; //Timer1 on.
PR1=0x2710; //Timer counter of 10 ( 10 is for mplab testing) in decimal(PR1=period-1).
TMR1=0;
//--------------------------------------------------------------------
while(key1)//secondary loop 1
{
//------------------------------------------------------------
//Read the temperature from sensor and display on LCD
if(_RB9==1){
count2+= _RB9;
}
_RB9=0;
if(count2>2){
count2=0;
}
converter();

//----------------------------------------------------------------------------------------------------------
};//secondary loop 1
while(key2)//secondary loop 2
{
// actually can decide for continue ing display latest temperature here 
_RB8=1;//testing alarm 
_RB15=1;//testing alarm

if(_RB9==1)//RB9 used for 
{
// all port reset before return to secondary loop 1
x=0;
y=0;
_RB9=0;
_RB7=0;
_RB14=0;
_RB15=0;
key1=1;
key2=0;
_RB9=0;
delayms(100);// delay needed to avoid interating the _RB9 (switched) input 
}
};//secondary loop 2
};//main loop
//**********************************************************************************
}//main

void _ISR _T1Interrupt()//interupt funtion for fan speed setting
{count++;
temp2=read_averageADC();// take reading form sensor
//temp2=45; use for testing different temperature here 
//----------------------------------------------------------
//DC fan speed and alarm control 
if (temp2<27 div="">
{
//go 40% fan speed mode 
_RB14=x;
} else {
if (temp2>26 && temp2<35 div="">
{
//go 80% fan speed mode
_RB14=y;
} else{
if (temp2 >35 && temp2<40 div="">
{
//go 100% fan speed mode 
_RB14=1;
} else if (temp2>39){
// alarm triggled (all at secondary loop 2)... 
_RB14=1; 
key1=0;// stop this loop and jump to secondary loop 2
key2=1;// enable key to secondary loop 2
T1CON=0x0000; //timer off and no more interrupt allowed
}
}
}
if(count==6)//at sixth time entering interrupt togger x
{
x=~x;
}else
if(count==2)//at second time entering interrupt togger y
{
y=~y;

}else
if(count==10)// togger both pins at tenth times entering interupt
{
x=~x;
y=~y;
count=0;
}

//-------------------------------------------------------------------------------------------------------------


_T1IF=0;
}
void initADC( )
{
AD1CON1 = 0x00E0; // auto convert after end of sampling
AD1CSSL = 0; // no scanning required
AD1CON3 = 0x1F02; // max sample time = 31Tad, Tad = 2 x Tcy = 500ns >75ns
AD1CON2 = 0x0000;// use MUXA, Vss and Vdd are used as Vref+/-
AD1CON1bits.ADON = 1; // turn on the ADC

int readADC( int tsensor)
{
AD1CHS =0x0000; // input at AN0
AD1CON1bits.SAMP = 1; // start sampling, auto-conversion will follow
while (!AD1CON1bits.DONE); // wait to complete the conversion
return ADC1BUF0; // read the conversion result

int read_averageADC()
{
int temp,j;
initADC();
temp = 0;
T3CON = 0x8030;
for ( j= 16; j >0; j--)
{
TMR3 = 0;
temp += readADC( SENSOR); 
}
temp >>= 4; //get the average of 16 readings
temp = temp*330/1024; //temp*330/1024
return temp;
}
//function used to get reminder and store in array
void convert(int no, char base)
{int i;
for(i=1;no!=0;i++) {
di[i]=no%base;
no=no/base;
}
}
//switching method use for convert the character stored in array
void display(char number)
{
switch(number){
case 0:{lcd_string("0");break;}
case 1:{lcd_string("1");break;}
case 2:{lcd_string("2");break;}
case 3:{lcd_string("3");break;}
case 4:{lcd_string("4");break;}
case 5:{lcd_string("5");break;}
case 6:{lcd_string("6");break;}
case 7:{lcd_string("7");break;}
case 8:{lcd_string("8");break;}
case 9:{lcd_string("9");break;}
case 10:{lcd_string("A");break;}
case 11:{lcd_string("B");break;}
case 12:{lcd_string("C");break;}
case 13:{lcd_string("D");break;}
case 14:{lcd_string("E");break;}
case 15:{lcd_string("F");break;}
default:{lcd_string("?");break;}
}}


//functiom below is main function for converting analogue signal from temperature sensor
//and display on LCD
void converter(){
AD1PCFG = 0x1FFE; //Set all pins to analogue input
TRISB &= 0x0000; //Port B set to output for D4,D5,D6 and D7
TRISA = 0x0001; //Port A set to output for RS and E and input for AN0
temperature[0]=read_averageADC(); //10mV per Celsius
temperature[1]=(int)(1.8*temperature[0]+32); //Celsius to Fahrenheit 320
temperature[2]=(int)(temperature[0]+273); //Celsius to Kelvin 2731.5
lcd_line(0,0);
lcd_string("Temp:");
lcd_line(5,0);
lcd_string("=");
lcd_line(6,0);
convert(temperature[count2],10);
for(i=3;i>=1;i--) // display the converted value in array [reminder 3,reminder 3,reminder 3]
{
display(di[i]);
}
for(i=1;i<4 div="" i="">
{
di[i]=0;
}
if(count2==0){
lcd_char(0xdf);
lcd_string("C");
    lcd_line(0,1);
}else
if(count2==1){
lcd_char(0xdf);
lcd_string("F");
}else if (count2==2)
{
lcd_char(0xdf);
lcd_string ("K");
}

//temperature condition display
lcd_line(0,1);
if(temperature[0]<27 div="">
lcd_string("Good         ");
}
else
if(temperature[0]>26 &&temperature[0]<35 div="">
lcd_string("Moderate     ");
}
else
if(temperature[0]>34 && temperature[0]<40 div="">
lcd_string("Hot         ");
}
else
if(temperature[0]>39){
lcd_string("Caution     ");
}


}


No comments: