Advertisement
JosepRivaille

CI - PreviS10

May 11th, 2016
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.13 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. }
  36.  
  37. void interrupt change_AD(void)
  38. {
  39.    if (INTCONbits.TMR0IE == 1 && INTCONbits.TMR0IF == 1) {
  40.       ADCON0bits.GO = 1;
  41.       while (GO == 1);
  42.       if (select == 1) {
  43.      select = 2;
  44.      state_changed = 1;
  45.      ADCON0 = 0x05;
  46.       }
  47.       else if (select == 2) {
  48.      select = 1;
  49.      state_changed = 2;
  50.      ADCON0 = 0x01;
  51.       }
  52.    }
  53.    INTCONbits.TMR0IF = 0;
  54.    return;
  55. }
  56.  
  57. void main(void)
  58. {
  59.    configAD();
  60.    GLCDinit();
  61.    config_interrupts();
  62.    config_timer0();
  63.    setStartLine(0);
  64.    clearGLCD(0, 7, 0, 127);
  65.    writeTxt(0, 0, "RA0 = ");
  66.    writeTxt(2, 0, "RA1 = ");
  67.    while (1) {
  68.       if (state_changed == 1) {
  69.      float aux = (ADRESL >> 6) + (ADRESH << 2);
  70.      aux = (aux*5)/1023;
  71.      int integer_part = aux;
  72.      int decimal_part = (aux * 100);
  73.      decimal_part = decimal_part%100;
  74.      clearGLCD(0, 0, 36, 127);
  75.      writeNum(0, 36, integer_part);
  76.      putch(0, 42, '.');
  77.      writeNum(0, 48, decimal_part);
  78.      state_changed = 0;
  79.      INTCON = 0b10100111;
  80.       }
  81.       else if (state_changed == 2) {
  82.      float aux = (ADRESL >> 6) + (ADRESH << 2);
  83.      aux = (aux*5)/1023;
  84.      int integer_part = aux;
  85.      int decimal_part = aux * 100;
  86.      decimal_part = decimal_part%100;
  87.      clearGLCD(2, 2, 36, 127);
  88.      writeNum(2, 36, integer_part);
  89.      putch(2, 42, '.');
  90.      writeNum(2, 48, decimal_part);
  91.      state_changed = 0;
  92.      INTCON = 0b10100111;
  93.       }
  94.    }
  95. }
  96.  
  97. //JosepRivaille
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement