Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Aim - Read Anbalog Voltage being applied on pad 7 through on board voltage divider circuit
- Display incoming voltage as a bar graph using on board LEDs (PORTB,PTJ)
- Program Flow -
- 1. Create temporary variable to store AD conversion
- 2. Create switch state depending on incoming AD conversion
- 3. Configure ATD0CTL2, ATDOCTL3, ATD0CTL4, ATD0CTL5
- 1. Check if ATD0STAT0 - SCF =1, FIFOR=0
- 2. Read ATD0DR0 to int variable
- 3. Send int variable to switching function
- Based on opted 8 bit configuration and we have divided the incoming Analog voltage into 8 bands-
- wherin the difference between the upper and lower boundary is 0.625V (5/2^8), as integers this is
- represented as a band height of 32 (2^8/8). In practical use there would be never be an instant with
- current configuration where a voltage of 5V or higher is input hence in the function LED_switch case 8
- is merely a dummy case.
- Written by - Tanishq Jain | 31/03/2020
- */
- #include <hidef.h> /* common defines and macros */
- #include "derivative.h" /* derivative-specific definitions */
- void Setup(void) ;
- int LED_switch(int Value) ;
- void Setup(void) {
- ATD0CTL2 = 0b11100000 ; /* Switch On, Fast clear, Disable wait mode, Disable Interrupt */
- ATD0CTL3 = 0b00001100 ; /* 1 Conversion, FIFO */
- ATD0CTL4 = 0b10100101 ; /* 8 bit, 2 AD conversion per cycle, Prescale - 12 */
- DDRB = 0xFF ; /* Making Port B as Output */
- DDRJ = 0xFF ; /* Making Port J as Output */
- PTJ = 0x00 ; /* Enable LED */
- }
- int LED_switch(int Value) {
- switch ( Value )
- {
- case 1:
- return 0x01 ; /* 1 LED */
- case 2:
- return 0x03 ; /* 2 LED */
- case 3:
- return 0x07 ; /* 3 LED */
- case 4:
- return 0x0F ; /* 4 LED */
- case 5:
- return 0x1F ; /* 5 LED */
- case 6:
- return 0x3F ; /* 6 LED */
- case 7:
- return 0x7F ; /* 7 LED */
- case 8:
- return 0xFF ; /* 8 LED */
- default:
- return 0x00 ; /* 0 LED */
- }
- }
- void main(void) {
- int AD_read ;
- Setup() ;
- while (1) {
- /* Initiate a Conversion */
- ATD0CTL5 = 0b10000111 ; /* RIGHT Justified, Unsigned, One Conversion, Single Channel, Channel 7 */
- while (!(ATD0STAT0 & 0x80)) {}; /* Waiting for Conversion to be completed */
- AD_read = ATD0DR0 ; /* Read Conversion result */
- AD_read = AD_read/32 ; /* Divide by 32 to get floor value to match band */
- PORTB = LED_switch(AD_read) ; /* Switching LEDs on based on switch case */
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement