Would like to get some ideas on a strange problem I'm having.
I'm using mikroBasic Pro for PIC v.6.0.0 with easyPIC v7 and PIC18F45K22
I am testing out a portion of a larger program I'd like to get right. I want to display the temperature from the LM35 temperature sensor on the seven segment display. I have the LM35 connected to RE1. My Vcc is set to 5V
Here's my test code that does display the correct temperature...to a point.
Code: Select all
' Name: LM35_to_7_SD_V01
' Voltage: Vcc = 5V (J5 to 5V)
' PIC: PIC18F45K22
' OSC: - 8MHz HS Crystal
' - 4xPLL enabled
' S/W: mikroBasic PRO for PIC V.6.0.0
' Dev-Board: easyPIC V7
' Libraries: - ADC
' Sensor: LM35 (J25 to RE1)
' Jumpers: - J25 to RE1 - LM35 to RE1 (AN5)
' - J5 to 5V - VCC = 5Volts
' Switches: - SW8.1, SW8.2, SW8.3, SW8.4 to "ON"
' Notes: - first write, to test out getting temperature from LM35 to 7 segment display
' - program is a mash of examples: LM35 Sensor (Temperature measurement) &
' Seven Segment Display (The 'Hello World' example for the Seven Segment Display)
' Problems: - Two numbers are shown. If temperature is around 23.92 Degrees Celsuis, 23.92 is shown for a period of time
' and then another unrelated number is shown eg. 17.57
' - These two numbers flicker. At the Delay_ms(2000) the flicker is slow, at Delay_ms(200) the flicker between
' them is fast. It is easier to see the two numbers as more time is added to the delay.
'
program LM35_to_7_SD_V01
'-------------------------------------------------------------------------------Declarations section
const VREF as byte = 5.00
dim temp_res as word
dim shifter, portd_index as byte
digit, number as word
portd_array as byte[4]
sub function mask(dim num as word) as word
select case num
case 0
result= $3F
case 1
result= $06
case 2
result= $5B
case 3
result= $4F
case 4
result= $66
case 5
result= $6D
case 6
result= $7D
case 7
result= $07
case 8
result= $7F
case 9
result= $6F
end select
end sub
sub procedure interrupt
LATA = 0 ' turn off all 7seg displays
LATD = portd_array[portd_index] ' bring appropriate value to PORTD
LATA = shifter ' turn on appropriate 7seg. display
' move shifter to next digit
shifter= shifter << 1
if (shifter > 8) then
shifter = 1
end if
' increment portd_index
Inc(portd_index)
if (portd_index > 3) then
portd_index = 0 ' turn on 1st, turn off 2nd 7seg.
end if
TMR0L = 0 ' reset TIMER0 value
TMR0IF_bit = 0 ' Clear TMR0IF
end sub
main:
'-------------------------------------------------------------------------------Main program
ANSELA = 0 ' Configure PORTA pins as digital
ANSELD = 0 ' Configure PORTD pins as digital
ANSELE = 0x02 ' Configure RE1 pin as analog
TRISE1_bit = 1 ' Configure RE1 pin as input
TRISA = 0 ' Configure PORTA as output
LATA = 0 ' Clear PORTA
TRISD = 0 ' Configure PORTD as output
LATD = 0 ' Clear PORTD
ADC_Init() ' Initialize ADC
T0CON = 0xC4 ' Set TMR0 in 8bit mode, assign prescaler to TMR0
TMR0L = 0 ' clear TMROL
digit = 0
portd_index = 0
shifter = 1
GIE_bit = 1
TMR0IE_bit = 1
while TRUE
temp_res = ADC_Get_Sample(6) ' Get 10-bit results of AD conversion
number = (temp_res * VREF * 100)/10.240 ' Convert to Celsuis
digit = number / 1000 ' extract thousands digit
portd_array[3] = mask(digit) ' and store it to PORTD array
digit = (number / 100) mod 10 ' extract hundreds digit
portd_array[2] = mask(digit) + 0x80 ' and store it to PORTD array, then add a decimal point
digit = (number / 10) mod 10 ' extract tens digit
portd_array[1] = mask(digit) ' and store it to PORTD array
digit = number mod 10 ' extract ones digit
portd_array[0] = mask(digit) ' and store it to PORTD array
delay_ms(2000) ' wait a time (could be 200ms, 500ms, 1sec or 2sec)
Wend
end.
Oh, and when you read the word 'flicker' in the problem section, that just relates to the display of one number and then the other. Nothing is wrong with the seven segment display itself, ie. it does not 'flicker' as an issue with the display rate or anything.
My question is why am I getting this other number?
What can I do to stop it and only get the correct temperature reading?
Any help or ideas would be cool....
Thanks
Chris