Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

MCP4921 with PIC16F877A

Status
Not open for further replies.

MrElectroniceng

Newbie level 5
Joined
Feb 15, 2014
Messages
9
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
55
Dear all,

I am trying to communicate PIC16F877A with MCP4921, which is a serial input DAC. I am using HI TECH C compiler for the code. I could not achieve to obtain analog output. I tried many things, still couldn't work. The design and the code is below. How can I fix it? Where is my mistake?

Thank you in advance.

connection.png

Code:
#include <htc.h>
#include <stdio.h>
#include <stdlib.h>
#define _XTAL_FREQ 20000000

#define DAC_CS     RB7
#define DAC_SCK    RB6
#define DAC_SDI    RC0
#define DAC_LDAC   RC1


void init_dac()
{
   DAC_CS = 0b1;
   DAC_LDAC = 0b1;
   DAC_SCK = 0b1;
   DAC_SDI = 0b1;
}

void write_dac(unsigned int data) {
   unsigned char i;
   unsigned short long tobesent;
   
   tobesent = (short)0x030000UL || data;                 
   
   DAC_LDAC = 0b1;
   DAC_SCK = 0b0;
   DAC_CS = 0b0;

   for(i=0; i<=23; ++i)
   {
		__delay_us(10);

      if(i<4 || (i>7 && i<12))
         tobesent = tobesent << 1u;
      else
      {
		 DAC_SDI = (bit) (tobesent / (short) 0x800000UL);
		 //DAC_SDI = 0b1;
		 tobesent = tobesent << 1u;
         DAC_SCK = 0b1;
         DAC_SCK = 0b0;
      }
   }
   DAC_CS = 0b1;

   DAC_LDAC = 0b0;
   __delay_us(10);
   
   DAC_LDAC = 0b1;
}


Code:
#include <htc.h>
#include <stdio.h>
#include <stdlib.h>

#define _XTAL_FREQ 20000000  

#include "mcp4921.h"


__CONFIG(HS & WDTDIS & PWRTEN & BOREN & LVPEN & WRTEN &
DEBUGDIS & DUNPROT & UNPROTECT);




void init(void) {


	TRISA = 0b111110;		
	TRISB = 0b00000000;  
	TRISC = 0b00000000;		
	TRISD = 0b00000000;		
	TRISE = 0b100;			
	
	PORTA = 0x00;
	PORTB = 0x00;
	PORTC = 0x00;
	PORTD = 0x00;
	PORTE = 0x00;

	OPTION = 0b10000000;  	
						  	
	
	INTCON = 0b01100000;	
							

	T1CON=0b00110101;		
							
						

	ADCON1=0x0f;			

}





void main(void) {
	init();
	init_dac();

	while(1) {
		write_dac(0x0000);
		__delay_ms(1000);
		write_dac(0x77F0);
		__delay_ms(1000);
		write_dac(0x77FF);
		__delay_ms(1000);
	}
}


View attachment TEST_DAC.zip
 
Last edited:

No time to test the code by creating a project in MPLAB and Proteus. If possible zip and attach complete MPLAB Hi-Tech PICC project files + Proteus file.
 

I attached the file with error. It is under the code part in the first post.

Thank you
 
Last edited:

Thank you very much, milan.rajik. The code works great!! When fixing the code I used SPI property of the pic but your code looks more efficient. If you want I can upload my code.
 

Upload your code. Yours is hardware SPI code and mine is software SPI code. For software SPI you can use any digital IO pin(s) for interfacing SPI device to MCU.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top