JosepRivaille

CI - LabS10

May 11th, 2016
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.44 KB | None | 0 0
  1. #include <xc.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include "config.h"
  5. #include "GLCD.h"
  6. #define _XTAL_FREQ 8000000
  7.  
  8. volatile unsigned int state_changed = 0, select = 1;
  9.  
  10. void configAD()
  11. {
  12.    TRISA = 0xFF;
  13.    TRISB = 0x00;
  14.    TRISD = 0x00;
  15.    
  16.    ADCON0 = 0x01; // Selects Channel AN0
  17.    ADCON1 = 0x0D; // RA0 and RA1 set as analog
  18.    ADCON2 = 0x24; // Left justified, TAD = 8, FOSC/4
  19. }  
  20.  
  21. void config_interrupts()
  22. {
  23.    INTCONbits.GIE = 1; // Enable global interrupts
  24.    INTCONbits.PEIE = 1; // Enable peripheral interrupts
  25.    INTCON = 0b10100111;
  26.    RCONbits.IPEN = 1;
  27. }
  28.  
  29. void config_timer0()
  30. {
  31.    T0CONbits.TMR0ON = 1; //Enable TMR0
  32.    T0CONbits.T08BIT = 0; //16-bit
  33.    T0CONbits.T0SE = 0;   //On rising edge
  34.    T0CONbits.PSA = 0;    //We choose prescale value
  35.    T0CON &= 0b111;
  36. }
  37.  
  38. void interrupt change_AD(void)
  39. {
  40.    if (INTCONbits.TMR0IE == 1 && INTCONbits.TMR0IF == 1) {
  41.       ADCON0bits.GO = 1;
  42.       while (GO == 1);
  43.       if (select == 1) {
  44.      select = 2;
  45.      state_changed = 1;
  46.      ADCON0 = 0x05;
  47.       }
  48.       else if (select == 2) {
  49.      select = 1;
  50.      state_changed = 2;
  51.      ADCON0 = 0x01;
  52.       }
  53.    }
  54.    INTCONbits.TMR0IF = 0;
  55.    return;
  56. }
  57.  
  58. void write_base_text()
  59. {
  60.    clearGLCD(0, 7, 0, 127);
  61.    writeTxt(0, 0, "RA0 = ");
  62.    writeTxt(2, 0, "RA1 = ");
  63.    putch(0, 42, '.');
  64.    putch(0, 60, 'V');
  65.    putch(2, 42, '.');
  66.    putch(2, 60, 'V');
  67.    writeTxt(4, 0, "Angle = ");
  68.    writeTxt(6, 0, "Distance = ");
  69. }
  70.  
  71. void main(void)
  72. {
  73.    configAD();
  74.    GLCDinit();
  75.    config_interrupts();
  76.    config_timer0();
  77.    setStartLine(0);
  78.    write_base_text();
  79.    while (1) {
  80.       if (state_changed == 1) {
  81.      float aux = (ADRESL >> 6) + (ADRESH << 2);
  82.      aux = (aux*5)/1023;
  83.      int integer_part = aux;
  84.      int decimal_part = (aux * 100);
  85.      decimal_part = decimal_part%100;
  86.      clearGLCD(0, 0, 36, 41);
  87.      clearGLCD(0, 0, 48, 59);
  88.      writeNum(0, 36, integer_part);
  89.      writeNum(0, 48, decimal_part);
  90.      //Angle converter **********//
  91.      if (aux < 0.66 || aux > 4.34) writeTxt(4, 48, "Out of range!");
  92.      else {
  93.         float angle = ((aux - 0.66)*180)/3.67;
  94.         integer_part = angle;
  95.         clearGLCD(4, 4, 48, 127);
  96.         writeNum(4, 48, integer_part);
  97.         //Draw your own º symbol instead of 127
  98.         if (integer_part < 10) putch(4, 54, 127);
  99.         else if (integer_part < 100) putch(4, 60, 127);
  100.         else putch(4, 66, 127);
  101.      }
  102.      //**************************//
  103.      state_changed = 0;
  104.      INTCON = 0b10100111;
  105.       }
  106.       else if (state_changed == 2) {
  107.      float aux = (ADRESL >> 6) + (ADRESH << 2);
  108.      aux = (aux*5)/1023;
  109.      int integer_part = aux;
  110.      int decimal_part = aux * 100;
  111.      decimal_part = decimal_part%100;
  112.      clearGLCD(2, 2, 36, 41);
  113.      clearGLCD(2, 2, 48, 59);
  114.      writeNum(2, 36, integer_part);
  115.      writeNum(2, 48, decimal_part);
  116.      // Distance converter ******//
  117.      float distance = 4.114619826424367*(aux*aux) - 20.619066532589823*aux + 30.114823444820473;
  118.      integer_part = distance;
  119.      decimal_part = distance * 100;
  120.      decimal_part = decimal_part%100;
  121.      if (integer_part < 10) {
  122.         clearGLCD(6, 6, 66, 127);
  123.         writeNum(6, 66, integer_part);
  124.         putch(6, 72, '.');
  125.         writeNum(6, 78, decimal_part);
  126.         writeTxt(6, 84, "cm");
  127.      }
  128.      else {
  129.         clearGLCD(6, 6, 66, 127);
  130.         writeNum(6, 66, integer_part);
  131.         putch(6, 78, '.');
  132.         writeNum(6, 84, decimal_part);
  133.         writeTxt(6, 90, "cm");
  134.      }
  135.      //**************************//
  136.      state_changed = 0;
  137.      INTCON = 0b10100111;
  138.       }
  139.    }
  140. }
  141.  
  142. //JosepRivaille
Add Comment
Please, Sign In to add comment