littletransistor
New Member
Hey there,
I'm planning to make a simple tone-generator, but first I need to get this PWM and the 440Hz sinewave tone out, before adding other waveforms like square and sawtooth.
However, I have a problem here - I can't get the 8ohm speaker to sound anything despite running through the wavetable.
By the way here is the code, and the passive LPF is a 470ohm resistor with a 100nF capacitor. Also, the sine wave is in 256 steps and each step is read at 31.25µsecs. The PWM frequency is in 64kHz. In the code, a square wave is used instead. Microcontroller is PIC 18F45K20.
Crystal is internal onboard and it is 16MHz.
I'm planning to make a simple tone-generator, but first I need to get this PWM and the 440Hz sinewave tone out, before adding other waveforms like square and sawtooth.
However, I have a problem here - I can't get the 8ohm speaker to sound anything despite running through the wavetable.
By the way here is the code, and the passive LPF is a 470ohm resistor with a 100nF capacitor. Also, the sine wave is in 256 steps and each step is read at 31.25µsecs. The PWM frequency is in 64kHz. In the code, a square wave is used instead. Microcontroller is PIC 18F45K20.
Crystal is internal onboard and it is 16MHz.
Code:
#include <p18f45k20.h>
#pragma config FOSC = INTIO67, WDTEN = OFF, MCLRE = OFF
void T0_ISR(void);
void T2_ISR(void);
#pragma interrupt chk_isr
void chk_isr(void)
{
if (PIR1bits.TMR2IF == 1)
T2_ISR();
if (INTCONbits.TMR0IF == 1)
T0_ISR();
}
#pragma code HiPrio_Int = 0x0008
void HiPrio_Int(void)
{
_asm goto chk_isr _endasm
}
#pragma code
rom near sqWaveForm[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00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0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
};
char counter = 0;
void main()
{
OSCCON = 0b01110011;
ADCON0 = 0x00;
TRISD = 0x00;
TRISC = 0x00;
PIR1bits.TMR2IF = 0;
PIE1bits.TMR2IE = 0;
T2CON = 0x00;
TMR2 = 0xC1;
T2CONbits.TMR2ON = 1;
INTCONbits.TMR0IF = 0;
INTCONbits.TMR0IE = 0;
T0CON = 0b00001000;
TMR0H = 0xFD;
TMR0L = 0x8F;
T0CONbits.TMR0ON = 0;
CCP1CON = 0x00;
PR2 = 255;
//CCPR1L = 63;
//CCP1CON = 0b00111100; // --00---- for 0.00
// --01---- for 0.25
// --11---- for 0.75
INTCONbits.PEIE = 1;
INTCONbits.GIE = 1;
while(1);
}
void T0_ISR(void)
{
CCPR1L = sqWaveForm[counter]; // values from waveform / PR2
CCP1CON = 0b00001100;
TMR0H = 0xFD;
TMR0L = 0x8F;
INTCONbits.TMR0IF = 0;
}
void T2_ISR(void)
{
TMR2 = 0x00;
PIR1bits.TMR2IF = 0;
}
Last edited: