Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <xc.h>
- #include <stdlib.h>
- #include <string.h>
- #include "config.h"
- #include "GLCD.h"
- #define _XTAL_FREQ 8000000
- volatile unsigned int state_changed = 0, select = 1;
- void configAD()
- {
- TRISA = 0xFF;
- TRISB = 0x00;
- TRISD = 0x00;
- ADCON0 = 0x01; // Selects Channel AN0
- ADCON1 = 0x0D; // RA0 and RA1 set as analog
- ADCON2 = 0x24; // Left justified, TAD = 8, FOSC/4
- }
- void config_interrupts()
- {
- INTCONbits.GIE = 1; // Enable global interrupts
- INTCONbits.PEIE = 1; // Enable peripheral interrupts
- INTCON = 0b10100111;
- RCONbits.IPEN = 1;
- }
- void config_timer0()
- {
- T0CONbits.TMR0ON = 1; //Enable TMR0
- T0CONbits.T08BIT = 0; //16-bit
- T0CONbits.T0SE = 0; //On rising edge
- T0CONbits.PSA = 0; //We choose prescale value
- T0CON &= 0b111;
- }
- void interrupt change_AD(void)
- {
- if (INTCONbits.TMR0IE == 1 && INTCONbits.TMR0IF == 1) {
- ADCON0bits.GO = 1;
- while (GO == 1);
- if (select == 1) {
- select = 2;
- state_changed = 1;
- ADCON0 = 0x05;
- }
- else if (select == 2) {
- select = 1;
- state_changed = 2;
- ADCON0 = 0x01;
- }
- }
- INTCONbits.TMR0IF = 0;
- return;
- }
- void write_base_text()
- {
- clearGLCD(0, 7, 0, 127);
- writeTxt(0, 0, "RA0 = ");
- writeTxt(2, 0, "RA1 = ");
- putch(0, 42, '.');
- putch(0, 60, 'V');
- putch(2, 42, '.');
- putch(2, 60, 'V');
- writeTxt(4, 0, "Angle = ");
- writeTxt(6, 0, "Distance = ");
- }
- void main(void)
- {
- configAD();
- GLCDinit();
- config_interrupts();
- config_timer0();
- setStartLine(0);
- write_base_text();
- while (1) {
- if (state_changed == 1) {
- float aux = (ADRESL >> 6) + (ADRESH << 2);
- aux = (aux*5)/1023;
- int integer_part = aux;
- int decimal_part = (aux * 100);
- decimal_part = decimal_part%100;
- clearGLCD(0, 0, 36, 41);
- clearGLCD(0, 0, 48, 59);
- writeNum(0, 36, integer_part);
- writeNum(0, 48, decimal_part);
- //Angle converter **********//
- if (aux < 0.66 || aux > 4.34) writeTxt(4, 48, "Out of range!");
- else {
- float angle = ((aux - 0.66)*180)/3.67;
- integer_part = angle;
- clearGLCD(4, 4, 48, 127);
- writeNum(4, 48, integer_part);
- //Draw your own º symbol instead of 127
- if (integer_part < 10) putch(4, 54, 127);
- else if (integer_part < 100) putch(4, 60, 127);
- else putch(4, 66, 127);
- }
- //**************************//
- state_changed = 0;
- INTCON = 0b10100111;
- }
- else if (state_changed == 2) {
- float aux = (ADRESL >> 6) + (ADRESH << 2);
- aux = (aux*5)/1023;
- int integer_part = aux;
- int decimal_part = aux * 100;
- decimal_part = decimal_part%100;
- clearGLCD(2, 2, 36, 41);
- clearGLCD(2, 2, 48, 59);
- writeNum(2, 36, integer_part);
- writeNum(2, 48, decimal_part);
- // Distance converter ******//
- float distance = 4.114619826424367*(aux*aux) - 20.619066532589823*aux + 30.114823444820473;
- integer_part = distance;
- decimal_part = distance * 100;
- decimal_part = decimal_part%100;
- if (integer_part < 10) {
- clearGLCD(6, 6, 66, 127);
- writeNum(6, 66, integer_part);
- putch(6, 72, '.');
- writeNum(6, 78, decimal_part);
- writeTxt(6, 84, "cm");
- }
- else {
- clearGLCD(6, 6, 66, 127);
- writeNum(6, 66, integer_part);
- putch(6, 78, '.');
- writeNum(6, 84, decimal_part);
- writeTxt(6, 90, "cm");
- }
- //**************************//
- state_changed = 0;
- INTCON = 0b10100111;
- }
- }
- }
- //JosepRivaille
Add Comment
Please, Sign In to add comment