سلام من کد زیر رو برای ارسال دیتا بر روی ایپرام نوشتم اما هیچی روی اپیرام نوشته نمیشه، سخت افزار درسته و همینطور آدرس سخت افزاری و من مطمئن هستم مشکل از نرم افزارم هست. با تشکر
کد:
[left]
#include <LPC23xx.h>
#include <irq.h>
#include <target.h>
#include "uart.h"
#include <swi.h>
#include <stdio.h>
#include "type.h"
unsigned int I2C_sla;
unsigned int I2C_adr;
unsigned char *I2C_buf;
unsigned char data_buf[8] = {0x62,0x61,0x68,0x61,0x64,0x6F,0x72,0x20};
unsigned char data_read[8];
unsigned int read;
unsigned int I2C_num;
unsigned int i;
void delay(int);
void twi_write(unsigned char id, unsigned int address, unsigned char *s, unsigned int num);
void i2c_init(void);
void i2c_isr(void);
int main (void)
{
PINSEL6 = 0x00000000;
FIO3DIR0 = 0xFF;
PCONP |= (1 << 19);
FIO3CLR0 = 0xff;
FIO3SET0 = 1;
uart_init();
putcharx(0x42);
FIO3CLR0 = 0xff;
FIO3SET0 = 2;
init_VIC();
FIO3CLR0 = 0xff;
FIO3SET0 = 3;
install_irq(I2C1_INT, i2c_isr, LOWEST_PRIORITY);
FIO3CLR0 = 0xff;
FIO3SET0 = 4;
IntEnable();
FIO3CLR0 = 0xff;
FIO3SET0 = 5;
//printf("writing to eeprom...");
FIO3CLR0 = 0xff;
FIO3SET0 = 6;
i2c_init();
FIO3CLR0 = 0xff;
FIO3SET0 = 7;
for(i=1;i<9;i++)
{
twi_write(0xA0, i, data_buf, 8);
delay(10);
}
for(i=0;i<8;i++)
{
putcharx(data_read[i]);
FIO3CLR0 = 0xff;
FIO3SET0 = data_read[i];
delay(100);
}
FIO3CLR0 = 0xff;
FIO3SET0 = 0xFF;
while(1);
}
void i2c_isr(void)
{
FIO3CLR0 = 0xff;
FIO3SET0 = 0x55;
switch(I21STAT & 0xF8)
{
case (0x08):
if(read == 1) //SLA + R
{
I21DAT = I2C_sla & 0xFE;
}
else //SLA + W
{
I21DAT = I2C_sla;
}
I21CONCLR = (1 << 3)| /* SI */
(1 << 5); /* STA */
break;
case (0x10):
I21DAT = I2C_sla;
I21CONCLR = 0x28;
//slave address + W, ACK
case (0x18):
case (0x28):
if (I2C_num > 0)
{
I21DAT = *I2C_buf++;
I21CONCLR = 0x28; /*STA */
I2C_num--;
}
else
{
I21CONSET = (1 << 4); /* STO */
I21CONCLR = 0x28; /* STA */
}
break;
default:
break;
}
I21CONCLR = 0x08; // clear I2C interrupt flag
VICVectAddr = 0x00;
}
void delay(int x)
{
int j;
x = x * 10000;
for(j=0;j<x;j++);
}
void twi_write(unsigned char id, unsigned int address, unsigned char *s, unsigned int num)
{
I2C_sla = id;
I2C_adr = address;
I2C_num = num;
I2C_buf = s;
read = 0;
I21CONCLR = (1 << 2)| /* AA */
(1 << 3)| /* SI */
(1 << 5); /* STA */
I21CONSET = (1 << 5)| /* STA */
(1 << 6); /* I2CEN*/
delay(10);
}
void i2c_init()
{
//I2Cbitfrequency = fPCLK / (2CSCLH + I2CSCLL)
PINSEL0 |= 0x0000000F; // set P0.0 and P0.1 to I2C1 SDA and SCK
I21SCLL = (Fpclk / 200000 + 1) / 2;
I21SCLH = (Fpclk / 200000) / 2;
I21CONCLR = 0x000000FF;
I21CONSET = 0x00000040;
}
[/left]




دیدگاه