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.

[PIC] C Code for LM35 with PIC16F887 in HTC c compiler

Status
Not open for further replies.

jay_3189

Banned
Full Member level 1
Joined
Sep 19, 2013
Messages
104
Helped
0
Reputation
0
Reaction score
0
Trophy points
16
Location
Ahmedabad
Visit site
Activity points
0
Hello everyone
i want to make one temperature control system which control the temperature using FAN and again that need to display temperature in Hyper terminal.

so anyone one have code then me the that for LM35 with PIC16f887 code using HItech c compiler.
 

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**
 
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**

Is that needed to put OPamp in between LM35 and PIC16f887?
and if yes then explain in brief why?
because I am not getting exact thing?
 

Direct connect the output pin of LM35 to the ADC pin of PIC , No need of any op-amp

then connect only on that pin which ( you are reading in your ADC logic )
 

hello


Do you have a LM35 with output = 2730mV at 0°C
or a LM35DZ with output= 0mV at 0°C ?
With ADC 10 bits and +VREf=5V , resolution is only +-0.5°C
if you want better resolution you can use
an PGA AOP MCP6S21 to amplifie the signal by 1,2,4,5,10,16 or 32 ...
 

LM35 with PIC16F887 using Serial communication in HTC C Compiler

Hello

I have C Code for LM35 and PIC16F887 using serial communication in HTC C Compiler as below.
but there is no output comes in Hyper Terminal.
Then I have check it into Docklight then there will be output comes but it displaying only Y with two dot above on it by every two seconds in ASCII value.

Then I have restart my controller's power supply then again check into Docklight but now it is not giving anything on the display.

Please help me to solve this as soon as possible.
I have connected LM35's output pin directly to Controller's AN4 th pin.


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
    }
}

 
Last edited:

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 ?
 

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 ?

Want to display temperature.
I am using LM35.
sending FF by Docklight software from PC using RS232.
Docklight is option of Hyper terminal.
If you have simple code in HTC C Compiler then please I need that as I am not getting output here.
 
Last edited:

I2C c code for PIC16F887 and HTC C Compiler

I have written c code for I2C in PIC16F887 using HTC c compiler and for DS1307. so, can I check that is right or wrong?

Is there any way to check that code is right or wrong?
I2C code is as below.

HTML:
#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 );
}
 

Re: I2C c code for PIC16F887 and HTC C Compiler

yes you can use proteus as controller you use and RTC u used all are present in proteus.

- - - Updated - - -

http://electrosome.com/rtc-ds1307-pic-microcontroller/
for your reference
 

I think you redefine the function to read ADC
and then apply following
Code:
 sprintf(char Variable , " %d " , Integer Variable)
and print the Char Variable ,
and also make it simple like Check for The UART Code also in hyperterminal I mean whether it is simply reading char and printing char this will assure that uart is working and then
make a function to read ADC as you have done also
 
The last statement is that you make a function
unsigned int read_adc() so that you could easily debug the scene that what exactly is not working

as I have told you check your UART , Then try with LM35 to get the value whatever it is giving then apply Sprintf Function then print that Char Variable instead of int
 
PIC16F887 DS1307 I2C Serial Communication Automation

Hello

I have written logic for I2C , UART, and DS1307 in pic16f887 controller using HTC C Compiler.
Now I want to ON and OFF to few pin of port D in controller using DS1307 RTC timing.
so, could I do this can any one suggest me?

Could I set my PC time to DS1307 time?
 
Last edited:

Re: PIC16F887 DS1307 I2C Serial Communication Automation

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 ??
 

Re: PIC16F887 DS1307 I2C Serial Communication Automation

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);
}

 
Last edited by a moderator:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top