#include<htc.h>
//__CONFIG(1,PLLDIV4 & CPUDIV_OSC1_PLL2 & USBDIV_2 & IESO_OFF & FCMEN_OFF & FOSC_HSPLL_FOSC = INTOSC_EC);
__CONFIG(1,PLLDIV2 & IESO_OFF & FCMEN_OFF & FOSC_HS);
__CONFIG(2,VREGEN_OFF & BOR_OFF & BORV_0 & PWRT_ON & WDT_OFF);
__CONFIG(3, MCLRE_ON & PBADEN_OFF & LPT1OSC_OFF & CCP2MX_ON);
__CONFIG(4,DEBUG_ON & LVP_OFF & STVREN_OFF & XINST_OFF);
#define _XTAL_FREQ 8000000
#define chip_sel RB3
void dac(unsigned int, unsigned int);
const unsigned int sine [28]={2048,2503,2936,3324,3648,3892,4044,4095,4044,3892,3648,3324,2936,2503,2048,1592,1159,771,447,203,51,0,51,203,447,771,1159,1592}; //works well modified sine wave
unsigned char count=0,new_sector=0, old_sector=0,i=0, j=7; //14
unsigned int value=0,time=0;
unsigned long time_div=0;
void main(){
TRISB=5;
PORTB=0;
TRISC=0;
PORTC=0;
PORTA=0;
TRISA=0;
ADCON1=0x0f;
CMCON=0x07;
GIE=1;
PEIE=1;
TMR2IP=1;
TMR2IE=1;
PR2=100; //hv to adjust this value for exact frequency
TMR2=0;
SSPSTAT=0xC0; //Status Register SSPSTAT=11000000
SSPCON1=0x20; //Enables serial port pins & set the SPI clock as clock = FOSC/4
T2CON=0x04;
while(1);
}
void dac(unsigned int data, unsigned int dac_sel)
{
unsigned int lower_bits;
unsigned int upper_bits;
//first obtain the upper 8 bits
upper_bits = data/256; // obtain the upper 4 bits
upper_bits = (dac_sel) | upper_bits; // append 48 to the above 4 bits for DACa o/p n 176 for DACb o/p
//now obtain the lower 8 bits
lower_bits = 255 & data; // ANDing separates the lower 8 bits
chip_sel=0;
SSPBUF=upper_bits; // sending the upper 8 bits serially
while(!SSPSTATbits.BF); // wait until the upper 8 bits are sent
SSPBUF=lower_bits; // sending the lower 8 bits serially
while(!SSPSTATbits.BF); // wait until the lower 8 bits are sent
chip_sel=1;
}
void interrupt tc_int(){
if(TMR2IE && TMR2IF){
dac(sine[i],48);
dac(sine[j],176);
i++;if(i>=28)i=0;
j++;if(j>=28)j=0;
TMR2IF=0;
}
}
DEBUG_ON
DEBUG_OFF
unsigned char SSP1_Wait_IF(void)
{
while(!SSPIF);
SSPIF = 0;
return SSPBUF;
}
#ifndef _SPI_H
#define _SPI_H
// ----------------------------
// MSSP1 SPI Port config
// ----------------------------
#define SPI1_CLK_TRIS TRISBbits.RB1
//#define SPI1_SDI_TRIS TRISCbits.RB0
#define SPI1_SDO_TRIS TRISCbits.RC7
// INIT I/O for MSSP1 SPI bus
// #define INIT_SPI1_PORT() {SPI1_CLK_TRIS=0; SPI1_SDO_TRIS=0; SPI1_SDI_TRIS=1;}
#define INIT_SPI1_PORT() {SPI1_CLK_TRIS=0;SPI1_SDO_TRIS=0;}
// MSSP1 SPI Config and control
// ----------------------------
#define SPI1_CONFIG 0b00000000 // Master, Clock = FOSC/4, MSSP Disabled
#define SPI1_Enable() (SSPCON1bits.SSPEN=1)
#define SPI1_Disable() (SSPCON1bits.SSPEN=0)
// Init MSSP1 SPI bus
#define SPI1_Init() {SSPCON1=SPI1_CONFIG; SSPSTATbits.CKE=1; SSPSTATbits.SMP=0; PIR1bits.SSPIF=0;} //ckp=0. cke=1 mode 0, 0
// ----------------------------
// SPI1 TX/RX primitives
// ----------------------------
// Wait transfer complete (pooling interrupt flag)
// return content of TX/RX Buffer after transfer complete (received data)
unsigned char SSP1_Wait_IF(void);
// Send a byte to the SPI1 bus
#define SPI1_SEND(DATA) {SSPBUF = DATA; SSP1_Wait_IF();}
// Read a byte from the SPI1 bus (send dummy to generate clock)
#define SPI1_RECEIVE(DATA) {SSPBUF = 0; DATA = SSP1_Wait_IF();}
#endif
#include<htc.h>
#include "spi.h"
__CONFIG(1,PLLDIV2 & CPUDIV_OSC1_PLL2 & USBDIV_2 & IESO_OFF & FCMEN_OFF & FOSC_HSPLL_HS);
//__CONFIG(1,PLLDIV2 & IESO_OFF & FCMEN_OFF & FOSC_HS);
__CONFIG(2,VREGEN_OFF & BOR_ON & BORV_3 & PWRT_OFF & WDT_OFF & WDTPS_32768);
__CONFIG(3, MCLRE_ON & PBADEN_OFF & LPT1OSC_OFF & CCP2MX_ON);
__CONFIG(4,DEBUG_ON & LVP_OFF & STVREN_ON & XINST_OFF);
__CONFIG(5,CP0_OFF & CP1_OFF & CP2_OFF & CP3_OFF & CPB_OFF & CPD_OFF);
__CONFIG(6,WRT0_OFF & WRT1_OFF & WRT2_OFF & WRT3_OFF & WRTC_OFF & WRTB_OFF & WRTD_OFF);
__CONFIG(7,EBTR0_OFF & EBTR1_OFF & EBTR2_OFF & EBTR3_OFF & EBTRB_OFF);
#define _XTAL_FREQ 32000000
#define chip_sel RB3
void dac(unsigned int, unsigned int);
const unsigned int sine [28]={2048,2503,2936,3324,3648,3892,4044,4095,4044,3892,3648,3324,2936,2503,2048,1592,1159,771,447,203,51,0,51,203,447,771,1159,1592}; //works well modified sine wave
unsigned char count=0,new_sector=0, old_sector=0,i=0, j=14; //7
unsigned int value=0,time=0;
unsigned long time_div=0;
void main(){
ADCON1=0x0f;
CMCON=0x07;
GIE=1;
PEIE=1;
SSPIE=1;
TMR2IP=1;
TMR2IE=1;
PR2=100; //freq 595 hz this value is without any calculations
TMR2=0;
INIT_SPI1_PORT(); //init spi pins
SPI1_Init() ; //init spi
T2CON=0x7C;
while(1){
}
}
void dac(unsigned int data, unsigned int dac_sel)
{
unsigned int lower_bits;
unsigned int upper_bits;
//first obtain the upper 8 bits
upper_bits = data/256; // obtain the upper 4 bits
upper_bits = (dac_sel) | upper_bits; // append 48 to the above 4 bits for DACa o/p n 176 for DACb o/p
//now obtain the lower 8 bits
lower_bits = 255 & data; // ANDing separates the lower 8 bits
SPI1_Enable() ;
chip_sel=0;
SPI1_SEND(upper_bits);
SPI1_SEND(lower_bits);
chip_sel=1;
SPI1_Disable();
}
void interrupt tc_int(){
if(TMR2IE && TMR2IF){
dac(sine[i],48);
dac(sine[j],176);
i++;if(i>=28)i=0;
j++;if(j>=28)j=0;
TMR2IF=0;
}
}
You are using bit-banging SPI instead of the PIC hardware SPI. It's hardly possible to achieve a similar speed this way.Ok. Here you go. I am attaching the mikroC PRO PIC project I made. You can't reduce the delays more.
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 sbit MCP4922_CS at LATB3_bit; sbit MCP4922_LDAC at LATB2_bit; sbit MCP4922_SHDN at LATB4_bit; sbit MCP4922_CS_Direction at TRISB3_bit; sbit MCP4922_LDAC_Direction at TRISB2_bit; sbit MCP4922_SHDN_Direction at TRISB4_bit; #define WRITE_TO_DAC_A 0 #define WRITE_TO_DAC_B 1 #define BUFFERED 1 #define UNBUFFERED 0 #define GAIN_1x 1 #define GAIN_2x 0 #define ACTIVE_MODE 1 #define SHUTDOWN 0 void MCP4922_Init() { MCP4922_CS_Direction = 0; MCP4922_LDAC_Direction = 0; MCP4922_SHDN_Direction = 0; MCP4922_CS = 1; MCP4922_LDAC = 1; MCP4922_SHDN = 1; SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV4, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH); } void MCP_Send(unsigned int ab, unsigned int buf, unsigned int ga, unsigned int mode, unsigned int value) { char i = 0; unsigned int mask = 0x8000; value = value & 0x0FFF; value |= (ab << 15); value |= (buf << 14); value |= (ga << 13); value |= (mode << 12); _asm { nop; nop; } MCP4922_LDAC = 1; _asm { nop; nop; } MCP4922_CS = 0; _asm { nop; nop; } SPI1_Write((value & 0xFF00) >> 8); SPI1_Write(value & 0x00FF); _asm { nop; } MCP4922_CS = 1; _asm { nop; nop; } MCP4922_LDAC = 0; _asm { nop; nop; } MCP4922_LDAC = 1; } signed int j = 0; void main() { ADCON1 = 0x0F; TRISA = 0xC0; TRISB = 0x00; TRISC = 0x00; PORTA = 0x00; PORTB = 0x00; PORTC = 0x00; LATA = 0x00; LATB = 0x00; LATC = 0x00; MCP4922_Init(); while(1) { for(j = 0; j < 4096; j++) { MCP_Send(WRITE_TO_DAC_A, UNBUFFERED, GAIN_2x, ACTIVE_MODE, j); Delay_ms(1); } Delay_ms(2000); for(j = 4095; j >= 0; j--) { MCP_Send(WRITE_TO_DAC_A, UNBUFFERED, GAIN_2x, ACTIVE_MODE, j); Delay_ms(1); } Delay_ms(2000); } }
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?