高中生
最后登录1970-1-1
在线时间 小时
注册时间2015-7-21
|
楼主 |
发表于 2015-8-27 22:02:40
|
显示全部楼层
#ifndef _USART_H_
#define _USART_H_
#include "stm32f10x.h"
#include "stdio.h"
void Usart_Config(void);
#endif
#include "usart.h"
void Usart_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_USART1,ENABLE);
/*配置PA9和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_Init(GPIOA,&GPIO_InitStructure);
/*配置串口usart1*/
USART_InitStructure.USART_BaudRate=115200;
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_Cmd(USART1,ENABLE);
}
int fputc(int ch,FILE *f)
{
USART_SendData(USART1,(uint8_t)ch);
while(USART_GetFlagStatus(USART1,USART_FLAG_TC==RESET));
return ch;
}
int Fgetc(FILE *f)
{
while(USART_GetFlagStatus(USART1,USART_FLAG_RXNE)==RESET);
return (int)USART_ReceiveData(USART1);
}
#include "I2C.h"
#define I2C_Own_Address 0xA0
#define I2C_PageSize 8
#define I2C_Speed 400000
uint16_t Block_Address;
void I2C_InitConfig(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
I2C_InitTypeDef I2C_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_I2C1,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6|GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_OD;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
I2C_InitStructure.I2C_Mode=I2C_Mode_I2C;
I2C_InitStructure.I2C_DutyCycle=I2C_DutyCycle_2;
I2C_InitStructure.I2C_OwnAddress1=I2C_Own_Address;
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);
I2C_Cmd(I2C1,ENABLE);
#ifdef Block0_Address
Block_Address=Block0_Address;
#endif
#ifdef Block1_Address
Block_Address=Block1_Address;
#endif
#ifdef Block2_Address
Block_Address=Block2_Address;
#endif
#ifdef Block3_Address
Block_Address=Block3_Address;
#endif
}
void I2C_BufferWrite(uint8_t *pBuffer,uint8_t WriteAddr,uint16_t NumByteWrite)//pBuffer缓冲区指针,WriteAddr写入的地址,NumByteWrite写的字节数
{
uint8_t NumPage=0,NumSingle=0,Addr=0,Count=0;
NumPage=NumByteWrite/I2C_PageSize;//要写进的数据的页数
NumSingle=NumByteWrite%I2C_PageSize;//不足一页数据的个数
Addr=WriteAddr%I2C_PageSize;//写入地址是每页的第几位
Count=I2C_PageSize-Addr;//开始页写入的数据及其个数
I2C_WaitStandbyStatus();//等待
if(Addr==0)//说明写入的地址是第一页的开始
{
if(NumPage==0)//说明写入的数据小于一页
{
I2C_PageWrite(pBuffer,WriteAddr,NumSingle);
I2C_WaitStandbyStatus();
}
else//写入的数据大一或者等于一页
{
while(NumPage--)//检查页数的多少,少于一页后退出while
{
I2C_PageWrite(pBuffer,WriteAddr,I2C_PageSize);
I2C_WaitStandbyStatus();
WriteAddr+=I2C_PageSize;//更新地址和指针的数据,以便继续输出
pBuffer+=I2C_PageSize;
}
if(NumSingle!=0)//剩余数据少于一页
{
I2C_PageWrite(pBuffer,WriteAddr,NumSingle);
I2C_WaitStandbyStatus();
}
}
}
else //开始的地址不是开始页的地址
{
if(NumPage==0)//说明数据少于一页
{
I2C_PageWrite(pBuffer,WriteAddr,NumSingle);
I2C_WaitStandbyStatus();
}
else//数据大于或者等于一页
{
NumByteWrite-=Count;
NumPage=NumByteWrite/I2C_PageSize;
NumPage=NumByteWrite%I2C_PageSize;
if(Count!=0)
{
I2C_PageWrite(pBuffer,WriteAddr,Count);
I2C_WaitStandbyStatus();
WriteAddr+=Count;
pBuffer+=Count;
}
while(NumPage--)
{
I2C_PageWrite(pBuffer,WriteAddr,I2C_PageSize);
I2C_WaitStandbyStatus();
WriteAddr+=I2C_PageSize;//更新地址和指针的数据,以便继续输出
pBuffer+=I2C_PageSize;
}
if(NumSingle!=0)
{
I2C_PageWrite(pBuffer,WriteAddr,NumSingle);
I2C_WaitStandbyStatus();
}
}
}
}
/*时序产生的
EV5 I2C_EVENT_MASTER_MODE_SELECT
EV6 I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED
EV7 I2C_EVENT_MASTER_BYTE_RECEIVED
EARM I2C_EVENT_MASTER_BYTE_TRANSMITTING
EARM_2 I2C_EVENT_MASTER_BYTE_TRANSMITTED
EV9 I2C_EVENT_MASTER_MODE_ADDRESS10
*/
void I2C_PageWrite(uint8_t *pBuffer,uint8_t WriteAddr,uint16_t NumByteWrite)
{
while(I2C_GetFlagStatus(I2C1,I2C_FLAG_BUSY));//如果I2C1不在忙的状态,那么进行数据的传送
//参考零死角玩转STM32的第258页的时序
I2C_GenerateSTART(I2C1,ENABLE);//发送开始信号
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT));//产生EV5并进行清除
I2C_Send7bitAddress(I2C1,Block_Address,I2C_Direction_Transmitter);//发送7位地址
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C_SendData(I2C1,WriteAddr);//发送数据的地址
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED));//产生EARM并清除
while(NumByteWrite--)//写数据
{
I2C_SendData(I2C1,*pBuffer); //通过外设I2C1发送数据
pBuffer++;
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED));//产生EARM并清除,并检查主机发送数据是否成功
}
I2C_GenerateSTOP(I2C1,ENABLE); //数据发送结束
}
void I2C_ByteWrite(uint8_t* pBuffer, uint8_t WriteAddr)
{
I2C_GenerateSTART(I2C1,ENABLE);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C1,Block_Address,I2C_Direction_Transmitter);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C_SendData(I2C1,WriteAddr);
while(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_SendData(I2C1,WriteAddr);
while((!I2C_CheckEvent(I2C1, I2C_EVENT_MASTER_BYTE_TRANSMITTED)));
I2C_SendData(I2C1,*pBuffer);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_GenerateSTOP(I2C1, ENABLE);
}
void I2C_BufferRead(uint8_t *pBuffer,uint8_t ReadAddr,uint16_t NumByteRead)
{
while(I2C_GetFlagStatus(I2C1,I2C_FLAG_BUSY));
I2C_GenerateSTART(I2C1,ENABLE);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_MODE_SELECT));
I2C_Send7bitAddress(I2C1,Block_Address,I2C_Direction_Transmitter);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C_Cmd(I2C1,ENABLE);
I2C_SendData(I2C1,ReadAddr);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_TRANSMITTED));
I2C_GenerateSTART(I2C1,ENABLE);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
I2C_Send7bitAddress(I2C1,Block_Address,I2C_Direction_Transmitter);
while(!I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_TRANSMITTER_MODE_SELECTED));
while(NumByteRead)
{
if(1==NumByteRead)
{
I2C_AcknowledgeConfig(I2C1,DISABLE);
I2C_GenerateSTOP(I2C1,ENABLE);
}
/*测试EV7并清除*/
if(I2C_CheckEvent(I2C1,I2C_EVENT_MASTER_BYTE_RECEIVED))
{
*pBuffer=I2C_ReceiveData(I2C1);
pBuffer++;
NumByteRead--;
}
}
I2C_AcknowledgeConfig(I2C1,ENABLE);
}
void I2C_WaitStandbyStatus(void)
{
vu16 Temp;
do
{
I2C_GenerateSTART(I2C1,ENABLE);
Temp=I2C_ReadRegister(I2C1,I2C_Register_SR1);
I2C_Send7bitAddress(I2C1,Block_Address,I2C_Direction_Transmitter);
}
while(!(I2C_ReadRegister(I2C1,I2C_Register_SR1)&0x0002));
I2C_ClearFlag(I2C1,I2C_FLAG_AF);
I2C_GenerateSTOP(I2C1,ENABLE);
}
#include "LED.h"
#include "stm32f10x.h"
void LED_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_3;
GPIO_Init(GPIOC,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_4;
GPIO_Init(GPIOC,&GPIO_InitStructure);
GPIO_SetBits(GPIOB,GPIO_Pin_0);
GPIO_SetBits(GPIOC,GPIO_Pin_3|GPIO_Pin_4);
}
#include "stm32f10x.h"
#include "I2C.h"
#include "usart.h"
#include "LED.h"
#define ON 0
#define OFF 1
#define FristPage 0x00
u8 I2C_Buf_Write[256];
u8 I2C_Buf_Read[256];
void I2C_Test(void)
{
uint16_t i;
printf("\r输入的数据:\n\r");
for(i=0;i<256;i++)
{
I2C_Buf_Write=i;
printf("0x%02x ",I2C_Buf_Write);
if(i%16==15)
{
printf("\n\r");
}
}
LED1(ON);
I2C_BufferWrite(I2C_Buf_Write,FristPage,256);
LED1(OFF);
printf("\n\t写入成功\t\t");
printf("\n\t读出的数据\t\t");
LED2(ON);
I2C_BufferRead(I2C_Buf_Read,FristPage,256);
LED2(OFF);
for(i=0;i<256;i++)
{
if(I2C_Buf_Write!=I2C_Buf_Read)
{
printf("0x%02X ",I2C_Buf_Read);
if(i%16==15)
{
printf("\n");
}
printf("\n\t错误,输入与输出不同");
return ;
}
printf("0x%02X ",I2C_Buf_Read);
if(i%16==15)
{
printf("\n");
}
}
printf("\n\n\r程序测试成功\n\r");
}
int main(void)
{
LED_Config();
Usart_Config();
I2C_InitConfig();
I2C_Test();
while(1)
{
;
}
}
|
|