Author Topic: PIC18F2550 sine wave generator using AD557 digital to analog converter  (Read 4725 times)

0 Members and 1 Guest are viewing this topic.

Offline sean87Topic starter

  • Regular Contributor
  • *
  • Posts: 150
  • Country: nl
  • EE WannaBee
    • EMBEDONIX
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: [Select]


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

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

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

void main (void)
{
    init();
    Xk = 1;
    Yk = (1.618 * Ykmin1) -Ykmin2 - (Xkmin1 * 0.588);
    result = Yk * 50 + 128;
    PORTB=result;
    Xkmin1=Xk;
    Ykmin2=Ykmin1;
    Ykmin1=Yk;
    Xk=0;
   
    while(1)
    {
        Yk = (1.618 * Ykmin1) - Ykmin2 - (Xkmin1 * 0.588);
        result = Yk * 50 + 128;
        PORTB = result;
        Xkmin1 = Xk;
        Ykmin2 = Ykmin1;
        Ykmin1 = Yk;
        Delay10KTCYx(100);   
 }
}

 
And here is the boad. I do not know what is the name of this board:
 
 
http://img191.imageshack.us/img191/994/photo0818c.jpg
EMBEDONIX - Embedded Systems, Linux and good stuff!
 

Offline TerminalJack505

  • Super Contributor
  • ***
  • Posts: 1310
  • Country: 00
Re: PIC18F2550 sine wave generator using AD557 digital to analog converter
« Reply #1 on: February 27, 2012, 12:21:01 am »
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?
 

Offline metalphreak

  • Frequent Contributor
  • **
  • Posts: 815
  • Country: au
  • http://d.av.id.au
    • D.av.id.AU
Re: PIC18F2550 sine wave generator using AD557 digital to analog converter
« Reply #2 on: February 27, 2012, 07:32:05 am »
From the datasheet:

Code: [Select]
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.

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.

Offline sean87Topic starter

  • Regular Contributor
  • *
  • Posts: 150
  • Country: nl
  • EE WannaBee
    • EMBEDONIX
Re: PIC18F2550 sine wave generator using AD557 digital to analog converter
« Reply #3 on: February 27, 2012, 09:19:32 am »
Thanks guys. I am not sure what kind of congfiguration I have to set :(. Is it should be something like
Code: [Select]
#pragma config PBADEN = OFF in the code file? I tried that but it didnt change anything. How to deal with that PBADEN correctly?

Thanks.
EMBEDONIX - Embedded Systems, Linux and good stuff!
 

Offline metalphreak

  • Frequent Contributor
  • **
  • Posts: 815
  • Country: au
  • http://d.av.id.au
    • D.av.id.AU
Re: PIC18F2550 sine wave generator using AD557 digital to analog converter
« Reply #4 on: February 27, 2012, 10:02:48 am »
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.
« Last Edit: February 27, 2012, 10:12:05 am by metalphreak »
 

Offline sean87Topic starter

  • Regular Contributor
  • *
  • Posts: 150
  • Country: nl
  • EE WannaBee
    • EMBEDONIX
Re: PIC18F2550 sine wave generator using AD557 digital to analog converter
« Reply #5 on: February 27, 2012, 03:14:43 pm »
Thanks for info, I got the idea of configuration but I am still not sure where and how I should add them in my code...I am using C18 compiler. Can you give a little example please?
EMBEDONIX - Embedded Systems, Linux and good stuff!
 

Offline Teknotronix

  • Regular Contributor
  • *
  • Posts: 146
  • Country: au
Re: PIC18F2550 sine wave generator using AD557 digital to analog converter
« Reply #6 on: February 27, 2012, 03:21:00 pm »
Here is an example of my includes and config section in the top of a main.c file for a project using an 18F4458. Target circuit uses a 4Mhz crystal.

Code: [Select]
/** INCLUDES *******************************************************/
#include "usb.h"
#include "HardwareProfile.h"
#include "usb_function_hid.h"
#include "usb_function_cdc.h"

#include "GenericTypeDefs.h"
#include "Compiler.h"
#include "usb_config.h"
#include "USB/usb_device.h"
#include "USB/usb.h"
#include "HardwareProfile.h"

/** CONFIGURATION **************************************************/
#pragma config PLLDIV   = 1
#pragma config CPUDIV   = OSC1_PLL2
#pragma config USBDIV   = 2
#pragma config FOSC     = XTPLL_XT
#pragma config FCMEN    = OFF
#pragma config IESO     = OFF
#pragma config PWRT     = OFF
#pragma config BOR      = ON
#pragma config BORV     = 3
#pragma config VREGEN   = ON
#pragma config WDT      = OFF
#pragma config WDTPS    = 32768
#pragma config MCLRE    = ON
#pragma config LPT1OSC  = OFF
#pragma config PBADEN   = OFF
//      #pragma config CCP2MX   = ON
#pragma config STVREN   = ON
#pragma config LVP      = OFF
//      #pragma config ICPRT    = OFF
#pragma config XINST    = OFF
#pragma config CP0      = OFF
#pragma config CP1      = OFF
//      #pragma config CP2      = OFF
//      #pragma config CP3      = OFF
#pragma config CPB      = OFF
//      #pragma config CPD      = OFF
#pragma config WRT0     = OFF
#pragma config WRT1     = OFF
//      #pragma config WRT2     = OFF
//      #pragma config WRT3     = OFF
#pragma config WRTB     = OFF
#pragma config WRTC     = OFF
//      #pragma config WRTD     = OFF
#pragma config EBTR0    = OFF
#pragma config EBTR1    = OFF
//      #pragma config EBTR2    = OFF
//      #pragma config EBTR3    = OFF
#pragma config EBTRB    = OFF
#pragma config DEBUG = OFF
« Last Edit: February 27, 2012, 03:23:33 pm by Teknotronix »
Don't drone me bro!

 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf