Modulo DAC rumore di fondo

12 Anni 8 Mesi fa #1 da Guido
Modulo DAC rumore di fondo è stato creato da Guido
Ho pasticciato questo codice, ora riesco a visualizzare l'onda (è quadra con le creste arrotondate), purtroppo non riesco a eliminare un rumore di fondo, tutte le volte che rilascio il pulsante si presenta, come faccio per eliminarlo?
Ecco il codice:

#include <p18f4550.h>
#include <portb.h>

#pragma config FOSC = HS
#pragma config WDT = OFF
#pragma config LVP = OFF
#pragma config PBADEN = OFF

//OSC = HS Set to work for high speed
//WDT = OFF watchdog timer is off
//LVP = OFF LVP programming is off
//PBADEN = OFF Analog input on PORTB are off

//************************************************************
// Functions Prototypes
//************************************************************
void set_duty_cycle (int duty_cycle);
void turn_on_PWM1 (void);
void High_Int_Event (void);


//************************************************************
// Global variables
//************************************************************

//Samples
int sine[] = {250,299,346,389,427,458,481,495,500,495,481,458,427,389,346,299,250,201,154,111,73,42,19,5,0,5,19,42,73,111,154,201};

// Repeat the sample for x time
// sampling frequency must be changed to keep analog signal frequency costnat
const unsigned char REPEATING_FACTOR = 3;


//************************************************************
// Interrupt Handler
//************************************************************

#pragma code high_vector = 0x08
void high_interrupt (void) {
// go to the interrupt handler
_asm GOTO High_Int_Event _endasm
}

#pragma code
#pragma interrupt High_Int_Event

void High_Int_Event (void) {

static unsigned char sample = 0;
static unsigned char repeat = 0;




if (PIR1bits.TMR2IF == 1) {




if (PORTBbits.RB0 == 0) {
set_duty_cycle (sine[sample]);

repeat++;

}
else {
set_duty_cycle (0);

}


if (repeat == REPEATING_FACTOR) {
repeat = 0;
sample++;

if (sample > 31) {
sample = 0;
}
}


}


//Clear Timer 2 Interrupt Flag
PIR1bits.TMR2IF = 0;

}


//************************************************************
// Main program
//************************************************************

void main (void){

// PORTA settings
LATA = 0x00;
TRISA = 0xFF;

// PORTB settings
LATB = 0x00;
TRISB = 0xFF;

// PORTC settings
LATC = 0x00;
TRISC = 0b11111010;

// PORTD settings
LATD = 0x00;
TRISD = 0x00;

// PORTE settings
LATE = 0x00;
TRISE = 0xFF;



//*************************************
// Modules Initializaion
//*************************************

// Set the PWM frequency to
PR2 = 198;

set_duty_cycle (250);

// Turn ON TMR2 and set Prescaler to 0
T2CON = 0x04;

turn_on_PWM1 ();

// Enable Timer 2 Interrupt
PIE1bits.TMR2IE = 1;

// Standrd interrupt (by default is already 0)
RCONbits.IPEN = 0;

// Enable global interrupt
INTCONbits.GIE = 1;

// Enable Peripheral Interrupt
INTCONbits.PEIE = 1 ;

// just loop for nothing...
while (1){
}
}


//************************************
// Set the Diuty Cicle
//************************************

void set_duty_cycle (int duty_cycle) {

CCPR1L = (unsigned char) (duty_cycle >> 2);

if (duty_cycle & 0x0001)
CCP1CONbits.DC1B0 = 0x0001;
else
CCP1CONbits.DC1B0 = 0x0000;

if (duty_cycle & 0x0002)
CCP1CONbits.DC1B1 = 0x0001;
else
CCP1CONbits.DC1B1 = 0x0000;
}


//************************************
// Turn ON the PWM module
//************************************
void turn_on_PWM1 (void) {

CCP1CON = CCP1CON | 0b00001100;

}

Si prega Accedi o Crea un account a partecipare alla conversazione.

  • Guido
  • Platinum Member
  • Platinum Member
Di più
Moderatori: Mauro LaurentiPinnaStefAMatteo Garia

Registrati al sito

Accedi a tutte le risorse e articoli non visibili pubblicamente, puoi registrarti con pochi passi.

Registrati al sito LaurTec.

Login