syamin0712
Member level 1
Hi,
I need some help to guide or overcome the error in writing coding that I have using MPlab.
Actually, I want to use dspic30f6010A as SPI Master, Frame master to communicate with my programmable waveform generator, AD9833.
I would like to produce sinewaveform with 2MHz.
Before this, I'm used the CCS C Compiler software to write the code and it was successfull.The micocontroller that Im used was PIC18f4580.
However, due to some reason, I would like to write the code using MPLAB with dspic30f family and I'm quite new with writing the code using MPLAB.
This is the code that I have before this by using PIC18F4580:
And what I'm going to do in MPLAB is actually the same code. I had set the SPI2 as the SPI module.
This is my code:
I did not have any more idea how to solve the error. Does the way code written is correct?
Really appreciate any help.
The error appears:
I need some help to guide or overcome the error in writing coding that I have using MPlab.
Actually, I want to use dspic30f6010A as SPI Master, Frame master to communicate with my programmable waveform generator, AD9833.
I would like to produce sinewaveform with 2MHz.
Before this, I'm used the CCS C Compiler software to write the code and it was successfull.The micocontroller that Im used was PIC18f4580.
However, due to some reason, I would like to write the code using MPLAB with dspic30f family and I'm quite new with writing the code using MPLAB.
This is the code that I have before this by using PIC18F4580:
#include <18F4580.h> //This both defines the chip, and includes everything.
#fuses HS,NOWDT,NOPROTECT,NOLVP,NODEBUG
#use delay(clock=20000000)
#use spi(SPI1, MODE=2, ENABLE=PIN_E2, STREAM=AD9833)
#define RESET 0x0100 //initialize reset bit D8=1
#define WRITE_FREQ0 0x2000 //initialize LSB & MSB write consecutively, D13=1
#define RUN 0 //intialize give output after reset off
#define PHASE0_ZERO 0xC000 //initalize PHASE0 use
#define spi_14(x) spi_xfer(AD9833,(x & 0x3FFF) | 0x4000,16)
//0x4000 is the register for FREQ0
void write_val_freq0(int32 divisor)
{
//Do the maths to send 28bits to freg - more reliable than DIY
spi_14(divisor); //send 14 LSb's
spi_14((divisor>>14)); //then the next 14 bits
}
void main()
{
setup_adc_ports(NO_ANALOGS); //E2 is an analog pin - make sure this
//is setup to digital.
while(TRUE)
{
// activate control register; both LSB & MSB FREQ write consecutively
// enable reset
spi_xfer(AD9833, RESET | WRITE_FREQ0, 16);
//on FREQ0 for generate 2MHz if using MCLK=20MHz for AD9833
//26843545 in decimal
write_val_freq0(26843545); //setup FREQ0
//on PHASE0 with zero phase shift
spi_xfer(AD9833, PHASE0_ZERO, 16);
//activate control register
//disable reset; enable sinewaveform signal
spi_xfer(AD9833, RUN, 16);
//generally you don't want to keep sending data to chips 'non stop'...
//with your existing code, only a few uSec after getting here, the chip
//would be programmed back to RESET, so the waveform would stop
//since the waveform doesn't start till 8 clocks after the RESET
//goes off, there would be almost nothing on the output.....
delay_ms(1000); //output the waveform for a second
}
}
And what I'm going to do in MPLAB is actually the same code. I had set the SPI2 as the SPI module.
This is my code:
#include <p30f6010A.h>
#define FCY 117920000UL
#include <libpic30.h>
_FOSC(CSW_FSCM_OFF & FRC_PLL16); //use internal clock with PLLx16
_FWDT(WDT_OFF);
_FBORPOR(MCLR_EN & PWRT_OFF);
// AD9833 SPI Slave Device
#define unsigned int x
#define SPI_read x
#define SPI_SLAVE_WRITE 0x4000
#define SPI_14(x)
// AD9833 Registers Definition
#define RESET 0x0100 //initialize reset bit D8=1
#define WRITE_FREQ0 0x2000 //initialize LSB & MSB write consecutively, D13=1
#define RUN 0 //intialize give output after reset off
#define PHASE0_ZERO 0xC000 //initalize PHASE0 use
/* Initial the dspic30f6010A SPI Peripheral */
TRISGbits.TRISG9 = 0; //Output RG9/SS2
TRISGbits.TRISG6 = 0; //Output RG6/SCK2
TRISGbits.TRISG8 = 0; //Output RG6/SDO2
/* SPI2 Register Settings */
SPI2CONbits.DISSDO = 0; //SDO2 pin is controlled by the module.
SPI2CONbits.MODE16 = 1; //Communication is word-wide (16 bits).
//setting for SPI mode 2, CKP=1,CKE=0
SPI2CONbits.CKP = 1; //Clock polarity select. Idle is high
SPI2CONbits.CKE = 0; //Clock edge select bit to 1
//SPI Master,Frame Master Mode; MSTEN=1,FRMEN=1,SPIFSD=0
SPI2CONbits.MSTEN = 1; //Master Mode Enabled
SPI2CONbits.FRMEN = 1; // Enable pin SCK2 as output. Active low
SPI2CONbits.SPIFSD = 0; //SS2 is the output pin
SPI2STATbits.SPIEN = 1; //Enable SPI Module
void SPI_Write(unsigned int addr) //function to write
{
// Activate the SS2 SPI Select pin
PORTGbits.RG9 = 0;
// Start AD9833 OpCode transmission
SPI2BUF = (SPI_read & 0x3FFF)| SPI_SLAVE_WRITE;
// Wait for Data Transmit/Receipt complete
while(!SPI2STATbits.SPITBF);
// Start AD9833 Register Address transmission
SPI2BUF = addr;
// Wait for Data Transmit/Receipt complete
while(!SPI2STATbits.SPITBF);
// SS2 pin is not active
PORTGbits.RG9 = 1;;
}
void write_val_freq0(unsigned int divisor) //function to do the maths to send 28 bits to freq0
{
SPI_14(divisor);
SPI_14((divisor>>14));
}
int main(void)
{
while (1)
{
SPI_Write(RESET|WRITE_FREQ0); //activate control register and enable reset
write_val_freq0(26843545); //on FREQ0 for freq 2MHz; 26843545 in decimal using formula=2^28x2M/20M, 20M is MCL for AD9833
SPI_Write(PHASE0_ZERO); //on PHASE0 with zero phase shift
SPI_Write(RUN); //activate control register and disable reset
__delay_ms(1000);
}
}
I did not have any more idea how to solve the error. Does the way code written is correct?
Really appreciate any help.
The error appears: