Advertisement
Guest User

Lab2-Task2

a guest
Mar 31st, 2020
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.76 KB | None | 0 0
  1. /*
  2.   Aim - Read Anbalog Voltage being applied on pad 7 through on board voltage divider circuit
  3.         Display incoming voltage as a bar graph using on board LEDs (PORTB,PTJ)
  4.        
  5.   Program Flow -
  6.   1. Create temporary variable to store AD conversion
  7.   2. Create switch state depending on incoming AD conversion
  8.   3. Configure ATD0CTL2, ATDOCTL3, ATD0CTL4, ATD0CTL5
  9.  
  10.  1. Check if ATD0STAT0 - SCF =1, FIFOR=0
  11.  2. Read ATD0DR0 to int variable
  12.  3. Send int variable to switching function
  13.  
  14.  
  15.  Based on opted 8 bit configuration and we have divided the incoming Analog voltage into 8 bands-
  16.  wherin the difference between the upper and lower boundary is 0.625V (5/2^8), as integers this is
  17.  represented as a band height of 32 (2^8/8). In practical use there would be never be an instant with
  18.  current configuration where a voltage of 5V or higher is input hence in the function LED_switch case 8
  19.  is merely a dummy case.
  20.  
  21.  Written by - Tanishq Jain | 31/03/2020
  22. */
  23.  
  24.  
  25. #include <hidef.h>      /* common defines and macros */
  26. #include "derivative.h"      /* derivative-specific definitions */
  27.  
  28. void Setup(void) ;
  29. int LED_switch(int Value) ;
  30.  
  31.  
  32. void Setup(void) {
  33.  
  34.   ATD0CTL2 = 0b11100000 ; /* Switch On, Fast clear, Disable wait mode, Disable Interrupt */
  35.   ATD0CTL3 = 0b00001100 ; /* 1 Conversion, FIFO */
  36.   ATD0CTL4 = 0b10100101 ; /* 8 bit, 2 AD conversion per cycle, Prescale - 12 */
  37.  
  38.  
  39.   DDRB = 0xFF ;   /* Making Port B as Output */
  40.   DDRJ = 0xFF ;   /* Making Port J as Output */
  41.   PTJ  = 0x00 ;   /* Enable LED */
  42. }
  43.  
  44. int LED_switch(int Value) {
  45.   switch ( Value )
  46.   {
  47.     case 1:
  48.             return 0x01 ;  /* 1 LED */
  49.            
  50.     case 2:
  51.             return 0x03 ;  /* 2 LED */
  52.            
  53.     case 3:
  54.             return 0x07 ;  /* 3 LED */
  55.        
  56.     case 4:
  57.             return 0x0F ;  /* 4 LED */
  58.            
  59.     case 5:
  60.             return 0x1F ;  /* 5 LED */
  61.            
  62.     case 6:
  63.             return 0x3F ;  /* 6 LED */
  64.            
  65.     case 7:
  66.             return 0x7F ;  /* 7 LED */
  67.            
  68.     case 8:
  69.             return 0xFF ;  /* 8 LED */
  70.            
  71.     default:
  72.             return 0x00 ;  /* 0 LED */
  73.   }
  74. }
  75.  
  76.  
  77. void main(void) {
  78.   int AD_read ;
  79.   Setup() ;
  80.  
  81.   while (1) {
  82.                                       /* Initiate a Conversion */
  83.     ATD0CTL5 = 0b10000111 ;           /* RIGHT Justified, Unsigned, One Conversion, Single Channel, Channel 7 */
  84.    
  85.     while (!(ATD0STAT0 & 0x80)) {};   /* Waiting for Conversion to be completed */
  86.    
  87.     AD_read = ATD0DR0 ;               /* Read Conversion result */
  88.     AD_read = AD_read/32 ;            /* Divide by 32 to get floor value to match band */
  89.  
  90.     PORTB = LED_switch(AD_read) ;     /* Switching LEDs on based on switch case */
  91.   }
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement