testing

mail@pastecode.io avatar
unknown
plain_text
2 months ago
3.6 kB
2
Indexable
Never
#include <xc.h>
#include<stdio.h>
#include<stdlib.h>
#include <time.h>
#define _XTAL_FREQ 4000000
#pragma config OSC = INTIO67  //OSCILLATOR SELECTION BITS (INTERNAL OSCILLATOR BLOCK, PORT FUNCTION ON RA6 AND RA7)
#pragma config WDT = OFF      //Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit))
#pragma config PWRT = OFF     //Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON     //Brown-out Reset Enable bits (Brown-out Reset enabled in hardware only (SBOREN is disabled))
#pragma config PBADEN = OFF   //PORTB A/D Enable bit (PORTB<4:0> pins are configured as digital I/O on Reset)
#pragma config LVP = OFF      //Single-Supply ICSP Enable bit (Single-Supply ICSP disabled)
#pragma config CPD = OFF      //Data EEPROM Code Protection bit (Data EEPROM not code-protected)

int id;

void banana(void)
{
    TRISC = 0x00;

    int note2freq[] = {523,587,659,698,783,880,987,1046};

    int i=0;

        long f = note2freq[id]*3;
        long p = 100000/f;
        long dur = 0;
        while(1){
            LATCbits.LC0 = 1;
            i=p;
            while(i--){}
            LATCbits.LC0 = 0;
            i=p;
            while(i--){}
            dur += p;
            if(dur>7000) {break;}
        }
    // for(int n = 0; n<8; n+=1){
    //     long f = note2freq[n]*3;
    //     long p = 100000/f;
    //     long dur = 0;
    //     while(1){
    //         LATCbits.LC0 = 1;
    //         i=p;
    //         while(i--){}
    //         LATCbits.LC0 = 0;
    //         i=p;
    //         while(i--){}
    //         dur += p;
    //         if(dur>7000) {break;}
    //     }
    // }

    return;
}

void __interrupt(high_priority)H_ISR(){

    //step4
    int value = ADRES;

    //do things
    if (value<975){
        if (id == 0)    LATD = 0b00000001;
        else if (id == 1)   LATD = 0b00000010;
        else if (id == 2)   LATD = 0b00000100;
        else if (id == 3)   LATD = 0b00001000;
        else if (id == 4)   LATD = 0b00010000;
        else if (id == 5)   LATD = 0b00100000;
        else if (id == 6)   LATD = 0b01000000;
        else if (id == 7)   LATD = 0b10000000;
    }else{
        LATD = 0;
    }
//    if(value >= 975)
//        LATD = 0b00001;
//    else if(value <= 975 & value >= 950)
//        LATD = 0b00010;
//    else if(value <= 950 & value >= 925)
//        LATD = 0b00100;
//    else if(value <= 925 & value >= 900)
//        LATD = 0b01000;
//    else
//        LATD = 0;
//
    //clear flag bit
    PIR1bits.ADIF = 0;
    __delay_us(20);
    return;
}

void delay(unsigned int itime){
    unsigned int i, j;
    for(i = 0; i < itime; i++){
        for(j = 0; j < 255; j++){
        }
    }
}

void main(void)
{
    //configure OSC and port
    OSCCONbits.IRCF = 0b111; //1MHz

    TRISD = 0x00;
    TRISA = 0x0F;       //analog input port
    TRISAbits.RA0 = 1;
    //step1
    ADCON1bits.VCFG0 = 0;
    ADCON1bits.VCFG1 = 0;
    ADCON1bits.PCFG = 0b1110; //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;
    ADCON2bits.ADFM = 1;    //left justified

    LATD = 0x00;

    //step2
    PIE1bits.ADIE = 1;
    PIR1bits.ADIF = 0;
    INTCONbits.PEIE = 1;
    INTCONbits.GIE = 1;

    //step3

    while(1){
        for(int i = 0; i < 3; i++){
            id = i;
            ADCON0bits.CHS = (unsigned char)i;
            ADCON0bits.GO = 1;

        }
    }
    return;
}
Leave a Comment