Reducing scope of ADC byte in code 0-255 to 50-100

For questions and comments on programming in general. And for any items that don't fit into the forums below.

Moderators: Benj, Mods

Post Reply
Chet
Posts: 61
Joined: Thu Oct 13, 2005 5:05 am
Location: USA
Has thanked: 2 times

Reducing scope of ADC byte in code 0-255 to 50-100

Post by Chet »

Hello to my fellow Flowcode fanatics. I'm trying to figure out a way to reduce the "scope" or in other words locate the "sweet spot" of a ADC byte in Flowcode 5. I have a simple pulse generator that uses a timer interrupt to toggle a pin. At the moment the pulse on off values are 0-255 ms but I need to shrink that to between say 50-100ms while keeping the full rotation of the 10k pot usable. In other words I can't truncate the upper and lower voltages (that would be cheating) ;). My PCB for this project leaves no room for adding trim resistors to the ADC so I need to do this in code. I can post my code if needed but hope this simple issue won't require it. I appreciate any feedback, thank you.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Reducing scope of ADC byte in code 0-255 to 50-100

Post by Benj »

Hello Chet,

What about sampling the ADC to collect the byte reading 0 - 255. Then divide this reading by 5 to give you 0 - 51. Finally add 50 to the reading and this will give you 50 - 101. Is this any good?

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times

Re: Reducing scope of ADC byte in code 0-255 to 50-100

Post by Enamul »

Hi,
This can be work around this way..
For example, ADC =0 to 255.
In the calculation box you can put the following immediately after ADC read..

Code: Select all

ADC = ((50 * ADC) / 255) + 50
so, for ADC = 0, new ADC will be ((50*0)/255)+50 = 50
and for ADC = 50, new ADC will be ((50*50)/255)+50 = 9+50 = 59
and for ADC=255, new ADC will be ((255*50)/255)+50 = 50+50 =100
Enamul
University of Nottingham
enamul4mm@gmail.com

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Reducing scope of ADC byte in code 0-255 to 50-100

Post by Benj »

Hi Enamul,

To get your calculations to work you may need to assign ADC as an INT variable rather then a Byte. Not 100% on this but 255 * 50 worries me using byte variables :D

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times

Re: Reducing scope of ADC byte in code 0-255 to 50-100

Post by Enamul »

Hi Ben,
Like you I was thinking the same but I tested that in Real PIC before posting which shows fine. I have just now tested in ECIO-40 works fine even with ADC is byte. :!:
Edit: I have looked through the assembly generated by Boostc. It's due to the intermediate registers used in multiplication and division are 2 locations not Byte.. :)

Code: Select all

__div_16_1_00003_arg_a           EQU	0x0000002E ; bytes:2
__div_16_1_00003_arg_b           EQU	0x00000030 ; bytes:2

Code: Select all

   MOVWF gbl_FCV_ADC
	MOVF gbl_FCV_ADC, W
	MULLW 0x32
	MOVF PRODL, W
	MOVWF __div_16_1_00003_arg_a
	MOVF PRODH, W
	MOVWF __div_16_1_00003_arg_a+D'1'
	MOVLW 0xFF
	MOVWF __div_16_1_00003_arg_b
	CLRF __div_16_1_00003_arg_b+D'1'
	CALL __div_16_1_00003
	MOVF CompTempVarRet178, W
	MOVWF gbl_FCV_ADC
	MOVF CompTempVarRet178+D'1', W
	MOVWF CompTempVar2252
	MOVLW 0x32
	ADDWF gbl_FCV_ADC, F
Attachments
ADC.fcf
(9.5 KiB) Downloaded 278 times
Enamul
University of Nottingham
enamul4mm@gmail.com

Chet
Posts: 61
Joined: Thu Oct 13, 2005 5:05 am
Location: USA
Has thanked: 2 times

Re: Reducing scope of ADC byte in code 0-255 to 50-100

Post by Chet »

Thank you all, I'm using a 12F683 so I think this will work even if I have to use int, I'll tweak my code and let you know how it goes when I get a spare moment. Cheers

Chet
Posts: 61
Joined: Thu Oct 13, 2005 5:05 am
Location: USA
Has thanked: 2 times

Re: Reducing scope of ADC byte in code 0-255 to 50-100

Post by Chet »

Enamul, as far as I can tell without running this in hardware your math works perfect. Thank you again, it took less than 5 minutes to edit my flow cart. :D

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times

Re: Reducing scope of ADC byte in code 0-255 to 50-100

Post by Enamul »

Glad to know that you can use it :)
Enamul
University of Nottingham
enamul4mm@gmail.com

Post Reply