SHARE
TWEET

Untitled

a guest Sep 19th, 2019 76 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * adc_driver.c
  3.  *
  4.  * Created: 09.09.2019 15:38:28
  5.  *  Author: chris
  6.  */
  7.  
  8. #include "header/initialize.h"
  9. #include "header/adc_driver.h"
  10.  
  11. #include <stdint.h>
  12. #include <avr/interrupt.h>
  13.  
  14.  
  15.  
  16. uint8_t percentage_from_8bit(uint8_t input_value)       //Used if a percentage is more intuitive than 0-255. Delete if not needed.
  17. {
  18.     uint8_t percentage = 100*(input_value)/255;
  19.     return percentage;
  20. }
  21.  
  22. char joystick_direction(uint8_t input_value,char axis)
  23. {
  24.    
  25.         if ((input_value < 124) & (axis == 'x')) {
  26.             return 'L';
  27.         }
  28.         else if((input_value > 130) & (axis == 'x')) {
  29.             return 'R';
  30.         }
  31.         else if((input_value < 124) & (axis == 'y')) {
  32.             return 'D';
  33.         }
  34.         else if((input_value > 130) & (axis == 'y')) {
  35.             return 'U';
  36.         }
  37.         else if((input_value <= 130) & (input_value >= 124)) {
  38.             return 'N';
  39.         }
  40.         else {
  41.             return 'E';
  42.         }
  43. }
  44.  
  45. uint8_t adc_read(uint8_t channel)   //Value is returned through an ISR
  46. {
  47.     volatile char *adc_address = 0x1400;
  48.    
  49.     switch (channel)
  50.     {
  51.        
  52.         case 1 :
  53.             adc_address[0] = 0b00000100;    //Select channel 1
  54.             break;
  55.         case 2:
  56.             adc_address[0] = 0b00000101;    //Select channel 2
  57.             break;
  58.         case 3:
  59.             adc_address[0] = 0b00000110;    //Select channel 3
  60.             break;
  61.         case 4:
  62.             adc_address[0] = 0b00000111;    //Select channel 4
  63.             break;
  64.         default:
  65.             printf("Invalid channel\r\n");
  66.     }
  67.    
  68.     while(PIND &= (1 << PD3));
  69.    
  70.     return adc_address[0];
  71. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top