Skip to content

Instantly share code, notes, and snippets.

@chukitow
Last active September 10, 2015 23:02
Show Gist options
  • Save chukitow/b826179c4ce92087bbfa to your computer and use it in GitHub Desktop.
Save chukitow/b826179c4ce92087bbfa to your computer and use it in GitHub Desktop.
#include <18F4550.h>
#device adc = 10
#fuses XT, PUT, NODEBUG , NOBROWNOUT , NOPROTECT , NOLVP
#use delay(clock=4000000)
#use rs232(baud=9600, bits = 8 , parity = N ,xmit=PIN_C6, rcv=PIN_C7)
void read_adc_chanel();
void evaluate_aceleration();
float adc_sensor_1 = 0;
float adc_sensor_2 = 0;
boolean trigger_timer0 = false;
long potency_wheel_1 = 1023;
long potency_wheel_2 = 1023;
float LIMIT_BLACK = 3.5;
#INT_TIMER0
void isr_timer(){
trigger_timer0 = true;
set_timer0(3036);
}
void main(){
set_tris_C(0xFF);
set_tris_B(0xFF);
enable_interrupts(GLOBAL);
enable_interrupts(INT_TIMER0);
setup_timer_0(RTCC_INTERNAL|RTCC_DIV_4);
set_timer0(3036);
setup_adc(ADC_CLOCK_INTERNAL);
setup_adc_ports(ALL_ANALOG);
setup_ccp1(CCP_PWM);
setup_ccp2(CCP_PWM);
setup_timer_2(T2_DIV_BY_4,249,1); //Onda de 4KHz
output_low(PIN_B1);
output_low(PIN_B3);
while(true){
set_pwm1_duty(potency_wheel_1);
set_pwm2_duty(potency_wheel_2);
if(trigger_timer0){
read_adc_chanel();
evaluate_aceleration();
}
}
}
void read_adc_chanel(){
set_adc_channel(0);
delay_ms(20);
adc_sensor_1 = (read_adc() * 0.004887);
set_adc_channel(1);
delay_ms(20);
adc_sensor_2 = (read_adc() * 0.004887);
trigger_timer0 = false;
}
void evaluate_aceleration(){
if(adc_sensor_1 >= LIMIT_BLACK && adc_sensor_2 >= LIMIT_BLACK){
potency_wheel_1 = 1023;
potency_wheel_2 = 1023;
}
else if(adc_sensor_1 < LIMIT_BLACK){
potency_wheel_1 = 1023;
potency_wheel_2 = 500;
}
else if(adc_sensor_2 < LIMIT_BLACK){
potency_wheel_1 = 500;
potency_wheel_2 = 1023;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment