Title: Decimal point in 8-bit Uc. Post by: naseerak on January 23, 2014, 01:56:51 13:56 Hi!
I am trying to build an voltmeter using PIC16f72 and 3-digit 7-segment display, but don,t know how to implement the decimal point my code snippets are: adreader: 'read a/d VIN = ADIn 0 'read AD cha 0 value, place in Vin VIN = VIN/4 VIN = VIN*511 VIN = VIN/10 'sort a/d value in 100, 10, 1 's adrd: ONE = Dig VIN , 0 TEN = Dig VIN , 1 HUN = Dig VIN , 2 Return My highest voltage is 50V and voltage divider consists of 10k in the upper arm and 1k in the lower arm. Title: Re: Decimal point in 8-bit Uc. Post by: TucoRamirez on January 23, 2014, 02:54:30 14:54 no idea, where do you want to display or use the point in?
Title: Re: Decimal point in 8-bit Uc. Post by: pablo2048 on January 23, 2014, 03:24:40 15:24 Ok, according to datasheet this type of MCU has 8 bit A/D converter. In line VIN = VIN/4 You loose 2 bits of resolution. Better is 1. multiply and then divide... What is Your reference voltage? If 5V, then maximum voltage on the A/D input is approximately 4.54V@50V input.
Title: Re: Decimal point in 8-bit Uc. Post by: sam_des on January 23, 2014, 06:16:11 18:16 @naseerak,
Now with precise 1/10th value at adc input, it is very easy to get 1 digit beyond decimal point, with only integer math. Simply scale the calculations by 10. For 50V -> ADC INPUT = 5V -> ADC Conversion = 1023, Now if 1023 represents 500, any adc result "x" will represent, (x*500)>>10. e.g. For ADC result = 150, V = (150*500)>>10 = 73.24 precise or 73 for integer math giving 07.3 as 3 digit display. Note that you will require 16-BIT x 16-BIT=>32-BIT math multiply. You can work on it further to optimize in terms of code as well as hardware. Hope this helps, sam_des Title: Re: Decimal point in 8-bit Uc. Post by: naseerak on January 25, 2014, 08:27:24 20:27 @Sam_des
You are right but my problem is 8-bit of ADC i 16f72. I have done my display code but stuck ni the calculation and display of decimal point, the 50v limit may increased in the upward direction as I am measuring two PVs in series, The Variable VIN has a word size thats why I have implied divided by 4. Title: Re: Decimal point in 8-bit Uc. Post by: gan_canny on January 26, 2014, 01:14:31 13:14 In mathematics numbers are notation. You have two types of notation base 10 decimal and yes your PIC is binary and uses base 2 notation. The two notations are compatible across the integers (whole numbers). Now we know the pic will use integers to maintain notational accuracy we need to think about the number of base 10 decimal places we need in our result. Suppose it is two base 10 decimal places. The pic is binary so we are only compatible in our notation if we scale everything up by 100 ( base 10).
An aside Now binary also has a binary point just as decimal has a decimal point you will encounter the binary point if you were to use float variables with your pic. Notation is not perfect ..Ex 1/3 can't be expressed fully in the decimal ( base 10) system and 0.1 decimal can't be fully expressed in the binary system. Coders often call this inability to fully express things as rounding errors. It is common usage but not perfectly pure mathematics. There is valid math rounding where the notation of numbers is shortened by looking at the least significant digit and rounding up or down. Again scale all your calculations up and use integers within the pic code. On output convert back to the decimal system and place the decimal point taking into account your scaling factor. Now when things need displaying another notation comes in namely ASCII. Your LCD uses this notation for activating pixels. The character representing '0' ( zero) in this notation is 48 (base10). The conversion from 1 base 2 ( binary) to ASCII character notation would involve adding 110000 ( 48 base 10) to get get the character '1'. Oh, coders can assume the pic calculates like a handheld calculator. A hand held calculator uses BCD ( binary coded decimal) for its calculations. It is slow and cumbersome but it never leaves the decimal notation system so it is notionally accurate in the decimal system. The hand calculator is fine for humans but too slow for a pic. Coders can think a 64 bit PC avoids the notational issue...it does in a way since it is pushed back to very small quote "coder rounding error" in about the 20th least significant digit place. Title: Re: Decimal point in 8-bit Uc. Post by: naseerak on January 27, 2014, 06:53:51 18:53 Comments from you people were helpful I have solved my problem
Title: Re: Decimal point in 8-bit Uc. Post by: thunderer on February 16, 2014, 02:09:47 02:09 Code for printing a voltage on a LCD (in a format like 12,345V).
Code: adc_rd0 = ADC_read(0) ' Read ADC channel 0 I know this answer comes late, but better have a working code posted. Title: Re: Decimal point in 8-bit Uc. Post by: pablo2048 on February 16, 2014, 08:40:56 08:40 I think that if you have 10bit A/D convertor, the accuracy is 60/1024 = 0.05859 V/bit. That means the maximum valuable precision is 1 decimal place... Displaying three decimal places is wrong.
Title: Re: Decimal point in 8-bit Uc. Post by: krishna.velu on July 30, 2014, 11:43:50 11:43 The ADC you have in PIC16f72 is 8Bit.
2^8 = 256. You will get 255 counts of ADC if your input is full (Assuming 50V = Full Analog Input). You need to increase your ADC resolution. An ADC resolution of 10 can get you a good result. |