LM35 read value via ADC read that value using ADC Pins,
1. first read the LM35 value and print it in terminal
if value is read successfully then put a condition i.e at what temperature you want the action
if (temp read >=?)
{
DO as per your requirement
}
2.for Controlling Fan you connect a dc Fan ,
- - - Updated - - -
**broken link removed**
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 /* * File: Temperature.c * Author: Jay Bhut *Controller : PIC16F887 * Compiler : HITECH C * DESCRIPTIONS :Temperature Control System using LM35 * / /*****************************************************************************/ /* Include Files */ /*****************************************************************************/ #include<htc.h> #include<stdlib.h> #include<stdio.h> /*****************************************************************************/ /* Global Variables */ /*****************************************************************************/ #define _XTAL_FREQ 4000000 // Define Internal Crystal frequecy to // 4 Mhz //#define CHECK_SENSOR unsigned char TimerCount = 1; unsigned char PreviousCount = 255; unsigned char ReceivedData = 0x00; struct Temperature { unsigned int TempDecimal; unsigned int TempMentissa; }; /******************************************************************************/ /* set_register() */ /* Configure required registors for setting Oscillator frequency and I/O pins.*/ /******************************************************************************/ void set_register(void) { OSCCON = 0X61; // Internal Oscilator selected with frequency of // 4MHz ANSEL = 0x10; // AN4 selected as Analog pin and else then from // AN0 - AN7 as digital pin ANSELH = 0X00; // AN8 - AN11 selected as digital pin INTCON = 0x00; // Disable all interrupts TRISA = 0x00; // Set all pins of PORT A as output pins TRISB = 0X20; // Sets RB5 as input pin for SERIAL_RX // Sets RB7 as output pin for SERIAL_TX #ifdef CHECK_SENSOR TRISC = 0X41; // RC0/AN4 selected as input pin and else then // all pins of port C is selected as output pin #else TRISC = 0X01; // RC0/AN4 selected as input pin and else then // all pins of port C is selected as output pin #endif } /******************************************************************************/ /* Interrupt_Init() */ /* Initialize Global, Peripheral, Timer and UART receiver interrupt */ /******************************************************************************/ void Interrupt_Init(void) { GIE = 1; // set global interrupt of the INTCON register PEIE = 1; // set peripheral interrupt of the INTCON register TMR1IE = 1; // timer1 interrupt enable of the PIE1 register RCIE = 1; // EUSART Receive Interrupt Enable bit of the PIE1 // register } /******************************************************************************/ /* ADCModule_Init(void) */ /* Initialize ADC module */ /******************************************************************************/ void ADCModule_Init(void) { ADCON1 = 0x20; // ADC conversation freq. set to FOSC/32 = 125KHz ADIE = 0; // Disables the A/D converter interrupt ADCON0 = 0x91 ; // A/D result format right justify, VDD as Vref, // AN4 ADC channels selected for ADC conversion // ADC is enable __delay_us(20); // 20usec of aqusition time GO =1; // start ADC conversion } /******************************************************************************/ /* UARTModule_Init(void) */ /* Initialize UART for serial transfer */ /******************************************************************************/ void UARTModule_Init(void) { SPBRGH =0x00; SPBRG = 0x19; // Set BAUDRATE of 2400 for 4MHz frequency SYNC = 0; // indicates asynchronous opeartion BRGH = 0; // for low speed data transfer BRG16 = 0; // indicates 8 bit of operation SPEN = 1; // enables the EUSART and automatically configures the // TX/CK I/O pin as an output and RX as an input TX9 = 0; // set to 8bit transmission RX9 = 0; CREN = 1; // enable the receiver circuitry of the EUSART. } /******************************************************************************/ /* Timer1_Init(void) */ /* Initialize Timer1 for 31.25msec */ /******************************************************************************/ void Timer1_Init(void) { TMR1CS = 0; // the clock source is FOSC/4 = 1MHz TMR1IF = 0; T1CON = 0X04; // prescaler 1:1, internal clock, disable timer1 TMR1H = 0X85; // set for 31.25msec TMR1L = 0XEE; TMR1ON = 1; // start timer1 of the T1CON register } /******************************************************************************/ /* interrupt tc_int(void) */ /* Interrupt Service Routing */ /******************************************************************************/ void interrupt tc_int(void) { if(TMR1IF) // if Timer1 interrupt { TimerCount += 1; // Increment timer count TMR1IF = 0; TMR1H = 0X85; // set for 31.25msec TMR1L = 0XEE; TMR1ON = 1; // Timer1 ON again } if(RCIF) // If Data received at UART { ReceivedData = RCREG; } } /******************************************************************************/ /* main(void) */ /* Provides a flow to the whole system */ /******************************************************************************/ void main(void) { struct Temperature T; unsigned int AvgVoltageDec; unsigned int AvgVoltageMent; unsigned int ADCresult = 0; float voltage = 0.0; float TotalVoltage = 0.0; set_register(); // Set register ADCModule_Init(); // Initialize ADC Interrupt_Init(); // Initialize Interrupt UARTModule_Init(); // Initialize UART Timer1_Init(); // Initialize Timer1 for(;;) { #ifdef CHECK_SENSOR if(!RC6) { #endif /* check for timer count change */ if(PreviousCount != TimerCount) { PreviousCount = TimerCount; if(GO == 0) /*If ADC conversion is finished*/ { ADCresult = ADRESH; ADCresult = ADCresult<<8; ADCresult += ADRESL; /* I/p voltage from temp. module is set to 48.88mv/C, * at 48.88mV, count = (48.88/1024)*5000 = 10 * so, (10*102.4)/1024 = 1.0 C */ voltage = (((float)ADCresult)/10); TotalVoltage += voltage; GO = 1; // Start ADC conversion again voltage = 0.0; } } /* Every 64 timer count means at every 64*31.25ms = 2sec time * take avg of 64 samples and store it */ if(TimerCount >= 64) { AvgVoltageDec = ((unsigned int)TotalVoltage) >> 6; AvgVoltageMent = (((unsigned int)(TotalVoltage*100))>> 6) %100 ; T.TempDecimal = AvgVoltageDec; T.TempMentissa = AvgVoltageMent; TimerCount = 0; TotalVoltage = 0; } if(ReceivedData == 0XFF) // Check for whether Received data from // PC is 0xFF or not { TXEN = 1; while(!TXIF) // Wait for TXIF flag Set which indicates // TXREG register is empty {} TXREG = T.TempDecimal; // Transmitt Decimal value of Temperature // to UART while(!TXIF) // Wait for TXIF flag Set which indicates // TXREG register is empty {} TXREG = T.TempMentissa; // Transmitt Mentissa value of Temperature // to UART ReceivedData = 0x00; // Erase Received data } #ifdef CHECK_SENSOR } else { if(ReceivedData == 0XFF) // Check for whether Received data from // PC is 0xFF or not { TXEN = 1; while(!TXIF) // Wait for TXIF flag Set which indicates // TXREG register is empty {} TXREG = 0xFF; // Transmitt Decimal value of Temperature // to UART ReceivedData = 0x00; // Erase Received data } } #endif } }
I have C Code for LM35 and PIC16F887 using serial communication in HTC C Compiler as below.
Sorry I have forgot to attach code now I have attached..I don't see any code
hello,
Do you want to display temperature or voltage ?
Your calculus are very strange ...
Are you using a LM35
or LM335 wich gives 2730mV at Zero °C
or LM35DZ wich gives 0 mV at Zero °C
You structure T use integer...
and you are loading integer into TXREG !!
it must be unsigned char ..
How do you send 0xFF to the MCU ?
#define I2C_WRITE 0
#define I2C_READ 1
void i2c_Init(void)
{
TRISC3 = 1; /* SDA and SCL as input pin */
TRISC4 = 1; /* these pins can be configured either i/p or o/p */
SSPSTAT |= 0x80; /* Slew rate disabled for standard speed mode(100kHz or 1 MHz) */
SSPCON = 0x28; /* SSPEN = 1, I2C Master mode, clock = FOSC/(4 * (SSPADD + 1)) */
SSPADD = 0x09; /* 100Khz @ 4Mhz Fosc */
SSPCON2 = 0x00;
SSPIF=0; // clear SSPIF interrupt flag
BCLIF=0; // clear bus collision flag
}
// i2c_Wait - wait for I2C transfer to finish
void i2c_Wait(void)
{
while (( SSPCON2 & 0x1F ) || ( SSPSTAT & 0x04 ));
}
// i2c_Start - Start I2C communication
void i2c_Start(void)
{
i2c_Wait();
SEN=1;
}
// i2c_Restart - Re-Start I2C communication
void i2c_Restart(void)
{
i2c_Wait();
RSEN=1;
}
// i2c_Stop - Stop I2C communication
void i2c_Stop(void)
{
i2c_Wait();
PEN=1;
}
// i2c_Write - Sends one byte of data
void i2c_Write(unsigned char data)
{
i2c_Wait();
SSPBUF = data;
}
// i2c_Address - Sends Slave Address and Read/Write mode
// mode is either I2C_WRITE or I2C_READ
void i2c_Address(unsigned char address, unsigned char mode)
{
unsigned char l_address;
l_address=address<<1;
l_address+=mode;
i2c_Wait();
SSPBUF = l_address;
}
// i2c_Read - Reads a byte from Slave device
unsigned char i2c_Read(unsigned char ack)
{
// Read data from slave
// ack should be 1 if there is going to be more data read
// ack should be 0 if this is the last byte of data read
unsigned char i2cReadData;
i2c_Wait();
RCEN=1;
i2c_Wait();
i2cReadData = SSPBUF;
i2c_Wait();
if(ack)
ACKDT=0; // Ack
else
ACKDT=1; // NAck
ACKEN=1; // send acknowledge sequence
return( i2cReadData );
}
Do you want to display voltage or temperature ?
LM35 ? 2730mV at 0°C ?
What about trying to push an integer into a char ?
sprintf(char Variable , " %d " , Integer Variable)
hello
Gives responses to questions ,if you want more help !
Jay can you upload the code of reading via I2C , actually I am having trouble with that reading DS1307,
Like I am suspecting that my "i2c_read()" function may not be correct ??
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 void init_ds1307(void) { i2c_Start(); // Start I2C communication i2c_Write(0XD0); // Connect to DS1307 by sending its ID on I2c Bus i2c_Write(CONTROL); // Select the Ds1307 ControlRegister to configure Ds1307 i2c_Write(0X00); // Write 0x00 to Control register to disable SQW-Out i2c_Stop(); // Stop I2C communication after initilizing DS1307 } void write_ds1307(unsigned short address, unsigned short w_data) { i2c_Start(); i2c_Write(0XD0); i2c_Write(address); i2c_Write(w_data); i2c_Stop(); } unsigned short read_ds1307(unsigned short address) { unsigned short temp i2c_Start(); i2c_Write(0XD0); i2c_Write(address); i2c_Restart(); i2c_Write(0XD1); temp = i2c_Read(0); // /* ACK if its not the last byte to read */ /* if its the last byte then send a NAK */ // if (i - 1) // i2c_ack(); // else // i2c_nack(); // i2c_Stop(); return (temp); } /*i2c_read()*/ unsigned char i2c_Read(unsigned char ack) { // Read data from slave // ack should be 1 if there is going to be more data read // ack should be 0 if this is the last byte of data read unsigned char i2cReadData; i2c_Wait(); RCEN = 1; i2c_Wait(); i2cReadData = SSPBUF; i2c_Wait(); if(ack) ACKDT=0; // Ack else ACKDT=1; // NAck ACKEN=1; // send acknowledge sequence return ( i2cReadData); }
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?