Chip seems to freeze up randomly

General discussion on mikroBasic PRO for PIC.
Post Reply
Author
Message
David Hansen
Posts: 12
Joined: 03 Aug 2011 19:38

Chip seems to freeze up randomly

#1 Post by David Hansen » 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:

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.
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?

janni
Posts: 5373
Joined: 18 Feb 2006 13:17
Contact:

Re: Chip seems to freeze up randomly

#2 Post by janni » 02 Jun 2012 14:49

It doesn't seem to be a software problem. Your code doesn't have any apparent mistakes and the main loop runs correctly many times before anything happens.
I'd look into the hardware - ground/supply connections, decoupling capacitors, free-floating inputs, and the like.

David Hansen
Posts: 12
Joined: 03 Aug 2011 19:38

Re: Chip seems to freeze up randomly

#3 Post by David Hansen » 11 Jun 2012 22:04

janni wrote:It doesn't seem to be a software problem. Your code doesn't have any apparent mistakes and the main loop runs correctly many times before anything happens.
I'd look into the hardware - ground/supply connections, decoupling capacitors, free-floating inputs, and the like.
All of the parts and connections are surrounded by a ground plane on the bottom of the board and I was using shielded wire to the pot and a 10uF electrolytic cap and a 1000pf ceramic on the AN13 connection. There is also a second PIC controller (a 16F1847) on this board with 4 analog inputs activated (each with a resistor network tied to it, but with no caps) and I have no problems with it.

I did find a couple of problems. In the register setup section I had RB5 (Analog channel 13) set as an output (TRISB = %00011111). This was causing all kinds of value problems on the display. I also had ALL of the analog channels turned on in register B (ANSELB = %00111111) and had nothing connected to them. I've since made the corrections (TRISB = %00100000 and ANSELB = %00100000) so that only one analog input port is active. The readings are now what they're supposed to be on the display, but the chip is still locking up. I have the chip set up so that FOSC is routed to pin 10 (RA6) and is monitored by a scope. I still get a clock signal on RA6 even when everything else seems to lock up.

Any further advice?

BarryP
Posts: 517
Joined: 02 Apr 2007 03:57
Location: New Zealand

Re: Chip seems to freeze up randomly

#4 Post by BarryP » 12 Jun 2012 08:28

Hi,
Have you considered tracking down where it is locking up by setting a port value for each location in the code.

Code: Select all

while TRUE

   'Read Data from AN13 on pin 26
   portb = 0
   Analog_In_1 = ADC_Read(13)
   portb = 1
   '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
   portb = 2
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
    portb = 8
    Lcd_Out(1,1,"Dec Val:")
    portb = 16
    Lcd_Out_CP(Dig_Value_Text)
    portb = 32
    FloatToStr(Analog_Volts, Analog_Volts_Text)     'Volt equivalent
    portb = 64
    Lcd_Out(2,1,"Volt Val:")
    portb = 128
    Lcd_Out_CP(Analog_Volts_Text)
    portb = 255

wend

David Hansen
Posts: 12
Joined: 03 Aug 2011 19:38

Re: Chip seems to freeze up randomly

#5 Post by David Hansen » 12 Jun 2012 16:01

BarryP wrote:Hi,
Have you considered tracking down where it is locking up by setting a port value for each location in the code.

Code: Select all

while TRUE

   'Read Data from AN13 on pin 26
   portb = 0
   Analog_In_1 = ADC_Read(13)
   portb = 1
   '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
   portb = 2
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
    portb = 8
    Lcd_Out(1,1,"Dec Val:")
    portb = 16
    Lcd_Out_CP(Dig_Value_Text)
    portb = 32
    FloatToStr(Analog_Volts, Analog_Volts_Text)     'Volt equivalent
    portb = 64
    Lcd_Out(2,1,"Volt Val:")
    portb = 128
    Lcd_Out_CP(Analog_Volts_Text)
    portb = 255

wend
Hmmmm.... Good tip. Will try it and see what happens.

Thanks

Post Reply

Return to “mikroBasic PRO for PIC General”