AnsweredHot!PIC32 or PIC18 How to creat a sine wave

Page: < 1234 Showing page 4 of 4
Author
dan1138
Super Member
  • Total Posts : 2490
  • Reward points : 0
  • Status: offline
Re: PIC32 or PIC18 How to creat a sine wave 2017/07/20 12:16:13 (permalink)
0
NorthGuy
You can just do: SSPBUF write (or iorwf if you like it better) - 9 cycles of unrelated stuff - SSPBUF write - 9 cycles of unrelated stuff.

I agree. According to the timing diagrams in the PIC18F4520 data sheet this "should" work. When I tried this implementation it would not work correctly. It seems that the PIC18F4520 requires at least 12 instruction clocks between writes to the SSPBUF.
 
Perhaps there are different silicon implementations of the MSSP function block in other PIC18F devices.
 
There are a lot of errata for the PIC18F4520.
#61
NorthGuy
Super Member
  • Total Posts : 4245
  • Reward points : 0
  • Joined: 2014/02/23 14:23:23
  • Location: Northern Canada
  • Status: online
Re: PIC32 or PIC18 How to creat a sine wave 2017/07/20 12:35:19 (permalink)
0
dan1138
I agree. According to the timing diagrams in the PIC18F4520 data sheet this "should" work. When I tried this implementation it would not work correctly. It seems that the PIC18F4520 requires at least 12 instruction clocks between writes to the SSPBUF.
 
Perhaps there are different silicon implementations of the MSSP function block in other PIC18F devices.



It looks that way. I guess they improved MSSP in newer devices.
 
