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.

Help me fix a code for PIC16F876A with LM35

Status
Not open for further replies.

savoc

Newbie level 6
Joined
May 16, 2012
Messages
13
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,382
I need help to solve this problem, the coding can be compile and trasfer to PIC, but LED that need to be UP when certain condition is met is not lightup, here are my coding:-



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
#include<pic.h>
 
 
__CONFIG (0x3F32);
 
 
#define     RS          RA2
#define     E           RA5
#define     CHANNEL0    0b10000001  // AN0
#define     buzzer      RB5
#define     ledA        RB2
#define     ledB        RB1
 
//==============FUNCTION PTOTOTYPE=========================
 
void delay(unsigned short i);
void read_adc(void);
unsigned short read_temp(void);
 
//====================MAIN================================
unsigned short result;
unsigned short temp,tempA,tempB;
 
void main(void)
{
    ADRESH=0;                   //clear A/D result
    ADRESL=0;                   //clear A/D result
 
    //setting ADCON1 Register
    ADCON1=0b11000101;          // A/D result right justified,              
                            
 
    TRISA=0b11011011;           //configure PORTA I/O direction
    TRISB=0b00000000;           //configure PORTB as output
    TRISC=0b00000000;           //configure PORTC as output
 
//  PORTA=0;
    PORTB=0;
    
    while(1)                    
    {
    
 
        while(1)                        //infinity loop
        {
        //sensor A
        ADCON0=CHANNEL0;                //CHANNEL1=0b10001001
/*      lcd_goto(8);            
        temp=read_temp();
        dis_num(temp/10);
        send_char('.');
        dis_num(temp%10);
        send_char(0b11011111);
        send_char('C');
        send_char(' ');
        send_char(' ');
*/  
        temp=read_temp();
        read_adc();
        tempA=result;
        
        
    
            if((tempA>220)&&(tempA<300))        //  *****************************************
                {                               //  *   LED A and Fan A activated only for  *
                    ledA=1;                     //  *   temperature A greater than 40'C     *  //lebih27
                    ledB=0;                     //  *   and temperature B less than 35'C    *
                                            //  *****************************************
                    
                    
                }   
 
            else((tempA>301)&&(tempA<400))  //  *****************************************
                {                               //  *   LED B and Fan B activated only for  *
                    ledA=1;                     //  *   temperature A less than 40'C and    *//lebih 22=26
                    ledB=1;                     //  *   temperature B greater than 35'C     *
                                            //  *****************************************
                    
                    buzzer=0;
                }   
 
        //  else if((tempA>210))    //  *****************************************************
        //      {                               //  *   All LED A & LED B, Fan A & Fan B and Buzzer     *
        //          ledA=0;                     //  *   activated for temperature A greater than 40'C   *//kurang 21
        //          ledB=1;                     //  *   and temperature B greater than 35'C             *
        //                                  //  *****************************************************
                    
        //          buzzer=1;
        //      }   
 
        
 
        delay(2000);
    
        }
    
    }
        
}
        
                    
 
