- #include <stdio.h>
- #include "NUC1xx.h"
- #include "Driver\DrvSYS.h"
- #include "Driver\DrvGPIO.h"
- #include "EEPROM_24LC64.h"
- #include "Driver\DrvI2C.h"
- #include "Driver\DrvUART.h"
- int main(void)
- {
- uint32_t addr = 0x00000010;
- uint8_t datain[] = "OK";
- uint8_t i, i2cdata;
- STR_UART_T sParam;
- /* Unlock the protected registers */
- UNLOCKREG();
- /* Enable the 12MHz oscillator oscillation */
- DrvSYS_SetOscCtrl(E_SYS_XTL12M, 1);
- /* Waiting for 12M Xtal stalble */
- DrvSYS_Delay(5000);
- /* HCLK clock source. 0: external 12MHz; 4:internal 22MHz RC oscillator */
- DrvSYS_SelectHCLKSource(0);
- /* Set UART I/O */
- DrvGPIO_InitFunction(E_FUNC_UART0);
- /* Select UART Clock Source From 12MHz */
- DrvSYS_SelectIPClockSource(E_SYS_UART_CLKSRC, 0);
- /*lock the protected registers */
- LOCKREG();
- DrvSYS_SetClockDivider(E_SYS_HCLK_DIV, 0); /* HCLK clock frequency = HCLK clock source / (HCLK_N + 1) */
- /* UART Setting */
- sParam.u32BaudRate = 9600;
- sParam.u8cDataBits = DRVUART_DATABITS_8;
- sParam.u8cStopBits = DRVUART_STOPBITS_1;
- sParam.u8cParity = DRVUART_PARITY_NONE;
- sParam.u8cRxTriggerLevel= DRVUART_FIFO_1BYTES;
- /* Set UART Configuration */
- DrvUART_Open(UART_PORT0, &sParam);
- DrvGPIO_InitFunction(E_FUNC_I2C1);
- for(i=0; i<2; i++)
- {
- Write_24LC64(addr,datain[i]);
- i2cdata= Read_24LC64(addr);
- _DRVUART_SENDBYTE (UART_PORT0, i2cdata);
- }
- while(1);
- }
下面是EEPROM_24LC64.c的函数
- #include "NUC1xx.h"
- #include "Driver\DrvGPIO.h"
- #include "Driver\DrvI2C.h"
- #include "Driver\DrvSYS.h"
- #include "EEPROM_24LC64.h"
- void Write_24LC64(uint32_t address,uint8_t data )
- {
- uint32_t i;
- SystemCoreClock = DrvSYS_GetHCLKFreq();
- //Open I2C1 and set clock = 50Kbps
- DrvI2C_Open(I2C_PORT1, 50000);
- //send i2c start
- DrvI2C_Ctrl(I2C_PORT1, 1, 0, 0, 0); //set start
- while (I2C1->CON.SI == 0); //poll si flag
- //send writer command
- I2C1->DATA = (address>>7) & 0x0E | 0XA0;
- //send writer command
- DrvI2C_Ctrl(I2C_PORT1, 0, 0, 1, 0); //clr si flag
- while( I2C1->CON.SI == 0 ); //poll si flag
- //send address
- I2C1->DATA = address&0XFF;
- DrvI2C_Ctrl(I2C_PORT1, 0, 0, 1, 1); //clr si and set ack
- while( I2C1->CON.SI == 0 ); //poll si flag
- //send data
- I2C1->DATA = data; //write data to
- DrvI2C_Ctrl(I2C_PORT1, 0, 0, 1, 1); //clr si and set ack
- while( I2C1->CON.SI == 0 ); //poll si flag
- //send i2c stop
- DrvI2C_Ctrl(I2C_PORT1, 0, 1, 1, 0); //send stop
- //while( I2C1->CON.SI == 0 );
- for(i=0;i<60;i++);
- DrvI2C_Close(I2C_PORT1);
- for(i=0;i<6000;i++);
- for(i=0;i<6000;i++);
- }
- uint8_t Read_24LC64(uint32_t address)
- {
- uint8_t TEMP;
- //Open I2C1 and set clock = 50Kbps
- SystemCoreClock = DrvSYS_GetHCLKFreq();
- DrvI2C_Open(I2C_PORT1, 50000);
- //send i2c start
- DrvI2C_Ctrl(I2C_PORT1, 1, 0, 1, 0); //set start
- while (I2C1->CON.SI == 0); //poll si flag
- //send writer command
- I2C1->DATA = (address>>7) & 0x0E | 0XA0;
- DrvI2C_Ctrl(I2C_PORT1, 0, 0, 1, 0); //clr si
- while( I2C1->CON.SI == 0 ); //poll si flag
- //send address
- I2C1->DATA = address&0XFF;
- DrvI2C_Ctrl(I2C_PORT1, 0, 0, 1, 0); //clr si and set ack
- while( I2C1->CON.SI == 0 ); //poll si flag
- //send start flag
- DrvI2C_Ctrl(I2C_PORT1, 1, 0, 1, 0); //clr si and send start
- while( I2C1->CON.SI == 0 ); //poll si flag
- //send read command
- I2C1->DATA = (address>>7) & 0x0E | 0XA1;
- DrvI2C_Ctrl(I2C_PORT1, 0, 0, 1, 1); //clr si
- while( I2C1->CON.SI == 0 ); //poll si flag
- //resive data
- I2C1->DATA = 0XFF;
- DrvI2C_Ctrl(I2C_PORT1, 0, 0, 1, 0); //clr si
- while( I2C1->CON.SI == 0 ); //poll si flag
- TEMP= I2C1->DATA;
- //send i2c stop
- DrvI2C_Ctrl(I2C_PORT1, 0, 1, 1, 0); //clr si and set stop
- DrvI2C_Close(I2C_PORT1);
- return TEMP;
- }
总共的功能是把一个“OK”字符串写入E2PROM,再读出装入另一个字符数组,用串口打印出来。
下面是效果图
好的,先写到这,主要的流程就是跟上面几张图的思想是一样的。下次介绍下CoOS吧,本人比较喜欢他们的开发环境CoIDE。