wanted to map adc into 0-255 range.But code did not working - Page 1
Forum section: electronics / beginners

author: khatus
author_rank: Regular Contributor
timestamp: April 13, 2020, 02:12:34 pm
content: Hello guys i wanted to map adc value (0-1023) to 0-255 range. But the code did not working on proteus 8.6.I have created a map function for this.Here is my code.But can not guess where is my fault?Code:sbit LCD_RS at RB4_bit;sbit LCD_EN at RB5_bit;sbit LCD_D4 at RB0_bit;sbit LCD_D5 at RB1_bit;sbit LCD_D6 at RB2_bit;sbit LCD_D7 at RB3_bit;sbit LCD_RS_Direction at TRISB4_bit;sbit LCD_EN_Direction at TRISB5_bit;sbit LCD_D4_Direction at TRISB0_bit;sbit LCD_D5_Direction at TRISB1_bit;sbit LCD_D6_Direction at TRISB2_bit;sbit LCD_D7_Direction at TRISB3_bit;float map(unsigned int value, unsigned int min, unsigned int max)//function definition{return(((max - min)/1023)*value);}char Txt;void main(){unsigned int adc_value;float d;PORTA = 0b00000000;TRISA = 0xFF;ADC_Init();Lcd_Init(); // Initialize LCDLcd_Cmd(_LCD_CLEAR); // Clear displayLcd_Cmd(_LCD_CURSOR_OFF); // Cursor offwhile(1){adc_value = ADC_read(0);d = map(adc_value,0,255);FloatToStr(d,Txt); // Convert voltage to stringLcd_Out(2,1,"d =");Lcd_Out(2,5,Txt);Delay_ms(300);}}

author: chayanforyou
author_rank: Newbie
timestamp: April 13, 2020, 02:23:59 pm
content: You can do simply by using this map function.QuoteCode:int map(int val, int in_min, int in_max, int out_min, int out_max) {return (val - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;}

author: Andy Watson
author_rank: Super Contributor
timestamp: April 13, 2020, 02:32:51 pm
content: Code:float map(unsigned int value, unsigned int min, unsigned int max)//function definition{return(((max - min)/1023)*value);}The compiler will evaluate that as an integer calculation. You need to force the compiler to evaluate it as a float - try casting one of the input variables, eg.return(( (float) (max - min)/1023)*value);

author: capt bullshot
author_rank: Super Contributor
timestamp: April 13, 2020, 02:43:51 pm
content: That requires a simple shift operation.0...1023 has ten significant bits, while 0...255 has 8. So just shift the ADC value right by two and you're done.d = adc_value >> 2;You should use rather a function IntToStr() than FloatToStr() since d is an integer.

author: khatus
author_rank: Regular Contributor
timestamp: April 13, 2020, 02:49:41 pm
content: Code:sbit LCD_RS at RB4_bit;sbit LCD_EN at RB5_bit;sbit LCD_D4 at RB0_bit;sbit LCD_D5 at RB1_bit;sbit LCD_D6 at RB2_bit;sbit LCD_D7 at RB3_bit;sbit LCD_RS_Direction at TRISB4_bit;sbit LCD_EN_Direction at TRISB5_bit;sbit LCD_D4_Direction at TRISB0_bit;sbit LCD_D5_Direction at TRISB1_bit;sbit LCD_D6_Direction at TRISB2_bit;sbit LCD_D7_Direction at TRISB3_bit;char Txt;void main(){unsigned int adc_value;unsigned int d;PORTA = 0b00000000;TRISA = 0xFF;ADC_Init();Lcd_Init(); // Initialize LCDLcd_Cmd(_LCD_CLEAR); // Clear displayLcd_Cmd(_LCD_CURSOR_OFF); // Cursor offwhile(1){adc_value = ADC_read(0);adc_value = adc_value>>2;//d = map(adc_value, 0, 1023, 0, 255);IntToStr(adc_value,Txt); // Convert voltage to stringLcd_Out(2,1,"d =");Lcd_Out(2,5,Txt);Delay_ms(300);}}yes!! this works.But i wanted to do it using Function call.

author: khatus
author_rank: Regular Contributor
timestamp: April 13, 2020, 03:24:51 pm
content: Thanks it worksCode:sbit LCD_RS at RB4_bit;sbit LCD_EN at RB5_bit;sbit LCD_D4 at RB0_bit;sbit LCD_D5 at RB1_bit;sbit LCD_D6 at RB2_bit;sbit LCD_D7 at RB3_bit;sbit LCD_RS_Direction at TRISB4_bit;sbit LCD_EN_Direction at TRISB5_bit;sbit LCD_D4_Direction at TRISB0_bit;sbit LCD_D5_Direction at TRISB1_bit;sbit LCD_D6_Direction at TRISB2_bit;sbit LCD_D7_Direction at TRISB3_bit;float map(int val, int in_min, int in_max, int out_min, int out_max) {return (float)(val - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;}char Txt;void main(){unsigned int adc_value;float d;PORTA = 0b00000000;TRISA = 0xFF;ADC_Init();Lcd_Init(); // Initialize LCDLcd_Cmd(_LCD_CLEAR); // Clear displayLcd_Cmd(_LCD_CURSOR_OFF); // Cursor offwhile(1){adc_value = ADC_read(0);d = map(adc_value,0,1023,0,255);FloatToStr(d,Txt); // Convert voltage to stringLcd_Out(2,1,"d =");Lcd_Out(2,5,Txt);Delay_ms(300);} }