//==================subroutine ADC=========================
 
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 
    {
        ADGO = 1;                   //ADGO is the bit 2 of the ADCON0 register
        while(ADGO==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
 
}
 
unsigned short read_temp(void)
{
    unsigned short temp;
    temp=result;
    return temp;
 
}
 
//==================subroutine DELAY==========================
void delay(unsigned short i)
{   
    for(;i>0;i--);
}

 
Last edited by a moderator:

Re: PIC16F876A with LM35

Why there is that external while loop?

Also,
why read_temp(); is called? temp has not been used enywhere.

How the LEDs are connected?

Display temperature on LCD 16x2 display using LM35 and Pic16F876A https://www.edaboard.com/blog/1486/
 

Re: PIC16F876A with LM35

Why there is that external while loop?

Also,
why read_temp(); is called? temp has not been used enywhere.

How the LEDs are connected?

Display temperature on LCD 16x2 display using LM35 and Pic16F876A https://www.edaboard.com/blog/1486/

the led is connected to rb2 and rb1, this just for check my board, the output should be displayed in my laptop using Xbee
 

Re: PIC16F876A with LM35

Code:
else((tempA>301)&&(tempA<400))

should be

Code:
else if ((tempA>301)&&(tempA<400))
 

Re: PIC16F876A with LM35

thank you for replying, how about ADGO? it should be ADGO or ADON?
 

Re: PIC16F876A with LM35

In Hi Tech C, Something like this

Code:
[syntax = c]

                ADCON0=0x80;
		Delay_ms(5);
		ADCON0bits.ADON = 1;
                Delay_ms(5);
                ADCON0bits.GO = 1;
                while(ADCON0bits.GO);
                ADCON0bits.ADON = 0;
                result = ADRESH;        // copy high byte to result
                result <<= 8;           // shift low byte to 2nd byte
                result |= ADRESL; 

[/syntax]

Which compiler are you using?

Check this https://www.edaboard.com/threads/252959/
 
Last edited:

Re: PIC16F876A with LM35

i`m using MPLAB.
 

Re: PIC16F876A with LM35

MPLAB is an IDE. Which compiler are you using. Is it Hi Tech C or mplab C? Can't you use Hi Tech C Lite?

It will be still better if you use mikroC PRO PIC 5.6............

If you can use mikroC, then I can give you the code.
 

Re: PIC16F876A with LM35

i`m using mplab C.. can u give me the code. i try it first.
 

Re: PIC16F876A with LM35

I've to write the code. Tell me which pic you are using?
 

Re: PIC16F876A with LM35

pic16f876a, thank you so much, you are so kind ^__^
 

Re: PIC16F876A with LM35

Tell me How your LCD is connected, and also your LEDs and buzzer.
 

Re: PIC16F876A with LM35

the led connected to rb1 and rb2, there is no lcd, because i want the data will be sent to others device(laptop/hyperterminal) using xbee for now, i just want to make the led become the output.
 

Re: PIC16F876A with LM35

Here is the mikroC code.

Code:
[syntax = mikroC]

#define LEDA PORTB.F1
#define LEDB PORTB.F2
#define Buzzer PORTB.F5

double tempA;

void main() {
       TRISB = 0x26;              //0b00100110; RB1, RB2, RB5 inputs , all others outputs
       PORTB  = 0x00;             // clear PORTB
       ADCON0 = 0x40;             //
       ADCON1 = 0xCE;             //
       
       //ADC_init();
       tempA = ADC_Read(0);
       
       if ((tempA >= 220) && (tempA <= 300)) {                          
                                  //        *        LED A and Fan A activated only for *
          LEDA = 1;               //        *        temperature A greater than 40'C         * //lebih27
          LEDB = 0;               //        *        and temperature B less than 35'C        *
       }        

       else if ((tempA > 300) && (tempA <= 400)) {
                                  //        *        LED B and Fan B activated only for *
                LEDA = 1;         //        *        temperature A less than 40'C and        *//lebih 22=26
                LEDB = 1;         //        *        temperature B greater than 35'C         *
                Buzzer = 1;
       }        

       else {
           LEDA = 0;
           LEDB = 0;
           Buzzer = 0;
       }
       
}

[/syntax]
 
Last edited:

Re: PIC16F876A with LM35

thank you. i at office right now, will try after i back at home

---------- Post added at 05:25 ---------- Previous post was at 04:52 ----------

thank you. i at office right now, will try after i back at home

---------- Post added at 05:26 ---------- Previous post was at 05:25 ----------

i cannot compile the code.



---------- Post added at 05:27 ---------- Previous post was at 05:26 ----------

i cannot compile the code.

 

Re: PIC16F876A with LM35

In the Library Manager, Check the ADC entry.
 

Attachments

  • adc.jpg
    adc.jpg
    173.4 KB · Views: 115

Re: PIC16F876A with LM35

ok.. thank you for your information. really appreciated it, after reach home i will transfer the hex file into pic, i will inform u the progress. thank you very much
 

Re: PIC16F876A with LM35

sorry for the late update. i has been sent to site for 2 week. by the way the project is not running. maybe because of compile. i will sent the diagram of my board tomorrow.
 

Re: PIC16F876A with LM35

Here is the mikroC code.



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
#define LEDA PORTB.F1
#define LEDB PORTB.F2
#define Buzzer PORTB.F5
 
double tempA;
 
void main() {
       TRISB = 0x26;              //0b00100110; RB1, RB2, RB5 inputs , all others outputs
       PORTB  = 0x00;             // clear PORTB
       ADCON0 = 0x40;             //
       ADCON1 = 0xCE;             //
       
       //ADC_init();
       tempA = ADC_Read(0);
       
       if ((tempA >= 220) && (tempA <= 300)) {                          
                                  //        *        LED A and Fan A activated only for *
          LEDA = 1;               //        *        temperature A greater than 40'C         * //lebih27
          LEDB = 0;               //        *        and temperature B less than 35'C        *
       }        
 
       else if ((tempA > 300) && (tempA <= 400)) {
                                  //        *        LED B and Fan B activated only for *
                LEDA = 1;         //        *        temperature A less than 40'C and        *//lebih 22=26
                LEDB = 1;         //        *        temperature B greater than 35'C         *
                Buzzer = 1;
       }        
 
       else {
           LEDA = 0;
           LEDB = 0;
           Buzzer = 0;
       }
       
}


Savoc,
Please try to go through some C program written for PIC micro first.
There are some errors present in the code and the way you are compiling the above program.
Some hints,

1. You must not let the control go out of the main(). Use an infinite loop to trap the control.

2. You are using microC pro compiler. To use the ADC_Read(unsigned short channel) function you must select ADC library from View-> Library Manager.

3. Enlarge the error message picture (as attached by you) and see message no 1. at the lower part. It says:-

mikroCPIC1618.exe -MSF -DBG -pP 18F45K22 -DL etc. etc.

Please check the microIde or project setting. Be sure that the currect device is selected. You are using 16F876A.

For LM35, take average of 2000 readings. use proper delay etc.
 

Re: PIC16F876A with LM35

can i used others compiler? like PICkit 2?
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top