With 12 instructions per byte, it is only 20 points per the sine cycle :(
 
 
 
#62
dan1138
Super Member
  • Total Posts : 2490
  • Reward points : 0
  • Status: offline
Re: PIC32 or PIC18 How to creat a sine wave 2017/07/20 13:28:13 (permalink)
0
NorthGuy
With 12 instructions per byte, it is only 20 points per the sine cycle :(

It is possible to replace the PIC18F4520 with a PIC18F4550 and use a 48MHz system clock. This would achieve a 2 microsecond step rate. It may even be possible to overclock the PIC18F4520 at 48MHz to get the same effect.
#63
CinziaG
PORCAMADONNA
  • Total Posts : 2438
  • Reward points : 0
  • Joined: 2016/12/07 14:20:36
  • Location: Wien
  • Status: offline
Re: PIC32 or PIC18 How to creat a sine wave 2017/07/20 13:33:51 (permalink)
0
I overclocked 4550 to 53~ Smile (4.43 crystal)

PORCAMADONNA
#64
NorthGuy
Super Member
  • Total Posts : 4245
  • Reward points : 0
  • Joined: 2014/02/23 14:23:23
  • Location: Northern Canada
  • Status: online
Re: PIC32 or PIC18 How to creat a sine wave 2017/07/20 13:36:46 (permalink)
4 (1)
dan1138
It is possible to replace the PIC18F4520 with a PIC18F4550 and use a 48MHz system clock. This would achieve a 2 microsecond step rate. It may even be possible to overclock the PIC18F4520 at 48MHz to get the same effect.



I'm not 100% sure, but I think 45K40 (or 47K40 for more memory) should be pin-compatible too. And they can run at 64MHz. Also some of the big (but newer) PIC16s with built-in DACs should fit too.
#65
dan1138
Super Member
  • Total Posts : 2490
  • Reward points : 0
  • Status: offline
Re: PIC32 or PIC18 How to creat a sine wave 2017/07/20 14:18:37 (permalink)
3 (1)
NorthGuy
I'm not 100% sure, but I think 45K40 (or 47K40 for more memory) should be pin-compatible too. And they can run at 64MHz. Also some of the big (but newer) PIC16s with built-in DACs should fit too.

The PIC18F45K20 or PIC18F46K20 are perhaps a better choice for someone new to PIC18F microcontrollers.
 
The PIC18F45K40 parts are nice that they include the Peripheral Pin Select (PPS), it seem cruel to throw this at a noob along with all of the other strangeness you must deal with when first learning about the PIC18F family of devices.
#66
PStechPaul
Super Member
  • Total Posts : 1155
  • Reward points : 0
  • Joined: 2006/06/27 16:11:32
  • Location: Cockeysville, MD, USA
  • Status: offline
Re: PIC32 or PIC18 How to creat a sine wave 2017/07/20 19:09:10 (permalink)
3 (1)
I just made a sine wave generator using a PIC16F1783 which has an 8 bit DAC. It's on a proto-board and powered by the PICkit3:
 

 
It is programmed to produce 5 cycles of a waveform using a sine table with 20 elements:
 

 

 
Here is a link to the project: http://enginuitysystems.com/pix/electronics/Sine_Wave_1783.zip
 
The code for main.c is:
#include <xc.h>
#include <stdint.h> //needed for uint8_t
#include "mcc_generated_files/mcc.h"

#define MAXSINE 20
#define XTAL_FREQ  16000000
// Sine Table based on 8 bit DAC, 20 elements 18 deg/point
const uint8_t sintable[] = {128, 167, 203, 231, 250, 255, 250, 231, 203, 167, 128, 88, 53, 25, 6, 1, 6, 25, 53, 88};

void main(void) {
    SYSTEM_Initialize();
    int i,j;
    while(1) {
        for( j=0; j<5; j++) {
            for( i=0; i<MAXSINE; i++) {
                DAC1_SetOutput(sintable[i]);
                __delay_us(1);
            }
        }
        __delay_ms(1);
        LATAbits.LATA0 = !PORTAbits.RA0;
    }
}


 
#67
PStechPaul
Super Member
  • Total Posts : 1155
  • Reward points : 0
  • Joined: 2006/06/27 16:11:32
  • Location: Cockeysville, MD, USA
  • Status: offline
Re: PIC32 or PIC18 How to creat a sine wave 2017/07/20 22:16:01 (permalink)
0
I made a few changes, and now I have produced a 20 kHz sine wave burst of five cycles:
 

 

 
The main code:
#include <xc.h>
#include <stdint.h> //needed for uint8_t
#include "mcc_generated_files/mcc.h"

#define MAXSINE 20
#define XTAL_FREQ  16000000
#define SAMPLE_DELAY    2
// Sine Table based on 8 bit DAC, 20 elements 18 deg/point
const uint8_t sintable[] = {167, 203, 231, 250, 255, 250, 231, 203, 167, 128, 88, 53, 25, 6, 1, 6, 25, 53, 88, 128};

void main(void) {
    SYSTEM_Initialize();
    int i,j;
    while(1) {
        for( j=0; j<5; j++) {
            DACCON1 = 167; __delay_us(SAMPLE_DELAY);
            DACCON1 = 203; __delay_us(SAMPLE_DELAY);
            DACCON1 = 231; __delay_us(SAMPLE_DELAY);
            DACCON1 = 250; __delay_us(SAMPLE_DELAY);
            DACCON1 = 255; __delay_us(SAMPLE_DELAY);
            DACCON1 = 250; __delay_us(SAMPLE_DELAY);
            DACCON1 = 231; __delay_us(SAMPLE_DELAY);
            DACCON1 = 203; __delay_us(SAMPLE_DELAY);
            DACCON1 = 167; __delay_us(SAMPLE_DELAY);
            DACCON1 = 128; __delay_us(SAMPLE_DELAY);
            DACCON1 = 88; __delay_us(SAMPLE_DELAY);
            DACCON1 = 53; __delay_us(SAMPLE_DELAY);
            DACCON1 = 25; __delay_us(SAMPLE_DELAY);
            DACCON1 = 6; __delay_us(SAMPLE_DELAY);
            DACCON1 = 1; __delay_us(SAMPLE_DELAY);
            DACCON1 = 6; __delay_us(SAMPLE_DELAY);
            DACCON1 = 25; __delay_us(SAMPLE_DELAY);
            DACCON1 = 53; __delay_us(SAMPLE_DELAY);
            DACCON1 = 88; __delay_us(SAMPLE_DELAY);
            DACCON1 = 128; __delay_us(SAMPLE_DELAY);
            
//            for( i=0; i<MAXSINE; i++) {
//                DACCON1  = sintable[i];
                      //     DAC1_SetOutput(sintable[i]);
//                __delay_us(1);
//            }
        }
        __delay_ms(1);
        LATAbits.LATA0 = !PORTAbits.RA0;
    }
}


 
#68
Page: < 1234 Showing page 4 of 4
Jump to:
© 2017 APG vNext Commercial Version 4.5