Advertisement
igendel

Continuity Tester Code

Jun 30th, 2021 (edited)
1,469
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.36 KB | None | 0 0
  1. // MPLAB X IDE v5.5, XC8 v2.32
  2. // PIC12F1572 Configuration Bit Settings
  3. // CONFIG1
  4. #pragma config FOSC = INTOSC    //  (INTOSC oscillator; I/O function on CLKIN pin)
  5. #pragma config WDTE = OFF       // Watchdog Timer Enable (WDT disabled)
  6. #pragma config PWRTE = OFF      // Power-up Timer Enable (PWRT disabled)
  7. #pragma config MCLRE = ON       // MCLR Pin Function Select (ON = MCLR/VPP pin function is MCLR, OFF = d.input)
  8. #pragma config CP = OFF         // Flash Program Memory Code Protection (Program memory code protection is disabled)
  9. #pragma config BOREN = OFF      // Brown-out Reset Enable
  10. #pragma config CLKOUTEN = OFF   // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
  11. // CONFIG2
  12. #pragma config WRT = OFF        // Flash Memory Self-Write Protection (Write protection off)
  13. #pragma config PLLEN = OFF      // PLL Enable (4x PLL disabled)
  14. #pragma config STVREN = ON      // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
  15. #pragma config BORV = LO        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
  16. #pragma config LPBOREN = OFF    // Low Power Brown-out Reset enable bit (LPBOR is disabled)
  17. #pragma config LVP = ON         // Low-Voltage Programming Enable (Low-voltage programming enabled)
  18.  
  19. #define _XTAL_FREQ 500000U
  20.  
  21. #include <stdint.h>
  22. #include <xc.h>
  23.  
  24.  
  25. //=========================================================
  26. void setup(void) {
  27.    
  28.     // RA4 is LED output
  29.     LATAbits.LATA4 = 0; // Important, it's undefined!
  30.     ANSELAbits.ANSA4 = 0;
  31.     TRISAbits.TRISA4 = 0;
  32.  
  33.     // RA5 is PWM1 output (Alternate pin function)
  34.     LATAbits.LATA5 = 0; // Important, it's undefined!
  35.     TRISAbits.TRISA5 = 0;
  36.     APFCONbits.P1SEL = 1;
  37.  
  38.     // RA2 will be used for INT while sleeping, and as ADC input for measurement
  39.     TRISAbits.TRISA2 = 1;
  40.    
  41.     // PWM
  42.     // Clock source is Fosc by default
  43.     // PWM Toggle On Match mode
  44.     PWM1CONbits.MODE = 2;
  45.     // period = 2 * ((PWM1PR + 1) * prescale / PWM1CLK), we need ~4KHz
  46.     PWM1PR = 61U;
  47.     PWM1PH = 1U;
  48.     // Set PWM "active" output to LOW
  49.     PWM1CONbits.POL = 1;
  50.    
  51.     // Disable internal LDO during sleep to save power
  52.     VREGCONbits.VREGPM = 1;
  53.    
  54.     // ADC channel 2 (from RA2)
  55.     ADCON0bits.CHS = 2;
  56.     // Right-align ADRES (ADC result register)
  57.     ADCON1bits.ADFM = 1;
  58.     // Set ADC conversion clock to FRC
  59.     ADCON1bits.ADCS = 7;
  60. }
  61.  
  62. //=========================================================
  63. void prepareForTesting(void) {
  64.  
  65.     // Disable INT
  66.     INTCONbits.INTE = 0;    
  67.    
  68.     // RA2 Needs to be analog input for the ADC to work
  69.     ANSELAbits.ANSA2 = 1;    
  70.    
  71.     // Turn on ADC module
  72.     ADCON0bits.ADON = 1;
  73.    
  74. }
  75.  
  76. //=========================================================
  77. void prepareForSleep(void) {
  78.  
  79.     // Turn off ADC module
  80.     ADCON0bits.ADON = 0;
  81.    
  82.     // RA2 Needs to be digital input for the interrupt to work
  83.     ANSELAbits.ANSA2 = 0;
  84.  
  85.     // Clear flag just in case, and enable INT
  86.     INTCONbits.INTF = 0;    
  87.     INTCONbits.INTE = 1;    
  88.    
  89.     // GIE not required for sleep wake-up
  90.    
  91. }
  92.  
  93. //=========================================================
  94. uint8_t continuityDetected(void) {
  95.  
  96.     // Start ADC conversion
  97.     ADCON0bits.GO_nDONE = 1;
  98.     while (1 == ADCON0bits.GO_nDONE) {
  99.     }
  100.    
  101.     // Adjusted for <= 100 Ohm, with 10K to GND
  102.     return (ADRES > 1013) ? 1 : 0;
  103. }
  104.  
  105. //=========================================================
  106. void giveFeedback(void) {
  107.    
  108.     uint8_t rounds = 4; // An even number!
  109.     uint8_t state = 1;
  110.    
  111.     // Enable PWM module, PWM1 output
  112.     PWM1CONbits.EN = 1;
  113.     PWM1CONbits.OE = 1;
  114.    
  115.     // LED blink
  116.     while (rounds--) {
  117.        
  118.         LATAbits.LATA4 = state;
  119.         state = 1 - state;
  120.         __delay_ms(80);
  121.        
  122.     }
  123.    
  124.     // Disable PWM1 output and PWM module
  125.     PWM1CONbits.OE = 0;    
  126.     PWM1CONbits.EN = 0;
  127.    
  128. }
  129. //=========================================================
  130. void main(void) {
  131.    
  132.     setup();
  133.    
  134.     for ( ; ; ) {
  135.        
  136.         prepareForSleep();
  137.         SLEEP();
  138.  
  139.         // Something got enough voltage to RA2 and woke us up
  140.        
  141.         prepareForTesting();
  142.         while (continuityDetected()) {
  143.             giveFeedback();
  144.         }
  145.        
  146.     }
  147.    
  148. }
  149.  
  150.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement