top of page

/*

 * FenceCharger_Transmitter.c

 *  Created on: Apr 22, 2017

 *      Author: Brad

 */

 

#include "derivative.h" /* include peripheral declarations */

#include "my_io_apps.c" /* include peripheral declarations */

#include "mcg.h" /* using XTAL and PLL does not work returns errors*/

#include "my_interrupts.c"

#include <stdio.h>

#include <string.h>

 

#define HVSwitch_On  gpo_b(0,1)

#define HVSwitch_Off gpo_b(0,0)

#define GreenLED_On  gpo_e(4,0)

#define GreenLED_Off gpo_e(4,1)

#define Transmitter_On gpo_e(22,1)

#define Transmitter_Off gpo_e(22,0)

int c = 0;

int i = 0, chargetime=0,x,y ;

 

void OneHVPulse(int numpulse)

{

      HVSwitch_On;

      delay_ms(numpulse);

      HVSwitch_Off;    

}

 

void INIT_UART2_SENDER (void)

{

      SIM_SCGC4 = SIM_SCGC4|SIM_SCGC4_UART2_MASK;

      SIM_SCGC5 = SIM_SCGC5|SIM_SCGC5_PORTE_MASK;

      PORTE_PCR22 = PORT_PCR_ISF_MASK|PORT_PCR_MUX(0x4);

      UART2_BDH = 0x10;

      UART2_BDL = 0xE2;

      UART2_C4 = 0x0F;

      UART2_C1 = 0x00;

      UART2_C3 = 0x00;

      UART2_S1 |= 0x1F;

      UART2_S2 |= 0xC0;

      UART2_C2 |= UART0_C2_TE_MASK;

}

 

void send_data(void)

{

       while(!(UART2_S1&UART_S1_TDRE_MASK) && !(UART2_S1&UART_S1_TC_MASK));   

       UART2_D  = c;

}

 

void INIT_CMP (void)

{

      SIM_SCGC4 = SIM_SCGC4|SIM_SCGC4_CMP_MASK;

      SIM_SOPT4 = SIM_SOPT4|SIM_SOPT4_TPM1CH0SRC_MASK;  // tpm1 ch0 selected

      SIM_SCGC5|=SIM_SCGC5_PORTC_MASK;

      SIM_SCGC5|=SIM_SCGC5_PORTE_MASK;

      SIM_SCGC5|=SIM_SCGC5_PORTA_MASK;

     

      CMP0_CR0 = 0X0;

      CMP0_CR1 = 0X97; //HIGH SPEED COMPARATOR MODE,COMPARATOR OUTPUT PIN ENABLED, ANALOG COMP. ENABLED   

      CMP0_FPR = 0X0;

     

      PORTC_PCR5 = PORTC_PCR5|PORT_PCR_MUX(0x6);

      PORTA_PCR12 = PORTA_PCR12|PORT_PCR_MUX(0x3);

 

      SIM_SCGC6|=SIM_SCGC6_DAC0_MASK;

     

      DAC0_C0 = 0XA0;

      DAC0_DAT0L = 0X50;  // 50 steps to give 64.5mV ((3.3v / 4096) x 80)

      DAC0_DAT0H = 0X00;

     

      PORTE_PCR30 = PORTE_PCR30|PORT_PCR_MUX(0X0);

      CMP0_MUXCR = 0X2c; //PSEL SET TO IN4, MSEL SET IT IN7(DAC OUTPUT)

     

      //activating tpm module

      SIM_SOPT2 |= SIM_SOPT2_PLLFLLSEL_MASK;

      SIM_SOPT2 &= ~(SIM_SOPT2_TPMSRC_MASK);

      SIM_SOPT2 |= SIM_SOPT2_TPMSRC(1);

      SIM_SCGC6 |= SIM_SCGC6_TPM1_MASK;

      //The TPM Module has Clock.  Now set up the peripheral

 

      //Blow away the control registers to ensure that the counter is not running

           TPM1_SC = 0;

           TPM1_CONF = 0;

         

          //While the counter is disabled we can setup the prescaler

          TPM1_SC = TPM_SC_PS(6);

          //TPM1_SC |= TPM_SC_TOIE_MASK; //Enable Interrupts for the Timer Overflow

         

          //Setup the mod register to get the correct  Period

          //TPM1_MOD = 38500;  //approx. 100ms period for trigger pulse

             

          //Sub-channels set up for input capture

        TPM1_C0SC = TPM_CnSC_ELSA_MASK; //rising edge

        //TPM1_C1SC = TPM_CnSC_ELSB_MASK;  //falling edge             

          //Last step, Enable the TPM COunter

          TPM1_SC |= TPM_SC_CMOD(1);

}

 

 main(void) {

 

      //******Initial Chip Settings******************

 

      int pll_err=pll_init(8000000,0,1,4,24,1);     //Use the crystal oscillator to drive the PLL

 

      //******User chip initializations***************************************

 

    init_gpio_b(0,1); //fencepulser

    init_gpio_b(1,1); // can also be used as the fencepulser

    init_gpio_e(4,1);  //green led indicator

    init_gpio_e(22,1);  //transmitter

 

    gpo_b(0,0);

    gpo_b(1,0);

    gpo_e(4,1);

 

    init_gpio_e(20,0); //sampling resistor data

   

//Initialize the ADC for the pot

    INIT_CMP();

    INIT_UART2_SENDER();

    syst_tick_init();      

 

      for(;;)

      {    

            sys_ticks=0;

            GreenLED_On;

            TPM1_CNT = 0; //resetting timer

            delay_100us(1);

            x=TPM1_CNT;

 

            HVSwitch_On;

            while(gpi_c(5)==0 && TPM1_CNT <20000)

                  chargetime = TPM1_CNT;

            if(chargetime<1000)

                  delay_ms(20);

            HVSwitch_Off;

            delay_ms(20);

           

            if(chargetime>475)

            {

                  c = 60 ;

                  send_data();

            }

            else

            {

                  c = 195;

                  send_data();

            }    

            while(sys_ticks<40);   

      }

      return 0;

    }

 

 

 

 

 

