Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[General] PIC24F with LCD and LM35

Status
Not open for further replies.

ings

Member level 4
Joined
Sep 8, 2014
Messages
78
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Activity points
988
Hello, I am a beginner in PIC and in my project I want to detect the temperture of a room with an LM35 sensor and a PIC24F. and finally, I want to display the temperature value on an LCD. I tried with this code that I'm sure that contains many errors. In fact my problem is with the configuraion of ADC registers and also that of LCD. Here is my code that I can not understand:


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
#include "p24fxxxx.h"
#include "adcDrv1.h"
// JTAG/Code Protect/Write Protect/Clip-on Emulation mode
//Watchdog Timer/ICD pins select
_CONFIG1(JTAGEN_OFF & GCP_OFF & GWRP_OFF & COE_OFF & FWDTEN_OFF & ICS_PGx2)
// Disable CLK switch and CLK monitor, OSCO or Fosc/2, HS oscillator,
// Primary oscillator
_CONFIG2(FCKSM_CSDCMD & OSCIOFNC_OFF & POSCMOD_XT & FNOSC_PRI)
/* Digital Thermometer using PIC24F and LM35
///Internal Oscillator @ 4MHz, MCLR Enabled, PWRT Enabled, WDT OFF Copyright @ Rajendra Bhatt November 8, 2010 */
// LCD module connections
sbit LCD_RS at RC4_bit;
sbit LCD_EN at RC5_bit;
sbit LCD_D4 at RC0_bit;
sbit LCD_D5 at RC1_bit;
sbit LCD_D6 at RC2_bit;
sbit LCD_D7 at RC3_bit;
sbit LCD_RS_Direction at TRISC4_bit;
sbit LCD_EN_Direction at TRISC5_bit;
sbit LCD_D4_Direction at TRISC0_bit;
sbit LCD_D5_Direction at TRISC1_bit;
sbit LCD_D6_Direction at TRISC2_bit;
sbit LCD_D7_Direction at TRISC3_bit;
// End LCD module connections
// Define Messages
char message0[] = "LCD Initialized";
char message1[] = "Room Temperature";
// String array to store temperature value to display
char *tempC = "000.0";
char *tempF = "000.0";
// Variables to store temperature values
unsigned int tempinF, tempinC;
unsigned long temp_value;
void Display_Temperature() {
 // convert Temp to characters
 if (tempinC/10000)
 // 48 is the decimal character code value for displaying 0 on LCD
 tempC[0] = tempinC/10000 + 48;
 else tempC[0] = ' ';
 tempC[1] = (tempinC/1000)%10 + 48; // Extract tens digit
 tempC[2] = (tempinC/100)%10 + 48; // Extract ones digit
 // convert temp_fraction to characters
 tempC[4] = (tempinC/10)%10 + 48; // Extract tens digit
 // print temperature on LCD
 Lcd_Out(2, 1, tempC);
 if (tempinF/10000)
 tempF[0] = tempinF/10000 + 48;
 else tempF[0] = ' ';
 tempF[1] = (tempinF/1000)%10 + 48; // Extract tens digit
 tempF[2] = (tempinF/100)%10 + 48;
 tempF[4] = (tempinF/10)%10 + 48;
 // print temperature on LCD
 Lcd_Out(2, 10, tempF);
}
int main (void)
{
// ANSEL = 0b00000100; // RA2/AN2 is analog input
// ADCON0 = 0b01001000; // Connect AN2 to S/H, select Vref=1.19V
 //CMCON0 = 0x07 ; // Disbale comparators
 //////////////////////////////////////////Converting One Channel, Manual Sample Start, Manual Conversion Start
 int ADCValue;
 AD1PCFG = 0xFFFB; // AN2 as analog, all other pins are digital
 AD1CON1 = 0x0000; // SAMP bit = 0 ends sampling and starts converting
 AD1CHS = 0x0002; // Connect AN2 as S/H+ input
 // in this example AN2 is the input
 AD1CSSL = 0;
 AD1CON3 = 0x0002; // Manual Sample, Tad = 3Tcy
 AD1CON2 = 0;
 AD1CON1bits.ADON = 1; // turn ADC ON
 while (1) // repeat continuously
 {
 AD1CON1bits.SAMP = 1; // start sampling...
 Delay(); // Ensure the correct sampling time has elapsed
 // before starting conversion.
 AD1CON1bits.SAMP = 0; // start converting
 while (!AD1CON1bits.DONE){}; // conversion done?
 ADCValue = ADC1BUF0; // yes then get ADC value
 }
 TRISC = 0b00000000; // PORTC All Outputs
 TRISA = 0b00001110; // PORTA All Outputs, Except RA3 and RA2
 Lcd_Init(); // Initialize LCD
 Lcd_Cmd(_LCD_CLEAR); // CLEAR display
 Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
 Lcd_Out(1,1,message0);
 Delay_ms(1000);
 Lcd_Out(1,1,message1); // Write message1 in 1st row
 // Print degree character
 Lcd_Chr(2,6,223);
 Lcd_Chr(2,15,223);
 // Different LCD displays have different char code for degree symbol
 // if you see greek alpha letter try typing 178 instead of 223
 Lcd_Chr(2,7,'C');
 Lcd_Chr(2,16,'F');
 do {
 temp_value = ADC_Read(2);
 temp_value = temp_value*1168;
 tempinC = temp_value/1000;
 tempinC = tempinC*10;
 tempinF = 9*tempinC/5 + 3200;
 Display_Temperature();
 Delay_ms(1000); // Temperature sampling at 1 sec interval
 }
 while(1);
}



