hello everyone,I'm a new user of MCU ARM Cortex M0. Please help me check my code.
I need to use I2C instruction to program and communicate with accelerometer(ADXL345),but,as beginning,I just face to the trouble that is I can't program the initial properties of ADXL345. Over all I use I2C to initialize the ADXL345. this is my code, please help me checking, this code is not work it seem like ADXL345 doesn't send ACK back to my board (Nuvoton NUC140),but I don't know all whether where the problems come from(My I2C code or ADXL345 )
/*---------------------------------------------------------------------------------------------------------*/ /* */ /* Copyright (c) Nuvoton Technology Corp. All rights reserved. */ /* */ /*---------------------------------------------------------------------------------------------------------*/ #include <stdio.h> #include "NUC1xx.h" #include "Driver\DrvSYS.h" #include "Driver\DrvGPIO.h" #include "LCD_Driver.h" #include "Seven_Segment.h" #include "Driver\DrvI2C.h" void Xoffset(void) { //do follow I2C hierarchy //x offset DrvI2C_Ctrl(I2C_PORT0,1,0,0,0); //start while(DrvI2C_GetIntFlag(I2C_PORT0)==0); DrvI2C_Ctrl(I2C_PORT0,0,0,1,0); //clear flag DrvI2C_WriteData(I2C_PORT0,0xA6); //slave address while(DrvI2C_GetIntFlag(I2C_PORT0)==0); //wait ack DrvI2C_Ctrl(I2C_PORT0,0,0,1,0); //clear flag DrvI2C_WriteData(I2C_PORT0,0x1E); // x offset while(DrvI2C_GetIntFlag(I2C_PORT0)==0); //wait ack DrvI2C_Ctrl(I2C_PORT0,0,0,1,0); //clear flag DrvI2C_WriteData(I2C_PORT0,0x00); // 0 while(DrvI2C_GetIntFlag(I2C_PORT0)==0); //wait ack DrvI2C_Ctrl(I2C_PORT0,0,1,1,0); //stop } int32_t main (void) { UNLOCKREG(); SYSCLK->PWRCON.XTL32K_EN = 1;//Enable 32Khz for RTC clock source SYSCLK->PWRCON.XTL12M_EN = 1; SYSCLK->CLKSEL0.HCLK_S = 0; LOCKREG(); DrvI2C_Open(I2C_PORT0,3200); DrvI2C_EnableInt(I2C_PORT0); Xoffset(); Initial_pannel(); //call initial pannel function clr_all_pannal(); print_lcd(1," I2C"); while(1) { __NOP(); } }
if you have any recommentation,please post it for helping me. thank you so much.