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.
View attachment TEST_DAC.zip
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.
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: