syamin0712
Member level 1
- Joined
- Jan 2, 2015
- Messages
- 36
- Helped
- 0
- Reputation
- 0
- Reaction score
- 0
- Trophy points
- 6
- Activity points
- 406
#device PIC18F4580
//configure a 10MHz crystal to operate at 40MHz, // System setting - Use 10MHz XTAL with PLL. New Freq: 10MHz x 4 = 40Mhz.
#fuses HS,NOWDT,NOPROTECT,NOLVP,NODEBUG
#define OSC_PLL_ON
#use delay(clock=10000000)
#use spi(DO= PIN_C4 , CLK = PIN_C3, ENABLE = PIN_A5, BITS = 16, MASTER, ENABLE_ACTIVE = 0, MSB_FIRST, IDLE = 1)
//DO=SDATA/SDA pin,
//CLK=SCLK/SCK pin,
//ENABLE=FSYNC/SS pin,
//BITS=16 is max num of bit in one transfer is 16,
//MASTER=PIC is master and AD9833 is slave,
//ENABLE_ACTIVE = 0 is FSYNC pin is active low,
//MSB_FIRST is accept MSB of each transfer first
//IDLE=1 is FSYNC always high when not in use
void main()
{
while(1)
{
// activate control register; both LSB & MSB FREQ write consequtively; enable reset; enable sinewaveform signal
spi_xfer(0b0010000100000000);
//on FREQ0 for generate 2MHz if using MCLK=40MHz ; 13421773 in decimal
spi_xfer(0b100110011001101);
spi_xfer(0b0100001100110011);
//on PHASE0 with zero phase shift
spi_xfer(0b1100000000000000);
//activate control register; both LSB & MSB FREQ write ; disable reset; enable sinewaveform signal
spi_xfer(0b0010000000000000);
}
}
#fuses HS,NOWDT,NOPROTECT,NOLVP,NODEBUG
#define OSC_PLL_ON
#use delay(clock=10000000)
#use delay(clock=40000000)
////////////////////////////////////////////////////////////////// INTERNAL RC
// Oscillator Prototypes:
_bif int8 setup_oscillator(int8 mode);
_bif int8 setup_oscillator(int8 mode, signed int8 tune);
// Constants used in setup_oscillator() are:
// First param:
#define OSC_31KHZ 0
#define OSC_125KHZ 0x10
#define OSC_250KHZ 0x20
#define OSC_500KHZ 0x30
#define OSC_1MHZ 0x40
#define OSC_2MHZ 0x50
#define OSC_4MHZ 0x60
#define OSC_8MHZ 0x70
#define OSC_16MHZ 0x4060
#define OSC_32MHZ 0x4070
// The following may be OR'ed in with the above using |
#define OSC_TIMER1 1
#define OSC_INTRC 2
#define OSC_NORMAL 0
// The following may be OR'ed in with the above using |
#define OSC_IDLE_MODE 0x80
#define OSC_31250 0x8000
#define OSC_PLL_ON 0x4000
#define OSC_PLL_OFF 0
// A second optional parameter may be used with this part to fine
// tune the speed (signed int,-32 to 31)
// Result may be (ignore all other bits)
#define OSC_STATE_STABLE 4
#define OSC_STATE_EXT_RUNNING 8
I don know whether its a software or hardware SPI, but if its a hardware then wrong pin configuration and this pins are not remappable.
I already once worked with this IC for FM.
I don know your application but I think you needed to put delay between SPI writes, so that it will be meaning full.
Check if there is any fuses like HSPLL that you have to include.
I think this doesn't make any difference.
I think that when you use 4xPLL you have to mention the actual clock that you get after PLL here like.
Code:#use delay(clock=40000000)
What do you mean the hardware or software SPI?
If you want a single frequency why dont you use a analog crystal oscillator.I want to transmit 2MHz sine waveform from the ic AD9833
For a single frequency sine output you can send the frequency byte only one time and release the reset. for varying the frequency usually we will send in some particular delay timings. Here you don need to send them again and again through a while loop. You can just put the while loop in the bottom to get it working.Do you mean I have to put the delay for every single instruction of SPI write? or just before the code "while"?
////////////////////////////////////////////////////////////////// SPI
// SPI Functions: SETUP_SPI, SPI_WRITE, SPI_READ, SPI_DATA_IN
// SPI Prototypes:
_bif void setup_spi(int32 mode);
_bif void spi_write(int8 value);
_bif int8 spi_read(void);
_bif int8 spi_read(int8 value);
_bif int1 spi_data_in(void);
_bif void setup_spi2(int32 mode);
_bif void spi_write2(int8 value);
_bif int8 spi_read2(void);
_bif int8 spi_read2(int8 value);
_bif int1 spi_data_in2(void);
// Constants used in SETUP_SPI() are:
#define SPI_DISABLED 0x00
#define SPI_MASTER 0x20
#define SPI_SLAVE 0x24
#define SPI_SCK_IDLE_HIGH 0x10
#define SPI_SCK_IDLE_LOW 0x00
#define SPI_CLK_DIV_4 0x00
#define SPI_CLK_DIV_16 0x01
#define SPI_CLK_DIV_64 0x02
#define SPI_CLK_T2 0x03
#define SPI_SS_DISABLED 0x01
#define SPI_XMIT_L_TO_H 0x4000
#define SPI_XMIT_H_TO_L 0x0000
#define SPI_SAMPLE_AT_MIDDLE 0x0000
#define SPI_SAMPLE_AT_END 0x8000
//The following are provided for compatibility
#define SPI_L_TO_H SPI_SCK_IDLE_LOW
#define SPI_H_TO_L SPI_SCK_IDLE_HIGH
Code C - [expand] 1#use spi(DO= PIN_C4 , CLK = PIN_C3, ENABLE = PIN_A5, BITS = 16, MASTER, ENABLE_ACTIVE = 0, MSB_FIRST, IDLE = 1)
unsigned int16 spi_xfer(void)
void main()
while(1)
Where is the spi initialization code ? MCLK is the clock for AD9833. 10 MHz + 4xPLL = 40 MHz is for your PIC. I don't know whether you can supply 40 MHz for AD9833s MCLK.
The PIC18F4580 device header shows these functions for SPI. I assume you are using CCS C's SPI functions.
Code:////////////////////////////////////////////////////////////////// SPI // SPI Functions: SETUP_SPI, SPI_WRITE, SPI_READ, SPI_DATA_IN // SPI Prototypes: _bif void setup_spi(int32 mode); _bif void spi_write(int8 value); _bif int8 spi_read(void); _bif int8 spi_read(int8 value); _bif int1 spi_data_in(void); _bif void setup_spi2(int32 mode); _bif void spi_write2(int8 value); _bif int8 spi_read2(void); _bif int8 spi_read2(int8 value); _bif int1 spi_data_in2(void); // Constants used in SETUP_SPI() are: #define SPI_DISABLED 0x00 #define SPI_MASTER 0x20 #define SPI_SLAVE 0x24 #define SPI_SCK_IDLE_HIGH 0x10 #define SPI_SCK_IDLE_LOW 0x00 #define SPI_CLK_DIV_4 0x00 #define SPI_CLK_DIV_16 0x01 #define SPI_CLK_DIV_64 0x02 #define SPI_CLK_T2 0x03 #define SPI_SS_DISABLED 0x01 #define SPI_XMIT_L_TO_H 0x4000 #define SPI_XMIT_H_TO_L 0x0000 #define SPI_SAMPLE_AT_MIDDLE 0x0000 #define SPI_SAMPLE_AT_END 0x8000 //The following are provided for compatibility #define SPI_L_TO_H SPI_SCK_IDLE_LOW #define SPI_H_TO_L SPI_SCK_IDLE_HIGH
So, please show the full code and show the initialization of SPI module.
[syntax]
#use spi(DO= PIN_C4 , CLK = PIN_C3, ENABLE = PIN_A5, BITS = 16, MASTER, ENABLE_ACTIVE = 0, MSB_FIRST, IDLE = 1)
[/syntax]
In the image you posted it the DO appears in blue and I think the compiler is thinking it as Do of Do...WHILE() loop, a reserved word. So, use names like SPI_DO, SPI_CLK, etc... and try.
In hardware SPI module without remappable pins you cant use other pin for the same purpose.
The DO is actually PIN_C5 in the hardware but PIN_C4 in your code.
If you want a single frequency why dont you use a analog crystal oscillator.
For a single frequency sine output you can send the frequency byte only one time and release the reset. for varying the frequency usually we will send in some particular delay timings. Here you don need to send them again and again through a while loop. You can just put the while loop in the bottom to get it working.
Good question. Actually you can't for two reasons.I don't know whether you can supply 40 MHz for AD9833s MCLK.
Where is the spi initialization code ? MCLK is the clock for AD9833. 10 MHz + 4xPLL = 40 MHz is for your PIC. I don't know whether you can supply 40 MHz for AD9833s MCLK.
The PIC18F4580 device header shows these functions for SPI. I assume you are using CCS C's SPI functions.
Code:////////////////////////////////////////////////////////////////// SPI // SPI Functions: SETUP_SPI, SPI_WRITE, SPI_READ, SPI_DATA_IN // SPI Prototypes: _bif void setup_spi(int32 mode); _bif void spi_write(int8 value); _bif int8 spi_read(void); _bif int8 spi_read(int8 value); _bif int1 spi_data_in(void); _bif void setup_spi2(int32 mode); _bif void spi_write2(int8 value); _bif int8 spi_read2(void); _bif int8 spi_read2(int8 value); _bif int1 spi_data_in2(void); // Constants used in SETUP_SPI() are: #define SPI_DISABLED 0x00 #define SPI_MASTER 0x20 #define SPI_SLAVE 0x24 #define SPI_SCK_IDLE_HIGH 0x10 #define SPI_SCK_IDLE_LOW 0x00 #define SPI_CLK_DIV_4 0x00 #define SPI_CLK_DIV_16 0x01 #define SPI_CLK_DIV_64 0x02 #define SPI_CLK_T2 0x03 #define SPI_SS_DISABLED 0x01 #define SPI_XMIT_L_TO_H 0x4000 #define SPI_XMIT_H_TO_L 0x0000 #define SPI_SAMPLE_AT_MIDDLE 0x0000 #define SPI_SAMPLE_AT_END 0x8000 //The following are provided for compatibility #define SPI_L_TO_H SPI_SCK_IDLE_LOW #define SPI_H_TO_L SPI_SCK_IDLE_HIGH
So, please show the full code and show the initialization of SPI module.
Code C - [expand] 1#use spi(DO= PIN_C4 , CLK = PIN_C3, ENABLE = PIN_A5, BITS = 16, MASTER, ENABLE_ACTIVE = 0, MSB_FIRST, IDLE = 1)
In the image you posted it the DO appears in blue and I think the compiler is thinking it as Do of Do...WHILE() loop, a reserved word. So, use names like SPI_DO, SPI_CLK, etc... and try.
Edit:
In the image you posted it shows something strange. See attached image. There is a code
Code:unsigned int16 spi_xfer(void)
betweenandCode:void main()
Code:while(1)
Is it a function prototype or a function definition. If yes, what is it doing in the void main() ?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?