- /**************************************************************************//**
- * [url=home.php?mod=space&uid=288409]@file[/url] main.c
- * [url=home.php?mod=space&uid=895143]@version[/url] V1.00
- * $Revision: 3 $
- * $Date: 15/08/18 11:54a $
- * [url=home.php?mod=space&uid=247401]@brief[/url] NUC200 Series LED Controller Sample Code
- *
- * @note
- * Copyright (C) 2011 Nuvoton Technology Corp. All rights reserved.
- *
- ******************************************************************************/
- #include <stdio.h>
- #include "NUC230_240.h"
- #include "NuEdu-Basic01.h"
- #define I2C_ADDRESS_w 0xa0
- #define I2C_ADDRESS_r 0xa1
- #define I2C_DELAY_TIME 10 /* us */
- #define ACK 0
- #define NO_ACK 1
- #define I2C_CLK PA11
- #define I2C_DAT PA10
- void I2C_init(void) {
- GPIO_SetMode(PA, BIT10, GPIO_PMD_OPEN_DRAIN);//I2C_CLK
- GPIO_SetMode(PA, BIT11, GPIO_PMD_OPEN_DRAIN);//I2C_DAT
- I2C_CLK=1;
- I2C_DAT=1;
- }
- void I2C_start(void) {
- /* I2C start sequence is defined as
- * a High to Low Transition on the data
- * line as the CLK pin is high */
- I2C_DAT=1;
- I2C_CLK=1;
- CLK_SysTickDelay(I2C_DELAY_TIME);
- I2C_DAT=0;
- I2C_CLK=0;
- CLK_SysTickDelay(I2C_DELAY_TIME);
- }
- void I2C_stop(void) {
-
- /* I2C stop sequence is defined as
- * data pin is low, then CLK pin is high,
- * finally data pin is high. */
- I2C_DAT=0;
- I2C_CLK=1;
- I2C_DAT=1;
- }
- unsigned char I2C_write(unsigned char data) {
-
- /* An I2C output byte is bits 7-0
- * (MSB to LSB). Shift one bit at a time
- * to the MDO output, and then clock the
- * data to the I2C Slave */
- unsigned char i,temp;
- /* Write to slave */
- for(i = 0; i < 8; i++)
- {
- /* Send data bit */
- if((data&0x80)==0x80)
- I2C_DAT=1;
- else
- I2C_DAT=0;
- data <<= 1; /* Shift one bit */
- I2C_CLK=1; /* SCL: High */
- CLK_SysTickDelay(I2C_DELAY_TIME);
- I2C_CLK=0; /* SCL: Low */
- CLK_SysTickDelay(I2C_DELAY_TIME);
- }
- I2C_DAT=1;
-
- I2C_CLK=1; /* SCL: High */
- CLK_SysTickDelay(I2C_DELAY_TIME/2);
- temp=I2C_DAT; /* Read ACK bit from slave */
- CLK_SysTickDelay(I2C_DELAY_TIME/2);
- I2C_CLK=0; /* SCL: Low */
- CLK_SysTickDelay(I2C_DELAY_TIME);
- return temp;
- }
- unsigned char I2C_read(unsigned char send_ack)
- {
-
- unsigned char i, data;
- data = 0x00;
- /* Read from slave */
- for(i = 0; i < 8; i++) {
- data <<= 1; /* Shift one bit */
- data |= I2C_DAT; /* Read data bit */
- I2C_CLK=1; /* SCL: High */
- CLK_SysTickDelay(I2C_DELAY_TIME);
- I2C_CLK=0; /* SCL: Low */
- CLK_SysTickDelay(I2C_DELAY_TIME);
- }
- /* Send ACK bit to slave */
- I2C_DAT=send_ack;
- I2C_CLK=1; /* SCL: High */
- CLK_SysTickDelay(I2C_DELAY_TIME);
- I2C_CLK=0; /* SCL: Low */
- CLK_SysTickDelay(I2C_DELAY_TIME);
- return data;
-
- }
- /*---------------------------------------------------------------------------------------------------------*/
- /* MAIN function */
- /*---------------------------------------------------------------------------------------------------------*/
- int main(void)
- {
- SYS_Init();
- UART0_Init();
- I2C_init();
- I2C_start();
- printf("ack=0x%x,\n\r",I2C_write(I2C_ADDRESS_w));
- printf("ack=0x%x,\n\r",I2C_write(0x00)); //eeprom address high
- printf("ack=0x%x,\n\r",I2C_write(0x00));//eeprom address low
- printf("ack=0x%x,\n\r",I2C_write(0x55));//write 0x55
- I2C_stop();
- CLK_SysTickDelay(10000); //delay 10ms
- I2C_start();
- printf("ack=0x%x,\n\r",I2C_write(I2C_ADDRESS_w));
- printf("ack=0x%x,\n\r",I2C_write(0x00)); //eeprom address high
- printf("ack=0x%x,\n\r",I2C_write(0x00));//eeprom address low
- I2C_start();
- printf("ack=0x%x,\n\r",I2C_write(I2C_ADDRESS_r));
- printf("data=0x%x,\n\r",I2C_read(NO_ACK));
- I2C_stop();
- while(1);
-
- }
|