Author Topic: A cheap and cheerful way to interface a potentiometer to an FPGA  (Read 2612 times)

0 Members and 1 Guest are viewing this topic.

Offline hamster_nzTopic starter

  • Super Contributor
  • ***
  • Posts: 2803
  • Country: nz
A cheap and cheerful way to interface a potentiometer to an FPGA
« on: September 05, 2014, 01:47:30 am »
Although it is possible to implement one-bit DAC using a couple of FPGA pins, I've never got a useful result and the high-frequency toggling of the pins is not very subtle. So here's a cheap and cheerful way to interface a potentiometer to an FPGA.

http://hamsterworks.co.nz/mediawiki/index.php/Cheap_Analogue_Input

It is based on the technique I first came across when looking at the Apple ][ joystick interface, many, many moons ago. Is much better for playing Pong than two buttons, and uses half the pins.

Calibration of the result  is not covered on the wiki page, but is pretty easy. Here it is half way between HDL and software.

Code: [Select]
if(min_value == 0xFFFF and max_value == 0) {
  // Detect the inital values for these variables
  min_value = this_value;
  max_value = this_value; 
}

if(min_value > this_value)
  min_value = this_value;

if(max_value < this_value)
  max_value = this_value;

calibrated = 0;
q = min_value*256;
for(i = 0; i < 256) {
  if(q < this_value*256)
    calibrated = i;
  q = q + (max_value - min_value);
  i++;
}
return calibrated;
Gaze not into the abyss, lest you become recognized as an abyss domain expert, and they expect you keep gazing into the damn thing.
 

Offline thm_w

  • Super Contributor
  • ***
  • Posts: 6378
  • Country: ca
  • Non-expert
Re: A cheap and cheerful way to interface a potentiometer to an FPGA
« Reply #1 on: September 06, 2014, 12:03:51 am »
Cool. Code is super readable.

Quote
One of the many downsides with this method is that the values will differ quite widely with the tolerance of the components, especially the capacitor. With my parts it goes from 0x0218 to about 0x93xx. The best way to deal with this is to keep track of the maximum and minimum values, and then scale reading the appropriately.

For a joystick it seems like this is typical though, as the stick might not be perfectly centered, etc. Although, perhaps they would just use massive dead zones in the middle and ends to work around this.
Profile -> Modify profile -> Look and Layout ->  Don't show users' signatures
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf