Final main
unknown
plain_text
2 years ago
3.2 kB
12
Indexable
#include "setting_hardaware/setting.h"
#include <stdlib.h>
#include "stdio.h"
#include "string.h"
#include <time.h>
#include <pic18f4520.h>
// using namespace std;
#define _XTAL_FREQ 4000000
int id;
int mode = 2;
char str[20];
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;}
}
return;
}
void print_button(unsigned char c) {
char message[] = "Enter your choice:";
char high[] = "High mode";
char medium[] = "Medium mode";
char low[] = "Low mode";
int i = 0;
UART_Write('\n');
UART_Write('\r');
if(c == 'a'){
while (high[i] != '\0') {
UART_Write(high[i]);
i++;
}
}
else if(c == 'b'){
while (medium[i] != '\0') {
UART_Write(medium[i]);
i++;
}
}
else if(c == 'c'){
while (low[i] != '\0') {
UART_Write(low[i]);
i++;
}
}
i = 0;
UART_Write('\n');
UART_Write('\r');
while (message[i] != '\0') {
UART_Write(message[i]);
i++;
}
}
void button(void){
if (PORTBbits.RB1 == 0){
mode++;
mode %= 3;
if(mode == 2){
print_button('a');
}
else if(mode == 1){
print_button('b');
}
else if(mode == 0){
print_button('c');
}
__delay_us(800);
return;
}
return;
}
void print_initial(void) {
char message[] = "Choose mode\n\r(a) high frequency\n\r(b) medium frequency\n\r(c) low frequency\n\rEnter your choice:";
int i = 0;
while (message[i] != '\0') {
UART_Write(message[i]);
i++;
}
}
void main(void)
{
SYSTEM_Initialize() ;
OSCCONbits.IRCF = 0b111; //1MHz
TRISC = 0x00;
TRISCbits.TRISC6 = 1;
TRISCbits.TRISC7 = 1;
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;
IPR1bits.ADIP = 0;
INTCONbits.PEIE = 1;
INTCONbits.GIE = 1;
print_initial();
while(1){
for(int i = 0; i < 5; i++){
id = i;
ADCON0bits.CHS = (unsigned char)i;
ADCON0bits.GO = 1;
__delay_us(50);
}
button();
}
return;
}
void __interrupt(low_priority) Lo_ISR(void)
{
int value = ADRES;
PIR1bits.ADIF = 0;
if (value<975) banana();
__delay_us(20);
return;
}Editor is loading...
Leave a Comment