| View previous topic :: View next topic |
| Author |
Message |
Diode
Joined: 27 Jul 2012 Posts: 35
|
|
Posted: Mon May 20, 2013 12:29 pm |
|
|
Maybe in the future I will update to the newest version but for now I find it quite expensive. Especially seen the fact that I just get back to programming.
I had a look at the datasheet and you are right about the PLL5. For the CPUdiv I just followed Ttelmah's comment.
With the new settings I get a 11KHz sine wave.
@Ckielstra, may I ask if you are or ever was a teacher at a school in Emmeloord? I had a teacher who's last name was Kielstra. |
|
 |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon May 20, 2013 12:33 pm |
|
|
| Quote: | | With the new settings I get a 11KHz sine wave. |
Post your current test program. Post the complete program with
#include, #fuses, #use delay, main(), etc. |
|
 |
Diode
Joined: 27 Jul 2012 Posts: 35
|
|
Posted: Mon May 20, 2013 12:39 pm |
|
|
Here are the fuses:
| Code: |
#include <18F4455.h>
#device ICD=TRUE
#device adc=8
#FUSES NOWDT //No Watch Dog Timer
#FUSES HSPLL //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES CPUDIV1
#FUSES PLL5
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOBROWNOUT //No brownout reset
#FUSES NOPUT //No Power Up Timer
#FUSES NOCPD //No EE protection
#FUSES DEBUG //Debug mode for use with ICD
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOWRT //Program memory not write protected
#FUSES NOWRTD //Data EEPROM not write protected
#FUSES IESO //Internal External Switch Over mode enabled
#FUSES FCMEN //Fail-safe clock monitor enabled
#FUSES NOPBADEN //PORTB pins are configured as digital I/O on RESET
#FUSES NOWRTC //Configuration registers not write protected
#FUSES NOWRTB //Boot block not write protected
#FUSES NOEBTR //Memory not protected from table reads
#FUSES NOEBTRB //Boot block not protected from table reads
#FUSES NOCPB //No Boot Block code protection
#FUSES LPT1OSC //Timer1 configured for low-power operation
#FUSES MCLR //Master Clear pin enabled
#FUSES NOXINST //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
//#use delay(clock=20000000,RESTART_WDT)
|
And the program:
| Code: |
#include "B:\Ozon voeding\Software\DA test\spi test.h"
#include <stdlib.h>
#include <mcp4921rev2.c>
#include <math.h>
#define SPI_MODE_0 (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#use delay(clock=48000000)
int16 i,j;
int16 sin_table [13];
void fill_sin_table()
{
for (i = 0; i <=12; i=i+1)
{
sin_table[i]=2046+floor(2046*sin((i*30) * PI /180));
}
}
void main()
{
fill_sin_table();
setup_spi(SPI_MASTER | SPI_MODE_0 | SPI_CLK_DIV_4);
//setup_adc_ports(AN0);
//setup_adc(ADC_CLOCK_DIV_64);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
delay_ms(100);
while(1)
{
j++;
if (j >= 12)
{
j = 0;
}
//output_high(PIN_D1);
write_mcp4921(sin_table[j]);
//output_low(PIN_D1);
}
} |
I'm not sure if I have defined the clock right, in the fuses it is declared as 20MHz and in the main program 48MHz because the PLL is enabled.
Edit: the clock wasn't setup right so I put the 20Mhz fuse in a comment |
|
 |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon May 20, 2013 2:38 pm |
