     1: //Project : memoryProgrammer.c

     2: //Date    : 5/23/2005

     3: //Author  : Curtis Rhodes with Additions from Dr. Song's SCI communication code

     4: //Purpose : I2C Memory Chip Interface Subroutines

     5: // Synchronous Serial Port

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

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

     8: //

     9: // Serial port

    10: //          RC6 (pin 25) == TX pin to send data to PC

    11: //          RC7 (pin 26) == RX pin to receive data from PC

    12: //          

    13: //Chip type   : PIC16F877A

    14: //Clock frequency     : 20 MHz

    15: 

    16: #include <pic.h>

    17: #include "memoryProgrammer.h"

    18: #include "stdlib.h"

    19: #include "stdio.h"

    20: #if defined(_16F84) 

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

    22: #endif

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

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

    25: #endif

    26: //===================================================

    27: // main program

    28: const char      TITLE1[] = {"Serial Communication Established."};

    29: const char      TITLE2[] = {"Please Place Front Left EEPROMs into circuit and send file."};

    30: const char      TITLE3[] = {"Please Place Front Right EEPROMs into circuit and send file."};

    31: const char      TITLE4[] = {"Please Place Rear Left EEPROMs into circuit and send file."};

    32: const char      TITLE5[] = {"Please Place Rear Right EEPROMs into circuit and send file."};

    33: const char      MESSAGE1[] = {"Chip "};

    34: const char      MESSAGE2[] = {" completed."};

    35: 

    36: unsigned char SCIword;

    37: unsigned char hexValue1;

    38: unsigned char hexValue2;

    39: unsigned int i;

    40: unsigned int chip_write;

    41: unsigned int write_control;

    42: unsigned int read_control;

    43: unsigned char write_data;

    44: unsigned char address_high;

    45: unsigned char address_low;

    46: unsigned char data;

    47: 

    48: #define WRITE_MODE 1

    49: #define READ_MODE 2

    50: #define WRITE_AND_READ_MODE 3

    51: 

    52: #define MODE_SELECT      WRITE_AND_READ_MODE

    53: #define NUMBER_OF_CHIPS  8

    54: 

    55: void main(void)

    56: {

    57:         portInitialization();

    58:         initializeI2C();

    59:         rs232_initliazation(BAUD_RATE, FREQUENCY);

    60:         display_string(TITLE1, 33);

    61:         display_string(TITLE2, 59);

    62:         address_high = 0x00;

    63:         address_low = 0x00;

    64:         chip_write = 1;

    65:         write_control = MEMORY_CHIP0_WRITE_CONTROL;

    66:         read_control = MEMORY_CHIP0_READ_CONTROL;

    67:         

    68:         if((MODE_SELECT == WRITE_MODE)|(MODE_SELECT == WRITE_AND_READ_MODE)) {

    69:                 for(i=0; i<NUMBER_OF_CHIPS; i++) {

    70:                         while(chip_write == 1) {        

    71:                                 receive_byte_from_PC(&SCIword);

    72:                                 if(SCIword < 0x41)

    73:                                         hexValue1 = SCIword - 0x30;

    74:                                 else

    75:                                         hexValue1 = SCIword - 0x41 + 0x0A;

    76:                                 receive_byte_from_PC(&SCIword);

    77:                                 if(SCIword < 0x41)

    78:                                         hexValue2 = SCIword - 0x30;

    79:                                 else

    80:                                         hexValue2 = SCIword - 0x41 + 0x0A;

    81:                                 write_data = (hexValue1 << 4) | hexValue2;

    82:                                 //PORTD = hexValue;

    83:                                 memory_chip_write(write_control,address_high,address_low,write_data);

    84:                                 if(address_low == 0xff) {

    85:                                         if(address_high == 0xff) {

    86:                                                 chip_write = 0;

    87:                                         }

    88:                                         address_high++;

    89:                                 }

    90:                                 address_low++;

    91:                         }

    92:                         display_string(MESSAGE1, 5);

    93:                         display_string(MESSAGE2, 11);

    94:                         chip_write = 1;

    95:                         write_control = write_control + 2;                              

    96:                 }

    97:         }

    98: 

    99:         if((MODE_SELECT == READ_MODE)|(MODE_SELECT == WRITE_AND_READ_MODE)) {

   100:                 while(1) {

   101:                         receive_byte_from_PC(&SCIword);

   102:                         if(SCIword < 0x41)

   103:                                 hexValue1 = SCIword - 0x30;

   104:                         else

   105:                                 hexValue1 = SCIword - 0x41 + 0x0A;

   106:         

   107:                         receive_byte_from_PC(&SCIword);

   108:                         if(SCIword < 0x41)

   109:                                 hexValue2 = SCIword - 0x30;

   110:                         else

   111:                                 hexValue2 = SCIword - 0x41 + 0x0A;

   112:         

   113:                         address_high = (hexValue1 << 4) | hexValue2;

   114:         

   115:                         receive_byte_from_PC(&SCIword);

   116:                         if(SCIword < 0x41)

   117:                                 hexValue1 = SCIword - 0x30;

   118:                         else

   119:                                 hexValue1 = SCIword - 0x41 + 0x0A;

   120:         

   121:                         receive_byte_from_PC(&SCIword);

   122:                         if(SCIword < 0x41)

   123:                                 hexValue2 = SCIword - 0x30;

   124:                         else

   125:                                 hexValue2 = SCIword - 0x41 + 0x0A;

   126:         

   127:                         address_low = (hexValue1 << 4) | hexValue2;

   128:         

   129:                         hexValue1 = memory_chip_read(read_control,address_high,address_low);

   130:         

   131:                         SCIword = (hexValue1 >> 4);

   132:                         if(SCIword < 0x0A)

   133:                                 SCIword = SCIword + 0x30;

   134:                         else

   135:                                 SCIword = SCIword - 0x0A + 0x41;

   136:                         send_byte_to_PC(SCIword);

   137:         

   138:                         SCIword = (hexValue1 & 0b00001111);

   139:                         if(SCIword < 0x0A)

   140:                                 SCIword = SCIword + 0x30;

   141:                         else

   142:                                 SCIword = SCIword - 0x0A + 0x41;

   143:                         send_byte_to_PC(SCIword);

   144:                 }

   145:         }

   146: }

   147: 

   148: void portInitialization() {

   149:         TRISD = 0x00; // PORTD is output

   150:         TRISE = 0x00; // PORTD is output

   151:         PORTD = 0xff; // Set PORTD to ensure proper initilization

   152:         TRISC7 = 1;     // RC7 receives data from PC

   153:         TRISC6 = 0;     // RC6 sends data to PC

   154: }

