Log in

View Full Version : Missing something ... But What ????



Acetronics2
- 5th November 2012, 13:23
confused:

Hi,
I wrote a little Expanded scale Voltmeter program ...

But I'm loosing my last hair on a little problem ! :D

Here's the program:




'************************************************* ******************************
'Voltmètre12.bas
'
' R/C Expanded scale Voltmeter
' Pic 12F1840
' 12 Multiplexed Leds

' 2 Display modes :
' - Jumper ON is Actual voltage BAR Display
' - Jumper OFF is Minimun reached Voltage in BAR Mode, plus Actual Voltage in DOT Mode
' ( intended to show Voltage swing under load )
'************************************************* ******************************

' Defines
'************************************************* ******************************
DEFINE OSC 32
'DEFINE



' Config
'************************************************* ******************************

#CONFIG

__CONFIG _CONFIG1, _FOSC_INTOSC & _WDTE_ON & _PWRTE_ON & _MCLRE_OFF & _BOREN_ON
__CONFIG _CONFIG2, _PLLEN_ON & _BORV_LO & _LVP_OFF

#ENDCONFIG

OPTION_REG = %10000001
WPUA = %00001000
OSCCON = %11110000 ' !!! SCS 1:0 = 00 pour PLL ON !!!
INTCON = 0
PIE1 = 0
T1CON = 0
T1GCON = 0
CM1CON0 = 7
ADCON0 = %00000001
ADCON1 = %11100011
FVRCON = %11010011

LATA = 0
TRISA = 255

'************************************************* ******************************
' Aliases

Modeselect VAR PORTA.3

'************************************************* ******************************
' Variables

Nvalue VAR WORD
Ivalue VAR WORD

Mvalue VAR BYTE
Dvalue VAR BYTE
Value VAR BYTE
I VAR BYTE

mode VAR BYTE
modeI VAR BYTE

CLEAR

Mvalue = 12
'************************************************* ******************************
test:

FOR Value = 1 to 12

Mode = 0 : ModeI = 0
GOSUB Display
PAUSE 200

NEXT Value

LATA = 0
Dvalue = 0

BUTTON Modeselect,0,255,0,I,0,First ' Display actual Voltage
ModeI = 1 ' IF Jumper ON then Alternate mode

'************************************************* ******************************
First: '

ADCON0.1 = 1 'Launch First Conversion

Wait0:
FOR I = 1 to 16

IF ADCON0.1 = 0 THEN
Nvalue = ADRESH*256 + ADRESL ' read ADC result
ELSE
GOTO Wait0
ENDIF

ADCON0.1 = 1 ' relaunch conversion

Ivalue = (Ivalue*9 + Nvalue )/10 ' First Value for Ivalue
' = True value for Mvalue !
NEXT I

'************************************************* ******************************
Main:

WHILE 1

Wait1:
IF ADCON0.1 = 0 THEN
Nvalue = ADRESH*256 + ADRESL ' read ADC result
ELSE
GOTO Wait1
ENDIF

ADCON0.1 = 1 ' relaunch conversion

Ivalue = (Ivalue*9 + Nvalue )/10 ' Rolling average

GOSUB Convert

IF Dvalue < Mvalue THEN Mvalue = Dvalue ' Memorize Minimum


' Prepare value to be displayed

IF ModeI == 1 THEN
FOR I = 0 to Dvalue ' Bar Display Actual value
Value = I
GOSUB Display
NEXT I

ELSE
FOR I = 0 to Mvalue ' Bar Display Min Value
Value = I
GOSUB Display
NEXT I

Value = Dvalue ' Add Actual Value dot
GOSUB Display
ENDIF

WEND
END

'************************************************* ******************************
Display:

LATA = 0 'Prevent Ghosting

LOOKUP Value,[%11111111,%11111001,%11111001,%11101101,%11101101, %11011101,%11011101,_
%11101011,%11101011,%11011011,%11011011,%11001111, %11001111], TRISA

