Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <16F886.h>
- #device ADC=10 *=16
- #device PASS_STRINGS=IN_RAM
- #include <math.h>
- #FUSES NOWDT //No Watch Dog Timer
- #FUSES PUT //Power Up Timer
- #FUSES NOMCLR //Master Clear pin not enabled
- #FUSES NOPROTECT //Code not protected from reading
- #FUSES NOCPD //No EE protection
- #FUSES BROWNOUT //Brownout reset
- #FUSES IESO //Internal External Switch Over mode enabled
- #FUSES FCMEN //Fail-safe clock monitor enabled
- #FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
- #FUSES NODEBUG //No Debug mode for ICD
- #FUSES NOWRT //Program memory not write protected
- #FUSES BORV40 //Brownout reset at 4.0V
- #FUSES RESERVED //Used to set the reserved FUSE bits
- #FUSES INTRC_IO
- #use delay(clock=8M)
- #use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8) // setup UART
- #use i2c(MASTER, I2C1, FORCE_HW) // configure the i2c port
- #define RUN_BUTTON PIN_B7
- #define HC_SR04_TRIGGER_PIN PIN_C1
- #define HC_SR04_ECHO_PIN PIN_C2
- #define Left_button PIN_B6
- #define Right_button PIN_B5
- #define Start_button PIN_B3
- // Right_motor PIN_B2,PIN_A7
- // Left_motor PIN_B1,PIN_A6
- unsigned int16 rise_cnt, fall_cnt, pulse_width_cnt;
- int1 isRisingEdge;
- int16 interruptCounter = 0;
- int16 dutyCycle = 0;
- #int_CCP1
- void CCP1_isr(void)
- {
- if (isRisingEdge)
- {
- isRisingEdge = 0;
- rise_cnt = CCP_1;
- setup_ccp1(CCP_CAPTURE_FE);
- }
- else
- {
- fall_cnt = CCP_1;
- if (fall_cnt >= rise_cnt)
- pulse_width_cnt = fall_cnt - rise_cnt;
- else
- pulse_width_cnt = 65536 + fall_cnt - rise_cnt;
- isRisingEdge = 1;
- setup_ccp1(CCP_CAPTURE_RE);
- }
- }
- //PWM
- #INT_TIMER1
- void timer1_isr(){
- set_timer1(62026);
- interruptCounter++;
- if (interruptCounter == dutyCycle){
- output_low(PIN_B4);
- }
- if (interruptCounter == 500) {
- interruptCounter = 0;
- if (dutyCycle > 0) {output_high(PIN_B4);}
- }
- }
- unsigned int16 HC_SR04_get_distance()
- {
- unsigned int16 value;
- value = 0;
- pulse_width_cnt = 0;
- output_low(HC_SR04_TRIGGER_PIN);
- delay_us(10);
- set_timer1(0);
- isRisingEdge = 1;
- setup_ccp1(CCP_CAPTURE_RE);
- // trigger measurement
- output_high(HC_SR04_TRIGGER_PIN);
- delay_us(10);
- output_low(HC_SR04_TRIGGER_PIN);
- delay_ms(40); // allow CCP interrupt to see both edges of max pulse=38ms
- // pulse_width_cnt is in 1us units
- value = pulse_width_cnt / 58L; // converts to cm unit
- return value;
- }
- void HC_SR04_setup()
- {
- setup_timer_1(T1_INTERNAL | T1_DIV_BY_2); //start 1us per inc timer
- enable_interrupts(INT_CCP1);
- enable_interrupts(GLOBAL);
- }
- int16 calc(int8 distance){
- int16 u=225;
- float error=0.17;
- int16 i=distance;
- if(distance<20){
- error = 0.155;
- }
- u+=i+(i*error);
- return u;
- }
- void main(){
- // ADC SETUP
- //setup_adc_ports(sAN0|sAN2); // setup PIN A0 and A2 as analog input
- //setup_adc(ADC_CLOCK_INTERNAL);
- HC_SR04_setup();
- set_timer1(62026);
- output_high(PIN_A2);
- // enable_interrupts(INT_TIMER1);
- while(1){
- printf("distance: %lu\n", HC_SR04_get_distance());
- if(input(Left_button)){
- // printf("Left: %lu\n", HC_SR04_get_distance());
- output_toggle(PIN_B1);
- output_toggle(PIN_A6);
- delay_ms(500);
- }else if(input(Right_button)){
- // printf("Right: %lu\n", HC_SR04_get_distance());
- output_toggle(PIN_B2);
- output_toggle(PIN_A7);
- delay_ms(500);
- }
- if(input(Start_button)){
- printf("Start System !!\n");
- dutyCycle=1;
- enable_interrupts(INT_TIMER1);
- delay_ms(10000);
- dutyCycle=calc(HC_SR04_get_distance());
- enable_interrupts(INT_TIMER1);
- printf("Press Start: %lu\n", dutyCycle);
- }
- delay_ms(500);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement