     1: //Project : memory_to_DAC.c

     2: //Date    : 5/23/2005

     3: //Author  : Curtis Rhodes

     4: //Purpose : Reads audio data from memory chips and

     5:                 //      sends the information to serial DAC

     6: // Synchronous Serial Port (for memory chip interface)

     7: //          RC3 (pin 18) == SCL (Clock for Memory Chip)

     8: //          RC4 (pin 23) == SDA (Serial Data for Memory Chip)

     9: //   

    10: //Algorithm:

    11: //              Reads sequentially through all 8 memory chips.

    12: //              After each read, the information is sent to a

    13: //              serial DAC.

    14: //Chip type   : PIC16F877A

    15: //Clock frequency     : 20 MHz

    16: 

    17: #include <pic.h>

    18: #include "memory_to_DAC.h"

    19: #include "stdlib.h"

    20: #include "stdio.h"

    21: #if defined(_16F84) 

    22: __CONFIG(HS & WDTDIS & PWRTEN & MPEDIS);

    23: #endif

    24: #if defined(_16F877) || defined(_16F877A) 

    25: __CONFIG(HS & WDTDIS & PWRTEN & LVPDIS);

    26: #endif

    27: //===================================================

    28: // main program

    29: 

    30: unsigned char SCIword;

    31: unsigned char hexValue1;

    32: unsigned char hexValue2;

    33: unsigned int i;

    34: unsigned int read_control;

    35: unsigned char write_data;

    36: unsigned char address_high;

    37: unsigned char address_low;

    38: unsigned char data;

    39: 

    40: //Serial Clock Definitions

    41: #define DAC_clock                RC2

    42: #define DAC_latch_enable RC1

    43: #define DAC_data                 RD7

    44: 

    45: //Play Status is an LED that toggles when

    46: // the program switches between memory chips

    47: #define Play_Status_LED  RC0

    48: 

    49: void main(void)

    50: {

    51:         portInitialization();

    52:         initializeI2C();

    53:         address_high = 0x00;

    54:         address_low = 0x00;                                                     // Starts read at first address

    55:         read_control = MEMORY_CHIP0_READ_CONTROL;       // of first memory chip

    56: 

    57:         Play_Status_LED = 1;  // RC0 is used to 

    58:         while(1) {

    59:                 start_sequential_read(read_control,0x00,0x00);

    60:         

    61:                 for(i=0; i<32768; i++) {

    62:                         hexValue1 = sequential_memory_chip_read();

    63:                         acknowledge();

    64:                         hexValue2 = sequential_memory_chip_read();

    65:                         if(i!=32767) {

    66:                                 acknowledge();

    67:                         }

    68:         ///////////////////////////////Clock in Serial DAC data

    69:                         DAC_clock = 0;

    70:                         PORTD = hexValue1;

    71:                         DAC_latch_enable = 0;

    72:                         DAC_clock = 1;

    73:         

    74:                         DAC_clock = 0;

    75:                         PORTD = (PORTD<<1);

    76:                         DAC_clock = 1;

    77:         

    78:                         DAC_clock = 0;

    79:                         PORTD = (PORTD<<1);

    80:                         DAC_clock = 1;

    81:         

    82:                         DAC_clock = 0;

    83:                         PORTD = (PORTD<<1);

    84:                         DAC_latch_enable = 1;

    85:                         DAC_clock = 1;

    86:         

    87:         ///////////////////////////////Clock in Serial DAC data

    88:                         DAC_clock = 0;

    89:                         PORTD = (PORTD<<1);

    90:                         DAC_clock = 1;

    91:         

    92:                         DAC_clock = 0;

    93:                         PORTD = (PORTD<<1);

    94:                         DAC_clock = 1;

    95:         

    96:                         DAC_clock = 0;

    97:                         PORTD = (PORTD<<1);

    98:                         DAC_clock = 1;

    99:         

   100:                         DAC_clock = 0;

   101:                         PORTD = (PORTD<<1);

   102:                         DAC_clock = 1;

   103:         ///////////////////////////////Clock in Serial DAC data

   104:                         DAC_clock = 0;

   105:                         PORTD = hexValue2;

   106:                         DAC_clock = 1;

   107:         

   108:                         DAC_clock = 0;

   109:                         PORTD = (PORTD<<1);

   110:                         DAC_clock = 1;

   111:         

   112:                         DAC_clock = 0;

   113:                         PORTD = (PORTD<<1);

   114:                         DAC_clock = 1;

   115:         

   116:                         DAC_clock = 0;

   117:                         PORTD = (PORTD<<1);

   118:                         DAC_clock = 1;

   119:         ///////////////////////////////Clock in Serial DAC data

   120:                         DAC_clock = 0;

   121:                         PORTD = (PORTD<<1);

   122:                         DAC_clock = 1;

   123:         

   124:                         DAC_clock = 0;

   125:                         PORTD = (PORTD<<1);

   126:                         DAC_clock = 1;

   127:         

   128:                         DAC_clock = 0;

   129:                         PORTD = (PORTD<<1);

   130:                         DAC_clock = 1;

   131:         

   132:                         DAC_clock = 0;

   133:                         PORTD = (PORTD<<1);

   134:                         DAC_clock = 1;

   135:                 }

   136:                 Play_Status_LED = !Play_Status_LED;

   137:                 send_Stop_Bit();

   138:         

   139:                 if(read_control == MEMORY_CHIP7_READ_CONTROL) {

   140:                         // Loops back to beginning of audio

   141:                         read_control = MEMORY_CHIP0_READ_CONTROL;

   142:                 }

   143:                 else {

   144:                         read_control = read_control + 2;

   145:                 }

   146:         }

   147: }

   148: 

   149: void portInitialization() {

   150:         TRISD = 0x00; // set PORTD as output

   151:         TRISE = 0x00; // set PORTE as output

   152: 

   153:         TRISC2 = 0;     // RC5 is DAC clock

   154:         TRISC1 = 0;     // RC4 is DAC latch enable

   155: 

   156:         TRISC0 = 0; //Toggled when moving to the next chip

   157: }

