![]() | ![]() | ![]() |
| |||||||
| Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc. |
![]() |
| | Tools |
| | #1 |
|
Hey there, Nice to be back! Ok, so I'm making my homebrew room thermometer, which is actually made from some transistors, a LM335Z temp. sensor (10mV per Kelvin), a 2 7-seg display and a PIC18F1320. The thing is the multiplexing works pretty well, but not for the A/D conversion. The numbers on the display is very freaky, and not displaying anything that made sense. (It ended up displaying 09 instead of a room temperature, which is approx. 26 degrees celcius!) Both of the A/D and Multiplexing are in interrupts by the way. Here's the code. By the way, I have included a hefty conversion from Kelvin to Celcius. I have made it right-justified, and placed all the interrupt vectors properly. What could I have been missing? ![]() Code: #pragma config OSC = HS
#pragma config PWRT = OFF, BOR = OFF
#pragma config WDT = OFF
#pragma config DEBUG = OFF, LVP = OFF, STVR = OFF, MCLRE = OFF
#include <P18F1320.h>
void T0_ISR(void);
void AD_ISR(void);
#pragma interrupt chk_isr
void chk_isr(void)
{
if (INTCONbits.TMR0IF == 1)
T0_ISR();
if (PIR1bits.ADIF == 1)
AD_ISR();
}
#pragma code HiPrioInt = 0x0008
void HiPrioInt(void)
{
_asm
GOTO chk_isr
_endasm
}
#pragma code
char d0, d1, flag, L_byte, H_byte;
char temp;
unsigned int num;
char lookup[] = { 0x7e, 0x30, 0x6d, 0x79, 0x33,
0x5b, 0x5f, 0x70, 0x7f, 0x73
};
void main(void)
{
TRISAbits.TRISA0 = 0; // First Digit (Most Significant)
TRISAbits.TRISA1 = 0; // Last Digit (Least Significant)
TRISAbits.TRISA2 = 0;
TRISB = 0x0;
PORTB = 0x00;
PORTAbits.RA0 = 0;
PORTAbits.RA1 = 0;
TMR0H = 0xCF;
TMR0L = 0x2C;
T0CON = 0x08;
INTCONbits.TMR0IF = 0;
INTCONbits.TMR0IE = 1;
T0CONbits.TMR0ON = 1;
ADCON0 = 0b00001001;
ADCON1 = 0b01110011;
ADCON2 = 0b10101101;
PIR1bits.ADIF = 0;
PIE1bits.ADIE = 1;
INTCONbits.PEIE = 1;
INTCONbits.GIE = 1;
flag = 0;
temp = 0;
// d0 = 1;
// d1 = 6;
while(1)
{
ADCON0bits.GO = 1;
num = 0.4762 * temp + 6.8042;
d0 = num / 10;
d1 = num % 10;
}
}
void T0_ISR(void)
{
flag = ~flag;
if (flag == 0)
{
PORTAbits.RA1 = 0;
PORTB = lookup[d0];
PORTAbits.RA0 = 1;
}
else
{
PORTAbits.RA0 = 0;
PORTB = lookup[d1];
PORTAbits.RA1 = 1;
}
TMR0H = 0xCF; // reload values into the TMR0H and TMR0L
TMR0L = 0x2C;
INTCONbits.TMR0IF=0;
}
void AD_ISR(void)
{
L_byte = ADRESL;
H_byte = ADRESH;
L_byte >> 2;
L_byte &= 0b00111111;
H_byte <<= 6;
H_byte &= 0b11000000;
temp = L_byte | H_byte;
PIR1bits.ADIF = 0;
}
| |
| |
| | #2 |
|
Why are you using interrupts for the ADC?, it's totally pointless (and can only mess things up), just read the ADC in your main loop, and leave the interrupts for the multiplexing.
| |
| |
| | #3 | |
| Quote:
![]() Apart from the multiplexing, is it also safe to use the USART functions in interrupt as well? | ||
| |
| | #4 |
|
Is this your kelvin to celcius conversion??? ![]() Code: num = 0.4762 * temp + 6.8042; The LM335 should give about 2.98v out for 25'C. That's 25'C = (2.98v/5v * 1024) = 610 ADC counts So 0'C = (2.73/5v *1024) = 559 ADC counts So get your 0-1023 ADC reading and subtract 559 counts, then you have a temp figure that is referenced to 0'C. You also know that (610-559) ie 51 ADC counts = 25'C. So you can work out a calibration based on that. | |
| |
| | #5 | |
| Quote:
If you're going to use interrupts for a USART input, then you most probably need to use it to feed a buffer, and check the buffer contents in your main loop. But again, it depends what's going on. | ||
| |
| | #6 | ||
| Quote:
Quote:
Last edited by littletransistor; 5th October 2009 at 10:02 AM. | |||
| |
| | #7 |
|
LM335 should put out about 2.98v DC when it is at 25'C. Check how you have it wired, that you used the right value resistor with it. Also you might try putting a big cap (47uF) right at the PIC ADC input pin, between ADC pin and Gnd. | |
| |
| | #8 | |
| Quote:
But the strange thing is, when the ADJ pin is grounded, it gives at least 3.15V and when it's ungrounded it gives like 2.75V. By the way, this thing should show up at least 2.99V in the voltmeter with just basic wiring, isn't it? | ||
| |
| | #9 |
|
The datasheet is your friend. | |
| |
| | #10 |
| I already read the datasheet. Need to see whether must I calibrate the ADC in the microcontroller or not, by means of software. ![]() Also, I have already checked the allowable current range: between 450µA and 5mA. However, when I placed a resistor on the +ve terminal, say like two 1K resistor in series, it still gives me 3.26V instead of anything within 2.99V. Here's the small schematic (LM335Z): V- ------------------------| GND V+ ---(1)--/\/\/\/\-------------| +5V 2k resistor (2x 1k resistors in series) ADJ ---- (floating) (1) to voltmeter or AD pin in µC Last edited by littletransistor; 6th October 2009 at 05:25 PM. | |
| |
| | #11 |
|
Then you probably destroyed it when you DIDN'T have the resistor connected!! | |
| |
| | #12 |
| | |
| |
| | #13 |
|
Okay, got the sensors giving the real values finally. It's the voltmeter's faulty and give funny results. However, another problem. From the voltmeter, the sensor gives around 3.01V at room temperature, and should be translated to 155 or 9Bh in ADC operation (fyi, Vref is Vdd, step size: 5V/1024 = 4.883mV and finally, 3.01/4.883mV = 616.42 -> 616. Then divide it to four, I get 154 or 9Ah). I connected that sensor to the AD pin in the microcontroller. It just gives me 168 or A8h. The value just runs away. I've double checked the datasheet and tuned the A/D oscillator configurations and stuff, but it's giving me the same thing. Anything else I could have missed out? | |
| |
|
| Tags |
| adc, interrupt, level, problems |
| Thread Tools | |
| Display Modes | |
| |
Similar | ||||
| Title | Starter | Forum | Replies | Latest |
| ADC Interrupt vector. | alphadog | Micro Controllers | 7 | 4th April 2009 01:59 PM |
| Interrupt problems | littletransistor | Micro Controllers | 2 | 20th December 2008 03:39 AM |
| interrupt problems with button | matlark | Micro Controllers | 33 | 23rd May 2008 07:48 AM |
| Interrupt problems! | amindzo | Micro Controllers | 16 | 9th November 2006 09:11 PM |
| RB0 interrupt problems | amindzo | Micro Controllers | 4 | 26th August 2006 01:15 PM |