Exemplo n.º 1
0
void main()
{
SET_TRIS_A(0b00000001);

SETUP_ADC_PORTS(AN0);

SETUP_ADC(ADC_CLOCK_INTERNAL);

ENABLE_INTERRUPTS(GLOBAL | INT_TIMER0);
SETUP_TIMER_0(RTCC_INTERNAL | RTCC_8_BIT| RTCC_DIV_16);
SET_TIMER0(100);

while(true)
     {
     SET_ADC_CHANNEL(0);        //Configura o canal de leitura 0
     delay_us(100);             //Tempo de ajuste do canal (necessário) 
     ad0 = READ_ADC();          //Faz a conversão AD e a salva na variável ad0
   
     ad0 = (ad0 * 5000)/1023;
          
     d1 = ad0/1000;
     d2 = (ad0/100) % 10;
     d3 = (ad0/10) % 10;
     
     delay_ms(500);    
     }
}
Exemplo n.º 2
0
int main(void) {

	SETUP_ADC(ADC_CLOCK_INTERNAL);
	SETUP_ADC_PORTS(AN0);
	set_adc_channel(0);

	while (TRUE) {

		if (!input(botStart)) {
			delay_ms(100);
			if (!input(botStart) && !ctrl) {
				ctrl = TRUE;
				cont = 0;
				while (cont < bufferSize) {
					vetor[cont++] = read_adc();
					while (!adc_done())
						;
				}
				for (cont = 0; cont < bufferSize; ++cont) {
					printf("%lu\n\r", vetor[cont]);
				}
				cont = 0;
			} else if (ctrl)
				ctrl = FALSE;
		}
	}
	return 0;
}
//-----------------------------------------------------------------//
// Programa Principal ---------------------------------------------//
//-----------------------------------------------------------------//
void main() {
   int i;
  set_tris_a(0xFF);      // Puerto A todo entradas
  port_b_pullups(FALSE); // Resistencias de polarización
  set_tris_b(0x38);      //
  set_tris_c(0x00);      // Puerto C todo salidas
  set_tris_d(0x00);     // Puerto D todo salidas

  //Configuro los canales del ADC
  SETUP_ADC_PORTS(AN0_AN1_AN2_AN3_AN4);
  SETUP_ADC(ADC_CLOCK_DIV_8 );
  SETUP_VREF(VREF_HIGH | 6 );


  //Habilito interrupciones
  //enable_interrupts(int_rda);
  //enable_interrupts(global);

  while(1){
  realizar_ensayo();
  //printf("%4ld \r",md);
  }
 }
Exemplo n.º 4
0
Arquivo: main.c Projeto: 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
     } 
  } 

}