قسم الميكروكنترولر والروبوت ودوائر الاتصال بالحاسب الالي قسم المتحكمات الـ microcontroller و المعالجات microprocessor و التحكم الرقمي بالكمبيوتر CNC والانظمة الآلية والروبوت Robots

أدوات الموضوع

moha-affa
:: مهندس ::
تاريخ التسجيل: May 2015
المشاركات: 14
نشاط [ moha-affa ]
قوة السمعة:0
قديم 19-05-2015, 08:00 AM المشاركة 1   
افتراضي pic18f4580 with eeprom by i2c Twitter FaceBook Google+



السلام عليكم عم احاول اقوم بعملية قراءة وكتابة من eeprom 24c32 بواسطة متحكم بيك .. البيئة المستخدمة PCWHD compiler والمشكلة هي أن القيمة اللي عم تظهرها الشاشة في كل مرة هي -1 ... لا اعلم هل الخطأ عم يتم في عملية الكتابة أم القراءة ... أرجو المساعدة ولكم الشكر .... هذا الكود :
#include "D:\mohammet\eprom.h"

#define LCD_ENABLE_PIN PIN_C5
#define LCD_RS_PIN PIN_D2
#define LCD_RW_PIN PIN_D3
#define LCD_DATA4 PIN_D4
#define LCD_DATA5 PIN_D5
#define LCD_DATA6 PIN_D6
#define LCD_DATA7 PIN_D7
#include <LCD.C>

#use i2c(MASTER, FAST, SCL=PIN_C3, SDA=PIN_C4, FAST)
#define CAN_USE_EXTENDED_ID FALSE
#include <can-18xxx8.c>
int8 EEPROM_WR = 0xA0; // I2C Address. 1010 0000. last bit = 0 => write command
// First Nibble is fixed as 1010 (internal address);
// Second Nibble is ABCD,
// A = Block Set
// B, C = Hardware Addresses (Configured when you Wire the CHIP)
// D = (0 = write, 1 = read)
int8 EEPROM_RD = 0xA1; // Same as EEPROM_WR except last bit = 1 for READ command

int read = 0;
int data = 128;
int16 loc;
int temp = 0;

void rand_write(int16 address, int data)
{
i2c_start(); // Claim I2C BUS
i2c_write(EEPROM_WR); // Tell all I2C devices you are talking to EEPROM in WRITE MODE
// Address High Byte (0x00 - 0x)
i2c_write(address);
i2c_write(address>>8); // Address Low Byte (0x00 - 0xAA)
i2c_write(data); // Data to write
i2c_stop(); // Release BUS
delay_ms(1000); // Let EEPROM Write Data (5ms from data sheet)
}

int rand_read(int16 address)
{
i2c_start(); // Claim I2C BUS
i2c_write(EEPROM_WR); // Tell all I2C devices you are talking to EEPROM in WRITE MODE
// (you are first writing to the EEPROM to set the address. Later you will read)

i2c_write(address>>8); // Address High Byte (0x00 - 0x
i2c_write(address); // Address Low Byte (0x00 - 0xAA)

i2c_start(); // RESTART I2C BUS (necissary for microchip protocol)
i2c_write(EEPROM_RD); // Tell all I2C you are talking to EEPROM in READ MODE.
read = i2c_read(); // Read in the data
i2c_read(0); // Read last byte with no ACK
i2c_stop(); // release the bus
return(read);
}




void main()
{
can_init();


can_set_mode(CAN_OP_CONFIG);

BRGCON1.brp=3;
BRGCON1.sjw=0;
BRGCON2.prseg=2;
BRGCON2.seg1ph=2;
BRGCON2.sam=FALSE;
BRGCON2.seg2phts=FALSE;
BRGCON3.seg2ph=2;
BRGCON3.wakfil=TRUE;

can_set_mode(CAN_OP_NORMAL);



setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_CLOCK_DIV_2|ADC_TAD_MUL_0);
setup_psp(PSP_DISABLED);
setup_spi(SPI_SS_DISABLED);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_ccp1(CCP_OFF);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
lcd_init();
int r;

while(true){
delay_ms(100); // *Not necissary pause, just for debugging

loc = 0x0f; // Random Location in memory
temp = 5; // Byte to write
rand_write(loc, temp); // Random Write: rand_write(address, data)


delay_ms(100); // *Not necissary pause, just for visual purposes

// Reset temp
r = rand_read(loc);
// Random Read: rand_read(address);
// Output temp

printf(lcd_putc,"\n%d",temp);

}
}

اعلانات
إضافة رد

العلامات المرجعية

«     الموضوع السابق       الموضوع التالي    »
أدوات الموضوع

الانتقال السريع إلى


الساعة معتمدة بتوقيت جرينتش +3 الساعة الآن: 12:52 PM
موقع القرية الالكترونية غير مسؤول عن أي اتفاق تجاري أو تعاوني بين الأعضاء
فعلى كل شخص تحمل مسئولية نفسه إتجاه مايقوم به من بيع وشراء وإتفاق وأعطاء معلومات موقعه
التعليقات المنشورة لا تعبر عن رأي موقع القرية الالكترونية ولايتحمل الموقع أي مسؤولية قانونية حيال ذلك (ويتحمل كاتبها مسؤولية النشر)

Powered by vBulletin® Version 3.8.6, Copyright ©2000 - 2025