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.

coding problem with lm35 interfacing with pic16f877a

Status
Not open for further replies.

yjia

Newbie level 3
Joined
Jul 27, 2013
Messages
4
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
26
my project is doing a fire alarm system. we are using mikroc compiler. in our project, we use 4 lm35 sensors that will light up 4 leds and sound the buzzer when the temperature is higher than 40 degree. besides, we are connecting our system to pc using max232. however, we are facing some coding errors that influence our process. i hope that anyone are willing to go through my coding and figure out what the problem we are actually facing. thank you.

Code:
#define LEDD PORTB.F4
#define Buzzer PORTB.F5
#include "built_in.h"
#include<string.h>
#include<stdio.h>
#include<math.h>

float temp, tempA, tempB, tempC, tempD;
int n;

void adc_conversion(unsigned int sensor_num);


const unsigned char msg1[] = "PIC16F877A";
const unsigned char msg2[] = "TEMP UNIT(v2)";

unsigned char i=0;

unsigned int adc_data = 0x00;
unsigned char buffer[16];
int sensor_num;
unsigned int temp_res;

void wait() {
  delay_ms(1000);
}

void main()
{

        TRISA = 0xFF;
        TRISB = 0;
        TRISC = TRISC & 0xFC;
        TRISD = 0x00;
        PORTB  = 0x00;

       UART1_Init(9600); // Initalize USART (9600 baud rate, 1 stop bit, ...
//Delay_ms(100); // wait uart module to se
        ADCON0 = 0x81;
//Fosc/64 is Selected
//Channel-0 is Selected
//Analog-to-Digital Converter Module is Powered Up
        ADCON1 = 0xC0;
//A/D Result Format Select Bit Right Justified
//and AN0 Channel as Analog Channel


        while(1)
        {
                ADCON0 = 0x81;        //First Channel
                sensor_num = 1;
                adc_conversion(sensor_num);
                ADCON0 = 0x89;
                sensor_num = 2;
                adc_conversion(sensor_num);
                ADCON0 = 0x91;
                sensor_num = 3;
                adc_conversion(sensor_num);
                ADCON0 = 0x99;
                sensor_num = 4;
                adc_conversion(sensor_num);

        }
}




