Advertisement
Guest User

LAB6

a guest
Nov 14th, 2019
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.56 KB | None | 0 0
  1. /*
  2.  * File:   main.c
  3.  * Author: Jon Mills
  4.  * Date: 2019-11-08
  5.  * Version: 1
  6.  * Title: Lab 6
  7.  * Description: Takes in an input analog value and turns on a corresponding LED
  8.  *
  9.  *
  10.  *
  11.  */
  12.  
  13.  
  14. // PIC18F45K22 Configuration Bit Settings
  15.  
  16. // 'C' source line config statements
  17.  
  18. // CONFIG1H
  19. #pragma config FOSC = INTIO67   // Oscillator Selection bits (Internal oscillator block)
  20. #pragma config PLLCFG = OFF     // 4X PLL Enable (Oscillator used directly)
  21. #pragma config PRICLKEN = ON    // Primary clock enable bit (Primary clock is always enabled)
  22. #pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
  23. #pragma config IESO = OFF       // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)
  24.  
  25. // CONFIG2L
  26. #pragma config PWRTEN = OFF     // Power-up Timer Enable bit (Power up timer disabled)
  27. #pragma config BOREN = OFF      // Brown-out Reset Enable bits (Brown-out Reset disabled in hardware and software)
  28. #pragma config BORV = 190       // Brown Out Reset Voltage bits (VBOR set to 1.90 V nominal)
  29.  
  30. // CONFIG2H
  31. #pragma config WDTEN = OFF      // Watchdog Timer Enable bits (Watch dog timer is always disabled. SWDTEN has no effect.)
  32. #pragma config WDTPS = 32768    // Watchdog Timer Postscale Select bits (1:32768)
  33.  
  34. // CONFIG3H
  35. #pragma config CCP2MX = PORTC1  // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
  36. #pragma config PBADEN = ON      // PORTB A/D Enable bit (PORTB<5:0> pins are configured as analog input channels on Reset)
  37. #pragma config CCP3MX = PORTB5  // P3A/CCP3 Mux bit (P3A/CCP3 input/output is multiplexed with RB5)
  38. #pragma config HFOFST = ON      // HFINTOSC Fast Start-up (HFINTOSC output and ready status are not delayed by the oscillator stable status)
  39. #pragma config T3CMX = PORTC0   // Timer3 Clock input mux bit (T3CKI is on RC0)
  40. #pragma config P2BMX = PORTD2   // ECCP2 B output mux bit (P2B is on RD2)
  41. #pragma config MCLRE = EXTMCLR  // MCLR Pin Enable bit (MCLR pin enabled, RE3 input pin disabled)
  42.  
  43. // CONFIG4L
  44. #pragma config STVREN = ON      // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
  45. #pragma config LVP = OFF        // Single-Supply ICSP Enable bit (Single-Supply ICSP disabled)
  46. #pragma config XINST = OFF      // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))
  47.  
  48. // CONFIG5L
  49. #pragma config CP0 = OFF        // Code Protection Block 0 (Block 0 (000800-001FFFh) not code-protected)
  50. #pragma config CP1 = OFF        // Code Protection Block 1 (Block 1 (002000-003FFFh) not code-protected)
  51. #pragma config CP2 = OFF        // Code Protection Block 2 (Block 2 (004000-005FFFh) not code-protected)
  52. #pragma config CP3 = OFF        // Code Protection Block 3 (Block 3 (006000-007FFFh) not code-protected)
  53.  
  54. // CONFIG5H
  55. #pragma config CPB = OFF        // Boot Block Code Protection bit (Boot block (000000-0007FFh) not code-protected)
  56. #pragma config CPD = OFF        // Data EEPROM Code Protection bit (Data EEPROM not code-protected)
  57.  
  58. // CONFIG6L
  59. #pragma config WRT0 = OFF       // Write Protection Block 0 (Block 0 (000800-001FFFh) not write-protected)
  60. #pragma config WRT1 = OFF       // Write Protection Block 1 (Block 1 (002000-003FFFh) not write-protected)
  61. #pragma config WRT2 = OFF       // Write Protection Block 2 (Block 2 (004000-005FFFh) not write-protected)
  62. #pragma config WRT3 = OFF       // Write Protection Block 3 (Block 3 (006000-007FFFh) not write-protected)
  63.  
  64. // CONFIG6H
  65. #pragma config WRTC = OFF       // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) not write-protected)
  66. #pragma config WRTB = OFF       // Boot Block Write Protection bit (Boot Block (000000-0007FFh) not write-protected)
  67. #pragma config WRTD = OFF       // Data EEPROM Write Protection bit (Data EEPROM not write-protected)
  68.  
  69. // CONFIG7L
  70. #pragma config EBTR0 = OFF      // Table Read Protection Block 0 (Block 0 (000800-001FFFh) not protected from table reads executed in other blocks)
  71. #pragma config EBTR1 = OFF      // Table Read Protection Block 1 (Block 1 (002000-003FFFh) not protected from table reads executed in other blocks)
  72. #pragma config EBTR2 = OFF      // Table Read Protection Block 2 (Block 2 (004000-005FFFh) not protected from table reads executed in other blocks)
  73. #pragma config EBTR3 = OFF      // Table Read Protection Block 3 (Block 3 (006000-007FFFh) not protected from table reads executed in other blocks)
  74.  
  75. // CONFIG7H
  76. #pragma config EBTRB = OFF      // Boot Block Table Read Protection bit (Boot Block (000000-0007FFh) not protected from table reads executed in other blocks)
  77.  
  78. // #pragma config statements should precede project file includes.
  79. // Use project enums instead of #define for ON and OFF.
  80.  
  81. #include <xc.h>
  82. #define _XTAL_FREQ 8000000
  83.  
  84. void main(void)
  85. {
  86.     int conversion = 51; //explained further down
  87.     TRISA = 1;
  88.     TRISC = 0; //set outputs
  89.     LATC = 0;
  90.     ANSELA = 1; //set all ports at port A to be analog input bits. we're only going to use 1, but im lazy
  91.     ADCON0 = 0x1; //select AN0, turn on the ADC, but don't start acqusition!
  92.     ADCON1 = 0x0; //the bits to configure the voltage reference at VSS and VDD are 00 and 00.
  93.     ADCON2 = 0b00101000;
  94.    
  95.     while (1)
  96.     {
  97.        
  98.         ADCON0bits.GODONE = 1; //start the acqusition
  99.         while (ADCON0bits.GODONE == 1)
  100.         {
  101.             //... empty, just waiting for the conversion to be done
  102.         }
  103.         //we will convert our result to integer so that it can be sent to the correct LED
  104.         //+1 to the overall byte is  0.004882.
  105.         //+1 to the HIGH result is  0.004882*4 or 0.0195.
  106.         //11111111 is 5-4.98V, and 00000000 is 0-0.02 V approx.
  107.         int ad_result = ADRESH;
  108.         //we should convert the result, since it isn't quite what we're asking for
  109.         //the divisions are approx. 51.
  110.         //therefore, we can divide the result by 51 to get a value between 0 and 5 to send
  111.         //we also
  112.         int converted = ((ad_result - 2) / 51);
  113.         //next, decode the value by setting the corresponding bit...
  114.         int decoded = 0;
  115.         decoded |= 1UL << converted; //i found this code on stack overflow.
  116.         //it uses the bit shift operator to to put a 1 at the correct bit.
  117.         //1UL is an unsigned long that has the form of 31 zeroes followed by a 1.
  118.         //this single bit is then shifted left by the amount that was found by the conversion.
  119.         //the bitwise OR operator guarantees that a 1 is written to this bit.
  120.         LATC = 0;
  121.         LATC = decoded; //send it to the corresponding bit
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement