Knowledge Base
...
Temperature
LM35 Project
LM35 Project Software
3min
the firmware required to interface with the sensor performs several tasks configures the microcontroller to read the analog value converts the analog value to a digital number formats that value for export microcontroller the device chosen is the pic16f887, and while it is far more in terms of resources it will provide a good base to build on and with for later projects or adding on additional features what is needed from the microcontroller is one of the analog inputs portb for the lcd display comport to share the data with the outside world full code sample the full code sample is shown below and can be copied from the code block the code sample is also available from github unsigned char ch; unsigned int adc rd; char text; long tlong; char tempc = "000 0"; void devicesetup(){ intcon = 0; // disable all interrupts ansel = 0x04; // configure an2 pin as analog input trisa = 0x04; anselh = 0; adcon1 = 0x82; // configure vdd as vref, and analog channels trisa = 0xff; // designate porta as input uart1 init(9600); delay ms(100); } void main() { lcd config(\&portb, 4, 5, 6, 3, 2, 1, 0); // lcd init ep5, see autocomplete lcd cmd(lcd cursor off); // send command to lcd (cursor off) lcd cmd(lcd clear); // send command to lcd (clear lcd) text = "lm 35 project"; // assign text to string lcd out(1,1,text); // print string a on lcd, 1st row, 1st column delay ms(2000); text = "temp "; // assign text to string while (1) { adc rd = adc read(2); // get adc value from 2nd channel lcd out(2,1,text); // print string a on lcd, 2nd row, 1st column tlong = (long)adc rd 5000; // covert adc reading to milivolts tlong = tlong / 1023; // 0 1023 > 0 5000mv ch = tlong / 1000; // extract leading temp digit tempc\[1] = ch+48; ch = (tlong / 100) % 10; // extract 0 1 temp digit lcd chr cp(48+ch); // write ascii digit at cursor point tempc\[2] = ch+48; ch = (tlong / 10) % 10; // extract 0 01 temp digit tempc\[3] = ch+48; lcd chr cp(48+ch); // write ascii digit at cursor point lcd chr cp(48+ch); // write ascii digit at cursor point lcd chr cp('c'); tempc\[4] = ch+48; uart1 write(tempc\[2]); uart1 write(tempc\[3]); uart1 write text(" "); uart1 write(tempc\[4]); uart1 write(13); uart1 write(10); delay ms(100); } }// !