/*

 * FenceCharger_Receiver.c

 *  Created on: Apr 27, 2017

 *      Author: Brad

 */

 

#include "derivative.h" /* include peripheral declarations */

#include "my_io_apps.c" /* include peripheral declarations */

#include "mcg.h" /* using XTAL and PLL does not work returns errors*/

#include "my_interrupts.c"

#include <stdio.h>

#include <string.h>

 

#define GreenLED_On  gpo_e(21,0)

#define GreenLED_Off gpo_e(21,1)

#define RedLED_On gpo_e(20,0)

#define RedLED_Off gpo_e(20,1)

 

unsigned char c,z=0;

 

void INIT_UART2_RECVR (void)

{

      SIM_SCGC4 = SIM_SCGC4_UART2_MASK;

      SIM_SCGC5 = SIM_SCGC5_PORTE_MASK;

     

      PORTE_PCR23 = PORT_PCR_ISF_MASK|PORT_PCR_MUX(0x4);

     

      UART2_BDH = 0x10;

      UART2_BDL = 0xE2;

      UART2_C4 = 0x0F;

      UART2_C1 = 0x00;

      UART2_C3 = 0x00;

      UART2_S1 |= 0x1F;

      UART2_S2 |= 0xC0;

      UART2_C2 |= UART0_C2_RE_MASK;

}

 

void recv_data(void)

{

       while(!(UART2_S1&UART_S1_RDRF_MASK));   

       c = UART2_D; 

}

 

main(void)

{

      //******Initial Chip Settings******************

      int pll_err=pll_init(8000000,0,1,4,24,1);     //Use the crystal oscillator to drive the PLL

 

      //******User chip initializations***************************************

 

    init_gpio_e(21,1);  //green led indicator

    init_gpio_e(20,1);  //red led indicator

 

    gpo_e(21,1);   // turning off green led

    gpo_e(20,1);

  

    init_gpio_e(23,0);  //receiver

    adc0_init();    //Initialize the ADC for the pot

    INIT_UART2_RECVR();

    syst_tick_init(); 

   

    sys_ticks=0;

 

      for(;;)

      {    

            recv_data();

            if(c<124)

            {GreenLED_On; RedLED_Off;}

            else if(c >130)

            {GreenLED_Off; RedLED_On;}

            else

                  z=c;

           

            delay_ms(1000);  

      }

     

      return 0;

    }

© 2023 by CowCatcher3000

bottom of page