15 July 2007

In order to be able to read from the output of the digital temperature sensor, I wrote algorithm to test it out.

At the beginning, I made use of both ccp1 and ccp2 to capture rise and fall of the input signal generated by the signal generator. During the testing, I found out that there is no pulse for the output signal. So later on, I changed to just test one of the ccp to capture just the rise. It also doesn't work out. I also tested with Roshan for several dozens of methods, but even the basic toggle wouldn't work out in the interrupt. At first we thought it might be the interrupt doesn't work properly for 16F77, until later on we changed the pin for outupt, and found out that it's the problem for that certain pin RD5. The following program is one that works when using CCP.

//It enable interrupt when ccp1 captures the rise, and would output high and low alternatively when the program enters the interrupt. //

#include <16F77.h>
#fuses HS,NOWDT,NOPROTECT,NOBROWNOUT
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

int num_rise = 0;

#int_ccp1
void isr1()
{
output_high(PIN_B7);
num_rise = num_rise + 1;
if (num_rise == 1){
output_high(PIN_D3);
}
else if (num_rise == 2){
output_low(PIN_D3);
num_rise = 0;
}
}

void main()
{
set_tris_d(0x00); //output pins
set_tris_b(0x00);
setup_ccp1(CCP_CAPTURE_RE); // Configure CCP1 to capture rise
//setup_ccp2(CCP_CAPTURE_FE); // Configure CCP2 to capture fall

enable_interrupts(INT_CCP1); // Setup interrupt on rising edge
enable_interrupts(GLOBAL);
output_d(0b00000000);
while(true){
}
}

The following one is another algorithm that does not make use of the ccp. It is because when we need to output PWM from pic to control the MOSFET, we will have to use of the ccp pin to generate PWM signal.

Whenever in the main, it calls the function calcTemp function, it will skip the first two states the signal is in (either high or low), so that the correct counting could start when the signal just changes from high to low or from low to high. In the following two while loops for output high and low, we will be using counters to count the time when the signal stays high and when it stays low, to get T1, and T2. And based on the formula given by the TMP04 datasheet, we would be able to get what is the temperature at that moment.

#include <16F77.h>
#fuses HS,NOWDT,NOPROTECT,NOBROWNOUT
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

void calcTemp(){
while(input(pin_B4)){;}
while(!input(pin_B4)){;}
while(input(pin_B4)){
output_high(PIN_D3);

}
while(!input(pin_B4)){
output_low(PIN_D3);
}
//delay_ms(2000);
//output_toggle(PIN_D5);
//output_toggle(PIN_D3);
}
void main()
{

set_tris_d(0x00); //output pins
set_tris_b(0xFF);

output_d(0b00000000);
while(true){
delay_ms(1700);
calcTemp();

}
}

22094576.36709e.jpg The picture shows the output we got reading RD3 of the above program. The yellow line is the output from RD3, and the green one below is the input. The signal we input is a pulse with a Vpp of 5V, and frequency of 100hz. Because every time we skip a high and a low in the function calcTemp, so the yellow one has period twice of green one.

The two TMP04 we bought, one of them is spoiled as it is given the same interval for both high and low, and indicating the temperature is not changed. Only one of them is functioning properly. The formula to calculate the temperature in celsius degree = 235 - (400*t1/t2), t1 refers to the time when input is high, t2 refers to time when input is low.

sha
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.