Untitled

mail@pastecode.io avatar
unknown
plain_text
2 months ago
3.6 kB
2
Indexable
Never
#include "setting.h"
#include <stdlib.h>

#include <pic18f4520.h>
#include "stdio.h"
#include "string.h"
// using namespace std;
#define _XTAL_FREQ 100000
char str[20];
int now = 1;
int counter = 0;

void Mode1(){   // Todo : Mode1 
    return ;
}
void Mode2(){   // Todo : Mode2 
    return ;
}


void main(void) 
{
    
   // INTERRUPT_Initialize();
    UART_Initialize();
    TRISA = 0xff;		// Set as input port
    ADCON1 = 0x0f;  	// Ref vtg is VDD & Configure pin as analog pin 
    // ADCON2 = 0x92;  	
    ADFM = 1 ;          // Right Justifie
    ADCON2bits.ADCS = 7; // 
    ADRESH=0;  			// Flush ADC output Register
    ADRESL=0;  
    
    TRISAbits.RA0 = 1;       //analog input port
    
    
    
    //step1
    ADCON1bits.VCFG0 = 0;
    ADCON1bits.VCFG1 = 0;
    ADCON1bits.PCFG = 0b1100; //AN0 ?analog input,???? digital
    ADCON0bits.CHS = 0b0000;  //AN0 ?? analog input
    ADCON2bits.ADCS = 0b000;  //????000(1Mhz < 2.86Mhz)
    ADCON2bits.ACQT = 0b001;  //Tad = 2 us acquisition time?2Tad = 4 > 2.4
    ADCON0bits.ADON = 1; //enable A/D
    ADCON2bits.ADFM = 0;    //left justified 
    
    OSCCONbits.IRCF = 0b100;
    
    //step2
    PIE1bits.ADIE = 1; //enable A/D interrupt
    PIR1bits.ADIF = 0; //clear A/D interrupt bit
    INTCONbits.PEIE = 1; //enable peripheral interrupt
    INTCONbits.GIE = 1; //enable interrupt
    
    INTCONbits.GIEH = 1;    //enable high priority interrupt
    INTCONbits.GIEL = 1; 

    //step3
    ADCON0bits.GO = 1; //set go/done bit
    
    
    //TRISBbits.RB0 = 1;
    
    //T1CON = 0x30; // pre = 8
    //PIE1bits.TMR1IE = 1;
    //PIR1bits.TMR1IF = 0;
    
    
    RCONbits.IPEN = 0;
    INTCONbits.INT0IE = 1;
    INTCONbits.INT0IF = 0;
    INTCONbits.GIE = 1;
    
    
    while(1) {
        
         
    }
    return;
}

int reset = 1;
void __interrupt(high_priority) Hi_ISR(void)
{
    //dINTCONbits.GIE = 0;
    
    int value = (ADRESH<<2) | (ADRESL>>6);
    
    ADCON0bits.CHS = ADCON0bits.CHS==0 ? 1 : 0;
//    if (ADCON0bits.CHS == 0b0000){
//        ADCON0bits.CHS = 0b0001;
//        now = 2;
//    }
//    else{
//        ADCON0bits.CHS = 0b0000;
//        now = 1;
//    }
    
    if (value > 50 && reset){
//        char c2[10] ;
//        sprintf(c2, "  %d \r\n", value);
//        c2[0] = '0'+ADCON0bits.CHS;
        
        if (ADCON0bits.CHS == 0)
        {
            UART_Write_Text("j");
            __delay_ms(50);
        }
        else
        {
            UART_Write_Text("k");
            __delay_ms(50);
        }

        reset = 0;
        //UART_Write_Text(c2);
//        if (now == 1){
//            char c2[10] ;
//            sprintf(c2, "1 %d \r\n", value);
//
//            UART_Write_Text(c2);
//        }
//        else{
//            char c2[10] ;
//            sprintf(c2, "2 %d \r\n", value);
//
//            UART_Write_Text(c2);
//            
//        }
        
    }
    reset = value<50 ? 1 : 0;
    //char cx = '0' + reset;
    //UART_Write(cx);
    //UART_Write_Text("\r\n");
    
    
    //clear flag bit
    PIR1bits.ADIF = 0;
    
    //step5 & go back step3
    __delay_ms(1);
    
    ADCON0bits.GO = 1;
    
    /*if (PIR1bits.TMR1IF == 1){
        
        if (LATDbits.LATD0 == 0){
            LATDbits.LATD0 =1 ;
        }
        else{
            LATDbits.LATD0 =0;
        }
        
        PIR1bits.TMR1IF = 0; 
    }*/
    //INTCONbits.GIE = 1;
    return ;

}
Leave a Comment