My code is a mixture of several of the codes on the net it shows me these errors:

TMR3ADC.c:19: error: syntax error before 'at'
TMR3ADC.c:19: warning: type defaults to 'int' in declaration of 'RC4_bit'
TMR3ADC.c:19: warning: data definition has no type or storage class
TMR3ADC.c:20: error: syntax error before 'LCD_EN'
TMR3ADC.c:20: warning: type defaults to 'int' in declaration of 'RC5_bit'
TMR3ADC.c:20: warning: data definition has no type or storage class
TMR3ADC.c:21: error: syntax error before 'LCD_D4'
TMR3ADC.c:21: warning: type defaults to 'int' in declaration of 'RC0_bit'
TMR3ADC.c:21: warning: data definition has no type or storage class
TMR3ADC.c:22: error: syntax error before 'LCD_D5'
TMR3ADC.c:22: warning: type defaults to 'int' in declaration of 'RC1_bit'
TMR3ADC.c:22: warning: data definition has no type or storage class
TMR3ADC.c:23: error: syntax error before 'LCD_D6'
TMR3ADC.c:23: warning: type defaults to 'int' in declaration of 'RC2_bit'
TMR3ADC.c:23: warning: data definition has no type or storage class
TMR3ADC.c:24: error: syntax error before 'LCD_D7'
TMR3ADC.c:24: warning: type defaults to 'int' in declaration of 'RC3_bit'
TMR3ADC.c:24: warning: data definition has no type or storage class
TMR3ADC.c:25: error: syntax error before 'LCD_RS_Direction'
TMR3ADC.c:25: warning: type defaults to 'int' in declaration of 'TRISC4_bit'
TMR3ADC.c:25: warning: data definition has no type or storage class
TMR3ADC.c:26: error: syntax error before 'LCD_EN_Direction'
TMR3ADC.c:26: warning: type defaults to 'int' in declaration of 'TRISC5_bit'
TMR3ADC.c:26: warning: data definition has no type or storage class
TMR3ADC.c:27: error: syntax error before 'LCD_D4_Direction'
TMR3ADC.c:27: warning: type defaults to 'int' in declaration of 'TRISC0_bit'
TMR3ADC.c:27: warning: data definition has no type or storage class
TMR3ADC.c:28: error: syntax error before 'LCD_D5_Direction'
TMR3ADC.c:28: warning: type defaults to 'int' in declaration of 'TRISC1_bit'
TMR3ADC.c:28: warning: data definition has no type or storage class
TMR3ADC.c:29: error: syntax error before 'LCD_D6_Direction'
TMR3ADC.c:29: warning: type defaults to 'int' in declaration of 'TRISC2_bit'
TMR3ADC.c:29: warning: data definition has no type or storage class
TMR3ADC.c:30: error: syntax error before 'LCD_D7_Direction'
TMR3ADC.c:30: warning: type defaults to 'int' in declaration of 'TRISC3_bit'
TMR3ADC.c:30: warning: data definition has no type or storage class
TMR3ADC.c: In function 'Display_Temperature':
TMR3ADC.c:60: warning: implicit declaration of function 'Lcd_Out'
TMR3ADC.c: In function 'main':
TMR3ADC.c:91: warning: implicit declaration of function 'Delay'
TMR3ADC.c:101: warning: implicit declaration of function 'Lcd_Init'
TMR3ADC.c:102: warning: implicit declaration of function 'Lcd_Cmd'
TMR3ADC.c:102: error: '_LCD_CLEAR' undeclared (first use in this function)
TMR3ADC.c:102: error: (Each undeclared identifier is reported only once
TMR3ADC.c:102: error: for each function it appears in.)
TMR3ADC.c:103: error: '_LCD_CURSOR_OFF' undeclared (first use in this function)
TMR3ADC.c:105: warning: implicit declaration of function 'Delay_ms'
TMR3ADC.c:108: warning: implicit declaration of function 'Lcd_Chr'
TMR3ADC.c:115: warning: implicit declaration of function 'ADC_Read'
Please help me
Thank you in advance
 
