没有任何运气通过180 F2231与DAC(MCP421)通过无数文档和例子进行扫描,似乎缺少一些东西。我附上了一个粗略的示意图,PIC和DACCODE的相关寄存器如下。
以上来自于百度翻译
以下为原文
Not having any luck communica
ting an 18F2331 with a DAC(MCP4921) via SPI
Looked at countless documents and examples and seem to be missing something.
I've attached a rough schematic and the relevant registers from the PIC and the DAC
Code is below.
#include "18f2331_Internal.h"
void initMain(){
OSCCON = 0b01101100;
OSCTUNE = 0x00;
TRISCbits.TRISC7 = 0; // PinRC7 output - SDO
//TRISCbits.TRISC6 = 0; //PinRC6 output - SS (not sure if required)
TRISCbits.TRISC5 = 0; // PinRC5 output - SPI Clock
TRISBbits.TRISB4 = 0; // PinRB4 output - for use as CS
TRISBbits.TRISB2 = 0; // PinRB2 output - for LED to test if code gets this far.
SSPCONbits.CKP = 0;
SSPCONbits.SSPM0 = 0;
SSPCONbits.SSPM1 = 0;
SSPCONbits.SSPM2 = 0;
SSPCONbits.SSPM3 = 0;
SSPSTATbits.SMP = 0;
SSPSTATbits.CKE = 0;
SSPCONbits.SSPEN = 1;
}
#define _XTAL_FREQ 4000000
void main(void) {
initMain();
while(1){
LATBbits.LATB4 = 1;
__delay_ms(500);
LATBbits.LATB4 = 0;
SSPBUF = 0b00011111;
while(SSPSTATbits.BF == 0)
{
}
SSPBUF = 0b11111111;
LATBbits.LATB4 = 1;
__delay_ms(2000);
LATBbits.LATB4 = 0;
SSPBUF = 0b00010000;
while(SSPSTATbits.BF == 0)
{
}
SSPBUF = 0b00111111;
LATBbits.LATB4 = 1;
// Toggle LED on and off to see if it gets this far
LATBbits.LATB2 = 1;
__delay_ms(500);
LATBbits.LATB2 = 0; //LED off
__delay_ms(500);
}
return;
}
Attached Image(s)
0