unsigned long int Temp;
unsigned int adc_sample_1, adc_sample_2;
unsigned int adc_sample_3, adc_sample;
int scale_coeff = 2932;
void main() {
WDTCON = 0x0018; //Enable Watchdog timer
PWM1_Init(19530); //Initialize PWM at 19.53KHz (per pic spec-sheet allows 10bit resolution)
ADC_Init(); //Initialize ADC
ADCON0 = 0x1F; //Set VDD as Vref, ANO as analog read
TRISIO.B2 = 0; //Set pin 5 (CCP1) as output, used for PWM
TRISIO.B0 = 1; //Set pin 7 (ANO) as input, used for ADC read
while(1){
//ADC Read at ANO
adc_sample_1 = ADC_Read(0);
Delay_ms(1000);
adc_sample_2 = ADC_Read(0);
Delay_ms(1000);
adc_sample_3 = ADC_Read(0);
Delay_ms(1000);
adc_sample = (adc_sample_1 + adc_sample_2 + adc_sample_3)/3; //Take 3 sample from ADC and take the average
//Read ADC Channel 1
Temp = (adc_sample*scale_coeff)/10000; //Using Integer calculation
//PWM formular for 10 bit resolution: (Percent*1023)/100
if(Temp > 30 && Temp < 40)
{
PWM1_Set_Duty(307); //Set PWM to 30%
PWM1_Start();
}
else if(Temp > 40 && Temp < 50)
{
PWM1_Set_Duty(512); //Set PWM to 50%
PWM1_Start();
}
else if(Temp <= 30)
{
PWM1_Set_Duty(0); //Trun off PWM
PWM1_Stop();
break; //Breaks program out of the while loop and causes it to re-initialize all the registers/start over the process.
}
}
}
really be:if(Temp > 30 && Temp < 40)
if((Temp > 30) && (Temp < 40))
TRISIO.B2 = 0;
TRISIO.B0 = 1;
ADC_Read(0);
PWM1_Set_Duty(307);
PWM1_Start();
PWM1_Stop();
PWM1_Init(19530); //Initialize PWM at 19.53KHz (per pic spec-sheet allows 10bit resolution)
ADC_Init();
PWM1_Set_Duty(512);
const unsigned short VREF = 5.00;
float Temp_C;
unsigned int adc_sample;
void main() {
WDTCON = 0x0018; //Enable Watchdog timer
ADC_Init(); //Initialize ADC
ADCON0 = 0x1F; //Set VDD as Vref, ANO as analog read
TRISIO.B2 = 0; //Set pin 5 (CCP1) as output, used for PWM
TRISIO.B0 = 1; //Set pin 7 (ANO) as input, used for ADC read
PWM1_Init(5000); //Initialize PWM at 19.53KHz (per pic spec-sheet allows 10bit resolution)
PWM1_Start();
while(1){
//Read ADC Channel 1
adc_sample = ADC_Get_Sample(0);
//Temp_C = (adc_sample * VREF)/10.240; // Calculate temperature in Celsuis
Temp_C = (adc_sample * VREF)/49.960;
//PWM formular for 8 bit resolution: (Percent*255)/100
if((Temp_C >= 30) && (Temp_C <= 40))
{
PWM1_Set_Duty(127); //Set PWM to 50%
}
else if((Temp_C >= 41) && (Temp_C <= 50))
{
PWM1_Set_Duty(191); //Set PWM to 75%
}
else if(Temp_C > 50)
{
PWM1_Set_Duty(255); //Set PWM to 100%
}
else if(Temp_C < 30)
{
PWM1_Set_Duty(0); //Trun off PWM
// PWM1_Stop();
}
Delay_ms(500);
}
}
//Read ADC Channel 1
adc_sample = ADC_Get_Sample(0);
if((adc_sample >= 100) && (adc_sample <= 120)) // using these numbers as examples
{
PWM1_Set_Duty(127); //Set PWM to 50%
}
WDTCON = 0x0018; //Enable Watchdog timer
bit 7-5 Unimplemented: Read as ‘0’
bit 4-1 WDTPS<3:0>: Watchdog Timer Period Select bits
Bit Value = Prescale Rate
0000 = 1:32
0001 = 1:64
0010 = 1:128
0011 = 1:256
0100 = 1:512 (Reset value)
0101 = 1:1024
0110 = 1:2048
0111 = 1:4096
1000 = 1:8192
1001 = 1:16384
1010 = 1:32768
1011 = 1:65536
1100 = Reserved
1101 = Reserved
1110 = Reserved
1111 = Reserved
bit 0 SWDTEN: Software Enable or Disable the Watchdog Timer(1)
1 = WDT is turned on
0 = WDT is turned off (Reset value)
Note 1: If WDTE Configuration bit = 1, then WDT is always enabled, irrespective of this control bit. If WDTE
Configuration bit = 0, then it is possible to turn WDT on/off with this control bit.
unsigned int Temp, Temp_Old;
unsigned int adc_sample;
void main() {
//WDTCON = 0x0018; //Enable Watchdog timer
ADC_Init(); //Initialize ADC
ADCON0 = 0x1F; //Set VDD as Vref, ANO as analog read
TRISIO.B2 = 0; //Set pin 5 (CCP1) as output, used for PWM
TRISIO.B0 = 1; //Set pin 7 (ANO) as input, used for ADC read
PWM1_Init(5000); //Initialize PWM at 19.53KHz (per pic spec-sheet allows 10bit resolution)
PWM1_Start();
while(1){
adc_sample = ADC_Read(0);
//Read ADC Channel 1
Temp = (adc_sample/4); //Convert 10 bit ADC value to 8 bit value
if(Temp != Temp_Old)
{
//PWM formular for 8 bit resolution: (Percent*255)/100
//if(Temp >= 30)
if(Temp >= 20)
{
PWM1_Set_Duty(255); //Set PWM to 100%
}
//else if(Temp < 30)
else if(Temp < 20)
{
PWM1_Set_Duty(0); //Turn off PWM
}
Temp_Old = Temp;
}
Delay_ms(500);
}
}
adc_sample = ADC_Read(0);
adc_sample = (unsigned long)ADC_Read(0) * 500 / 1024;
unsigned int Temp, Temp_Old;
unsigned long adc_sample;
unsigned int raw_adc_value = 0;
void main() {
//WDTCON = 0x0018; //Enable Watchdog timer
ADCON0 = 0x1F; //Set VDD as Vref, ANO as analog read
TRISIO.B2 = 0; //Set pin 5 (CCP1) as output, used for PWM
TRISIO.B0 = 1; //Set pin 7 (ANO) as input, used for ADC read
PWM1_Init(5000); //Initialize PWM at 19.53KHz (per pic spec-sheet allows 10bit resolution)
PWM1_Start();
while(1){
raw_adc_value = ADC_Read(0);
adc_sample = (unsigned long)raw_adc_value * 500 / 1024;
//Read ADC Channel 1
Temp = (raw_adc_value/4); //Convert 10 bit ADC value to 8 bit value
if(Temp != Temp_Old)
{
//PWM formular for 8 bit resolution: (Percent*255)/100
if(Temp >= 30)
{
PWM1_Set_Duty(255); //Set PWM to 100%
}
else if(Temp < 30)
{
PWM1_Set_Duty(0); //Turn off PWM
}
Temp_Old = Temp;
}
Delay_ms(500);
}
}
Could you please explain the need for a capacitor at the base of the transistor. This circuit is not originally mine but was found elsewhere.
According to the LM35 data sheet (https://www.ti.com/lit/ds/symlink/lm35.pdf, Table 6.3), Tmin is -55C and Tmax is 150C.If LM35 gives 1.5V then it is 150 deg C. So, for 5V it should be 500 deg C.
LM35 really is that simple, except for -ve temps you need the appropriate package and a negative bias R on the output.According to the LM35 data sheet (https://www.ti.com/lit/ds/symlink/lm35.pdf, Table 6.3), Tmin is -55C and Tmax is 150C.
The output voltage is linearly proportional to the temperature (at 10mV/C) but that does not mean it is as simple as 10mV = 1C. For example, if that were the case, then what is the output voltage for -30C (for example) with respect to GND.
Susan
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?