PIC18F2550 sine wave generator using AD557 digital to analog converter - Page 1
Forum section: electronics / general

author: sean87
author_rank: Regular Contributor
timestamp: February 26, 2012, 10:43:22 pm
content: 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>//Variablesunsigned int result;double Yk=0;double Ykmin1=0;double Ykmin2=0;double Xk=0, Xkmin1=0;//Initialize the chipvoid init(void){PORTA=0;TRISA=0b00000000;PORTB=0; //portB latches at 0TRISB=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

author: TerminalJack505
author_rank: Super Contributor
timestamp: February 27, 2012, 12:21:01 am
content: 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?

author: metalphreak
author_rank: Frequent Contributor
timestamp: February 27, 2012, 07:32:05 am
content: From the datasheet:Code:On a Power-on Reset, RB4:RB0 areconfigured as analog inputs by default andread as ‘0’; RB7:RB5 are configured asdigital 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.

author: sean87
author_rank: Regular Contributor
timestamp: February 27, 2012, 09:19:32 am
content: Thanks guys. I am not sure what kind of congfiguration I have to set. Is it should be something likeCode:#pragma config PBADEN = OFFin the code file? I tried that but it didnt change anything. How to deal with that PBADEN correctly?Thanks.

author: metalphreak
author_rank: Frequent Contributor
timestamp: February 27, 2012, 10:02:48 am
content: 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.pdfYou 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 clockThe rest of the configs aren't as important and you can find out what they do in the data sheet.

author: sean87
author_rank: Regular Contributor
timestamp: February 27, 2012, 03:14:43 pm
content: 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?

author: Teknotronix
author_rank: Regular Contributor
timestamp: February 27, 2012, 03:21:00 pm
content: 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:/** 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