luis872727
New Member
my problem is whit a 24LC256 and a pic 18f1320, that pic haven´t pins for I2C comunication, but the C18 libraries contain functions to make that comunication, I changed the pins default whit pin RA1 for clock and RA4 for data, I already recompile the function, and I can see in proteus that the pins works well, the problem is that I cant write or read, when I try to read the LCD shows only extrangers characters, and for that reason I don´t think that the write function is well.
I´m lookin for some help or and advise because I´m tired to try and fail, I got the pull up resistor on the SDA and SCL pins, 2 Kohm for each, I think that is an common error, but I cant find the reason, Im new whit memories, I post my code for some help. thank you.
the keypad 4X4 works whit an ADC in the pin RA1, the option 1 for read, the second one is for write, and the trhird one is for show the adquiered value, the option 1 (tecla 1) takes the value of "contador" (seted to 35), and changed of int to char, and this values is writen in the location 0000 of the memory. then in option 2( tecla 2), read that position of memory and the value is saved to i2c, and then option 3 (tecla 3) is for show value of contador, and then the value of i2c, both values must be equal, but the value of i2c always is extranger, what is my problem???
#include <p18f1320.h>
#include <delays.h>
#include <adc.h>
#include <xlcd.h>
#include <sw_i2c.h>
#include <stdlib.h>
#include <stdio.h>
#pragma config OSC=XT, WDT=OFF, MCLRE=ON, LVP=OFF
void DelayFor18TCY( void )
{
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
}
void DelayPORXLCD (void)
{
Delay1KTCYx(60); // Delay of 15ms
// Cycles = (TimeDelay * Fosc) / 4
// Cycles = (15ms * 16MHz) / 4
// Cycles = 60,000
return;
}
void DelayXLCD (void)
{
Delay1KTCYx(20); // Delay of 5ms
// Cycles = (TimeDelay * Fosc) / 4
// Cycles = (5ms * 16MHz) / 4
// Cycles = 20,000
return;
}
void main(void)
{
int dato,x,otra,cont,contador2;
unsigned char buf[9],cosa,contchar,i2c,nuevo;
ADCON1=0x7F;
TRISA=0x0F;
TRISB=0x00;
PORTBbits.RB7=0;
OpenXLCD( FOUR_BIT & LINES_5X7 );
WriteCmdXLCD( BLINK_ON );
putrsXLCD("ESIME CULHUACAN");
Delay1KTCYx(10);
WriteCmdXLCD( 0x01 );
putrsXLCD("1ER AVANCE");
Delay1KTCYx(10);
WriteCmdXLCD( 0x01 );
cont=35;
while(1)
{
/*Inicializa el convertidor ADC con canal 0 como entrada analógica */
OpenADC(ADC_FOSC_32 & ADC_RIGHT_JUST & ADC_12_TAD,
ADC_VREFPLUS_VDD & ADC_VREFMINUS_VSS &
ADC_CH0 & ADC_INT_OFF, 0x7E );
SetDDRamAddr(0x00);
//while(BusyXLCD());
putrsXLCD("PRESIONA UNA TECLA");
do{
x=0;
ConvertADC();
while(BusyADC());
dato= ReadADC();
}while(dato<20);
while(BusyXLCD());
if(dato>855&&dato<875)
{
WriteCmdXLCD( 0x01 );
putrsXLCD("tecla 1");
Delay1KTCYx(10);
itoa(cont, contchar);
WriteCmdXLCD( 0x01 );
SWStartI2C();
SWPutcI2C(0xA0); // control byte
SWAckI2C();
SWPutcI2C(0x00); // address high byte
SWAckI2C();
SWPutcI2C(0x00); //address low byte
SWAckI2C();
SWPutcI2C(contchar); // data
SWAckI2C();
SWStopI2C();
Delay100TCYx(100);
}
if(dato>645&&dato<655)
{
WriteCmdXLCD( 0x01 );
putrsXLCD("tecla 2");
Delay1KTCYx(10);
WriteCmdXLCD( 0x01 );
SWStartI2C();
SWPutcI2C(0xA0); // control byte
SWAckI2C();
SWPutcI2C(0x00); // address high byte
SWAckI2C();
SWPutcI2C(0x00); //address low byte
SWAckI2C();
SWStartI2C();
SWPutcI2C(0xA1);
SWAckI2C();
i2c=SWGetcI2C(); // data
SWNotAckI2C();
SWStopI2C();
Delay100TCYx(100);
WriteCmdXLCD( 0x01 );
}
if(dato>495&&dato<515)
{
WriteCmdXLCD( 0x01 );
putrsXLCD("tecla 3");
Delay1KTCYx(10);
WriteCmdXLCD( 0x01 );
itoa(cont, nuevo);
putsXLCD( nuevo );
Delay1KTCYx(10);
WriteCmdXLCD( 0x01 );
putsXLCD( i2c );
Delay1KTCYx(10);
WriteCmdXLCD( 0x01 );
}
if(dato>335&&dato<345)
{
WriteCmdXLCD( 0x01 );
putrsXLCD("tecla 4");
cont++;
Delay1KTCYx(10);
WriteCmdXLCD( 0x01 );
}
if(dato>797&&dato<809)
{
WriteCmdXLCD( 0x01 );
putrsXLCD("tecla 5");
Delay1KTCYx(10);
}
if(dato>610&&dato<621)
{
WriteCmdXLCD( 0x01 );
putrsXLCD("tecla 6");
Delay1KTCYx(10);
WriteCmdXLCD( 0x01 );
}
if(dato>480&&dato<491)
{
WriteCmdXLCD( 0x01 );
putrsXLCD("tecla 7");
Delay1KTCYx(10);
WriteCmdXLCD( 0x01 );
}
if(dato>322&&dato<333)
{
WriteCmdXLCD( 0x01 );
putrsXLCD("tecla 8");
Delay1KTCYx(10);
WriteCmdXLCD( 0x01 );
}
if(dato>728&&dato<740)
{
WriteCmdXLCD( 0x01 );
putrsXLCD("tecla 9");
Delay1KTCYx(10);
WriteCmdXLCD( 0x01 );
}
if(dato>568&&dato<580)
{
WriteCmdXLCD( 0x01 );
putrsXLCD("corrertecla");
do{ //WriteCmdXLCD( 0x01 );
Delay1KTCYx(10);
WriteCmdXLCD( SHIFT_CUR_LEFT );
x++;
}while(x<4);
Delay1KTCYx(10);
while ( BusyXLCD() );
cosa = ReadDataXLCD();
btoa(cosa, buf);
WriteCmdXLCD( 0x01 );
putsXLCD(buf);
Delay1KTCYx(10);
atoi(cosa);
WriteCmdXLCD( 0x01 );
putsXLCD(cosa);
Delay1KTCYx(10);
WriteCmdXLCD( 0x01 );
}
CloseADC();
while(BusyADC());
}
}
I´m lookin for some help or and advise because I´m tired to try and fail, I got the pull up resistor on the SDA and SCL pins, 2 Kohm for each, I think that is an common error, but I cant find the reason, Im new whit memories, I post my code for some help. thank you.
the keypad 4X4 works whit an ADC in the pin RA1, the option 1 for read, the second one is for write, and the trhird one is for show the adquiered value, the option 1 (tecla 1) takes the value of "contador" (seted to 35), and changed of int to char, and this values is writen in the location 0000 of the memory. then in option 2( tecla 2), read that position of memory and the value is saved to i2c, and then option 3 (tecla 3) is for show value of contador, and then the value of i2c, both values must be equal, but the value of i2c always is extranger, what is my problem???
#include <p18f1320.h>
#include <delays.h>
#include <adc.h>
#include <xlcd.h>
#include <sw_i2c.h>
#include <stdlib.h>
#include <stdio.h>
#pragma config OSC=XT, WDT=OFF, MCLRE=ON, LVP=OFF
void DelayFor18TCY( void )
{
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
Nop();
}
void DelayPORXLCD (void)
{
Delay1KTCYx(60); // Delay of 15ms
// Cycles = (TimeDelay * Fosc) / 4
// Cycles = (15ms * 16MHz) / 4
// Cycles = 60,000
return;
}
void DelayXLCD (void)
{
Delay1KTCYx(20); // Delay of 5ms
// Cycles = (TimeDelay * Fosc) / 4
// Cycles = (5ms * 16MHz) / 4
// Cycles = 20,000
return;
}
void main(void)
{
int dato,x,otra,cont,contador2;
unsigned char buf[9],cosa,contchar,i2c,nuevo;
ADCON1=0x7F;
TRISA=0x0F;
TRISB=0x00;
PORTBbits.RB7=0;
OpenXLCD( FOUR_BIT & LINES_5X7 );
WriteCmdXLCD( BLINK_ON );
putrsXLCD("ESIME CULHUACAN");
Delay1KTCYx(10);
WriteCmdXLCD( 0x01 );
putrsXLCD("1ER AVANCE");
Delay1KTCYx(10);
WriteCmdXLCD( 0x01 );
cont=35;
while(1)
{
/*Inicializa el convertidor ADC con canal 0 como entrada analógica */
OpenADC(ADC_FOSC_32 & ADC_RIGHT_JUST & ADC_12_TAD,
ADC_VREFPLUS_VDD & ADC_VREFMINUS_VSS &
ADC_CH0 & ADC_INT_OFF, 0x7E );
SetDDRamAddr(0x00);
//while(BusyXLCD());
putrsXLCD("PRESIONA UNA TECLA");
do{
x=0;
ConvertADC();
while(BusyADC());
dato= ReadADC();
}while(dato<20);
while(BusyXLCD());
if(dato>855&&dato<875)
{
WriteCmdXLCD( 0x01 );
putrsXLCD("tecla 1");
Delay1KTCYx(10);
itoa(cont, contchar);
WriteCmdXLCD( 0x01 );
SWStartI2C();
SWPutcI2C(0xA0); // control byte
SWAckI2C();
SWPutcI2C(0x00); // address high byte
SWAckI2C();
SWPutcI2C(0x00); //address low byte
SWAckI2C();
SWPutcI2C(contchar); // data
SWAckI2C();
SWStopI2C();
Delay100TCYx(100);
}
if(dato>645&&dato<655)
{
WriteCmdXLCD( 0x01 );
putrsXLCD("tecla 2");
Delay1KTCYx(10);
WriteCmdXLCD( 0x01 );
SWStartI2C();
SWPutcI2C(0xA0); // control byte
SWAckI2C();
SWPutcI2C(0x00); // address high byte
SWAckI2C();
SWPutcI2C(0x00); //address low byte
SWAckI2C();
SWStartI2C();
SWPutcI2C(0xA1);
SWAckI2C();
i2c=SWGetcI2C(); // data
SWNotAckI2C();
SWStopI2C();
Delay100TCYx(100);
WriteCmdXLCD( 0x01 );
}
if(dato>495&&dato<515)
{
WriteCmdXLCD( 0x01 );
putrsXLCD("tecla 3");
Delay1KTCYx(10);
WriteCmdXLCD( 0x01 );
itoa(cont, nuevo);
putsXLCD( nuevo );
Delay1KTCYx(10);
WriteCmdXLCD( 0x01 );
putsXLCD( i2c );
Delay1KTCYx(10);
WriteCmdXLCD( 0x01 );
}
if(dato>335&&dato<345)
{
WriteCmdXLCD( 0x01 );
putrsXLCD("tecla 4");
cont++;
Delay1KTCYx(10);
WriteCmdXLCD( 0x01 );
}
if(dato>797&&dato<809)
{
WriteCmdXLCD( 0x01 );
putrsXLCD("tecla 5");
Delay1KTCYx(10);
}
if(dato>610&&dato<621)
{
WriteCmdXLCD( 0x01 );
putrsXLCD("tecla 6");
Delay1KTCYx(10);
WriteCmdXLCD( 0x01 );
}
if(dato>480&&dato<491)
{
WriteCmdXLCD( 0x01 );
putrsXLCD("tecla 7");
Delay1KTCYx(10);
WriteCmdXLCD( 0x01 );
}
if(dato>322&&dato<333)
{
WriteCmdXLCD( 0x01 );
putrsXLCD("tecla 8");
Delay1KTCYx(10);
WriteCmdXLCD( 0x01 );
}
if(dato>728&&dato<740)
{
WriteCmdXLCD( 0x01 );
putrsXLCD("tecla 9");
Delay1KTCYx(10);
WriteCmdXLCD( 0x01 );
}
if(dato>568&&dato<580)
{
WriteCmdXLCD( 0x01 );
putrsXLCD("corrertecla");
do{ //WriteCmdXLCD( 0x01 );
Delay1KTCYx(10);
WriteCmdXLCD( SHIFT_CUR_LEFT );
x++;
}while(x<4);
Delay1KTCYx(10);
while ( BusyXLCD() );
cosa = ReadDataXLCD();
btoa(cosa, buf);
WriteCmdXLCD( 0x01 );
putsXLCD(buf);
Delay1KTCYx(10);
atoi(cosa);
WriteCmdXLCD( 0x01 );
putsXLCD(cosa);
Delay1KTCYx(10);
WriteCmdXLCD( 0x01 );
}
CloseADC();
while(BusyADC());
}
}