|
|
View previous topic :: View next topic |
Author |
Message |
hashmi1122
Joined: 12 Jun 2020 Posts: 1
|
sshahryiar's map() function not working for me |
Posted: Fri Jun 12, 2020 7:29 am |
|
|
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: 19589
|
|
Posted: Sat Jun 13, 2020 1:11 am |
|
|
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: 9269 Location: Greensville,Ontario
|
|
Posted: Sat Jun 13, 2020 4:20 am |
|
|
comment.
Have you tried using the ADC in 10 bit mode without using the map() function ??
Jay |
|
|
Jerson
Joined: 31 Jul 2009 Posts: 126 Location: Bombay, India
|
|
Posted: Sun Jun 14, 2020 4:28 am |
|
|
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) |
|
|
|
|
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
|