LATA.1 = !!(value == 2)|| (value == 4) || (value == 6)
LATA.2 = !!(value == 1)|| (value == 8) || (value ==10)
LATA.4 = !!(value == 3)|| (value == 7) || (value ==12)
LATA.5 = !!(value == 5)|| (value == 9) || (value ==11)


PAUSE 1
RETURN

'************************************************* ******************************
Convert: 'Convert ADC Counts to LED#

LOOKDOWN2 Ivalue, < [513,526,538,551,563,576,588,601,613,626,638,651,66 3], Dvalue

'************************************************* ******************************
'Test purpose values set
'LOOKDOWN2 Ivalue, < [551,563,576,588,601,613,626,638,651,663,676,688,70 0], Dvalue
'************************************************* ******************************
RETURN
END


END

scheme joined.

Everything could be Ok ...

BUT

in Default mode ( jumper OFF ) ... ONLY the Actual voltage is displayed by its dot : the minimum voltage doesn't appear ( the BAR )

THIS section fails:


ELSE
FOR I = 0 to Mvalue ' Bar Display Min Value
Value = I
GOSUB Display
NEXT I

Value = Dvalue ' Add Actual Value dot
GOSUB Display
ENDIF



MPLAB SIM doesn't show anything strange, as it shows the BAR is simulated correctly ( timing also OK ...)

So, ... I do not see what's wrong

@ Darrel ... 12F1840 not supported by ISIS ... so, no more simulation solution :rolleyes:


Thanks to the one who will see anything ...


Alain

Jerson
- 5th November 2012, 13:59
I suggest you try this modification. I think it will work.

Regards
Jerson



IF ADCON0.1 = 0 THEN
Nvalue = ADRESH*256 + ADRESL ' read ADC result
ELSE
GOTO Wait10
ENDIF

ADCON0.1 = 1 ' relaunch conversion

Ivalue = (Ivalue*9 + Nvalue )/10 ' Rolling average

GOSUB Convert

IF Dvalue < Mvalue THEN Mvalue = Dvalue ' Memorize Minimum

Wait10:

' Prepare value to be displayed

Acetronics2
- 5th November 2012, 14:16
Hi, Jerson

Already had implemented Waiting loop while testing ... but conversion occurs while doing something else ... and has much enough time to be completed ! :)
after that, multiplexing leds doesn't allow dead times ...;)

Alain

Jerson
- 5th November 2012, 16:47
Hi Alain

It is not about the conversion. Let me explain what I think could be the problem you see.

When you are in ModeI=1, you are putting out a bar graph + a dot position. That is what I understand.

What happens is when you are doing the bar graph, you are rapidly swiping the leds with the dot to enable your eyes to see a bar.

After that happens, you wish to have another dot that stays on to represent the current reading. This too happens immediately after the bar is displayed; but, it happens after you erase the bar with LATA=0. Now, only the dot persists till the next time you refresh the display for bar and dot. This time between 2 display refreshes is tied to the delay for ADC in Wait1(your code).

I choose to not wait for ADC, but process it when ready. Therefore the Wait10

Regards

Acetronics2
- 5th November 2012, 19:34
Jerson,

I just commented the rolling average line


Ivalue = (Ivalue*9 + Nvalue )/10 ' Rolling average


replaced it by


Ivalue = Nvalue


... and it began to work fine @ first run !!!

that is somewhat incredible ... ;)

6 month ago, I wrote a similar program displaying on a couple of 7 segments : everything was fine.
but I didn't use any average ...

I do not understand that because the value was initialized with the average calculated just before entering the display loop ...

Further tests look to show Nvalue = 0 @ first loop ( why ??? we are looping until a conversion result being available ... and it's ages the Pic ADC is stabilised ! ) : value is close to 9/10 Actual value, and then ... minimum value is generally under the minimum, and then not shown !

that's how it should work ... but not why !:o

I'll try to make further tests ...

Alain