IIC Interface Operation

Publisher:xi24Latest update time:2016-04-20 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere
Philips

SDA SCL

Each device has a unique address and there is a master/slave relationship between them

Start signal End signal Response signal (sent by the receiver)

The RTC chip M41t11 used in this development board is connected to the processor using the IIC bus.

@**********************************************************************************
@ File: head.S
@ Function: Set up SDRAM, copy the program to SDRAM, and then jump to SDRAM to continue execution
@**************************************************************************       
  
.extern     main
.text
.global _start
_start:
@**********************************************************************************       
@ Interrupt vector. In this program, except Reset and HandleIRQ, other exceptions are not used
@******************************************************************************       
    Reset

@ 0x04: The vector address of the undefined instruction abort mode
HandleUndef:
    HandleUndef
 
@ 0x08: The vector address of the management mode, which is entered by the SWI instruction
HandleSWI:
    HandleSWI

@ 0x0c: vector address of the exception caused by instruction prefetch termination
HandlePrefetchAbort:
    HandlePrefetchAbort

@ 0x10: The vector address of the exception caused by data access termination
HandleDataAbort:
    HandleDataAbort

@ 0x14: Reserved
HandleNotUsed:
    HandleNotUsed

@ 0x18: interrupt mode vector address
    HandleIRQ

@ 0x1c: Vector address of fast interrupt mode
HandleFIQ:
    HandleFIQ

Reset:                  
   ldr sp, =4096           @ Set the stack pointer. The following are all C functions. You need to set the stack before calling them.
   bl  disable_watch_dog   @ Disable WATCHDOG, otherwise the CPU will restart continuously.
   bl  clock_init          @ Set up MPLL, change FCLK, HCLK, PCLK
   bl  memsetup            @ Set up the memory controller to use SDRAM
   bl  nand_init           @ Initialize NAND Flash
   
                           @ Copy code to SDRAM
   ldr r0, =0x30000000  @ 1. Destination address = 0x30000000    @ 1. Destination address = 0x30000000, which is the starting address of SDRAM
   mov r1, #4096           @ 2. Source address   = 4096. The code running at the address in SDRAM is saved at the beginning of NAND Flash address 4096
   mov r2, #16*1024        @ 3. Copy length = 16K, which is enough for this experiment
   bl  CopyCode2SDRAM      @ Call C function CopyCode2SDRAM
   
   bl  clean_bss           @ Clear the bss segment. Uninitialized global/static variables or those with an initial value of 0 are stored in the bss segment.

   msr cpsr_c, #0xd2       @ Enter interrupt mode
   ldr sp, =0x31000000     @ Set interrupt mode stack pointer

   msr cpsr_c, #0xdf       @ Enter system mode
   ldr sp, =0x34000000     @ Set the system mode stack pointer,

   ldr lr, =ret_initirq    @ Set the return address    
   ldr pc, =init_irq       @ Call the interrupt initialization function
ret_initirq:
   msr cpsr_c, #0x5f       @ Set I-bit=0, open IRQ interrupt

   ldr lr, =halt_loop      @ set the return address
   ldr pc, =main           @ call main function
halt_loop:
    halt_loop

HandleIRQ:
   sub lr, lr, #4                  @ Calculate the return address
   stmdb   sp!,    { r0-r12,lr }   @ Save the used registers
                                   @ Note that sp at this time is the sp of interrupt mode
                                   @ The initial value is 4096 set above
   
   ldr lr, =int_return             @ Set the return address after calling the IRQ_Handle function 
   ldr pc, =IRQ_Handle             @ Call the interrupt distribution function, in interrupt.c
int_return:
   ldmia   sp!,    { r0-r12,pc }^  @ Interrupt return, ^ means copy the value of spsr to cpsr

 

#include
#include "serial.h"
#include "i2c.h"
#include "m41t11.h"


int main()
{
   char c;
   char str[200];
   int i;
   struct rtc_time dt;
   
   uart0_init();   // baud rate 115200, 8N1 (8 data bits, no parity bit, 1 stop bit)
   
   i2c_init();
   
   while (1)
   {
       printf("rn##### RTC Menu #####rn");
       printf("Data format: 'year.month.day w hour:min:sec', 'w' is week daynr");
       printf("eg: 2007.08.30 4 01:16:57nr");
       printf("[S] Set the RTCnr");
       printf("[R] Read the RTCnr");
       printf("Enter your selection: ");

       c = getc();
       printf("%cnr", c);
       switch (c)
       {
           case 's':
           case 'S':
           {
               printf("Enter date&time: ");
               i = 0;
               do
               {
                   c = getc ();
                   str[i++] = c;
                   putc(c);
               } while(c != 'n' && c != 'r');
               str[i] = '';

               while(--i >= 0)
               {
                   if (str[i] < '0' || str[i] > '9')
                       str[i] = ' ';
               }

               sscanf(str, "%d %d %d %d %d %d %d",         
                   &dt.tm_year, &dt.tm_mon, &dt.tm_mday,   
                   &dt.tm_wday,                            
                   &dt.tm_hour, &dt.tm_min, &dt.tm_sec);

               printf("nr*** Date set as: %04d.%02d.%02d %d %02d:%02d:%02d ***nr",
                   dt.tm_year, dt.tm_mon, dt.tm_mday,     
                   dt.tm_wday ,                            
                   dt.tm_hour, dt.tm_min, dt.tm_sec);

               if (m41t11_set_datetime(&dt))
                   printf("Set RTC failed!nr");
                   
               break;
           }
           
           case 'r':
           case 'R':
           {
               if (m41t11_get_datetime(&dt))
                   printf("Read RTC failed!nr");

               printf("nr*** Now is: %04d.%02d.%02d %d %02d:%02d:%02d ***nr", 
                   dt.tm_year, dt.tm_mon, dt.tm_mday,                  
                   dt.tm_wday,                                         
                   dt.tm_hour, dt.tm_min, dt.tm_sec);

               break;
           }
       }
       
   }


Reference address:IIC Interface Operation

Previous article:SQLite transplantation
Next article:UART Operation

Latest Microcontroller Articles
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号