天嵌 ARM开发社区

 找回密码
 注册
查看: 1697|回复: 1

at24c02a读写没有ack,进入不了中断

[复制链接]
dhynust 发表于 2012-6-15 15:44:38 | 显示全部楼层 |阅读模式
rt,代码如下
#include "option.h"
#include "def.h"
#include "2440addr.h"
#include "lcd_tft.h"
#include "mmu.h"

#define pISR_IIC        (*(unsigned *)(_ISR_STARTADDRESS+0x8c))
#define pISR_UART0        (*(unsigned *)(_ISR_STARTADDRESS+0x90))

//IIC部分的变量
U8 iic_wrbuffer[8];
U8 iic_rdbuffer[8];
U8 iic_flag;
U8 address;
int length;
U8 comm;
U8 devAddr=0xa0;
static char command;
static char count;
int i;

void delay(U32 ms)
{
        for(;ms>0;ms--);
}

void __irq uartisp(void)
{

        //读写AT24C02A的串口
        U8 ch;
       
        rSUBSRCPND |=0x1;
        rSRCPND |=0x1<<28;
        rINTPND |=0x1<<28;
       
        ch=rURXH0;
        if(command==0)
        {
                switch(ch){
                        case 0xc0:
                                command=0xc0;
                                count=0;
                                comm=0;
                               
                        break;
                        case 0xc1:
                                command=0xc1;
                                count=0;
                                comm=0;
                        break;
                        default:
                                command=0;
                                count=0;
                                rUTXH0=ch;
                        break;
                }
               
               
        }
        else
        {
               
                if(command == 0xc0)
                {

                        count++;
                        if(count==1)
                        {
                                address=ch;
                        }
                        else if(count==2)
                        {
                                length=ch;
                        }
                        else
                        {
                                iic_wrbuffer[count-3]=ch;
                                if(count==length+2)
                                {
                                        rUTXH0=0xc0;
                                        count=0;
                                        command=0;
                                        comm=1;                //标志写命令       
                                }
                        }
               
                }
                else if(command==0xc1)
                {
       
                        count++;
                        if(count==1)
                        {
                                address=ch;
                        }
                        else
                        {
                                length=ch;
                                count=0;
                                command=0;
                                comm=2;
                                rUTXH0=0xc1;
                        }
                       
                }
               
        }

        }
void uart_init(void)
{
        rGPHCON = 0x00ffaa;
        rGPHUP = 0x7ff;
       
        rULCON0 = 0x3;
        rUCON0 = 0x5;
        rUFCON0 = 0x0;
        rUMCON0 = 0x0;
        rUBRDIV0 = 26;
       
        rSRCPND = (0x1<<28);
//        rSUBSRCPND = 0x3;
        rSUBSRCPND = 0x1;
        rINTPND = (0x1<<28);
//        rINTSUBMSK &=~0x3;
        rINTSUBMSK &=~0x1;
        rINTMSK &=~(0x1<<28);
       
}

void interrupt_init(void)
{
        rSRCPND |=0x3;
        rINTMSK &=~0x3;
        rINTPND |=0x3;
        rGPFCON &=~0xf;
        rGPFCON |=0xa;
       
}
//IIC中断
void __irq iicISR(void)
{
        rSRCPND |=0x1<<27;
        rINTPND |=0x1<<27;
        iic_flag=0;
        rGPBDAT = ~0x80;
}

//IIC的写程序
void wr_at24c02(U8 wordAddr,U8 *buffer,int sizeofdata)
{
       
//        U8 i;
        iic_flag=1;
        rIICDS=devAddr;
        rIICCON &=~0x10;        //清中断标志
        delay(5);
        rIICSTAT =0xf0;                //主设备发送模式
       
        while(iic_flag)
        {
                delay(200);
        }
        rGPBDAT = ~0x80;
        iic_flag=1;
        rIICDS=wordAddr;
        rIICCON &=~0x10;
        while(iic_flag)
        delay(100);
       
        for(i=0;i<sizeofdata;i++)
        {
                iic_flag=1;
                rIICDS=*(buffer+i);
                rIICCON &=~0x10;
                while(iic_flag)
                delay(100);
        }
       
        rIICSTAT = 0xd0;        //发出stop命令
        rIICCON =0xe0;
       
        delay(100);
       
        if(comm==1)
                rGPBDAT = ~0x20;

}

//IIC读程序
void rd_at24c02a(U8 wordAddr,U8 *buffer,int sizeofdata)
{
       
//        int i;
        U8 temp;
        iic_flag=1;
        rIICDS=devAddr;
        rIICCON &=~0x10;
        rIICSTAT =0xf0;
        rGPBDAT = ~0x80;
        while(iic_flag)
        delay(100);
       
       
        iic_flag=1;
        rIICDS=wordAddr;
        rIICCON &=~0x10;
        while(iic_flag)
        delay(100);
       
        iic_flag=1;
        rIICDS=devAddr;
        rIICCON &=~0x10;
        rIICSTAT=0xb0;
        while(iic_flag)
        delay(100);
       
        iic_flag=1;
        temp=rIICDS;
        rIICCON &=~0x10;
        while(iic_flag)
        delay(100);
       
        for(i=0;i<sizeofdata;i++)
        {
                iic_flag=1;
                if(i==sizeofdata-1)
                        rIICCON &=~0x80;
                *(buffer+i)=rIICDS;
                rIICCON &=~0x10;
                while(iic_flag==1)
                        delay(100);
               
        }
        rIICSTAT =0x90;
        rIICCON=0xe0;
        delay(100);
        if(comm==1)
                rGPBDAT = ~0x40;
}
//IIC初始化
void iic_init(void)
{
        rGPEUP |=0xc000;
        rGPECON &=~0xf0000000;
        rGPECON |=0xa0000000;
        rSRCPND |=0x1<<27;
        rINTMSK &=~(0x1<<27);
        rINTPND |=0x1<<27;
       
        rIICCON =0xe0;
        rIICSTAT=0x10;
       
//        iic_flag=1;
        comm=0;
}

void Main(void)
{
rGPBCON=0x015400;
        rGPBUP=0x7ff;
//        light=0x10;
        command=0;
        count=0;
        MMU_Init();
                uart_init();
                iic_init();
               pISR_UART0 = (U32)uartisp;
                pISR_IIC   = (U32)iicISR;
                while(1)
        {

                       
        switch(comm)
        {
                case 1:
                        wr_at24c02(address,iic_wrbuffer,length);
                        comm=0;
                        iic_flag=1;
                        address=0;
                        length=0;
                break;
                case 2:
                       
                        rd_at24c02a(address,iic_rdbuffer,length);
                        comm=0;
                        iic_flag=1;
                        address=0;
                        for(i=0;i<length;i++)
                        {
                                delay(500);
                                rUTXH0=iic_rdbuffer[i];
                        }
                        length=0;
                break;
        }
        }
}
 楼主| dhynust 发表于 2012-6-15 15:45:39 | 显示全部楼层
自己顶一个
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

关闭

i.MX8系列ARM cortex A53 M4 工控板上一条 /1 下一条

Archiver|手机版|小黑屋|天嵌 嵌入式开发社区 ( 粤ICP备11094220号-2 )

GMT+8, 2025-6-19 11:59 , Processed in 2.043034 second(s), 20 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表