Last edited by a moderator:

You seem to be missing many of the definitions that are normally held in header (.h) files. Which PIC24 are you using? You probably want to explicitly name it instead of "p24fxxxx.h" which sounds very generic. The last three lines of the errors are telling you there is no such functon in the program so you are probably missing a library file as well.

Brian.
 

what defintion please must be exactly definned in the header file .h .I will use pic24f256gb110 and I changed it in my project and errors remain the same.
 

What Compiler are you using ? The code you have posted is mikroC PRO dsPIC code.

Please mention which PIC24 you are using and what frequency crystal you have used. If possible Zip and post complete project files and also your circuit.
 

Do you know which compiler this code is written for ? You are using which compiler ? Might be you are missing LCD driver files.
 

i work with Mplab so with c and i have one file .c and i find this project in this link embedded-lab.com/blog/?p=916
so i wold like to do the same work but with pic24f
 

As mentioned earlier the code you have posted is mikroC PRO PIC code and you have modified a little to MPLAB C30 or XC16 code. There are lot of errors. In mikroC there is library for ADC and LCD and that is what the author of code has used.

I compiled code in mikroC PRO dsPIC for 256GS110 not 256GB110. It worked.
 

Attachments

  • PIC24 ADC.rar
    206.5 KB · Views: 91
  • adc.png
    adc.png
    90.3 KB · Views: 104
Last edited:

I'm modify my code with this program

Code:
#include "p24fj256GB110.h"

//#include "adcDrv1.h"


// JTAG/Code Protect/Write Protect/Clip-on Emulation mode
//Watchdog Timer/ICD pins select
_CONFIG1(JTAGEN_OFF & GCP_OFF & GWRP_OFF & COE_OFF & FWDTEN_OFF & ICS_PGx2) 
// Disable CLK switch and CLK monitor, OSCO or Fosc/2, HS oscillator,
// Primary oscillator
_CONFIG2(FCKSM_CSDCMD & OSCIOFNC_OFF & POSCMOD_XT & FNOSC_PRI)


////////////////////////////////////////////////////////* Digital Thermometer using PIC24F and LM35 

///Internal Oscillator @ 4MHz, MCLR Enabled, PWRT Enabled, WDT OFF Copyright @ Rajendra Bhatt November 8, 2010 */

// LCD module connections
// LCD display ports etc
#define LCDdata LATE					// data port
#define LCDdataEnable	TRISE
#define RS LATBbits.LATB15				// RS bit in LCDstatus
#define RW LATDbits.LATD5				// read/write bit in LCDstatus
#define E  LATDbits.LATD4   			// enable bit in LCDstatus
#define Eenable TRISDbits.TRISD4
#define ERSenable TRISDbits.TRISD5
#define RWenable TRISBbits.TRISB15

void lcd_delay() { mSecDelay(2); }  // if LCD does not work make this longer

// Write a nibble to the LCD
// may have to adjust delays to suit the processor and clock
void lcdNibble(int n)
{
    int lcd=LCDdata;
	lcd_delay();
    LCDdata=n;
	lcd_delay();
    E=1;					// take clock E high 
	lcd_delay();
    E=0;
	lcd_delay();
 }

