Waitms 2000
Adcin 0,x
x = x * 150
x = x / 1023
LCDOUT "Temp: ",#x
#include <htc.h>
#include <math.h>
#include "lcd.h"
//Chip Settings
__CONFIG(1,0x0200);
__CONFIG(2,0X1E1F);
__CONFIG(3,0X8100);
__CONFIG(4,0X00C1);
__CONFIG(5,0XC00F);
//Simple Delay Routine
void Wait(unsigned int delay)
{
for(;delay;delay--)
__delay_us(100);
}
//Function to Initialise the ADC Module
void ADCInit()
{
//We use default value for +/- Vref
//VCFG0=0,VCFG1=0
//That means +Vref = Vdd (5v) and -Vref=GEN
//Port Configuration
//We also use default value here too
//All ANx channels are Analog
/*
ADCON2
*ADC Result Right Justified.
*Acquisition Time = 2TAD
*Conversion Clock = 32 Tosc
*/
ADCON2=0b10001010;
}
//Function to Read given ADC channel (0-13)
unsigned int ADCRead(unsigned char ch)
{
if(ch>13) return 0; //Invalid Channel
ADCON0=0x00;
ADCON0=(ch<<2); //Select ADC Channel
ADON=1; //switch on the adc module
GODONE=1;//Start conversion
while(GODONE); //wait for the conversion to finish
ADON=0; //switch off adc
return ADRES;
}
void main()
{
//Let the LCD Module start up
Wait(100);
//Initialize the LCD Module
LCDInit(LS_BLINK);
//Initialize the ADC Module
ADCInit();
//Clear the Module
LCDClear();
//Write a string at current cursor pos
LCDWriteString("LM35 Test");
LCDWriteStringXY(4,1,"Degree Celcius");
while(1)
{
unsigned int val; //ADC Value
unsigned int t; //Temperature
val=ADCRead(0); //Read Channel 0
t=round(val*0.48876);//Convert to Degree Celcius
LCDWriteIntXY(0,1,t,3);//Prit IT!
Wait(1000);
}
}
It uses a fractional multiplier, the value 0.48876, which Oshonsoft Basic will not accept because it only understands whole (integer) numbers. The math trick I used was to scale the numbers up so the fraction part gave a smaller error.t=round(val*0.48876);//Convert to Degree Celcius
There is a common and inexpensive driver that is ideally suited to this job, the ULN2803 but it is hugely over rated for driving display segments. I suggest just rewiring your existing circuit like this:
NPN transistors
Emitters directly to 0V
Bases driven from PIC through 2200 (2.2K) resistors
Collects to segments through 270 Ohm resistors.
Assuming the transistors have a gain of about 100, that should fully saturate them and make the segments pass about 15mA each.
If you can, run one regulator for the display (although within reason it doesn't have to regulated at all) and a different regulator for the PIC. The reason for that is the display current can vary from 60mA (15mA * 2 segments * 2 digits) if '11' is displayed right up to 210mA if '88' is displayed and that mighgt cause a slight variation in the regulator output. Inside the PIC, if the ADC uses VDD as it's reference or if the reference is divided down from VDD, the reading will change if VDD changes.
I did a design a few years ago that used a PIC with VDD as the reference and couldn't work out why some units worked and some others didn't, my investigation found the 7805 voltage regulators were the problem. They were all well within the manufacturers specification but the slight differences in output voltage made the ADC produce different results. Based on the problems that caused, I would urge you to make sure your VDD at the PIC is as stable as possible!
Brian.
Define CLOCK_FREQUENCY = 20
Define ADC_SAMPLEUS = 2
Define ADC_CLOCK = 0
Dim x As Long
main:
WaitMs 500
Adcin 0, x
WaitMs 500
x = x * 500
x = x / 1023
Gosub checktemp
Goto main
End
checktemp:
If x = 4 Then
High PORTB.0
High PORTB.1
High PORTB.3
High PORTB.5
High PORTB.6
High PORTB.7
Low PORTB.2
Low PORTB.4
High PORTC.2
High PORTC.3
High PORTC.5
High PORTC.7
Low PORTC.0
Low PORTC.1
Low PORTC.4
Low PORTC.6
Endif
If x = 5 Then
High PORTB.0
High PORTB.1
High PORTB.3
High PORTB.5
High PORTB.6
High PORTB.7
Low PORTB.2
Low PORTB.4
High PORTC.2
High PORTC.3
High PORTC.6
High PORTC.7
High PORTC.1
Low PORTC.0
Low PORTC.1
Low PORTC.5
Low PORTC.6
Endif
If x = 6 Then
High PORTB.0
High PORTB.1
High PORTB.3
High PORTB.5
High PORTB.6
High PORTB.7
Low PORTB.2
Low PORTB.4
High PORTC.6
High PORTC.7
High PORTC.0
High PORTC.1
High PORTC.2
High PORTC.3
Low PORTC.4
Low PORTC.5
Endif
If x = 7 Then
High PORTB.0
High PORTB.1
High PORTB.3
High PORTB.5
High PORTB.6
High PORTB.7
Low PORTB.2
Low PORTB.4
High PORTC.6
High PORTC.5
High PORTC.2
High PORTC.3
Low PORTC.0
Low PORTC.1
Low PORTC.4
Low PORTC.7
Endif
If x = 8 Then
High PORTB.0
High PORTB.1
High PORTB.3
High PORTB.5
High PORTB.6
High PORTB.7
Low PORTB.2
Low PORTB.4
High PORTC.6
High PORTC.7
High PORTC.0
High PORTC.1
High PORTC.2
High PORTC.3
Low PORTC.4
High PORTC.5
Endif
If x = 9 Then
High PORTB.0
High PORTB.1
High PORTB.3
High PORTB.5
High PORTB.6
High PORTB.7
Low PORTB.2
Low PORTB.4
High PORTC.6
High PORTC.7
High PORTC.5
High PORTC.1
High PORTC.2
High PORTC.3
Low PORTC.4
Low PORTC.0
Endif
If x = 10 Then
High PORTB.0
Low PORTB.1
Low PORTB.3
High PORTB.5
Low PORTB.6
Low PORTB.7
Low PORTB.2
Low PORTB.4
High PORTC.6
High PORTC.7
High PORTC.0
High PORTC.1
Low PORTC.2
High PORTC.3
Low PORTC.4
High PORTC.5
Endif
If x = 11 Then
High PORTB.0
Low PORTB.1
Low PORTB.3
High PORTB.5
Low PORTB.6
Low PORTB.7
Low PORTB.2
Low PORTB.4
Low PORTC.6
Low PORTC.7
High PORTC.0
Low PORTC.1
Low PORTC.2
Low PORTC.3
Low PORTC.4
High PORTC.5
Endif
If x = 12 Then
High PORTB.0
Low PORTB.1
Low PORTB.3
High PORTB.5
Low PORTB.6
Low PORTB.7
Low PORTB.2
Low PORTB.4
High PORTC.6
High PORTC.5
High PORTC.2
High PORTC.0
Low PORTC.4
High PORTC.1
Low PORTC.3
Low PORTC.5
Endif
If x = 13 Then
High PORTB.0
Low PORTB.1
Low PORTB.3
High PORTB.5
Low PORTB.6
Low PORTB.7
Low PORTB.2
Low PORTB.4
High PORTC.6
High PORTC.2
Low PORTC.0
High PORTC.1
Low PORTC.7
High PORTC.3
Low PORTC.4
High PORTC.5
Endif
If x = 14 Then
High PORTB.0
Low PORTB.1
Low PORTB.3
High PORTB.5
Low PORTB.6
Low PORTB.7
Low PORTB.2
Low PORTB.4
High PORTC.2
High PORTC.3
High PORTC.5
High PORTC.7
Low PORTC.0
Low PORTC.1
Low PORTC.4
Low PORTC.6
Endif
If x = 15 Then
High PORTB.0
Low PORTB.1
Low PORTB.3
High PORTB.5
Low PORTB.6
Low PORTB.7
Low PORTB.2
Low PORTB.4
High PORTC.2
High PORTC.3
High PORTC.6
High PORTC.7
High PORTC.1
Low PORTC.0
Low PORTC.1
Low PORTC.5
Low PORTC.6
Endif
If x = 16 Then
High PORTB.0
Low PORTB.1
Low PORTB.3
High PORTB.5
Low PORTB.6
Low PORTB.7
Low PORTB.2
Low PORTB.4
High PORTC.6
High PORTC.7
High PORTC.0
High PORTC.1
High PORTC.2
High PORTC.3
Low PORTC.4
Low PORTC.5
Endif
If x = 17 Then
High PORTB.0
Low PORTB.1
Low PORTB.3
High PORTB.5
Low PORTB.6
Low PORTB.7
Low PORTB.2
Low PORTB.4
High PORTC.6
High PORTC.5
High PORTC.2
High PORTC.3
Low PORTC.0
Low PORTC.1
Low PORTC.4
Low PORTC.7
Endif
If x = 18 Then
High PORTB.0
Low PORTB.1
Low PORTB.3
High PORTB.5
Low PORTB.6
Low PORTB.7
Low PORTB.2
Low PORTB.4
High PORTC.6
High PORTC.7
High PORTC.0
High PORTC.1
High PORTC.2
High PORTC.3
Low PORTC.4
High PORTC.5
Endif
If x = 19 Then
High PORTB.0
Low PORTB.1
Low PORTB.3
High PORTB.5
Low PORTB.6
Low PORTB.7
Low PORTB.2
Low PORTB.4
High PORTC.6
High PORTC.7
High PORTC.5
High PORTC.1
High PORTC.2
High PORTC.3
Low PORTC.4
Low PORTC.0
Endif
If x = 20 Then
High PORTC.6
High PORTC.5
High PORTC.2
High PORTC.0
Low PORTC.4
High PORTC.1
Low PORTC.3
Low PORTC.5
High PORTB.0
High PORTB.1
High PORTB.3
High PORTB.5
High PORTB.6
High PORTB.7
Low PORTB.2
Low PORTB.4
Endif
Return
Define CLOCK_FREQUENCY = 20
Define ADC_SAMPLEUS = 2
Define ADC_CLOCK = 0
Dim x As Long
Dim lsd As Byte
Dim msd As Byte
main:
TRISB = 0
TRISC = 0
loop:
WaitMs 500
Adcin 0, x
WaitMs 500
x = x * 500
x = x / 1023
'put some limits checking here!
msd = x / 10
lsd = x Mod 10
Gosub checktemp
Goto loop
End
checktemp:
LATB = LookUp(0xeb, 0x21, 0xc7, 0x67, 0x2d, 0x6e, 0xee, 0x23, 0xef, 0x6f), msd
LATC = LookUp(0xeb, 0x28, 0x67, 0x6e, 0xac, 0xce, 0xcf, 0x68, 0xef, 0xee), lsd
Return
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?