Fetch Data From Database

Codice C per conversione DAC PIC16F873A

Il seguente codice permette di convertire 2 canali da analogico a digitale visualizzando i valori su un display LCD a 2 righe. Simulazione Proteus:

 File Main.c

*
* File: main.c
* Author: PRIMOK_V
*
* Created on 21 Giugno 2021, 20.47
*/


// CONFIG
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = ON // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3/PGM pin has PGM function; low-voltage programming enabled)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)


#include <xc.h>
#include "Lcd.h"
#include <stdlib.h>
#include <string.h>

#define OUT_RB0 PORTBbits.RB0
#define _CH ADCON0bits.CHS
unsigned char p[]=" ";
unsigned char C_AD[]="CH1_AD=";
unsigned char C_AD2[]="CH2_AD=";
volatile unsigned int trimmer;
volatile unsigned int trimmer2;
volatile unsigned int seltrimmer;
char lung;
unsigned char string[]="";
volatile unsigned char ch=0;
volatile unsigned char rg=1;
void interrupt CON_AD(void){

if( PIR1bits.ADIF){
if(!ch){
trimmer =(unsigned int) (ADRESL + (ADRESH<<8));
_CH=0;
}else{
trimmer =(unsigned int) (ADRESL + (ADRESH<<8));
_CH=1;
}

ch^=1;
rg=ch;
PIR1bits.ADIF=0;
}

}

void main(void) {

INTCON = 0b00000000;
TRISA = 0b00001011; // AN0,AN1,AN3 input
TRISB = 0b00000000; // all PORTB bits are output
TRISC = 0b00000000;
PORTB = 0b00000000; // clear PORTB
PORTC = 0b00000000; // clear PORTC
ADCON1 =0b10000100;
ADCON0 = 0b00001001;

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

LCD_INIT(); //Inizializza display LCD
__delay_ms(10);
LCD_CLEAR();
__delay_ms(100);
_CH=0;


lcd_gotoxy(1,1);
lcd_puts(C_AD);
lcd_gotoxy(2,1);
lcd_puts(C_AD2);
ADCON0bits.GO = 1;
// while (ADCON0bits.GO_DONE);
//trimmer =(unsigned int) (ADRESL + (ADRESH<<8));
while(1){


itoa(string,trimmer, 10);
lcd_gotoxy(rg,8);
lcd_puts(string);
lung=strlen(string);
lung+=8;
lcd_gotoxy(rg,lung);
lcd_puts(p);
lung=0;
ADCON0bits.GO = 1;


}
return;
}

 

 

Il quarzo utilizzato è 20Mhz. I 2 canali sono gestiti ad interrupt, il compilatore è XC8 della Microchip versione v2.32

Nel progetto dovrete includere i files lcd.h e lcd.c prelevabili al seguente 

link

 Saluti Primok_V

No comments

Leave your comment

In reply to Some User