Untitled
unknown
plain_text
2 years ago
3.5 kB
9
Indexable
#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;
int mode = 2;
void banana(void)
{
int note2freq[] = {523,587,659,698,783,880,987,1046};
int i=0;
long f = note2freq[id]*(mode+1);
long p = 100000/f;
long dur = 0;
while(1){
LATCbits.LC1 = 1;
i=p;
while(i--){}
LATCbits.LC1 = 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 button(void){
if (PORTBbits.RB1 == 0){
mode++;
mode %= 3;
__delay_us(800);
return;
}
return;
}
//void __interrupt(high_priority)H_ISR(void){
// high_p();
// return;
//}
void __interrupt(high_priority)H_ISR(void){
//step4
int value = ADRES;
PIR1bits.ADIF = 0;
//do things
if (value<975) banana();
//clear flag bit
__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
TRISC = 0x00;
TRISB = 0x0F;
TRISA = 0xFF; //analog input port
//step1
ADCON1bits.VCFG0 = 0;
ADCON1bits.VCFG1 = 0;
ADCON1bits.PCFG = 0b0110; //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
//step2
PIE1bits.ADIE = 1;
PIR1bits.ADIF = 0;
INTCONbits.PEIE = 1;
INTCONbits.GIE = 1;
// RCONbits.IPEN = 1;
// INTCON3bits.INT1IF = 0;
// INTCONbits.GIE = 1;
// INTCON3bits.INT1IE = 1;
//
// IPR1bits.ADIP = 0;
// INTCON3bits.INT1IP = 1;
// INTCON2bits.RBIP = 1;
//step3
while(1){
for(int i = 0; i < 5; i++){
id = i;
ADCON0bits.CHS = (unsigned char)i;
ADCON0bits.GO = 1;
__delay_us(50);
}
button();
}
return;
}
Editor is loading...
Leave a Comment