// Write a Control Command to the LCD
// This is written as two nibbles
void lcdCmd(int c)
{
 	RS=0;			        // Take RS pin low for command
	lcdNibble(c);			        // Makeup Lower Nibble
}

// write a data byte to LCD
int lcdPutchar(int d)
{
//    printf("%c", d);
	RS=1; 				// Take RS pin high for data
	lcdNibble(d);		            // Makeup Lower Nibble
    return 1;
}

// Initialise the LCD in 4bit Mode
void lcdInit()
{
	E=0;				// take E low
 	RS=0;				// Take RS pin low for command
 	RW=0;				// Take RS pin low for command
    // set RS, RW and E bits as output
	Eenable =0;
	ERSenable =0;
	RWenable =0;
    LCDdataEnable &= 0x0;          // set bits 0-3 output for data
	lcdNibble(0x3);		// This put the LCD into Soft Reset 
	lcdNibble(0x3);
	lcdNibble(0x3);
	lcdNibble(0x2);
	lcd_delay();
//	lcdCmd(0x28);			// 2 line, 4 bit mode 
	lcdCmd(0x38);			// 2 line, 8 bit mode 
	lcd_delay();
    lcdCmd(0x6);			// increment cursor after each write
	lcd_delay();
    lcdCmd(0x1);			// clear display
	lcd_delay();
    lcdCmd(0x2);			// home
	lcd_delay();
    lcdCmd(0xF);			// turn disply on
	lcd_delay();
}
// End LCD module connections


// Define Messages
char message0[] = "LCD Initialized";
char message1[] = "Room Temperature";


// String array to store temperature value to display
char *tempC = "000.0";
char *tempF = "000.0";


// Variables to store temperature values
unsigned int tempinF, tempinC;
unsigned long temp_value;


void Display_Temperature() {
 // convert Temp to characters
 if (tempinC/10000)
 // 48 is the decimal character code value for displaying 0 on LCD
 tempC[0] = tempinC/10000 + 48;
 else tempC[0] = ' ';
 tempC[1] = (tempinC/1000)%10 + 48; // Extract tens digit
 tempC[2] = (tempinC/100)%10 + 48; // Extract ones digit
 // convert temp_fraction to characters
 tempC[4] = (tempinC/10)%10 + 48; // Extract tens digit
 // print temperature on LCD
 Lcd_Out(2, 1, tempC);
 if (tempinF/10000)
 tempF[0] = tempinF/10000 + 48;
 else tempF[0] = ' ';
 tempF[1] = (tempinF/1000)%10 + 48; // Extract tens digit
 tempF[2] = (tempinF/100)%10 + 48;
 tempF[4] = (tempinF/10)%10 + 48;
 // print temperature on LCD
 Lcd_Out(2, 10, tempF);
}

int main (void)
{

// ANSEL = 0b00000100; // RA2/AN2 is analog input
// ADCON0 = 0b01001000; // Connect AN2 to S/H, select Vref=1.19V
 //CMCON0 = 0x07 ; // Disbale comparators

 //////////////////////////////////////////Converting One Channel, Manual Sample Start, Manual Conversion Start Code///////////////////////////////////////////////////
 /*int ADCValue;
 AD1PCFG = 0xFFFB; // AN2 as analog, all other pins are digital
 AD1CON1 = 0x0000; // SAMP bit = 0 ends sampling and starts converting
 AD1CHS = 0x0002; // Connect AN2 as S/H+ input
 // in this example AN2 is the input
 AD1CSSL = 0;
 AD1CON3 = 0x0002; // Manual Sample, Tad = 3Tcy
 AD1CON2 = 0;
 AD1CON1bits.ADON = 1; // turn ADC ON
 while (1) // repeat continuously
 {
 AD1CON1bits.SAMP = 1; // start sampling...
 Delay(); // Ensure the correct sampling time has elapsed
 // before starting conversion.
 AD1CON1bits.SAMP = 0; // start converting
 while (!AD1CON1bits.DONE){}; // conversion done?
 ADCValue = ADC1BUF0; // yes then get ADC value
 }
*/
 AD1CON1=0;			         // clear config
    AD1CHS = 1;			         // setup ADC channel
    AD1PCFGLbits.PCFG1 = 0;          //Disable digital input on AN1
    AD1CSSL = 0;		                //No scanned inputs
    AD1CON1 = 0x0E4;		        //auto sample start, auto-convert
    AD1CON2 = 0;		                //AVdd, AVss, int every conversion, MUXA only
    AD1CON3 = 0x1F05;		        //31 Tad auto-sample, Tad = 5*Tcy
    AD1CON1bits.ADON=1;		 // switch ON ADC
    while(!AD1CON1bits.DONE);        // Wait for conversion to complete
    int adc=ADC1BUF0;		        // read ADC value


 TRISC = 0b00000000; // PORTC All Outputs
 TRISA = 0b00001110; // PORTA All Outputs, Except RA3 and RA2
 lcdInit()
 Delay_ms(1000);
 Lcd_Out(1,1,message1); // Write message1 in 1st row
 // Print degree character
 Lcd_Chr(2,6,223);
 Lcd_Chr(2,15,223);
 // Different LCD displays have different char code for degree symbol
 // if you see greek alpha letter try typing 178 instead of 223
 Lcd_Chr(2,7,'C');
 Lcd_Chr(2,16,'F');
 do {
 temp_value = ADC_Read(2);
 temp_value = temp_value*1168;
 tempinC = temp_value/1000;
 tempinC = tempinC*10;
 tempinF = 9*tempinC/5 + 3200;
 Display_Temperature();
 Delay_ms(1000); // Temperature sampling at 1 sec interval
 } 
 while(1);
}

