CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

sshahryiar's map() function not working for me

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
hashmi1122



Joined: 12 Jun 2020
Posts: 1

View user's profile Send private message Send e-mail

sshahryiar's map() function not working for me
PostPosted: Fri Jun 12, 2020 7:29 am     Reply with quote

map() function in Code Library:
http://www.ccsinfo.com/forum/viewtopic.php?t=49501

Below is my code, it is working good without double map function (told by Shehryar).

I am not an expert and not a newbie too.

I am using PIC12F675 and i need to control the blinking rate of an LED at (PIN_A1) through Potentiometer at (PIN_A2).

For more better and longer blinking rate (delay)...

I tried double map function after visiting this post but it is not working and compiler is asking to "define" map instruction ... why is it so ???


Code:


#include <12f675.h>

#device ADC=8

#define GP2 PIN_A2

#device *= 8

#use delay(clock = 4MHz)

#define map

#FUSES NOWDT, NOMCLR, NOWDT, NOPROTECT, INTRC_IO, NOBROWNOUT

double map(double value, int x_min, int x_max, int y_min, int y_max);




void main()

{     

   unsigned int16   adc_value=0;
   unsigned int16   delay_time=0;

   
   setup_adc(ADC_CLOCK_DIV_16);
   SETUP_ADC_PORTS(sAN2);
   set_adc_channel(2); 
   delay_ms(1);

while(true)
   
{
     
   set_adc_channel(2);
   adc_value = read_adc();
   delay_time = map(adc_value, 0, 1023, 0, 255);
   
   
   delay_ms(2);
   
   
   output_high(PIN_A1);                      // SELF IS ON
   delay_ms(delay_time);   
   output_low(PIN_A1);                       // SELF IS OFF
   delay_ms(delay_time);
 
}
}

double map(double value, int x_min, int x_max, int y_min, int y_max)   
{                   
    return (0 + (((255 - 0)/(1023 - 0)) * (value - 0)));
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Sat Jun 13, 2020 1:11 am     Reply with quote

The 'double' type, does not exist on the smaller PIC's. Good thing too, even
the 'float' version, will be using a huge amount of your processor's memory
and time....
temtronic



Joined: 01 Jul 2010
Posts: 9097
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Sat Jun 13, 2020 4:20 am     Reply with quote

comment.
Have you tried using the ADC in 10 bit mode without using the map() function ??

Jay
Jerson



Joined: 31 Jul 2009
Posts: 122
Location: Bombay, India

View user's profile Send private message Visit poster's website

PostPosted: Sun Jun 14, 2020 4:28 am     Reply with quote

Firstly, the #define map conflicts with the double map() function. You should start by removing the #define map

Small systems are not suited for floats/doubles, so you should try to avoid them as best as you can. The native operations are best in 8 bit or 16 bit variables, so try using int16 or int32 where needed.

Mapping is not difficult if you know how it works.

mapped zero = z
mapped full scale = f
adc reading @ zero = a0
adc reading @ full scale = af
adc reading to be mapped to scale z-f = ain

map(ain, z,f, a0,af) = (ain-a0)/(af-a0) * (f-z)

Again, if you're working with int16 types, make sure you perform the operations as
( (ain-a0) * (f-z) ) / (af - a0)
You may need to use int32 for the (first part)
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group