void adc_conversion(unsigned int sensor_num){

                sensor_num--;
                ADCON0 = (0x81)|(sensor_num<<3);
                delay_ms(1000);
                ADCON0.GO  = 1; //Start Conversion
                while(ADCON0.GO  == 1);
                adc_data = ADRESH & 0x00FF;
                adc_data = adc_data<<8;
                adc_data = adc_data | ADRESL;
                adc_data = (500)*adc_data/1023;
                adc_data = temp;
                
                for (n = 0; n < 4; n++) {
                temp(n) = temp;
                }


                tempA = temp(0);
                tempB = temp(1);
                tempC = temp(2);
                tempD = temp(3);

       if ((tempA >= 40) ) {
          while(1){
          LEDA = 1; 
          delay_ms(1000);
          LEDA=0;
          }
          LEDB = 0;               //        *        and temperature B less than 35'C        *
          LEDC = 0;
          LEDD = 0;
          Buzzer = 1;
       }

       else if ((tempB >= 40) ) {
                                  //        *        LED B and Fan B activated only for *
                LEDA = 0;         //        *        temperature A less than 40'C and        *//lebih 22=26
                while(1){
                LEDB = 1;
                delay_ms(1000);
                LEDB=0;
                }

                LEDC = 0;
                LEDD = 0;
                Buzzer = 1;
       }

       else if ((tempC >= 40) ) {

                LEDA = 0;
                LEDB = 0;
                while(1){
          LEDC = 1;
          delay_ms(1000);
          LEDC=0;
          }
                LEDD = 0;
                Buzzer = 1;
       }

        else if ((tempD >= 40) ) {

                LEDA = 0;
                LEDB = 0;
                LEDC = 0;
                while(1){
          LEDD = 1;
          delay_ms(1000);
          LEDD=0;
          }
                Buzzer = 1;
       }

        else if ((tempA==tempB)&&(tempA >= 40)) {

                while(1){
          LEDA = 1;
          delay_ms(1000);
          LEDA=0;
          }
                while(1){
          LEDB = 1;
          delay_ms(1000);
          LEDB=0;
          }
                LEDC = 0;
                LEDD = 0;
                Buzzer = 1;
       }

       else if ((tempA==tempC)&&(tempA >= 40)) {

                while(1){
          LEDA = 1;
          delay_ms(1000);
          LEDA=0;
          }
                LEDB = 0;
                while(1){
          LEDC = 1;
          delay_ms(1000);
          LEDC=0;
          }
                LEDD = 0;
                Buzzer = 1;
       }

       else if ((tempA==tempD)&&(tempA >= 40)) {

                while(1){
          LEDA = 1;
          delay_ms(1000);
          LEDA=0;
          }
                LEDB = 0;
                LEDC = 0;
                while(1){
          LEDD = 1;
          delay_ms(1000);
          LEDD=0;
          }
                Buzzer = 1;
       }

       else if ((tempB==tempC)&&(tempB >= 40)) {

                LEDA = 0;
                while(1){
          LEDB = 1;
          delay_ms(1000);
          LEDB=0;
          }
                while(1){
          LEDC = 1;
          delay_ms(1000);
          LEDC=0;
          }
                LEDD = 0;
                Buzzer = 1;
       }

       else if ((tempB==tempD)&&(tempB >= 40)) {

                LEDA = 0;
                while(1){
          LEDB = 1;
          delay_ms(1000);
          LEDB=0;
          }
                LEDC = 0;
                while(1){
          LEDD = 1;
          delay_ms(1000);
          LEDD=0;
          }
                Buzzer = 1;
       }

       else if ((tempD==tempC)&&(tempD >= 40)) {

                LEDA = 0;
                LEDB = 0;
                while(1){
          LEDC = 1;
          delay_ms(1000);
          LEDC=0;
          }
                while(1){
          LEDD = 1;
          delay_ms(1000);
          LEDD=0;
          }
                Buzzer = 1;
       }

       else if ((tempA==tempB==tempC)&&(tempA >= 40)) {

                while(1){
          LEDA = 1;
          delay_ms(1000);
          LEDA=0;
          }
                while(1){
          LEDB = 1;
          delay_ms(1000);
          LEDB=0;
          }
                while(1){
          LEDC = 1;
          delay_ms(1000);
          LEDC=0;
          }
                LEDD = 0;
                Buzzer = 1;
       }

       else if ((tempB==tempC==tempD)&&(tempB >= 40)) {

                LEDA = 0;
                while(1){
          LEDB = 1;
          delay_ms(1000);
          LEDB=0;
          }
                while(1){
          LEDC = 1;
          delay_ms(1000);
          LEDC=0;
          }
                while(1){
          LEDD = 1;
          delay_ms(1000);
          LEDD=0;
          }
                Buzzer = 1;
       }

       else if ((tempC==tempD==tempA)&&(tempC >= 40)) {

                while(1){
          LEDA = 1;
          delay_ms(1000);
          LEDA=0;
          }
                LEDB = 0;
                while(1){
          LEDC = 1;
          delay_ms(1000);
          LEDC=0;
          }
                while(1){
          LEDD = 1;
          delay_ms(1000);
          LEDD=0;
          }
                Buzzer = 1;
       }
       else if ((tempD==tempA==tempB)&&(tempD >= 40)) {

                while(1){
          LEDA = 1;
          delay_ms(1000);
          LEDA=0;
          }
                while(1){
          LEDB = 1;
          delay_ms(1000);
          LEDB=0;
          }
                LEDC = 0;
                while(1){
          LEDD = 1;
          delay_ms(1000);
          LEDD=0;
          }
                Buzzer = 1;
       }
       else if ((tempA==tempB==tempC==tempD)&&(tempA >= 40)) {

                while(1){
          LEDA = 1;
          delay_ms(1000);
          LEDA=0;
          }
                while(1){
          LEDB = 1;
          delay_ms(1000);
          LEDB=0;
          }
                while(1){
          LEDC = 1;
          delay_ms(1000);
          LEDC=0;
          }
                while(1){
          LEDD = 1;
          delay_ms(1000);
          LEDD=0;
          }
                Buzzer = 1;
       }

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

do
{
temp_res = ADC_Read(0); // Get 10-bit results of AD conversion
UART1_write(Lo(temp_res)); // Send lower 8 bits to UART
Delay_ms(100);
UART1_write(Hi(temp_res)); // Send upper 8 bits to UART
PORTB = Lo(temp_res); // Send lower 8 bits to PORTB
PORTC = Hi(temp_res); // Send 2 most significant bits to RC7, RC6
}while(1);
}
 
Last edited:

hello,

you can not write
Code:
       tempA = adc_data(0);
       tempB = adc_data(1);
       tempC = adc_data(2);
       tempD = adc_data(3);

adc_data is defined as an integer ! not a table of value.
TempA is defined as a double (float)?
you don't use sensor_num to select the correct sensor
shift with 8 bits for MSB ... adc_data = ADRESH<<8 + ADRESL;
you are using int definition ADCON0 instead of bit definition like ADCON0bits.GO or ADCON0bits.DONE
...
 

Can you explain what these do?


Code C - [expand]
1
2
3
4
tempA = adc_data(0);
tempB = adc_data(1);
tempC = adc_data(2);
tempD = adc_data(3);

 

actually i`m trying to declare the value of temperature sensed by each of the sensors to temp A-D. could u guys suggest me for a better coding to be declared?
 
Last edited:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top