Sunday, June 2, 2019

Simple Cooling System

// center of X-ohm potentiometer connected to PE3/AIN0
// bottom of X-ohm potentiometer connected to ground
// top of X-ohm potentiometer connected to +3.3V
#include <stdint.h>
#include <stdbool.h>
#include "ADCSWTrigger.h"
#include "tm4c123gh6pm.h"
#include "PLL.h"

#define GPIO_PORTF2             (*((volatile uint32_t *)0x40025010))

#define ON 1
#define OFF 0

void DisableInterrupts(void); // Disable interrupts
void EnableInterrupts(void);  // Enable interrupts
long StartCritical (void);    // previous I bit, disable interrupts
void EndCritical(long sr);    // restore I bit to previous value
void WaitForInterrupt(void);  // low power mode

bool Light = OFF;

volatile uint32_t ADCvalue;
// This debug function initializes Timer0A to request interrupts
// at a 10 Hz frequency.  It is similar to FreqMeasure.c.
void Timer0A_Init10HzInt(void){
  volatile uint32_t delay;
  DisableInterrupts();
  // **** general initialization ****
  SYSCTL_RCGCTIMER_R |= 0x01;// activate timer0
  delay = SYSCTL_RCGCTIMER_R;          // allow time to finish activating
  TIMER0_CTL_R &= ~TIMER_CTL_TAEN; // disable timer0A during setup
  TIMER0_CFG_R = 0;                // configure for 32-bit timer mode
  // **** timer0A initialization ****
                                   // configure for periodic mode
  TIMER0_TAMR_R = TIMER_TAMR_TAMR_PERIOD;
  TIMER0_TAILR_R = 2499999;        // start value for 10 Hz interrupts
  TIMER0_IMR_R |= TIMER_IMR_TATOIM;// enable timeout (rollover) interrupt
  TIMER0_ICR_R = TIMER_ICR_TATOCINT;// clear timer0A timeout flag
  TIMER0_CTL_R |= TIMER_CTL_TAEN;  // enable timer0A 16-b, periodic, interrupts
  // **** interrupt initialization ****
                                   // Timer0A=priority 2
  NVIC_PRI4_R = (NVIC_PRI4_R&0x00FFFFFF)|0x40000000; // top 3 bits
  NVIC_EN0_R = NVIC_EN0_INT19;     // enable interrupt 19 in NVIC
}

void PortB_Init(void)
{
SYSCTL_RCGCGPIO_R |= 0x00000002; // bit1 --> bit0 we are changing portb to porte for lab10

while ( SYSCTL_RCGCGPIO_R ==0){}
GPIO_PORTB_DIR_R = 0x01;
GPIO_PORTB_AFSEL_R = 0x00;
GPIO_PORTB_DEN_R = 0x01;
}
void Timer0A_Handler(void){
//DisableInterrupts();
  TIMER0_ICR_R = TIMER_ICR_TATOCINT;    // acknowledge timer0A timeout
  GPIO_PORTF2 = 0x04;                   // profile
  ADCvalue = ADC0_InSeq3();
  GPIO_PORTF2 = 0x00;
//if (Light == OFF)
//{
if (ADCvalue >= 5000) // trial and error
{
GPIO_PORTB_DATA_R |= 0x01;
//Light = ON;
}
else
{
GPIO_PORTB_DATA_R = 0x00;
}

}
int main(void){
  PLL_Init();                           // 25 MHz
  SYSCTL_RCGC2_R |= SYSCTL_RCGC2_GPIOF; // activate port F
  ADC0_InitSWTriggerSeq3(0);            // allow time to finish activating
//  ADC0_InitAllTriggerSeq3(0);           // allow time to finish activating
  Timer0A_Init10HzInt();                // set up Timer0A for 10 Hz interrupts
  GPIO_PORTF_DIR_R |= 0x04;             // make PF2 out (built-in LED)
  GPIO_PORTF_AFSEL_R &= ~0x04;          // disable alt funct on PF2
  GPIO_PORTF_DEN_R |= 0x04;             // enable digital I/O on PF2
                                        // configure PF2 as GPIO
  GPIO_PORTF_PCTL_R = (GPIO_PORTF_PCTL_R&0xFFFFF0FF)+0x00000000;
  GPIO_PORTF_AMSEL_R = 0;               // disable analog functionality on PF
  GPIO_PORTF2 = 0;                      // turn off LED
  EnableInterrupts();
PortB_Init();
  while(1){
    WaitForInterrupt();
//    GPIO_PORTF2 = 0x04;                 // profile
//    ADCvalue = ADC0_InSeq3();
//    GPIO_PORTF2 = 0x00;
  }
}