Author Topic: wanted to map adc into 0-255 range.But code did not working  (Read 1056 times)

0 Members and 1 Guest are viewing this topic.

Offline khatusTopic starter

  • Regular Contributor
  • *
  • Posts: 147
  • Country: gl
wanted to map adc into 0-255 range.But code did not working
« on: April 13, 2020, 02:12:34 pm »
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: [Select]

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[15];
void main()
{
  unsigned int adc_value;
  float d;
  PORTA = 0b00000000;
  TRISA = 0xFF;
  ADC_Init();
  Lcd_Init();                        // Initialize LCD
  Lcd_Cmd(_LCD_CLEAR);               // Clear display
  Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off

 while(1)
 {
 adc_value = ADC_read(0);
 d = map(adc_value,0,255);
 FloatToStr(d,Txt);    // Convert voltage to string
 Lcd_Out(2,1,"d =");
 Lcd_Out(2,5,Txt);
 Delay_ms(300);
 }


}

« Last Edit: April 13, 2020, 02:47:56 pm by khatus »
 

Offline chayanforyou

  • Newbie
  • Posts: 2
  • Country: bd
Re: wanted to map adc into 0-255 range.But code did not working
« Reply #1 on: April 13, 2020, 02:23:59 pm »
You can do simply by using this map function.

Quote
int d = map(adc_value, 0, 1023, 0, 255);

Code: [Select]
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;
}
« Last Edit: April 13, 2020, 02:25:49 pm by chayanforyou »
 

Online Andy Watson

  • Super Contributor
  • ***
  • Posts: 2116
Re: wanted to map adc into 0-255 range.But code did not working
« Reply #2 on: April 13, 2020, 02:32:51 pm »
Code: [Select]
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);
 

Offline capt bullshot

  • Super Contributor
  • ***
  • Posts: 3033
  • Country: de
    • Mostly useless stuff, but nice to have: wunderkis.de
Re: wanted to map adc into 0-255 range.But code did not working
« Reply #3 on: April 13, 2020, 02:43:51 pm »
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.

Safety devices hinder evolution
 

Offline khatusTopic starter

  • Regular Contributor
  • *
  • Posts: 147
  • Country: gl
Re: wanted to map adc into 0-255 range.But code did not working
« Reply #4 on: April 13, 2020, 02:49:41 pm »
Code: [Select]
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[15];
void main()
{
  unsigned int adc_value;
  unsigned int d;
  PORTA = 0b00000000;
  TRISA = 0xFF;
  ADC_Init();
  Lcd_Init();                        // Initialize LCD
  Lcd_Cmd(_LCD_CLEAR);               // Clear display
  Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off

 while(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 string
 Lcd_Out(2,1,"d =");
 Lcd_Out(2,5,Txt);
 Delay_ms(300);
 }


}

yes!! this works.But i wanted to do it using Function call.
 

Offline khatusTopic starter

  • Regular Contributor
  • *
  • Posts: 147
  • Country: gl
Re: wanted to map adc into 0-255 range.But code did not working
« Reply #5 on: April 13, 2020, 03:24:51 pm »
Thanks it works :) :)
Code: [Select]
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[15];
void main()
{
  unsigned int adc_value;
  float d;
  PORTA = 0b00000000;
  TRISA = 0xFF;
  ADC_Init();
  Lcd_Init();                        // Initialize LCD
  Lcd_Cmd(_LCD_CLEAR);               // Clear display
  Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off

 while(1)
 {
 adc_value = ADC_read(0);
 d = map(adc_value,0,1023,0,255);
 FloatToStr(d,Txt);    // Convert voltage to string
 Lcd_Out(2,1,"d =");
 Lcd_Out(2,5,Txt);
 Delay_ms(300);
 }  }

 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf