Hi just started getting into the whole Microcontroller world and have a problem that I can't wrap my head around. I've made some code up that will use the LM34 to measure the temperature in C and F and display them on the LCD. But my problem now is that I want to be able use SW1 on the 16F877 to switch from a Celsius reading to a Farenheit reading but still being able to get current temperature readings. Below I have the code that I'm using at the moment but figuring out how to use the SW1 to switch back and forth is bothering me to say the least. Any help or guide me in the right direction would be appreciated.
The board I'm using is a LabX-1 16F877.
DEFINE LCD_DREG PORTD 'data register
DEFINE LCD_RSREG PORTE 'register select
DEFINE LCD_RSBIT 0 'pin number
DEFINE LCD_EREG PORTE 'enable register
DEFINE LCD_EBIT 1 'enable bit
DEFINE LCD_RWREG PORTE 'read/write register
DEFINE LCD_RWBIT 2 'read/write bit
DEFINE LCD_BITS 8 'width of data
DEFINE LCD_LINES 2 'lines in display
DEFINE LCD_COMMANDUS 2000 'delay in micro seconds
DEFINE LCD_DATAUS 20 'delay in micro seconds
'Set the port directions. We are setting PORTA.0 to an input, all rest outputs
'all of PORTD is set to outputs to run the LCD
'and all of PORTE as outputs even though PORTE has only 3 lines.
TRISA = %11111111 'set PORTA lines
TRISD = %00000000 'set all PORTD lines to output
TRISE = %00000000 'set all PORTE lines to output
'Set the Analog to Digital control register
ADCON1=%10000010 'see discussion above
'Define the variables used
ADVAL VAR BYTE 'Create ADVAL to store result
TEMP VAR BYTE 'temperature Farenheit variable
TEMPC VAR BYTE 'temperature Celsius variable
DEG CON 223 'write an degree mark on lcd
'define the adcin parameters
DEFINE ADC_BITS 10 'Set number of bits in result
DEFINE ADC_CLOCK 3 'Set clock source (3=rc)
DEFINE ADC_SAMPLEUS 50 'Set sampling time in uS
LCDOUT $FE, 1 'clear screen
LOOP: 'The main loop of the program
PAUSE 250 'pause 0.5 seconds
ADCIN 4, ADVAL 'Read channel 0 to adval
ADVAL =(ADVAL*/500)>>2
TEMP=ADVAL
TEMPC=((TEMP-32) *100) /180
LCDOUT $FE, 2 'go to home position
LCDOUT "Temp=", DEC3 TEMP,DEG, "F" 'print to line 1 of 2 line display
LCDOUT $FE,$C0, "Temp=", DEC3 TEMPC,DEG, "C"
GOTO LOOP
END
Bookmarks