Pic16f690 - I2C - portexpander - EEprom

Status
Not open for further replies.

moe_3_moe

New Member
Hello All,

I am working on a projecting that include reading a temperature and send it to 2 port expanders via I2c and Si once to display on LCD and once to save on memory EEProm Chip. the port expander for I2C is the MCP23017 and the EEPROM is 24LC04. Below is my code. I am planning to save 10 values(temperatures) and read them again and bubble sort the values read. Can i get any help?
I was thinking that i can write 10 times in a for loop: for (int i=0; i<10; i++){ write to memory};

this is the part i need help with, i could use a for loop in my main and i don't know if the location of the saved value should be incremented manually or not and how to read the values again?

Code:
#define RS PORTC.F6
#define RW PORTC.F5
#define EN PORTC.F3
#define TRIS_RS TRISC.F6
#define TRIS_RW TRISC.F5
#define TRIS_EN TRISC.F3
#define DATA PORTC
//#define TRIS_DATA TRISC
#define SDA_PIN 5
#define SCL_PIN 2
#define IODIRA 0x00       //Tris for the porta on the expander  (see datasheet)
#define EXPANDER_PORTA 0X14        //This is PORTA on the expander (datasheet calls it OLATA)
#define A2A1A0 0x00       //Set address for specific port expander
#define IODIRB 0x01       //Tris for the porta on the expander  (see datasheet)
#define EXPANDER_PORTB 0X15        //This is PORTA on the expander (datasheet calls it OLATA)
//#include "spi-stuff.c"
// Define stuff only if it is not already defined. This way we can include the
// file in any code we want

#ifndef BIT
#       define BIT(n) (1 << (n) )
#endif

#ifndef CSBAR
        // Wiring info
#         define CSBAR PORTC.F7
#         define CSTRIS TRISC.F7
#endif

#ifndef SDI
#       define SDI 1  // Connected to RC0
#       define SDO 0  // RC1
#       define SCK 2  // RC2
#       define SPI_PORT PORTC
#endif

char *puts(char *s) {
     char *save;
     save =s;
     while(*s) Usart_Write(*s++);
     return save;
}

void SPIsend2bytes(char byte1, char byte2) {
     // Set the value of the potentiometer 0
     // Value is a 10 bit number
     // Caller sets the wiper value as needed
     CSBAR=0; // Bring the CS line down
     Soft_Spi_Write(byte1);
     Soft_Spi_Write(byte2);
     delay_ms(10);
     CSBAR=1;
}


void SPIsend3bytes(char byte1, char byte2, char byte3) {
      //puts("Send byte1-2-3 funtion\r\n");
     // Set the value of the potentiometer 0
     // Value is a 10 bit number
     // Caller sets the wiper value as needed
     CSBAR=0; // Bring the CS line down
     Soft_Spi_Write(byte1); delay_us(100);
     Soft_Spi_Write(byte2); delay_us(100);
     Soft_Spi_Write(byte3);  delay_us(100);
      // delay_ms(100);

     CSBAR=1;
        delay_ms(1);
}


char buffer[10];

void putd(int d) {
     IntToStr(d,buffer);  // Ansi itoa
     puts(buffer);
}

void sendbyte(char d) {
    //puts("sendbyte function\r\n ");
    RW=0;
    SPIsend3bytes(0x40, 0x15, d); //DATA=d;
    EN=1;
    delay_ms(10);
    EN=0;
    delay_ms(10);
}

void sendcommand(char x) {
     RS=0;
     sendbyte(x);
}

void senddata(char x) {
     RS=1;
     sendbyte(x);
}

char start[]={
     0x38, //00111000 -> 001 DL N F XX
     0x0E, //00001110 -> 0000 1 D C B
     0x01, //00000001 -> CLEAR
     0x06, //00000110 -> 0000 00 ID S
     //0x40
} ;
#define NSTART sizeof(start)/sizeof(start[0])
void lcdinit() {

     char k;
      delay_ms(1);
     for (k=0;k<NSTART;k++) {
         sendcommand(start[k]);
         delay_ms(5);
     }
}

void clear_screen() {
     sendcommand(0x01);
}

void sendstring(char *s) {
     while(*s) senddata(*s++);
}

//char buffer[10];

void sendvalue(int val) {
              IntToStr(val,buffer);
              sendstring(buffer);
}

void init_SPI() {
     //puts("init_spi function\r\n");
     CSTRIS=0;
     CSBAR=1;
     Soft_Spi_config(&PORTC, SDI,SDO,SCK);
    // while(1) SPIsend3bytes(0x40, 0x01,0);
 //    Soft_Spi_Config(&SPI_PORT, SDI, SDO, SCK);
}

#define CONTROL(a2a1a0)  (0x40 | ((a2a1a0&7)<<1))
void start_expander(char a2a1a0) {
     int attempts;
     char ack;

     for(attempts=0; attempts<25; attempts++) {
         // Make 50 attempts
         Soft_I2C_Start();
         ack = Soft_I2C_Write(CONTROL(a2a1a0));
         Usart_write('.');
         if (ack==0) return; /* we got an ack */

         delay_ms(50);
     }
  }

void write_to_mem(char a2a1a0, char location, char value) {
     start_expander(a2a1a0);
     usart_write('#');
     Soft_I2C_Write(location);
     Soft_I2C_Write(value);
     Soft_I2C_Stop();
}
char read_from_mem(char a2a1a0, char location) {
     char value;

     start_expander(a2a1a0);
     Soft_I2C_Write(location);
     Soft_I2C_Start();
     Soft_I2C_Write(CONTROL(a2a1a0)+1);
     value=Soft_I2C_Read(0);
     Soft_I2C_Stop();
     return value;
}

void init() {
       //puts("in init\r\n");

       //delay_ms(1000);
       TRISA=TRISB=TRISC=0XFF;
       PORTA=PORTB=PORTC=0;
       ANSEL=ANSELH=0;
       
       Soft_I2C_Config(&PORTA, SDA_PIN, SCL_PIN);
       init_SPI();
       TRIS_RS=0;
       TRIS_RW=0;
       TRIS_EN=0;
       SPIsend3bytes(0x40, 0x01, 0); //      TRIS_DATA=0;
       //puts("end\r\n");
}

void main() {
       int val, tc;
       char c;
       char buffer_temp[7];
       ANSEL=ANSELH=0;                  // input analog
       ANSEL.F3=1;                      // choosing channel 3
       ADCON1.F4=1;                     // set clock to   4KHz
       Usart_init(9600);
       delay_ms(1000);
       //puts("ECE 4951 Project 3 test\r\n");

       init();
       //delay_ms(1000);
       lcdinit();

       Usart_Write(13);
       Usart_Write(10);
       
       write_to_mem(A2A1A0,IODIRB,0);

       while(1) {
         //puts("in while loop \r\n ");

         val=Adc_Read(3);          // read analog value from channel 3
         putd(val);
         tc=((val>>1 )- 41)>>1;
         puts("Temp. is ");
         putd(tc);
         puts("\r\n");
         IntToStr(tc,buffer_temp);
         sendstring("Temp. :") ;
         sendstring(buffer_temp);
         sendstring("F");
         


         write_to_mem(A2A1A0,EXPANDER_PORTB,tc);  //Device,Location,Value

                             
         //putd(buffer_temp);
         delay_ms(1000);
         clear_screen();

       }
}
 
Last edited:
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…