void _int_(27) isr_adc(void){ AD1CON1bits.ASAM = 1; // Start conversion delay(10); if(j++ == 20){ sum = 0; V = 0; AD1CON1bits.ASAM = 1; // Start conversion while( IFS1bits.AD1IF == 0 ); // Wait while conversion not done int *p = (int *)(&ADC1BUF0); for( i = 0; i < 4; i++ ) { sum += p[i*4]; printf("%d\n", p[i*4]); } V=((sum/4)*50+511)/1023; j = 0; } send2displays(toBcd(V)); IFS1bits.AD1IF = 0; }
int main(void){ //ADC TRISBbits.TRISB14 = 1; AD1CON1bits.SSRC = 7; AD1CON1bits.CLRASAM = 1; AD1CON3bits.SAMC = 16; AD1PCFGbits.PCFG4 = 0; AD1CHSbits.CH0SA = 14; AD1CON2bits.SMPI = 3; AD1CON1bits.ON = 1; AD1CON1bits.ASAM = 1; while(1){ AD1CON1bits.ASAM = 1; // Start conversion while(IFS1bits.AD1IF == 0 );// Wait while conversion not done (AD1IF == 0) delay(7); if(j++ == 57) { int *p = (int *)(&ADC1BUF0); for( i = 0; i < 16; i++ ){ sum += p[i*4]; } V=((sum/4)*33+511)/1023; printf("%d\n", V); j = 0; sum = 0; } send2displays(toBcd(V)); // Send voltage value to displays }; }
int main(void){ int v=0,i=0; TRISE = TRISE | 0xF0; TRISBbits.TRISB14=1; AD1PCFGbits.PCFG14=0; AD1CON1bits.SSRC = 7; AD1CON1bits.CLRASAM = 1; AD1CON3bits.SAMC = 16; AD1CON2bits.SMPI = 0; AD1CHSbits.CH0SA = 14; AD1CON1bits.ON = 1; while(1){ AD1CON1bits.ASAM = 1; // Start conversion while( IFS1bits.AD1IF == 0 ); // Wait while conversion not done IFS1bits.AD1IF = 1; adc_val=ADC1BUF0; v=(((adc_val*66)+33)/1024)-9; printf("test: %d\n", v); i=0; if(v<0){ do{ delay(5); send2displays(toBcd((char)v)); }while(++i <4); delay(500); LATBbits.LATB8=0; LATBbits.LATB9=0; delay(500); }else{ do{ delay(5); send2displays(toBcd((char)v)); }while(++i <4); } } return 0; }
void _int_(24) isr_uart1(void){ // esta função é chamada quer seja pela receção, quer pela transmissão if(IFS0bits.U1RXIF){ char c = U1RXREG; if(c == 'A'){ AD1CON1bits.ASAM = 1; }else if( c == 'D'){ T4CONbits.TON = 1; x = V; }else if( c == 'E'){ putc('0' + ((toBcd(V) & 0xF0)>>4)); putc('0' + (toBcd(V) & 0x0F)); }else if( c == 'P'){
// Timer 3 void _int_(12) isr_T3(void) { unsigned char val = toBcd(value2displays); // Send "value2display" global variable to displays send2displays(val); if (readMode() != 2) send2dots(0x0010); // Send "value2displays" global variable to PC through UART1 if (counter++ >= 99) { putc(((val & 0x0F0) >> 4) + 48); putc('.'); putc((val & 0x0F) + 48); putc('\n'); counter = 0; }
void _int_(27) isr_adc(void){ int *p = (int *)(&ADC1BUF0); int i, media = 0, v = 0; for(i=0; i<8; i++){ media += p[i*4]; v += (p[i*4]*33+511)/1023; } media /= 8; v /= 8; value2display = toBcd(v); IFS1bits.AD1IF = 0; }
void Clock::setDateTime(DateTime& dateTime) { wire.beginTransmission(CLOCK_ADDR); wire.write(0x00); // tell clock to start writing at register 0; wire.write(toBcd(dateTime.seconds)); wire.write(toBcd(dateTime.minutes)); wire.write(toBcd(dateTime.hours)); wire.write(dateTime.wday); wire.write(toBcd(dateTime.day)); wire.write(toBcd(dateTime.month)); wire.write(toBcd(dateTime.year - 2000)); wire.endTransmission(); }
int main(void) { unsigned int i = 0; initADC(); EnableInterrupts(); // Configure displays as outputs TRISB = TRISB & 0xFC00; while (1) { delay(10); if (i++ == 2) { AD1CON1bits.ASAM = 1; i = 0; } send2displays(toBcd(value2displays)); } return 0; }
int main(void) { unsigned int i = 0; unsigned int value = 0; int* p; initADC(); TRISB = TRISB & 0xFC00; while (1) { delay(10); // Displays refresh rate = 100Hz if (i++ == 2) { AD1CON1bits.ASAM = 1; while( IFS1bits.AD1IF == 0 ); p = (int*)(&ADC1BUF0); value = convert2celsius(average(p)); IFS1bits.AD1IF = 0; i = 0; } send2displays(toBcd(value)); } return 0; }
int main(void) { TRISBbits.TRISB14 = 1; /* RB14 digital output disconnected */ AD1PCFGbits.PCFG14 = 0; /* RB14 configured as analog input (AN14) */ AD1CHSbits.CH0SA = 14; /* Selects AN14 as input for the A/D converter */ AD1CON2bits.SMPI = 3; /* 4 samples will be converted and stored */ /* Conversion trigger selection bits: in this // mode an internal counter ends sampling and // starts conversion*/ AD1CON1bits.SSRC = 7; /* // Stop conversions when the 1st A/D converter // interrupt is generated. At the same time, // hardware clears the ASAM bit*/ AD1CON1bits.CLRASAM =1; /* // Sample time is 16 TAD (TAD = 100 ns) */ AD1CON3bits.SAMC = 16; /* Enable A/D converter */ AD1CON1bits.ON=1; int i = 0, media = 0, v = 0; while(1){ delay(10); //Wait 10 ms using the core timer if(i++ == 25){//250ms v = media = 0; /* START THE CONVERSION */ AD1CON1bits.ASAM = 1; /* Start conversion */ /* Wait while conversion not done (AD1IF == 0) */ while( IFS1bits.AD1IF == 0 ); /* Read conversion result (ADC1BUF0 value) and print it */ int *p = (int *)(&ADC1BUF0); for(i=0; i<4; i++){ media += p[i*4]; v += (p[i*4]*33+511)/1023; } media /= 4; //printInt(media, 0x0004000A); //putChar('\n'); v /= 4; //printInt(v, 0x0004000A); /* Reset AD1IF */ IFS1bits.AD1IF = 0; i = 0; v = toBcd(v); } send2displays(v); } return 0; }
void _int_(4) isr_T1(void){ send2displays(toBcd(V)); IFS0bits.T1IF = 0; }
void _int_(12) isr_T3(void) // Replace VECTOR by the timer T3 // vector number { send2display(toBcd((char) value2display)); IFS0bits.T3IF = 0; }
void _int_(4) isr_T1(void) { // Send "value2display" global variable to displays send2displays(toBcd(value2displays)); IFS0bits.T1IF = 0; // Reset T3IF flag }