but i have some erors :

Code:
TMR3ADC.c:159: error: 'AD1CHS' undeclared (first use in this function)
TMR3ADC.c:159: error: (Each undeclared identifier is reported only once
TMR3ADC.c:159: error: for each function it appears in.)
TMR3ADC.c:160: error: 'AD1PCFGLbits' undeclared (first use in this function)
TMR3ADC.c:173: error: syntax error before 'Delay_ms'
so please can you help me

- - - Updated - - -

So what is this solution with Mplab and C
 
Last edited by a moderator:

So in my case what isthis solution please
 

Change to XC16 as your compiler. At the moment you are trying to use code written for mikroC Pro with MPLABs libraries. They are not that different but mikroC uses some unusual constructs and names of it's own which do not exactly match Microchips versions. In my opinion, MPLAB has far better debugging facilities than mikroC offers but the free version may not optimize the code quite as well.

Brian.
 

Please ZIp and post your complete project files so that it can be modified and tested. Mention Compiler used, Hi-Tech, C30 or XC16.

- - - Updated - - -

I found this. http://www.engscope.com/pic24-tutorial/11-adc/
 

My compiler is C30 and My project is in this zip file
 

Attachments

  • project_adc_lm35.rar
    21.3 KB · Views: 80

i'm modify my poject but i have this errorrs :
Code:
TMR3ADC.c: In function 'lcd_delay':
TMR3ADC.c:30: warning: implicit declaration of function 'mSecDelay'
TMR3ADC.c: In function 'lcdNibble':
TMR3ADC.c:36: warning: unused variable 'lcd'
TMR3ADC.c: In function 'Display_Temperature':
TMR3ADC.c:120: warning: implicit declaration of function 'Lcd_Out'
TMR3ADC.c: In function 'main':
TMR3ADC.c:151: warning: implicit declaration of function 'Delay'
TMR3ADC.c:162: error: syntax error before 'Delay'
TMR3ADC.c:165: warning: implicit declaration of function 'Lcd_Chr'
TMR3ADC.c:172: warning: implicit declaration of function 'ADC_Read'
TMR3ADC.c:178: warning: implicit declaration of function 'Delay_ms'
I'm sure that my code have many errors especially at the declaration of the DAC registers ,the LCD and the order of instructions
Thanks in advance
 

Attachments

  • project_adc_lm35(1).rar
    21.5 KB · Views: 78

choice of material for an electric card with PIC24F and diferent sensor

I need in my project a temperature sensor (20,75 °C), current sensor (-20A, 20A) and voltage senor (0,600V) to detect the current ,temperature and voltage of an electrical system.
Can you advise me with accurate sensors which are small and do not need a lot of conditioning circuit with the PIC24F.
Then I would like to show the temperature, current and voltage on a LCD display (lcd but what?) And even for pic24F is that you have more adequate than the other reference.
Thank you in advance
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top