#include "stm32f10x_lib.h"
#include <math.h>
//Keil library
GPIO_InitTypeDef GPIO_InitStructure;
ErrorStatus HSEStartUpStatus;
#define uchar
unsigned char
#define uint
unsigned int
//********
//************************************++++++++++++++++++++++++++++++++
/*模拟IIC端口输出输入定义*/
#define SCL_H GPIOB->BSRR = GPIO_Pin_6
#define SCL_L GPIOB->BRR = GPIO_Pin_6
#define SDA_H GPIOB->BSRR = GPIO_Pin_7
#define SDA_L GPIOB->BRR = GPIO_Pin_7
#define SCL_read GPIOB->IDR & GPIO_Pin_6
#define SDA_read GPIOB->IDR & GPIO_Pin_7
/* 函数申明 -----------------------------------------------*/
void RCC_Configuration(
void);
void GPIO_Configuration(
void);
void USART1_Configuration(
void);
void WWDG_Configuration(
void);
void Delayms(vu32 m);
/*******************************************************************************
* Function Name: I2C_GPIO_Config
* Description : Configration Simulation IIC GPIO
* Input : None
* Output : None
* Return : None
****************************************************************************** */
void I2C_GPIO_Config(
void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
/*******************************************************************************
* Function Name: I2C_delay
* Description : Simulation IIC Timing series delay
* Input : None
* Output : None
* Return : None
****************************************************************************** */
void I2C_delay(
void)
{
u8 i=
30;
//这里可以优化速度 ,经测试最低到5还能写入
while(i)
{
i--;
}
}
/*******************************************************************************
* Function Name: I2C_Start
* Description : Master Start Simulation IIC Communication
* Input : None
* Output : None
* Return : Wheather Start
****************************************************************************** */
bool I2C_Start(
void)
{
SDA_H;
SCL_H;
I2C_delay();
if(!SDA_read)
return FALSE;
//SDA线为低电平则总线忙,退出
SDA_L;
I2C_delay();
if(SDA_read)
return FALSE;
//SDA线为高电平则总线出错,退出
SDA_L;
I2C_delay();
return TRUE;
}
/*******************************************************************************
* Function Name: I2C_Stop
* Description : Master Stop Simulation IIC Communication
* Input : None
* Output : None
* Return : None
****************************************************************************** */
void I2C_Stop(
void)
{
SCL_L;
I2C_delay();
SDA_L;
I2C_delay();
SCL_H;
I2C_delay();
SDA_H;
I2C_delay();
}
/*******************************************************************************
* Function Name: I2C_Ack
* Description : Master Send Acknowledge Single
* Input : None
* Output : None
* Return : None
****************************************************************************** */
void I2C_Ack(
void)
{
SCL_L;
I2C_delay();
SDA_L;
I2C_delay();
SCL_H;
I2C_delay();
SCL_L;
I2C_delay();
}
/*******************************************************************************
* Function Name: I2C_NoAck
* Description : Master Send No Acknowledge Single
* Input : None
* Output : None
* Return : None
****************************************************************************** */
void I2C_NoAck(
void)
{
SCL_L;
I2C_delay();
SDA_H;
I2C_delay();
SCL_H;
I2C_delay();
SCL_L;
I2C_delay();
}
/*******************************************************************************
* Function Name: I2C_WaitAck
* Description : Master Reserive Slave Acknowledge Single
* Input : None
* Output : None
* Return : Wheather Reserive Slave Acknowledge Single
****************************************************************************** */
bool I2C_WaitAck(
void)
//返回为:=1有ACK,=0无ACK
{
SCL_L;
I2C_delay();
SDA_H;
I2C_delay();
SCL_H;
I2C_delay();
if(SDA_read)
{
SCL_L;
I2C_delay();
return FALSE;
}
SCL_L;
I2C_delay();
return TRUE;
}
/*******************************************************************************
* Function Name: I2C_SendByte
* Description : Master Send a Byte to Slave
* Input : Will Send Date
* Output : None
* Return : None
****************************************************************************** */
void I2C_SendByte(u8 SendByte)
//数据从高位到低位//
{
u8 i=
8;
while(i--)
{
SCL_L;
I2C_delay();
if(SendByte&0x80)
SDA_H;
else
SDA_L;
SendByte<<=
1;
I2C_delay();
SCL_H;
I2C_delay();
}
SCL_L;
}
/*******************************************************************************
* Function Name: I2C_RadeByte
* Description : Master Reserive a Byte From Slave
* Input : None
* Output : None
* Return : Date From Slave
****************************************************************************** */
unsigned char I2C_RadeByte(
void)
//数据从高位到低位//
{
u8 i=
8;
u8 ReceiveByte=
0;
SDA_H;
while(i--)
{
ReceiveByte<<=
1;
SCL_L;
I2C_delay();
SCL_H;
I2C_delay();
if(SDA_read)
{
ReceiveByte|=0x01;
}
}
SCL_L;
return ReceiveByte;
}
//ZRX
//单字节写入*******************************************
bool Single_Write(
unsigned char SlaveAddress,
unsigned char REG_Address,
unsigned char REG_data)
//void
{
if(!I2C_Start())
return FALSE;
I2C_SendByte(SlaveAddress);
//发送设备地址+写信号//I2C_SendByte(((REG_Address & 0x0700) >>7) | SlaveAddress & 0xFFFE);//设置高起始地址+器件地址
if(!I2C_WaitAck()){I2C_Stop();
return FALSE;}
I2C_SendByte(REG_Address );
//设置低起始地址
I2C_WaitAck();
I2C_SendByte(REG_data);
I2C_WaitAck();
I2C_Stop();
delay5ms();
return TRUE;
}
//单字节读取*****************************************
unsigned char Single_Read(
unsigned char SlaveAddress,
unsigned char REG_Address)
{
unsigned char REG_data;
if(!I2C_Start())
return FALSE;
I2C_SendByte(SlaveAddress);
//I2C_SendByte(((REG_Address & 0x0700) >>7) | REG_Address & 0xFFFE);//设置高起始地址+器件地址
if(!I2C_WaitAck()){I2C_Stop();test=
1;
return FALSE;}
I2C_SendByte((u8) REG_Address);
//设置低起始地址
I2C_WaitAck();
I2C_Start();
I2C_SendByte(SlaveAddress+
1);
I2C_WaitAck();
REG_data= I2C_RadeByte();
I2C_NoAck();
I2C_Stop();
//return TRUE;
return REG_data;
}
/*
********************************************************************************
** 函数名称 : RCC_Configuration(void)
** 函数功能 : 时钟初始化
** 输 入 : 无
** 输 出 : 无
** 返 回 : 无
********************************************************************************
*/
void RCC_Configuration(
void)
{
/* RCC system reset(for debug purpose) */
RCC_DeInit();
/* Enable HSE */
RCC_HSEConfig(RCC_HSE_ON);
/* Wait till HSE is ready */
HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS)
{
/* HCLK = SYSCLK */
RCC_HCLKConfig(RCC_SYSCLK_Div1);
/* 
CLK2 = HCLK */
RCC_PCLK2Config(RCC_HCLK_Div1);
/* 
CLK1 = HCLK/2 */
RCC_PCLK1Config(RCC_HCLK_Div2);
/* Flash 2 wait state */
FLASH_SetLatency(FLASH_Latency_2);
/* Enable 
refetch Buffer */
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/* 
LLCLK = 8MHz * 9 = 72 MHz */
RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
/* Enable 
LL */
RCC_PLLCmd(ENABLE);
/* Wait till 
LL is ready */
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
{
}
/* Select 
LL as system clock source */
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/* Wait till 
LL is used as system clock source */
while(RCC_GetSYSCLKSource() != 0x08)
{
}
}
/* Enable GPIOA, GPIOB, GPIOC, GPIOD, GPIOE, GPIOF, GPIOG and AFIO clocks */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB , ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD , ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE | RCC_APB2Periph_GPIOF , ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOG | RCC_APB2Periph_AFIO , ENABLE);
}
/*
********************************************************************************
** 函数名称 : GPIO_Configuration(void)
** 函数功能 : 端口初始化
** 输 入 : 无
** 输 出 : 无
** 返 回 : 无
********************************************************************************
*/
void GPIO_Configuration(
void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_GPIOD, ENABLE );
/* Configure USART1 Tx (PA.09) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
// 选中管脚9
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
// 复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
// 最高输出速率50MHz
GPIO_Init(GPIOA, &GPIO_InitStructure);
// 选择A端口
/* Configure USART1 Rx (PA.10) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
//选中管脚10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
//浮空输入
GPIO_Init(GPIOA, &GPIO_InitStructure);
//选择A端口
/* Configure USART2 Tx (PA.02) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
// 选中管脚2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
// 复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
// 最高输出速率50MHz
GPIO_Init(GPIOA, &GPIO_InitStructure);
// 选择A端口
/* Configure USART2 Rx (PA.03) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
//选中管脚3
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
//浮空输入
GPIO_Init(GPIOA, &GPIO_InitStructure);
//选择A端口
/* Configure USART3 Tx (PB.10) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
// 选中管脚10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
// 复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
// 最高输出速率50MHz
GPIO_Init(GPIOB, &GPIO_InitStructure);
// 选择B端口
/* Configure USART3 Rx (PB.11) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
//选中管脚11
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
//浮空输入
GPIO_Init(GPIOB, &GPIO_InitStructure);
//选择B端口
/* Configure USART4 Tx (PC.10) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
// 选中管脚10
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
// 复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
// 最高输出速率50MHz
GPIO_Init(GPIOC, &GPIO_InitStructure);
// 选择C端口
/* Configure USART4 Rx (PC.11) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11;
//选中管脚11
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
//浮空输入
GPIO_Init(GPIOC, &GPIO_InitStructure);
//选择C端口
/* Configure USART5 Tx (PC.12) as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12;
// 选中管脚12
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
// 复用推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
// 最高输出速率50MHz
GPIO_Init(GPIOC, &GPIO_InitStructure);
// 选择C端口
/* Configure USART5 Rx (PD.2) as input floating */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
//选中管脚2
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
//浮空输入
GPIO_Init(GPIOD, &GPIO_InitStructure);
//选择D端口
}
/*
********************************************************************************
** 函数名称 : USART1_Configuration(void)
** 函数功能 : 串口1初始化
** 输 入 : 无
** 输 出 : 无
** 返 回 : 无
********************************************************************************
*/
void USART1_Configuration(
void)
{
USART_InitTypeDef USART_InitStructure;
USART_ClockInitTypeDef USART_ClockInitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1 |RCC_APB2Periph_USART1, ENABLE );
USART_ClockInitStructure.USART_Clock = USART_Clock_Disable;
// 时钟低电平活动
USART_ClockInitStructure.USART_CPOL = USART_CPOL_Low;
// 时钟低电平
USART_ClockInitStructure.USART_CPHA = USART_CPHA_2Edge;
// 时钟第二个边沿进行数据捕获
USART_ClockInitStructure.USART_LastBit = USART_LastBit_Disable;
// 最后一位数据的时钟脉冲不从SCLK输出
/* Configure the USART1 synchronous paramters */
USART_ClockInit(USART1, &USART_ClockInitStructure);
// 时钟参数初始化设置
USART_InitStructure.USART_BaudRate =
115200;
// 波特率为:115200
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
// 8位数据
USART_InitStructure.USART_StopBits = USART_StopBits_1;
// 在帧结尾传输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;
// 发送使能+接收使能
/* Configure USART1 basic and asynchronous paramters */
USART_Init(USART1, &USART_InitStructure);
/* Enable USART1 */
USART_ClearFlag(USART1, USART_IT_RXNE);
//清中断,以免一启用中断后立即产生中断
USART_ITConfig(USART1,USART_IT_RXNE, ENABLE);
//使能USART1中断源
USART_Cmd(USART1, ENABLE);
//USART1总开关:开启
}
/*
********************************************************************************
** 函数名称 : Delay(vu32 nCount)
** 函数功能 : 延时函数
** 输 入 : 无
** 输 出 : 无
** 返 回 : 无
********************************************************************************
*/
void Delay(vu32 nCount)
{
for(; nCount !=
0; nCount--);
}
/*
********************************************************************************
** 函数名称 : void Delayms(vu32 m)
** 函数功能 : 长延时函数 m=1,延时1ms
** 输 入 : 无
** 输 出 : 无
** 返 回 : 无
********************************************************************************
*/
void Delayms(vu32 m)
{
u32 i;
for(; m !=
0; m--)
for (i=
0; i<
50000; i++);
}
void USART1_SendData(uchar SendData)
{
USART_SendData(USART1, SendData);
Delayms(
1);
}
/*
********************************************************************************
** 函数名称 : main(void)
** 函数功能 : 主函数
** 输 入 : 无
** 输 出 : 无
** 返 回 : 无
********************************************************************************
*/
int main(
void)
{
RCC_Configuration();
GPIO_Configuration();
USART1_Configuration();
I2C_GPIO_Config();
Delayms(
1200);
USART1_SendData(
'@');
Delayms(
12);
USART1_SendData(
'#');
}
/*************结束***************/