Hello everyone, this is my first post here and I apologize if my thread is confusing in any way.
I am a beginner when it comes to PIC programming and i encountered a strange problem while trying to command a DDS chip ( AD9850 ). I have successfully created a program able to load parallel data on the chip and it worked very. I have implemented the code using Mikro C ( in which I have a little bit of experience ) but I am trying to convert the program to MPLAB X because I find the XC8 compiler to be more suited for working with float variables. I have zero experience with MPLAB, but I followed some tutorial online and I was able to successfully blink a LED just to get a hang of it.
This is the code in MPLAB with the main function and the other two I am using :
#include <xc.h>
#include "XC8.h"
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#define FU_UD RA1
#define RESET RA2
#define W_CLK RA0
#define data_DDS PORTC
void DDS_RESET()
{
W_CLK = 0 ;
FU_UD = 0 ;
RESET = 0 ;
__delay_ms(5) ;
FU_UD = 1 ;
__delay_ms(5) ;
RESET = 1 ;
__delay_ms(5) ;
RESET = 0 ;
__delay_ms(5) ;
}
void parallel_DDS( float freq )
{
int i ;
static uint8_t W_1[5] ;
uint32_t tuning_word ;
long double pow_2 = 4294967295;
long double fs = 125000000;
tuning_word = (uint32_t) ((freq /fs) * pow_2 + 0.5);
W_1[4] = tuning_word ;
W_1[3] = tuning_word>> 8 ;
W_1[2] = tuning_word >> 16 ;
W_1[1] = tuning_word >> 24 ;
W_1[0] = 0x00 ;
FU_UD = 0 ;
for( i = 0 ; i < 5 ; i++ )
{
data_DDS = W_1 ;
W_CLK = 1 ;
W_CLK = 0 ;
}
FU_UD = 1 ;
}
void main(void)
{
OSCCON = 0x77 ; // Internal osc 8MHz
TRISA = 0x00 ;
TRISC = 0x00 ;
DDS_RESET() ;
ANSEL = 0 ;
ANSELH = 0 ;
while(1)
{
parallel_DDS( 1000 ) ;
}
}
and the configuration bits :
#pragma config FOSC = INTRC_NOCLKOUT// Oscillator Selection bits (INTOSCIO oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = OFF // RE3/MCLR pin function select bit (RE3/MCLR pin function is digital input, MCLR internally tied to VDD)
#pragma config CP = OFF // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = OFF // Brown Out Reset Selection bits (BOR disabled)
#pragma config IESO = OFF // Internal External Switchover bit (Internal/External Switchover mode is disabled)
#pragma config FCMEN = ON // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is enabled)
#pragma config LVP = OFF // Low Voltage Programming Enable bit (RB3 pin has digital I/O, HV on MCLR must be used for programming)
// CONFIG2
#pragma config BOR4V = BOR40V // Brown-out Reset Selection bit (Brown-out Reset set to 4.0V)
#pragma config WRT = OFF // Flash Program Memory Self Write Enable bits (Write protection off)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#include <xc.h>
#define _XTAL_FREQ 8000000
The CLK, frequency upload, and reset bit are connected to RA. It is strange why the code works well in Mikro C and I can't get it to work in MPLAB , I miss something for sure. If some one could help me it would be grate.
The code build without errors, I am using MPLAB X IDE v5.35 and XC8 v2.10
I am a beginner when it comes to PIC programming and i encountered a strange problem while trying to command a DDS chip ( AD9850 ). I have successfully created a program able to load parallel data on the chip and it worked very. I have implemented the code using Mikro C ( in which I have a little bit of experience ) but I am trying to convert the program to MPLAB X because I find the XC8 compiler to be more suited for working with float variables. I have zero experience with MPLAB, but I followed some tutorial online and I was able to successfully blink a LED just to get a hang of it.
This is the code in MPLAB with the main function and the other two I am using :
#include <xc.h>
#include "XC8.h"
#include <stdio.h>
#include <string.h>
#include <stdint.h>
#define FU_UD RA1
#define RESET RA2
#define W_CLK RA0
#define data_DDS PORTC
void DDS_RESET()
{
W_CLK = 0 ;
FU_UD = 0 ;
RESET = 0 ;
__delay_ms(5) ;
FU_UD = 1 ;
__delay_ms(5) ;
RESET = 1 ;
__delay_ms(5) ;
RESET = 0 ;
__delay_ms(5) ;
}
void parallel_DDS( float freq )
{
int i ;
static uint8_t W_1[5] ;
uint32_t tuning_word ;
long double pow_2 = 4294967295;
long double fs = 125000000;
tuning_word = (uint32_t) ((freq /fs) * pow_2 + 0.5);
W_1[4] = tuning_word ;
W_1[3] = tuning_word>> 8 ;
W_1[2] = tuning_word >> 16 ;
W_1[1] = tuning_word >> 24 ;
W_1[0] = 0x00 ;
FU_UD = 0 ;
for( i = 0 ; i < 5 ; i++ )
{
data_DDS = W_1 ;
W_CLK = 1 ;
W_CLK = 0 ;
}
FU_UD = 1 ;
}
void main(void)
{
OSCCON = 0x77 ; // Internal osc 8MHz
TRISA = 0x00 ;
TRISC = 0x00 ;
DDS_RESET() ;
ANSEL = 0 ;
ANSELH = 0 ;
while(1)
{
parallel_DDS( 1000 ) ;
}
}
and the configuration bits :
#pragma config FOSC = INTRC_NOCLKOUT// Oscillator Selection bits (INTOSCIO oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = OFF // RE3/MCLR pin function select bit (RE3/MCLR pin function is digital input, MCLR internally tied to VDD)
#pragma config CP = OFF // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = OFF // Brown Out Reset Selection bits (BOR disabled)
#pragma config IESO = OFF // Internal External Switchover bit (Internal/External Switchover mode is disabled)
#pragma config FCMEN = ON // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is enabled)
#pragma config LVP = OFF // Low Voltage Programming Enable bit (RB3 pin has digital I/O, HV on MCLR must be used for programming)
// CONFIG2
#pragma config BOR4V = BOR40V // Brown-out Reset Selection bit (Brown-out Reset set to 4.0V)
#pragma config WRT = OFF // Flash Program Memory Self Write Enable bits (Write protection off)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
#include <xc.h>
#define _XTAL_FREQ 8000000
The CLK, frequency upload, and reset bit are connected to RA. It is strange why the code works well in Mikro C and I can't get it to work in MPLAB , I miss something for sure. If some one could help me it would be grate.
The code build without errors, I am using MPLAB X IDE v5.35 and XC8 v2.10
Last edited: