CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Temperature sensor and display with PIC16F690

 
Post new topic   Reply to topic    CCS Forum Index -> Code Library
View previous topic :: View next topic  
Author Message
Arakel



Joined: 06 Aug 2016
Posts: 107
Location: Moscow

View user's profile Send private message Visit poster's website AIM Address

Temperature sensor and display with PIC16F690
PostPosted: Sat Dec 03, 2016 5:42 am     Reply with quote

I thank to: Ttelmah, temtronic !

The program starts a heater(LED) if the value of a potentiometer is higher than the value of a sensor. It shows from -20 to 50 degrees Centigrade temperature on a 7segment display. If the temperature is in minus degrees a second "Minus_LED" is started to indicate that the display shows minus degrees.

Theoretically it can show from -50 to 59 but I do not think the electronics will survive!

Code:
//A program to scan the values of an ADC and potentiometer and start a heater if they are different with the debouncing routine
//The difference is that the minus degrees on the display are with a different coding realization

#include <16F690.h>

#device ADC=10

#fuses NOMCLR, NOWDT, NOPROTECT, INTRC_IO
#use delay(clock=4M)

#define Minus_LED PIN_B5

void main()
{
   /**********Initiasization*************/
   int i,z;     //Local variables
   unsigned int16 ADC_10bit_Sensor;
   unsigned int16 ADC_10bit_Potentiometer;
   unsigned int16 OutputDigit;  //This digit is only used to be put inside FinalFormOfThePORTCOutput so we know which element number of the array to use
   unsigned int8 FinalFormOfThePORTCOutput[80] =
   {
      0b00000000, // 0
      0b00000001,
      0b00000010,
      0b00000011,
      0b00000100,
      0b00000101,
      0b00000110,
      0b00000111,
      0b00001000,
      0b00001001,
      0b00010000, // 10
      0b00010001,
      0b00010010,
      0b00010011,
      0b00010100,
      0b00010101,
      0b00010110,
      0b00010111,
      0b00011000,
      0b00011001,
      0b00100000, // 20
      0b00100001,
      0b00100010,
      0b00100011,
      0b00100100,
      0b00100101,
      0b00100110,
      0b00100111,
      0b00101000,
      0b00101001,
      0b00110000, // 30
      0b00110001,
      0b00110010,
      0b00110011,
      0b00110100,
      0b00110101,
      0b00110110,
      0b00110111,
      0b00111000,
      0b00111001,
      0b01000000, // 40
      0b01000001,
      0b01000010,
      0b01000011,
      0b01000100,
      0b01000101,
      0b01000110,
      0b01000111,
      0b01001000,
      0b01001001,
      0b01010000, // 50
      0b01010001,
      0b01010010,
      0b01010011,
      0b01010100,
      0b01010101,
      0b01010110,
      0b01010111,
      0b01011000,
      0b01011001,
   };
   
      
      
   
   setup_comparator(NC_NC_NC_NC); //We disable the comparators so we can use the PINs

   setup_adc_ports( sAN0 | sAN1 );   //Adc functions
   setup_adc( ADC_CLOCK_DIV_16 );
   
   while(TRUE)
   {
    /**********We set and read from the ADC channels, meaning from the potentiometer and sensor*************/ 
      
   set_adc_channel( 0 );   //We set the ADC to read from CHANNEL 0 (Where the potentiometer is connected)
   for(i=0; i<15; i++)
   {
      delay_ms(1);         //The ADC is 10 bit so we have "2 to Grade 10" values, meaning 1024
      ADC_10bit_Potentiometer = read_adc(); //So for 5 volts we have: 5/1024 = 0.0048828125 Volts per ADC tick!
         if(ADC_10bit_Potentiometer != ADC_10bit_Potentiometer)// Meaning that for an ADC value of 600 we have: 600 x 0.0048828125 Volts
            i=0;         
   }
   
   set_adc_channel( 1 );   //We set the ADC to read from CHANNEL 1 (Where the sensor is connected)
   for (i=0; i<15; i++)
   {
      delay_ms(1);
      ADC_10bit_Sensor = read_adc();
      if (ADC_10bit_Sensor != ADC_10bit_Sensor)
         i=0;
   }
   
         if (ADC_10bit_Sensor <= ADC_10bit_Potentiometer)    //If the sensor is lower than the potentiometer turn ON the heater
       {
          delay_ms(1);
          output_b(0b00010000);
       } else {                                        //If the sensor is higher than the potentiometer turn OFF the heater
         output_b(0b00000000);
       }
      
      {
         
         if (ADC_10bit_Sensor >= 578) //578 from the ADC equals zero degrees("2.82276"Volts). This "if statement" is only for the positive degrees plus the zero
         {
         OutputDigit = ((ADC_10bit_Sensor*0.0048828125) - 2.82276)/ 0.01163;
         output_c(FinalFormOfThePORTCOutput[OutputDigit]);
         }
      }
      if (ADC_10bit_Sensor < 578)     //This "if statement" is only for the negative degrees(under "2.82276"Volts)
      {   //When we get a positive result we use the same "FinalFormOfThePORTCOutput" table but we start a LED( "Minus_LED") to indicate its minus degrees
         OutputDigit = ((ADC_10bit_Sensor*0.0048828125) - 2.82276)/ (-0.01163); //We divide by "-0.01163" in order to get a positive result because "minus/minus = plus"
         output_c(FinalFormOfThePORTCOutput[OutputDigit]);
         delay_ms(1);
         output_high(Minus_LED);    //If the ADC for the Sensor is under 578 we have minus degrees so we output "high" the "Minus_LED".
         delay_ms(1);
      }
      
         
 }//Close while
}

_________________
Yo! I love learning and technology! I just do not have experience so do not be angry if I ask a stupid question about a detail! From so much to remember sometimes I forget the details in order to remember the big problems!
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Mon Dec 05, 2016 5:14 pm     Reply with quote

why or why are you using those floats with a
10 bit integer ADC reading ??
you are losing vast numbers of cpu cycles
and code space to the floating point math .
OMG what a slow poke program
Arakel



Joined: 06 Aug 2016
Posts: 107
Location: Moscow

View user's profile Send private message Visit poster's website AIM Address

PostPosted: Mon Dec 05, 2016 5:19 pm     Reply with quote

It works perfect! Plus I need the float math in order to do the accuracy so I can get 1 degree difference!

I do not have float variables only constants, it should not be a problem for the coding and the processor is fast enough.

If you can give me another way to make the accuracy, I am listening?
After all I am learning here and miracles will not happen with the first program. After 20 years I might be able to do it perfect but not yet!

I welcome all critiques! The more I learn the better!
_________________
Yo! I love learning and technology! I just do not have experience so do not be angry if I ask a stupid question about a detail! From so much to remember sometimes I forget the details in order to remember the big problems!
Gabriel



Joined: 03 Aug 2009
Posts: 1067
Location: Panama

View user's profile Send private message

PostPosted: Wed Dec 07, 2016 6:13 am     Reply with quote

Quote:
It works perfect! Plus I need the float math in order to do the accuracy so I can get 1 degree difference!



35.56 float vs. 3556 int.

Ask yourself: What is the accuracy diference?

G.
_________________
CCS PCM 5.078 & CCS PCH 5.093
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> Code Library All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group