Chip seems to freeze up randomly
Posted: 01 Jun 2012 20:14
I have an interesting problem. I have a basic circuit involving a 16F1938 and an LCD display. The value of a potentiometer is read from port RB5 (AN13) and displayed on the LCD display along with a calculated voltage value. This is on a custom made board. The code is as follows:
The problem is that after a few seconds functioning normally, the display stops responding. If I hit the reset button the process starts over and after a few seconds it freezes up again. I've racked my brain till it hurts, but I just can't seem to make this go away. Anybody have some ideas?
Code: Select all
program PWM_Control_Board_16F1938
'Internal system clock 8MHz
'****************************************************************
dim Analog_Volts as float
Analog_Volts_Text as char[30]
Dig_Value as word
Dig_Value_Text as char[10]
dim Analog_In_1 as word 'Analog Input variables
' Define LCD module connections
dim LCD_RS as sbit at RA4_bit
LCD_EN as sbit at RA5_bit
LCD_D4 as sbit at RA0_bit
LCD_D5 as sbit at RA1_bit
LCD_D6 as sbit at RA2_bit
LCD_D7 as sbit at RA3_bit
LCD_RS_Direction as sbit at TRISA4_bit
LCD_EN_Direction as sbit at TRISA5_bit
LCD_D4_Direction as sbit at TRISA0_bit
LCD_D5_Direction as sbit at TRISA1_bit
LCD_D6_Direction as sbit at TRISA2_bit
LCD_D7_Direction as sbit at TRISA3_bit
'****************************************************************
sub procedure Initialize_LCD
Lcd_Init() ' Initialize Lcd
Lcd_Cmd(_LCD_CURSOR_OFF) ' Cursor off
Lcd_Cmd(_LCD_CLEAR) ' Clear display
end sub
'****************************************************************
main:
'Set up registers
TRISA = %10000000 'RA7 as Input, RB0-RB6 as outputs (page 132)
TRISB = %00011111 'RB0-RB4 as inputs, RB5-RB7 as outputs (page 137)
TRISC = %11111000 'RC0-RC2 as inputs, RC3-RC7 as outputs (page 140)
C1ON_bit = 0 'Disable C1ON Comparitor bit on CM1CON0 register (page 180)
C2ON_bit = 0 'Disable C2ON Comparitor bit on CM2CON0 register (page 180)
ADON_bit = 1 'Pg 161 Global Analog On
ANSELA = %00000000 'RA0-RA7 as Digital (Pg 133)
ANSELB = %00111111 'RB0-RB5 as Analog, RB6-RB7 as Digital (Pg 137)
OSCCON = %01110010 'Set internal oscillator frequency to 8 MHz (PG 82)
option_reg = %10000000 'disable global pullups (page 191)
Initialize_LCD ' Go to Initialize_LCD sub routine
'****************************************************************
while TRUE
'Read Data from AN13 on pin 26
Analog_In_1 = ADC_Read(13)
'Value Conversions
Dig_Value = Analog_In_1
If Dig_Value < 1 then
Dig_Value = 0 'Make sure value doesn't cause divide or multiply error
End If
Analog_Volts = Analog_In_1 * 0.004887585532
If Analog_Volts < 0.01 then
Analog_Volts = 0 'Make sure value doesn't cause divide or multiply error
End If
'Send values to LCD
WordToStr(Dig_Value, Dig_Value_Text) 'Digital value of ADC
Lcd_Out(1,1,"Dec Val:")
Lcd_Out_CP(Dig_Value_Text)
FloatToStr(Analog_Volts, Analog_Volts_Text) 'Volt equivalent
Lcd_Out(2,1,"Volt Val:")
Lcd_Out_CP(Analog_Volts_Text)
wend
end.