新手上路
- 积分
- 27
- 金钱
- 27
- 注册时间
- 2018-5-15
- 在线时间
- 2 小时
|
2金钱
我是模拟手册上面做的,采取了一些别人的程序,现在主要的问题是读取的问题,代码贴在下面了,本人是新人,第一次学stm32,在大学只学过c++,做的很蠢的地方轻骂,谢谢啦。
#include "stm32f10x.h"
#include <sys.h>
#include "delay.h"
#include "usart.h"
#define LED0 PAout(8)
#define LED1 PDout(2)
#define IIC_SCL GPIO_Pin_12//PC12
#define IIC_SDA GPIO_Pin_11//PC11
#define GPIO_IIC GPIOC
#define IIC_SCL_H GPIO_SetBits(GPIO_IIC,IIC_SCL)
#define IIC_SCL_L GPIO_ResetBits(GPIO_IIC,IIC_SCL)
#define IIC_SDA_H GPIO_SetBits(GPIO_IIC,IIC_SDA)
#define IIC_SDA_L GPIO_ResetBits(GPIO_IIC,IIC_SDA)
#define IIC_Add 0x23
#define IIC_RX_Length 15
unsigned char IIC_TX_Buffer[8];
unsigned char IIC_RX_Buffer[8];
unsigned char WR_Flag;
//iic.C
/************************************************/
void IIC_INIT(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
GPIO_InitStructure.GPIO_Pin = IIC_SCL|IIC_SDA;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
IIC_SCL_H; IIC_SDA_H;
}
void IIC_SDA_OUT(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = IIC_SDA;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOC, &GPIO_InitStructure);
void IIC_SDA_IN(void)//SDAêäèë
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = IIC_SDA;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOC, &GPIO_InitStructure);
}
void IIC_Start(void)
{
IIC_SDA_OUT();
IIC_SDA_H;
IIC_SCL_H;
delay_us(10);
IIC_SDA_L;
delay_us(10);
IIC_SCL_L;
}
void IIC_Stop(void)
{
IIC_SDA_OUT();
IIC_SDA_L;
IIC_SCL_L;
IIC_SCL_H;
delay_us(10);
IIC_SDA_H;
delay_us(10);
}
void IIC_ACK(void)
{
IIC_SCL_L;
IIC_SDA_OUT();
IIC_SDA_L;
delay_us(10);
IIC_SCL_H;
delay_us(10);
IIC_SCL_L;
}
void IIC_NACK(void)
{
IIC_SCL_L;
IIC_SDA_OUT();
IIC_SDA_H;
delay_us(10);
IIC_SCL_H;
delay_us(10);
IIC_SCL_L;
}
u8 IIC_Write_Ack(void)
{
u8 tempTime=0;
IIC_SDA_IN();
IIC_SDA_H;
delay_us(10);
IIC_SCL_H;
delay_us(10);
while(GPIO_ReadInputDataBit(GPIO_IIC,IIC_SDA))
{
tempTime++;
if(tempTime>250)
{
IIC_Stop();
return 1;
}
}
IIC_SCL_L;
return 0;
}
void IIC_Send_Byte(u8 txd)
{
u8 i=0;
IIC_SDA_OUT();
IIC_SCL_L;
for(i=0;i<8;i++)
{
if((txd&0x80)>0)
IIC_SDA_H;
else
IIC_SDA_L;
txd<<=1;
IIC_SCL_H;
delay_us(10);
IIC_SCL_L;
delay_us(10);
}
IIC_SCL_H;
delay_us(10);
IIC_SCL_L;
delay_us(10);
}
u8 IIC_Read_Byte(u8 ack)
{
u8 i=0,receive=0;
IIC_SDA_IN();
for(i=0;i<8;i++)
{
IIC_SCL_L;
delay_us(10);
IIC_SCL_H;
delay_us(10);
receive<<=1;
if(GPIO_ReadInputDataBit(GPIO_IIC,IIC_SDA))
{
receive++;
}
delay_us(10);
}
if(ack==0)
IIC_NACK();
else
IIC_ACK();
return receive;
}
/********************************************/
//LED.C
/********************************************/
void Led_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_8;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_Init(GPIOA,&GPIO_InitStructure);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD,ENABLE);
GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2;
GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
GPIO_Init(GPIOD,&GPIO_InitStructure);
}
/********************************************/
//BH1750
//***************************************************
u8 WriteNByte(unsigned char sla,unsigned char *s,unsigned char n)
{
unsigned char i;
IIC_Start();
IIC_Send_Byte(sla);
if(!IIC_Write_Ack())
{
WR_Flag = 1;
return 0;
}
for(i=0;i<n;i++)
{
IIC_Send_Byte(*(s+i));
if(!IIC_Write_Ack())
{
WR_Flag = 1;
return(0);
}
}
IIC_Stop();
return 1;
}
u8 ReadNByte(unsigned char Sal, unsigned char *p,unsigned char n)
{
unsigned char i;
IIC_Start();
IIC_Send_Byte(Sal+0x01);
if(!IIC_Write_Ack())
{
WR_Flag = 1;
return 0;
}
delay_us(30);
for(i=0;i<n-1;i++)
{
*(p+i)=IIC_Read_Byte(1);
//IIC_ACK();
}
*(p+n-1)=IIC_Read_Byte(0);
//IIC_NACK();
IIC_Stop();
return 1;
}
unsigned int CRC16(unsigned char *ptr, unsigned char len)
{
unsigned int crc=0xffff;
unsigned char i;
while(len--)
{
crc ^=*ptr++;
for(i=0;i<8;i++)
{
if(crc & 0x1)
{
crc>>=1;
crc^=0xa001;
}
else
{
crc>>=1;
}
}
}
return crc;
}
unsigned char CheckCRC(unsigned char *ptr,unsigned char len)
{
unsigned int crc;
crc=(unsigned int)CRC16(ptr,len-2);
if(ptr[len-1]==(crc>>8) && ptr[len-2]==(crc & 0x00ff))
{
return 0xff;
}
else
{
return 0x0;
}
}
void Waken(void)
{
IIC_Start();
IIC_Send_Byte(IIC_Add);
if(IIC_Write_Ack())
{printf("---------");}
delay_ms(2);
IIC_SCL_L;
delay_us(800);
IIC_Stop();
}
void Clear_Data (void)
{
int i;
for(i=0;i<IIC_RX_Length;i++)
{
IIC_RX_Buffer = 0x00;
}
}
void UARTSend_Nbyte(void)
{
double Light;
/***************************************************/
Clear_Data();
WR_Flag = 0;
Waken();
WriteNByte(IIC_Add,IIC_TX_Buffer,8);
delay_ms(2);
ReadNByte(IIC_Add,IIC_RX_Buffer,4);
delay_ms(2);
IIC_SDA_H;
IIC_SCL_H;
/***************************************************/
if(WR_Flag == 0)
{
if(CheckCRC(IIC_RX_Buffer,8))
{
Light= IIC_RX_Buffer[2]*256+IIC_RX_Buffer[3];
printf("光照度:%.1f \n",Light);
}
else
{
printf("Data: CRC Wrong\n");
}
}
else
{
printf("Sensor Not Connected\n");
}
}
/******************************************************************/
int main()
{
Led_Init();
delay_init();
uart_init(115200);
IIC_INIT();
while(1)
{
LED1=1;
UARTSend_Nbyte();
delay_ms(500);
delay_ms(500);
delay_ms(500);
delay_ms(500);
}
}
|
|