Friday, 28 September 2012

C LANGUAGE PIC

#include<pic.h>//includePICmicrocontrollerlibrary

//configuration//==========================================================================
__CONFIG(0x3F32);//PICmicrocontrollerconfiguration

//define//==========================================================================
#define rs RB4//RSpinofLCDdisplay
#define e RB5//EpinoftheLCDdisplay
#define lcd_data PORTD//LCDdisplaydataPORT(8-bit)
#define led1 RB7//led1(activehigh)
#define led2 RB6//led2(activehigh)

void delay(unsigned long data);
void send_config(unsigned char data);
void send_char(unsigned char data);
void lcd_goto(unsigned char data);
void lcd_clr(void);
void send_string(const char *s);
void send_num(unsigned short data);
unsigned char usart_rec(void);
void beep_short(void);
void beep_short2(void);
void beep_long(void);
unsigned char read_ad(unsigned char channel);

void main(void)
    {
        //assignvariable
        unsigned char temp,temp1,jwp,jwp1;//declareatemporaryvariableforreadingADC
        unsigned char mode;//declareavariabletorepresentcurrentmode

        //setI/Oinputoutput
        TRISB=0b00000011;//configurePORTBI/Odirection
        TRISA=0b11011011;//configurePORTAI/Odirection
        TRISD=0b00000000;//configurePORTCI/O
       
               //configure lcd
    send_config(0b00000001);            //clear display at lcd
    send_config(0b00000010);            //lcd return to home
    send_config(0b00000110);            //entry mode-cursor increase 1
    send_config(0b00001100);            //display on, cursor off and cursor blink off
    send_config(0b00111000);
               
        //configure
            ADCON0=0b10000001;//enableADCconvertermodule
            ADCON1=0b01000100;//configureADCandANxpin
               
        //initialcondition


    lcd_goto(0);                        //set the lcd cursor to location 0
    send_string("    ELECTRIC    ");        //display "Cytron Tech."
    lcd_goto(20);                        //set the lcd cursor to location 20
    send_string("  TURBOCHARGE");                //display "SK40C"
    delay(100000);
    delay(100000);
             led1=0;//onled1
            led2=0;//offled2
    lcd_clr();
    while(1)//infinityloop
               
    {
    mula:
    lcd_goto(0);//setlcdcursortolocation0
    send_string("HumidityHeatSen ");//display"HumiditySensor"
    temp=read_ad(0);//readAN1(HumiditySensor)
    jwp1= (temp*100)/255;
    temp1=read_ad(1);//readAN1(HumiditySensor)
    jwp=temp1/2;
    lcd_goto(20);//setlcdcursortolocation20
    send_num(jwp1);//displaytheanalogvalueofthegassensor
    lcd_goto(25);//setlcdcursortolocation20
    send_string("%");        //display "Cytron Tech."
    lcd_goto(28);//setlcdcursortolocation20
    send_num(jwp);//displaytheanalogvalueofthegassensor
    lcd_goto(33);//setlcdcursortolocation20
    send_string("C");        //display "Cytron Tech."
   
    if (temp>93 && temp1>100)
    {
    led2=1;
    led1=1;
    goto mula;
    }
    else if (temp1>100)
    {
    led1=1;
    goto mula;
    }
    else if (temp>93)
    {
    led2=1;
    goto mula;
    }

    else
    {   
    led1=0;
    led2=0;
    }
    }
    }
//functions//==========================================================================

void delay(unsigned long data)            //delay function, the delay time
{                                        //depend on the given value
    for( ;data>0;data--);
}

void send_config(unsigned char data)    //send lcd configuration
{
    rs=0;                                //set lcd to configuration mode
    lcd_data=data;                        //lcd data port = data
    e=1;                                //pulse e to confirm the data
    delay(50);
    e=0;
    delay(50);
}

void send_char(unsigned char data)        //send lcd character
{
     rs=1;                                //set lcd to display mode
    lcd_data=data;                        //lcd data port = data
    e=1;                                //pulse e to confirm the data
    delay(10);
    e=0;
    delay(10);
}
   
void lcd_goto(unsigned char data)        //set the location of the lcd cursor
{                                        //if the given value is (0-15) the
     if(data<16)                            //cursor will be at the upper line
    {                                    //if the given value is (20-35) the
         send_config(0x80+data);            //cursor will be at the lower line
    }                                    //location of the lcd cursor(2X16):
    else                                // -----------------------------------------------------
    {                                    // | |00|01|02|03|04|05|06|07|08|09|10|11|12|13|14|15| |
         data=data-20;                    // | |20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35| |
        send_config(0xc0+data);            // -----------------------------------------------------   
    }
}


void lcd_clr(void)                        //clear the lcd
{
     send_config(0x01);
    delay(600);   
}

void send_string(const char *s)            //send a string to display in the lcd
{         
      while (s && *s)send_char (*s++);
}

    void send_num(unsigned short data)//functiontodisplayavalueonlcddisplay
{
    unsigned char tenthou,thou,hund,tenth;
    tenthou=data/10000;//gettenthousandvalue
    data=data%10000;
    thou=data/1000;//getthousandvalue
    data=data%1000;
    hund=data/100;//gethundredvalue
    data=data%100;
    tenth=data/10;//gettenthvalue
    data=data%10;//getunitvalue
   
   
    send_char(0x30+tenthou);//displaythetenthousandvalue
    send_char(0x30+thou);//displaythethousandvalue
    send_char(0x30+hund);//displaythehundredvalue
    send_char(0x30+tenth);//displaythetenthvalue
    send_char(0x30+data);//displaytheunitvalue
   
    }
   
        unsigned char read_ad(unsigned char channel)//fucntionreadanaloginputaccordingtothegivenchannel
    {
   
        unsigned char result;//declareavariablecallresult
        switch(channel)
        {
        case 0://if
       
    ADCON0 = 0b10000001;
       
        break;
        case 1://ifchannel=1
       
            ADCON0 = 0b10001001;
       
        break;
       
        }
        GO=1;//startADCconvertion
        while(GO);//waitforADCconvertiontocomplete
        result=ADRESH;//readtheresult
        return result;//returntheresult
       
        }

No comments:

Post a Comment