コード例 #1
0
ファイル: EX_ADMM.C プロジェクト: joristork/robots
main() {

    int i,value,min,max;

    printf("Sampling:");

    setup_port_a( ALL_ANALOG );
    setup_adc( ADC_CLOCK_INTERNAL );
    set_adc_channel( 0 );

    do {
        min=255;
        max=0;
        for(i=0; i<=30; ++i) {
            delay_ms(100);
            value = Read_ADC();
            if(value<min)
                min=value;
            if(value>max)
                max=value;
        }
        printf("\n\rMin: %2X  Max: %2X\r\n",min,max);

    } while (TRUE);

}
コード例 #2
0
ファイル: ex_pwm.c プロジェクト: carriercomm/robots
main() {
   char selection;
   byte value;


   printf("\r\nFrequency:\r\n");
   printf("    1) 19.5 khz\r\n");
   printf("    2) 4.9 khz\r\n");
   printf("    3) 1.2 khz\r\n");

   do {
     selection=getc();
   } while((selection<'1')||(selection>'3'));


   setup_ccp1(CCP_PWM);   // Configure CCP1 as a PWM

          //   The cycle time will be (1/clock)*4*t2div*(period+1)
	       //   In this program clock=10000000 and period=127 (below)
          //   For the three possible selections the cycle time is:
		    //     (1/10000000)*4*1*128 =  51.2 us or 19.5 khz
		    //     (1/10000000)*4*4*128 = 204.8 us or 4.9 khz
		    //     (1/10000000)*4*16*128= 819.2 us or 1.2 khz

   switch(selection) {
     case '1' : setup_timer_2(T2_DIV_BY_1, 127, 1);
                break;
     case '2' : setup_timer_2(T2_DIV_BY_4, 127, 1);
                break;
     case '3' : setup_timer_2(T2_DIV_BY_16, 127, 1);
                break;
   }



  setup_port_a(ALL_ANALOG);
  setup_adc(adc_clock_internal);
  set_adc_channel( 0 );
  printf("%c\r\n",selection);

  while( TRUE ) {
    value=read_adc();

    printf("%2X\r",value);

    set_pwm1_duty(value);          // This sets the time the pulse is
                                   // high each cycle.  We use the A/D
                                   // input to make a easy demo.
                                   // the high time will be:
                                   //  if value is LONG INT:
				   //    value*(1/clock)*t2div
                                   //  if value is INT:
                                   //    value*4*(1/clock)*t2div
				   // for example a value of 30 and t2div
                                   // of 1 the high time is 12us
                                   // WARNING:  A value to high or low will
                                   //           prevent the output from
                                   //           changing.
  }

}
コード例 #3
0
void init()
{
	// Inicializa puertos
	set_tris_a(0b11111111);
	set_tris_b(trisB_value);

	// ***ADC***
	setup_port_a(sAN0);
	setup_adc(ADC_CLOCK_INTERNAL);
	set_adc_channel(0);
	setup_adc_ports(sAN0);
	setup_vref(VREF_HIGH | 8);
	
	// Seteo el Timer1 como fuente interna
	setup_timer_1(T1_INTERNAL | T1_DIV_BY_8);
	set_timer1(26786);
	
	// Interrupcion sobre el Timer1
	enable_interrupts(INT_TIMER1);

	// Seteo el pin RB0 - Sensor de ultrasonido :)
	ext_int_edge(L_TO_H);
	enable_interrupts(INT_EXT);

	// Habilito las interrupciones
	enable_interrupts(GLOBAL);
	
	// Variable para hacer el reset
	reset = false;

	// Apaga todos los sensores
	sensor1 = SENSOR_OFF;
	sensor2 = SENSOR_OFF;
	sensor3 = SENSOR_OFF;
	sensor4 = SENSOR_OFF;
	sensor5 = SENSOR_OFF;

	// Inicializa los valores
	values[0] = 0x0000;
	values[1] = 0x0000;
	values[2] = 0x0000;
	values[3] = 0x0000;
	values[4] = 0x0000;
	values[5] = 0x0000;

	// Muestras iniciales
	samples = SAMPLES_DEFAULT;
	
	// Inicializa la mascara -> todos habilitados (0x3F)
	sensorMask = DEFAULT_MASK;
	
	//Determina el estado actual
	state = STATE_FREE;
	
	// Sin lectura temprana
	readSensor = 0x00;
	bufferedReadSensor = 0x00;
	actalTO = 0x00;
	requestFrom = 0x00;
	bufferedFrom = 0x00;
	actalCmd = 0x00;
	requestCmd = 0x00;
	bufferedCmd = 0x00;

	alarmType = 0x00;
	triggerAlarm = 0;
#if TRIGGER_TYPE == SWITCH_SENSOR
	disable_interrupts(INT_EXT);
#endif

#if TRIGGER_TYPE == LED
	// TRIGGER como escritura
	bit_clear(trisB_value, 0);
	set_tris_b(trisB_value);
#endif

	return;	
}