|
|
| Quote: | | With the new settings I get a 11KHz sine wave. |
Just playing around with it, I got it up to 19.5 KHz.
The bulk of the wasted time is in the code execution time. To speed up
the sinewave frequency, you need to remove code. You can look at the
.LST file to see the ASM code that must be removed.
To measure the overall loop speed, I put markers in the while(1) loop,
just as you did. I put a scope probe on the marker pin and looked at
the scope's frequency counter. Then each time I made an adjustment
to the code, I compiled, downloaded and ran it, and looked at the freq
display on the scope screen to see how much speed improvement I got.
First technique is to unroll the loop. Get rid of the 'j' counter and put
in 13 sequential instances of calling write_mcp4921().
Next thing is, get rid of the indirect addressing of the sine table array.
Inside the while() loop, write it like this:
| Code: |
while(1)
{
output_high(MARKER_PIN);
write_mcp4921(sin_table[0]);
write_mcp4921(sin_table[1]);
write_mcp4921(sin_table[2]);
.
.
.
write_mcp4921(sin_table[12]);
output_low(MARKER_PIN);
} |
Next thing is to inline the write_mcp4921() routine. This reduces the
function calling overhead. Add #inline just above the function declaration.
| Code: | #inline
void write_mcp4921(int16 value_1)
{
|
Other things to do:
Go to #fast_io mode for all output ports. Use the output_drive() function
to set the TRIS for the three individual pins that you are using (SCK,
SDO, MARKER) to be outputs.
Get rid of the limit check code at the beginning of write_mcp4921().
Just make sure your sine table is filled with correct values (not greater
than 12-bits, which is 4095 max).
The three bits that are setup at the start of write_mcp4921() could be
combined into the sine table generation code. Bits 12 and 13 are the
only ones that are set. You could OR them into each sine table element
ahead of time. Then delete that code from write_mcp4921().
You can see what I'm trying to do. Go through the .LST file and cut out
anything possible out of the while() loop code. |
|
 |
Ttelmah
Joined: 11 Mar 2010 Posts: 20161
|
|
Posted: Mon May 20, 2013 2:46 pm |
|
|
| Diode wrote: | Maybe in the future I will update to the newest version but for now I find it quite expensive. Especially seen the fact that I just get back to programming.
I had a look at the datasheet and you are right about the PLL5. For the CPUdiv I just followed Ttelmah's comment.
With the new settings I get a 11KHz sine wave.
@Ckielstra, may I ask if you are or ever was a teacher at a school in Emmeloord? I had a teacher who's last name was Kielstra. |
No, I didn't give the fuses. I gave divisions needed. Assumed you would look at the data sheet.
You need the clock/5 to the PLL, and the PLL/2 to the CPU. This is the PLL5 fuse, and the CPUDIV1 fuse.
You needed to look at the data sheet, then work out what the CCS names are for these ratios.....
Best Wishes |
|
 |
Diode
Joined: 27 Jul 2012 Posts: 35
|
|
Posted: Tue May 21, 2013 1:17 am |
|
|
Ttelmah, in that case I did not understood you correctly, sorry.
@PCM, I see the point you try to make. With some optimization I might be able to achieve 25~30kHz. The point is that the PIC will be used for quite a lot of other tasks so the available time for the sine wave generation will be limited.
That's the reason that I think it will not be feasible in this setup.
A PIC with an onboard DA probably isn't the solutions as well because it still need a high update ratio. The use of a DDS seems as a better solution to me. |
|
 |
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Tue May 21, 2013 1:47 am |
|
|
| Diode wrote: | | Maybe in the future I will update to the newest version but for now I find it quite expensive. Especially seen the fact that I just get back to programming. | If you don't need the new v4 compiler features it is advisable to revert to the last stable release of that time, v3.249. This was a very good compiler version. Even when you don't have it any more CCS perhaps can provide you an old copy when you provide you license code.
| Quote: | | @Ckielstra, may I ask if you are or ever was a teacher at a school in Emmeloord? I had a teacher who's last name was Kielstra. | Small world.
My ancestors come from that region, but I don't know any of them. |
|
 |
Diode
Joined: 27 Jul 2012 Posts: 35
|
|
Posted: Tue May 21, 2013 2:13 am |
|
|
I might need the new features in the future because I'm planning to do some more projects with newer microcontrollers. At that time I will update to the newest version.
It is a small world indeed  |
|
 |
|