/****************************************Copyright (c)*********************************************** *****
**
**
浩宇电子 - 嵌入式系统解决方案提供商
**
**--------------File Info---------------------------------------------------------------------------------
** File name: main.c
** Descriptions: The RTC application function
**
**--------------------------------------------------------------------------------------------------------
** Created by: AVRman
** Created date: 2010-10-30
** Version: v1.0
** Descriptions: The original version
**
**--------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
** Version:
** Descriptions:
**
************************************************** ************************************************** *****/
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#include "stm32f10x_i2c.h"
#include "stm32f10x_usart.h"
#include "stm32f10x_rcc.h"
//#include "RTC_Time.h"
#include <stdio.h>
#define I2C2_SLAVE_ADDRESS7 0x1E //address for magnetometer
#define I2C_SPEED 50000 //50Khz speed for I2C
#ifdef __GNUC__
/* With GCC/RAISONANCE, small printf (option LD Linker->Libraries->Small printf
set to 'Yes') calls __io_putchar() */
#define PUTCHAR_PROTOTYPE int __io_putchar(int ch)
#else
#define PUTCHAR_PROTOTYPE int fputc(int ch, FILE *f)
#endif /* __GNUC__ */
void Delay (uint32_t nCount)
{
for(; nCount != 0; nCount--);
}
unsigned char Data;
/* Private function prototypes -----------------------------------------------*/
void GPIO_Configuration(void);
void USART_Configuration(void);
void I2C_Configuration(void);
void RCC_Configuration(void);
void delay(void);
void STM32_I2C_test(void);
void Write_data(unsigned char Data_address,unsigned char Data_Value);
unsigned char Read_data(unsigned char Data_address_r);
/************************************************** *****************************
* Function Name : main
* Description : Main program
* Input : None
* Output : None
* Return : None
* Attention : None
************************************************** *****************************/
int main(void)
{
GPIO_Configuration();
USART_Configuration();
RCC_Configuration();
I2C_Configuration();
// printf("\r\n");
// printf("****************************************** *********************\r\n");
// printf("* *\r\n");
//printf("Heloo\r\n");
// printf("* *\r\n");
// printf("****************************************** *********************\r\n");
/* LED1-ON LED2-ON */
//GPIO_SetBits(GPIOB , GPIO_Pin_0);
//GPIO_SetBits(GPIOB , GPIO_Pin_1);
GPIO_SetBits(GPIOA, GPIO_Pin_2);
Delay(0xfffff);
Delay(0xfffff);
Delay(0x5ffff);
//RTC_Init();
printf(" Helooooooooooooooooooooooooooooooooooooooooooooooo ooooo\r\n");
/* Infinite loop */
//STM32_I2C_test();
while (1){
//USART_SendData(USART1,'T');
Write_data(0x10,0x8B);
//STM32_I2C_test();
Data=Read_data(0x10);
USART_SendData(USART1,Data);
// Write_data(0x10,0x80);
GPIO_ResetBits(GPIOA , GPIO_Pin_2);
Delay(0xfffff);
Delay(0xfffff);
Delay(0x5ffff);
GPIO_SetBits(GPIOA , GPIO_Pin_2);
Delay(0xfffff);
Delay(0xfffff);
Delay(0x5ffff);
// USART_SendData(USART1,'T');
// printf("* *\r\n");
}
}
/************************************************** *************************//**
* @brief Configures the different system clocks.
************************************************** ****************************/
void RCC_Configuration(void){
/* Setup the microcontroller system. Initialize the Embedded Flash Interface,
initialize the PLL and update the SystemFrequency variable. */
SystemInit();}
/************************************************** *****************************
* Function Name : GPIO_Configuration
* Description : Configure GPIO Pin
* Input : None
* Output : None
* Return : None
* Attention : None
************************************************** *****************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA , ENABLE);
/**
* LED1 -> PB0 LED2 -> PB1
*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 ;//| GPIO_Pin_1;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
/************************************************** *****************************
* Function Name : USART_Configuration
* Description : Configure USART1
* Input : None
* Output : None
* Return : None
* Attention : None
************************************************** *****************************/
void USART_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA |RCC_APB2Periph_AFIO| RCC_APB2Periph_USART1,ENABLE);
/*
* USART1_TX -> PA9 , USART1_RX -> PA10
*/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
USART_InitStructure.USART_BaudRate =4800;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
USART_Init(USART1, &USART_InitStructure);
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
USART_Cmd(USART1, ENABLE);
//NVIC_EnableIRQ(USART1_IRQn);
}
void STM32_I2C_test(void)
{
uint8_t I2C_address;
uint32_t I2C_speed;
GPIO_InitTypeDef GPIO_InitStructure;
I2C_InitTypeDef I2C_InitStructure;
I2C_address = 0xA0; //
//address = 0x94; //
I2C_speed = 1000; // 1000Hz
/*enable I2C*/
I2C_Cmd(I2C1,ENABLE);
/* I2C1 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
/* I2C1 SDA and SCL configuration */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_OD;
GPIO_Init(GPIOB, &GPIO_InitStructure);
//SCL is pin06 and SDA is pin 07 for I2C1
/* I2C1 configuration */
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = 0x30;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed = I2C_speed ;
I2C_Init(I2C1, &I2C_InitStructure);
//
/* check start bit flag */
while(I2C_GetFlagStatus(I2C1, I2C_FLAG_BUSY));
/* initiate start sequence */
I2C_GenerateSTART(I2C1,ENABLE);
while(!I2C_GetFlagStatus(I2C1, I2C_FLAG_SB));
/*send write command to chip*/
I2C_SendData(I2C1,I2C_address);//I2C_Send7bitAddress(I2C1, address, I2C_Direction_Transmitter);
/*check master is now in Tx mode*/
// while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
/*mode register address*/
I2C_SendData(I2C1, 0x02);
/*wait for byte send to complete*/
// while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
/*clear bits*/
// I2C_SendData(I2C1, 0x00);
/*wait for byte send to complete*/
// while(!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED));
/*generate stop*/
// I2C_GenerateSTOP(I2C1, ENABLE);
/*stop bit flag*/
// while(I2C_GetFlagStatus(I2C1, I2C_FLAG_STOPF));
}
void I2C_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
I2C_InitTypeDef I2C_InitStructure;
/*enable I2C*/
/* I2C1 clock enable */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB , ENABLE);
/* I2C1 SDA and SCL configuration */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;// Open Drain, I2C bus pulled high externally
GPIO_Init(GPIOB, &GPIO_InitStructure);
/*SCL is pin06 and SDA is pin 07 for I2C1*/
I2C_Cmd(I2C2,ENABLE);
/* I2C2 configuration */
I2C_InitStructure.I2C_Mode = I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle = I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1 = 0x20;
I2C_InitStructure.I2C_Ack = I2C_Ack_Enable;
I2C_InitStructure.I2C_AcknowledgedAddress = I2C_AcknowledgedAddress_7bit;
I2C_InitStructure.I2C_ClockSpeed =100000 ;
I2C_Init(I2C2, &I2C_InitStructure);
}
void Write_data(unsigned char Data_address,unsigned char Data_Value)
{
//I2C_AcknowledgeConfig(I2C2,ENABLE);
while (I2C_GetFlagStatus(I2C2,I2C_FLAG_BUSY))
I2C_GenerateSTART(I2C2, ENABLE);
while (I2C_GetFlagStatus(I2C2,I2C_FLAG_SB) == RESET)
I2C_Send7bitAddress(I2C2, 0x20, I2C_Direction_Transmitter);
I2C_AcknowledgeConfig(I2C2,ENABLE);
while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED))
I2C_SendData(I2C2,Data_address);
I2C_AcknowledgeConfig(I2C2,ENABLE);
while ((!I2C_GetFlagStatus(I2C2,I2C_FLAG_TXE)) && (!I2C_GetFlagStatus(I2C2,I2C_FLAG_BTF)))
I2C_SendData(I2C2,Data_Value);
I2C_AcknowledgeConfig(I2C2,ENABLE);
while ((!I2C_GetFlagStatus(I2C2,I2C_FLAG_TXE)) && (!I2C_GetFlagStatus(I2C2,I2C_FLAG_BTF)))
I2C_GenerateSTOP(I2C2, ENABLE);
//USART_SendData(USART1,'T');
}
unsigned char Read_data(unsigned char Data_address_r)
{
unsigned char Data_Value_r;
I2C_AcknowledgeConfig(I2C2,ENABLE);
while (I2C_GetFlagStatus(I2C2,I2C_FLAG_BUSY)) {}
I2C_GenerateSTART(I2C2, ENABLE);
//while (I2C_GetFlagStatus(I2C2,I2C_FLAG_SB) == RESET){}
// ersale adrese marbot be AT24C02
/* Transmit the slave address and enable writing operation */
I2C_Send7bitAddress(I2C2, 0x20, I2C_Direction_Transmitter);
//while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED)){}
I2C_AcknowledgeConfig(I2C2,ENABLE);
// ersale adrese registere marbot be time
I2C_SendData(I2C2,Data_address_r);
// while ((!I2C_GetFlagStatus(I2C2,I2C_FLAG_TXE)) && (!I2C_GetFlagStatus(I2C2,I2C_FLAG_BTF))){}
I2C_GenerateSTART(I2C2, ENABLE);
//while (I2C_GetFlagStatus(I2C2,I2C_FLAG_SB) == RESET){}
// ersale adrese marbot be AT24C02
/* Transmit the slave address and enable reading operation */
I2C_Send7bitAddress(I2C2, 0x21, I2C_Direction_Receiver);
// while (!I2C_CheckEvent(I2C2, I2C_EVENT_MASTER_RECEIVER_MODE_SELECTED )){}
I2C_AcknowledgeConfig(I2C2,ENABLE);
// while ( !I2C_GetFlagStatus(I2C2,I2C_FLAG_RXNE) ){}
Data_Value_r = I2C_ReceiveData(I2C2); //reading data
I2C_AcknowledgeConfig(I2C2,ENABLE);
//USART_SendData(USART1,Data_Value_r);
//while (I2C_GetFlagStatus(I2C2,I2C_FLAG_BTF)){}
I2C_GenerateSTOP(I2C2, ENABLE);
//I2C_AcknowledgeConfig(I2C2,ENABLE);
return Data_Value_r;
}
/**
// * @brief Retargets the C library printf function to the USART.
// * @param None
// * @retval None
*/
PUTCHAR_PROTOTYPE
{
/* Place your implementation of fputc here */
/* e.g. write a character to the USART */
USART_SendData(USART1, (uint8_t) ch);
/* Loop until the end of transmission */
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
{}
return ch;
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t line)
{
/* User can add his own implementation to report the file name and line number,
ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif
/************************************************** ************************************************** *****
END FILE
************************************************** ************************************************** *****/