Skip to content

Instantly share code, notes, and snippets.

@sapher
Last active March 5, 2019 20:14
Show Gist options
  • Save sapher/20c5ffa30c7f75bd38d0 to your computer and use it in GitHub Desktop.
Save sapher/20c5ffa30c7f75bd38d0 to your computer and use it in GitHub Desktop.
Map function for PIC microcontroller that map a value from a range to another range
uint16_t map(uint16_t value, uint16_t fS, uint16_t fE, uint16_t tS, uint16_t tE) {
return ((long)(value - fS) * (long)(tE - tS)) / ((fE - fS) + tS);
}
// map(512, 0, 1024, 0, 512); > 127
@oschonrock
Copy link

Nice snippet

shouldn't it be:

 return ((long)(value - fS) * (long)(tE - tS)) / (fE - fS) + tS;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment