Advertisement
upup2544

embedlab-15

Apr 6th, 2021
2,200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pic 16 4.08 KB | None | 0 0
  1. #include <16F886.h>
  2. #device ADC=10 *=16
  3. #device PASS_STRINGS=IN_RAM
  4. #include <math.h>
  5.  
  6. #FUSES NOWDT                    //No Watch Dog Timer
  7. #FUSES PUT                      //Power Up Timer
  8. #FUSES NOMCLR                   //Master Clear pin not enabled
  9. #FUSES NOPROTECT                //Code not protected from reading
  10. #FUSES NOCPD                    //No EE protection
  11. #FUSES BROWNOUT                 //Brownout reset
  12. #FUSES IESO                     //Internal External Switch Over mode enabled
  13. #FUSES FCMEN                    //Fail-safe clock monitor enabled
  14. #FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
  15. #FUSES NODEBUG                  //No Debug mode for ICD
  16. #FUSES NOWRT                    //Program memory not write protected
  17. #FUSES BORV40                   //Brownout reset at 4.0V
  18. #FUSES RESERVED                 //Used to set the reserved FUSE bits
  19. #FUSES INTRC_IO
  20.  
  21. #use delay(clock=8M)
  22.  
  23. #use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8)   // setup UART
  24. #use i2c(MASTER, I2C1, FORCE_HW)                               // configure the i2c port
  25.  
  26. #define RUN_BUTTON   PIN_B7
  27. #define HC_SR04_TRIGGER_PIN     PIN_C1
  28. #define HC_SR04_ECHO_PIN        PIN_C2
  29. #define Left_button PIN_B6
  30. #define Right_button PIN_B5
  31. #define Start_button PIN_B3
  32. // Right_motor PIN_B2,PIN_A7
  33. // Left_motor PIN_B1,PIN_A6
  34.  
  35.  
  36. unsigned int16 rise_cnt, fall_cnt, pulse_width_cnt;
  37. int1 isRisingEdge;
  38. int16 interruptCounter = 0;
  39. int16 dutyCycle = 0;
  40.  
  41. #int_CCP1
  42. void CCP1_isr(void)
  43. {
  44.     if (isRisingEdge)
  45.     {
  46.         isRisingEdge = 0;
  47.         rise_cnt = CCP_1;
  48.         setup_ccp1(CCP_CAPTURE_FE);
  49.     }
  50.     else
  51.     {
  52.         fall_cnt = CCP_1;
  53.         if (fall_cnt >= rise_cnt)
  54.             pulse_width_cnt = fall_cnt - rise_cnt;
  55.         else
  56.             pulse_width_cnt = 65536 + fall_cnt - rise_cnt;
  57.  
  58.         isRisingEdge = 1;
  59.         setup_ccp1(CCP_CAPTURE_RE);
  60.     }
  61. }
  62.  
  63. //PWM
  64. #INT_TIMER1
  65.   void timer1_isr(){
  66.    set_timer1(62026);    
  67.    interruptCounter++;
  68.    if (interruptCounter == dutyCycle){
  69.        output_low(PIN_B4);
  70.    }
  71.    if (interruptCounter == 500) {
  72.       interruptCounter = 0;
  73.       if (dutyCycle > 0) {output_high(PIN_B4);}
  74.    }
  75. }
  76.  
  77.  
  78. unsigned int16 HC_SR04_get_distance()
  79. {
  80.     unsigned int16 value;
  81.     value = 0;
  82.     pulse_width_cnt = 0;
  83.     output_low(HC_SR04_TRIGGER_PIN);
  84.     delay_us(10);
  85.     set_timer1(0);
  86.     isRisingEdge = 1;
  87.     setup_ccp1(CCP_CAPTURE_RE);
  88.  
  89.     // trigger measurement
  90.     output_high(HC_SR04_TRIGGER_PIN);
  91.     delay_us(10);
  92.     output_low(HC_SR04_TRIGGER_PIN);
  93.  
  94.     delay_ms(40); // allow CCP interrupt to see both edges of max pulse=38ms
  95.  
  96.     // pulse_width_cnt is in 1us units
  97.     value = pulse_width_cnt / 58L; // converts to cm unit
  98.    
  99.     return value;
  100. }
  101.  
  102. void HC_SR04_setup()
  103. {
  104.     setup_timer_1(T1_INTERNAL | T1_DIV_BY_2); //start 1us per inc timer
  105.     enable_interrupts(INT_CCP1);
  106.     enable_interrupts(GLOBAL);
  107. }
  108.  
  109. int16 calc(int8 distance){
  110.    int16 u=225;
  111.    float error=0.17;
  112.    int16 i=distance;
  113.    if(distance<20){
  114.       error = 0.155;
  115.    }
  116.    u+=i+(i*error);
  117.    return u;
  118. }
  119.  
  120.  
  121. void main(){
  122.  
  123.    // ADC SETUP
  124.    //setup_adc_ports(sAN0|sAN2);   // setup PIN A0 and A2 as analog input  
  125.    //setup_adc(ADC_CLOCK_INTERNAL);
  126.    HC_SR04_setup();
  127.    set_timer1(62026);
  128.    output_high(PIN_A2);
  129.   // enable_interrupts(INT_TIMER1);
  130.    while(1){
  131.    printf("distance: %lu\n", HC_SR04_get_distance());
  132.    if(input(Left_button)){
  133.      // printf("Left: %lu\n", HC_SR04_get_distance());
  134.       output_toggle(PIN_B1);
  135.       output_toggle(PIN_A6);
  136.       delay_ms(500);
  137.    }else if(input(Right_button)){
  138.      // printf("Right: %lu\n", HC_SR04_get_distance());
  139.       output_toggle(PIN_B2);
  140.       output_toggle(PIN_A7);
  141.       delay_ms(500);
  142.    }
  143.    if(input(Start_button)){
  144.       printf("Start System !!\n");
  145.       dutyCycle=1;
  146.       enable_interrupts(INT_TIMER1);
  147.       delay_ms(10000);
  148.       dutyCycle=calc(HC_SR04_get_distance());
  149.       enable_interrupts(INT_TIMER1);
  150.       printf("Press Start: %lu\n", dutyCycle);
  151.    }
  152.  
  153.    delay_ms(500);
  154.    }
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement