Ejemplo n.º 1
0
void main()
{
                                                                                                                                                                                 
setup_psp(PSP_DISABLED);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_adc_ports(NO_ANALOGS);
setup_adc(ADC_OFF);
setup_CCP1(CCP_OFF);
setup_CCP2(CCP_OFF);

set_tris_b(0x00);
set_tris_d(0b00000001);
set_tris_a(0x00);


output_b(0x00);


   start:
    if (input(pin_a0)==true)
    { 
    while(1==1){
      for(a=pin_b0; a<=pin_b5; a++)
      {
         output_high(a);
         delay_ms(100);
         output_low(a);
      
      }
      for( b=a; b>=pin_b0; b--)
      {
         output_high(b);
         delay_ms(100);
         output_low(b);
       
      }
 
    }
    a=0;
    b=0;
    }
     goto start;             
}
Ejemplo n.º 2
0
Archivo: main.c Proyecto: ednspace/EAR
//==================================== 
void main() 
{ 


int16 local_ccp_delta;
got_pulse_width = FALSE; 
first_press = TRUE;

//DAC_address = 0xC4; //Address of the DAC don't ever forget this please
DAC_address = 0xC0; //I2C Address of parts with marking AJ

//Oscillator Config

//setup_oscillator(OSC_8MHZ|OSC_INTRC|OSC_PLL_ON); //I am giving it all shes got, she can't take any more Captain
//setup_oscillator(OSC_500KHZ|OSC_INTRC|OSC_PLL_OFF);  //Can measure a little over 4 seconds with timer1
setup_oscillator(OSC_2MHZ|OSC_INTRC|OSC_PLL_OFF);  //Can measure a little over 1 second with timer1 Use this one

//Capture Compare Config
setup_CCP1(CCP_CAPTURE_FE); //Sets up Capture Compare for Falling Edge - Reads Tap Input
bit_set(APFCON,0); //Set CCP1 Pin to A5 instead of A2

//ADC Config
SETUP_ADC(ADC_CLOCK_INTERNAL);
SETUP_ADC_PORTS(sAN3); //Analog in on RA4
set_adc_channel(3);
delay_us(10);



setup_timer_1(T1_INTERNAL | T1_DIV_BY_8 ); 
setup_timer_2(T2_DIV_BY_16 , 31, 1);  //Need a real understanding of what this lines does here
enable_interrupts(INT_CCP1);
enable_interrupts(INT_TIMER2);
enable_interrupts(GLOBAL);

//DAC Test Code
//setVoltage(4095, FALSE); //Sets DAC output to Max voltage
//setVoltage(0, FALSE); 

mode = 1; //Start off in POT mode

while(1) 
  { 
  pot_val = read_adc(); //Read the ADC everytime
  
  if (mode == 1){ //We are in POT mode just set the value
      pot_save = pot_val;
      setVoltage(pot_val * 4, FALSE); //scale to 12bit DAC
  }
  
  if (((pot_val > pot_save + 10 && mode ==0)) || ((pot_val < pot_save - 10) && (mode == 0))){ //We are in TAP mode only set voltage if POT moves a lot
      mode = 1; //Jump out of tap mode and enter pot mode
  }
 
   
  
  if(got_pulse_width) 
     { 
      disable_interrupts(GLOBAL); 
      local_ccp_delta = ccp_delta;  
      enable_interrupts(GLOBAL);
      
      pulse_width_ms = local_ccp_delta / 62; //2E-6 per tick * 8 Prescale = 1.6E-5 so .001 / 1.6E-5 = 62.5 
      got_pulse_width = FALSE;
      
     // For Testing set Pulse width ms here
     // pulse_width_ms = 524;
      
      LUT_count = 1;
      while   (millisecond_delay[LUT_count] >= pulse_width_ms){
      LUT_count = LUT_count + 1;
      }
      calculated_voltage = map(pulse_width_ms, millisecond_delay[LUT_count - 1], millisecond_delay[LUT_count] , dac_out[LUT_count-1],dac_out[LUT_count]);
      
      
     // printf("%lu ms \n\r", pulse_width_ms); //Debug Message
     // printf("%lu dac \n\r", calculated_voltage); //Debug Message
      setVoltage(calculated_voltage,FALSE);
      mode = 0; //Set Mode to tap mode, requires larger movement of POT to break out
     } 
  } 

}