|
|
View previous topic :: View next topic |
Author |
Message |
desertkids
Joined: 30 Nov 2008 Posts: 8
|
how to generate 44.1kHz square wave???help |
Posted: Mon Jul 13, 2009 7:39 pm |
|
|
Hi, currently I have to do a project of voice module in my fyp. So I need to connect sd card to 18f4620, then output digital data from 18f4620 to DAC to connect to speaker. Now my first problem is to generate a 44.1kHz square wave to control DAC's function. I have tried many ways and examples from CCS, but still can't generate the 44.1kHz. Please help. Following is my testing code before. Thanks. Compiler version, PCH 4.032.
Code: |
#define (__PCH__)
#include <18F4620.h>
#use delay(clock=20000000)
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, parity=n, bits=8, BRGH1OK, stream=UART)
int outs=1;
#int_CCP1
CCP1_isr()
{
CCP_1 = CCP_1 + 1250;//1250 is to generate 1kHz square waveform,from CCS example
outs ^= 1;
output_bit(PIN_D0, outs);
}
void main()
{
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
setup_ccp1(CCP_COMPARE_INT);
enable_interrupts(INT_CCP1);
enable_interrupts(global);
while(1);
}
|
|
|
|
Ttelmah Guest
|
|
Posted: Tue Jul 14, 2009 2:39 am |
|
|
Use the PWM, not the CCP.
A number of comments.
You should not define __PCH__. The compiler does this, when the PCH compiler is used. Overriding this yourself, would cause problems if you later wanted to use a different compiler. The 'point' of the define, is to tell _your_ code, which compiler is being used.
Get rid of BRGH10K. This is for certain specific chips that have the high speed UART BRG fault. None of the PIC18 chips have this. Add 'ERRORS' instead. This should _always_ be included fo hardware UART setups, unless you are handling overrun errors yourself.
Add 'NOXINST' to the fuses. Again, always do this, for PIC's that have the extended instruction set. CCS, does not support the extended set.
Now, the CCP, is really designed to measure times, not generate pulse trains. You can use it to generate pulses, but this involves you in quite a bit of work, and becomes less and less suitable at higher speeds. To generate a train at 44.1KHz, would require the output to change state, every 1/88200th second. At 20MHz (5MIPS), this is every 56.7 instructions. Quite simply, you can't get into, and out of the interrupt handler this fast. The handler itself, typically has an 'overhead' of about 60 instruction times. Add the time to update the CCP counter, toggle the bit, and output this to the pin, and you have something in the order of probably 70 to 75 instruction times, giving a maximum possible frequency, of perhaps 35KHz....
For the PWM, if you use:
Code: |
setup_timer_2(T2_DIV_BY_1,112,1);
setup_ccp1(CCP_PWM);
set_pwm1_duty(226L);
|
You should get a pulse train on the PWM output pin, at 44247.7Hz (as close as you can get from this master clock, and well wthin the sort of tolerance allowed for this type of system).
Best Wishes |
|
|
desertkids
Joined: 30 Nov 2008 Posts: 8
|
|
Posted: Tue Jul 14, 2009 4:28 am |
|
|
Thanks for you Ttelmah, with your simple code help me a lot to understand more. Now I will continue my work with dumping some 16-bit data to DAC to generate output audio.
Please give me some advice on how to send 32bit of data (first 16 bits are don't care bit, bit 17- bit 32 are audio data bit) in a pulse of 44.1kHz when a rise edge was detect for the 44.1kHz waveform generated. Please give me advice how to handle the timing problem?? Thanks a lot. |
|
|
bkamen
Joined: 07 Jan 2004 Posts: 1615 Location: Central Illinois, USA
|
|
Posted: Tue Jul 14, 2009 2:10 pm |
|
|
You declare a unsigned int32 as your output buffer and just pack it appropriately and then use the PWM as an IRQ to roll the bits out a port.
( You might consider using a PIC18F for this task as they have fast interrupts which have a minimum latency time.)
Honestly though, your best bet is to pick up a book on digital communications and thoroughly read about methods that apply to your application.
Telling you what to do isn't the same as you figuring it out for yourself and working all that needed knowhow into your brain. (since everyone learns differently)
Now you would know how to fish --- instead of someone having to give you one.
Best regards,
-Ben _________________ Dazed and confused? I don't think so. Just "plain lost" will do. :D |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|