Code to display temperature on LCD 16x2 display using LM35 and Pic16F876A, 20MHz crystal
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 sbit LCD_RS at RC2_bit; sbit LCD_EN at RC3_bit; sbit LCD_D4 at RC7_bit; sbit LCD_D5 at RC6_bit; sbit LCD_D6 at RC5_bit; sbit LCD_D7 at RC4_bit; sbit LCD_RS_Direction at TRISC2_bit; sbit LCD_EN_Direction at TRISC3_bit; sbit LCD_D4_Direction at TRISC7_bit; sbit LCD_D5_Direction at TRISC6_bit; sbit LCD_D6_Direction at TRISC5_bit; sbit LCD_D7_Direction at TRISC4_bit; // End LCD module connections void read_adc(void); void custchar(char x, char y); unsigned int result; float n; int temp,rs; char txt[7]; const char character[]={4,10,4,0,0,0,0,0}; //degree symbol void main(void) { CMCON=7; //Comparator off TRISA.RA0=1; ADCON0=0b10000000; ADCON1=0b11000000; ADCON0.ADON=1; // turn on the A2D conversion module Delay_ms(300); LCD_init(); Lcd_Cmd(_LCD_CLEAR); // Clear LCD display Lcd_Cmd(_LCD_CURSOR_OFF); // Turn cursor off while(1){ Delay_ms(30); read_adc(); n=result*0.48876; n+=0.05; temp=n; n=(n-temp)*10; rs=n; InttoStr(temp,txt); Lcd_Out(2,3,txt); Lcd_chr(2,10,'.'); Lcd_chr(2,11,rs+48); custchar(2,12); Lcd_chr(2,13,'C'); Delay_ms(30); } } void read_adc(void) { unsigned short i; unsigned long result_temp=0; for(i=2000;i>0;i-=1) //looping 2000 times for getting average value { ADCON0.GO = 1; //ADGO is the bit 2 of the ADCON0 register while(ADCON0.GO==1); //ADC start, ADGO=0 after finish ADC progress result=ADRESH; result=result<<8; //shift to left for 8 bit result=result|ADRESL; //10 bit result from ADC result_temp+=result; } result = result_temp/2000; //getting average value } void custchar(char x, char y){ char i; Lcd_Cmd(64); for(i=0;i<=7;i++) Lcd_Chr_CP(character[i]); Lcd_Cmd(_LCD_RETURN_HOME); LCD_chr(x,y,0); }