Electronics > Microcontrollers

PIC18F2550 sine wave generator using AD557 digital to analog converter

(1/2) > >>

Hi all,
I am trying to make a simple sine wave generator using a PIC18F2550 and AD557 DAC from Analog Devices. The idea is, using equation for sine wave, generate the numbers and make the DAC chip to produce corresponding voltage. What I have done so far is the following code but the PORTB value is always 0...no idea why.

--- Code: ---

#include <p18cxxx.h>
#include <p18F2550.h>
#include <adc.h>
#include <delays.h>
#include <math.h>

unsigned int result;
double Yk=0;
double Ykmin1=0;
double Ykmin2=0;
double Xk=0, Xkmin1=0;

//Initialize the chip
void init(void)
    PORTB=0; //portB latches at 0
    TRISB=0b00000000; // PORTB as output

void main (void)
    Xk = 1;
    Yk = (1.618 * Ykmin1) -Ykmin2 - (Xkmin1 * 0.588);
    result = Yk * 50 + 128;
        Yk = (1.618 * Ykmin1) - Ykmin2 - (Xkmin1 * 0.588);
        result = Yk * 50 + 128;
        PORTB = result;
        Xkmin1 = Xk;
        Ykmin2 = Ykmin1;
        Ykmin1 = Yk;

--- End code ---

And here is the boad. I do not know what is the name of this board:

I don't know much about PICs but I don't see anything wrong with the code.  I even stuck it into the compiler on my laptop and had it spit out the numbers it was generating and they were non-zero values. 

You might start with simply putting a constant non-zero value on PORTB and go from there since, unless there's a bug in your compiler, the number generator code looks okay.

Also, there is a configuration bit that affects PORTB<4:0>.   (See attached.)  Maybe that bit needs to be cleared?

From the datasheet:

--- Code: ---On a Power-on Reset, RB4:RB0 are
configured as analog inputs by default and
read as ‘0’; RB7:RB5 are configured as
digital inputs.
--- End code ---

I don't see any configuration stuff in your code, but you need to program the PBADEN bit to 0 if you haven't already.

Thanks guys. I am not sure what kind of congfiguration I have to set :(. Is it should be something like
--- Code: ---#pragma config PBADEN = OFF
--- End code ---
in the code file? I tried that but it didnt change anything. How to deal with that PBADEN correctly?


Do you have any other pragma config lines in your code?

If not, it sounds like your PIC isn't even booting up correctly without a proper config to match the board.

(if you haven't explicitly set a clock source, it will be in the default "EC oscillator" mode, which is an external clock signal on the OSC1 pin... which means your crystal isn't being driven, and there is no clock for the PIC to run on!)

You need them to configure clock source and many other things. Looking at your board it has an external crystal for clocking.

Read page 63 onwards of this for the 18F2550: http://ww1.microchip.com/downloads/en/DeviceDoc/51537a.pdf

You need to add all the relevant configs for your setup.

Look at the markings on the crystal, and work out what you need to divide it by to get 4Mhz. Then set the PLL prescaler appropriately. (ex. 20MHz marking, needs DIV by 5 to get 4mhz)

CPUDIV = OSC1_PLL2 will give you 48MHz clock speed (12million instructions per second)

FOSC = HSPLL_HS sets the clock source to be external crystal, which also drives the PLL (to get 96MHz). Previous config (OSC1_PLL2) divides 96MHz by 2 to get ur system clock

The rest of the configs aren't as important and you can find out what they do in the data sheet.


[0] Message Index

[#] Next page

There was an error while thanking
Go to full version