// D2A.C // // MCP4921 Diver // RC0: CS // RC3: CLK // RC5: DATA //Global Variables const unsigned char MSG0[21] = "D/A.C "; const unsigned char MSG1[21] = "Square Wave "; const unsigned int TABLE[16] = {2204, 2336, 2425, 2456, 2425, 2336, 2204, 2047, 1890, 1758, 1669, 1638, 1669, 1758, 1890, 2047}; // Subroutine Declarationsb #include // Subroutines #include "lcd_portd.c" void D2A(unsigned int X) { unsigned char i; TRISC0 = 0; TRISC3 = 0; TRISC5 = 0; // Add 0011 to the first four bits to set up the D/A X = X & 0x0FFF; X = X + 0x3000; RC0 = 1; RC3 = 1; // CS goes low to select the D/A chip RC0 = 0; // Send out 16 bits of data for (i=0; i<16; i++) { if (X & 0x8000) RC5 = 1; else RC5 = 0; RC3 = 0; X = X << 1; RC3 = 1; } // CS goes high to terminate the communicaitons RC0 = 1; } // Main Routine void main(void) { unsigned int i, j; int x, y, dx; ADCON1 = 0x0F; TRISA = 0; TRISB = 0; TRISC = 0; TRISD = 0; TRISE = 0; // Turn on the LCD LCD_Init(); // initialize the LCD LCD_Move(0,0); for (i=0; i<20; i++) LCD_Write(MSG0[i]); LCD_Move(1,0); for (i=0; i<20; i++) LCD_Write(MSG1[i]); // Square Wave /* while(1) { D2A(1638); for(i=1; i<3100; i++); D2A(2457); for(i=0; i<3100; i++); } */ // Sawtooth Wave /* x = 1638; while(1) { D2A(x); x = x + 10; if(x > 2457) x = 1638; for(i=0; i<100; i++); } */ // Triangle Wave /* x = 1638; dx = +10; while(1) { D2A(x); x = x + dx; if(x > 2457) dx = -10; if(x < 1638) dx = +10; for(i=0; i<100; i++); } */ //Parabolic Sine Wave /* x = 0; dx = 0; while(1) { x = (x + 1) % 64; if(x == 0) dx = dx ^ 1; if(dx) y = 2048 + (2 * x * (64-x))/5; else y = 2048 - (2 * x * (64-x))/5; D2A(y); for(i=0; i<100; i++); } */ // Look Up Table while(1) { i = (i + 1) % 16; D2A(TABLE[i]); for(j=0; j<177; j++); } }