Pic12f675 code (XC8 compiler) for utrasonic sensor


Pic12f675 code (XC8 compiler) for utrasonic sensor


/*
 * File:   main.c
 * Author: root
 * interrupt-on-pin change
 * Created on March 28, 2023, 7:25 AM
 */
// PIC12F675 Configuration Bit Settings

// 'C' source line config statements

// CONFIG
#pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator: High speed crystal/resonator on GP4/OSC2/CLKOUT and GP5/OSC1/CLKIN)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF      // Power-Up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = OFF      // GP3/MCLR pin function select (GP3/MCLR pin function is digital I/O, MCLR internally tied to VDD)
#pragma config BOREN = OFF      // Brown-out Detect Enable bit (BOD disabled)
#pragma config CP = OFF         // Code Protection bit (Program Memory code protection is disabled)
#pragma config CPD = OFF        // Data Code Protection bit (Data memory code protection is disabled)

#include <xc.h>
#define _XTAL_FREQ 16000000

#define dataline GP1
#define clock  GP0 

volatile int tmp;

void send(char data) {

        dataline = 1;
        for (int i=0; i < data; i++) {
           clock = 1;
           __delay_ms(5);
           clock = 0;
           __delay_ms(5);
        }
        dataline = 0;   // end  transmission
        clock = 1;
         __delay_ms(5);
        clock = 0;
}

void __interrupt() isr(void)
{
  if(GPIF == 1)                       //Makes sure that it is PORTB On-Change Interrupt
  {
    GPIE = 0;                         //Disable On-Change Interrupt
  
    if (GP3 == 1)                      //If ECHO is HIGH
        TMR1ON = 1;                       //Start Timer
    else if(GP3 == 0)                      //If ECHO is LOW
    {
      TMR1ON = 0;                     //Stop Timer
      tmp = (TMR1L | (TMR1H<<8))/ 57;   //Calculate Distance at 30 C.
    }
  
  }   
  GPIF = 0;                           //Clear PORTB On-Change Interrupt flag
  GPIE = 1;                           //Enable PORTB On-Change Interrupt
}

void main(void) {
    
    TRISIO = 0b00001000;    
    ANSEL = 0x00;  
    GPIO = 0x00;
    CMCON = 0x07; // IMPORTANT, without interrupt on change is not working
    GIE = 1;    //Global Interrupt Enable
   
    
    IOCB = 0x08;                        // GP3 interrupt on change
    GPIF = 0;                           //Clear PORTB On-Change Interrupt Flag
    GPIE = 1;                           //Enable PORTB On-Change Interrupt

    
    T1CON = 0x20;   // 0001 0000  prescaler 1:4     FOsc/4  
 
    while(1) {    
        TMR1H = 0;                        //Sets the Initial Value of Timer
        TMR1L = 0;                        //Sets the Initial Value of Timer

        GP2 = 1;                          //TRIGGER HIGH
        __delay_us(10);                   //10uS Delay
        GP2 = 0;                          //TRIGGER LOW 

        __delay_ms(100);                  //Waiting for ECHO
        tmp = tmp + 1;                    //Error Correction Constant
        
        if (tmp > 16) {
            send(1);  // no signal
        } else if (tmp > 12) {    // 13,14,15,16 cm
            send(2);    // green light 
        } else if (tmp > 7) { // 9,10,11,12
            send(3);        // yellow
        } else if (tmp > 5) { // 6,7,8,9
            send(4);   // red
        } else if (tmp < 6) {  // 5, 4,3,2
            send(5); // red + one tone
        }
        __delay_ms(200);
        
    }
    
    
    return;
}

Pic12f675 code (XC8 compiler) for park assist warnings
/* * File: main.c * Author: root * 2 line communication = data line GP3 and clock GP2 (interrupt driven) * When clock = high and data = high it adds 2 * When clock = high and data = low it adds 1 and ends communication * Created on March 20, 2023, 11:22 AM */ // CONFIG #pragma config FOSC = INTRCIO // Oscillator Selection bits (INTOSC oscillator: I/O function on GP5/OSC2/CLKOUT pin, I/O function on GP5/OSC1/CLKIN) #pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled) #pragma config PWRTE = OFF // Power-Up Timer Enable bit (PWRT disabled) #pragma config MCLRE = OFF // GP3/MCLR pin function select (GP3/MCLR pin function is digital I/O, MCLR internally tied to VDD) #pragma config BOREN = OFF // Brown-out Detect Enable bit (BOD disabled) #pragma config CP = OFF // Code Protection bit (Program Memory code protection is disabled) #pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled) #include <xc.h> #define _XTAL_FREQ 4000000 char transmit; void __interrupt() isr(void) { if( INTCONbits.INTF==1) { if (transmit % 2 == 1) // odd number transmit = 0; INTCONbits.INTF = 0; // INTCONbits.INTE = 1; if (GP3 == 1) transmit += 2; // even number else transmit += 1; // odd number = end transmission } } int main(int argc, char** argv) { TRISIO = 0b00001100; // Configure all I/O. GPIO = 0x00; ANSEL = 0x00; CMCON = 0x07; // turn off comparator // INTCON INTCON = 0b11010000; // option_reg OPTION_REG = 0b01000000; // Interrupt on rising edge of GP2/INT pin transmit = 0; while (1) { if (transmit == 3) { GP0 = 0; GP1 = 0; GP4 = 0; GP5 = 0; } else if (transmit == 5) { GP0 = 1; // green GP1 = 0; GP4 = 0; GP5 = 1; __delay_ms(200); GP5 = 0; __delay_ms(200); } else if (transmit == 7) { GP0 = 0; GP1 = 1; // yellow GP4 = 0; GP5 = 1; __delay_ms(120); GP5 = 0; __delay_ms(120); } else if (transmit == 9) { GP0 = 0; GP1 = 0; GP4 = 1; // red GP5 = 1; __delay_ms(60); GP5 = 0; __delay_ms(60); } else if (transmit == 11) { GP0 = 0; GP1 = 0; GP4 = 1; // red GP5 = 1; } } return(0); }