1、为了更好的方便调试,串口必须要有的,主要打印一些信息,当前时钟、转换后的电压值和I2C读出的数据。
2、通过GPIO 模拟I2C对镁光的MT9V024进行参数初始化。之前用我以前公司SP0A19芯片,是I2C是8位宽的,而镁光的地址是8位,而数据位是16个字节,
其实不管是8位还是16位,每次发送都是8个字节然后一个应答位,所以只要稍微改下代码即可。
3、实现两路ADC连续转换,两路ADC转换:一路是检测锂电池电压;一路是检测压力传感器,其实都是检测电压,当检测到压力为零,并累计多长时间后进入停止模式,在进入停止模式后,再次通过压力挤压唤醒单片机,从而退出停止模式,进入正常工作状态。
4、中断,唤醒后会进入中断EXTI9_5_IRQHandler()函数,对ADC转换在一次进行配置;串口接收中断void USART2_IRQHandler(void)数据处理,串口接收数据,并通过I2C给Sensor进行配置,从而实现在线调试Camera Sensor,就不需在代码中改参数进行编译再下载。
5、代码实现:
1)、USART
.h
#ifndef __USART_H
#define __USART_H #include "stm32f2xx_gpio.h"
#include <stdio.h> void USART_Config(void); #endif
.c
#include "USART.h"
#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 USART_Config(void)
{
GPIO_InitTypeDef GPIO_InitSturct;
USART_InitTypeDef USART_InitSturct; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA,ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE); GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_USART2);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_USART2); //TX
GPIO_InitSturct.GPIO_Pin = GPIO_Pin_2;
GPIO_InitSturct.GPIO_OType = GPIO_OType_PP;//推挽输出
GPIO_InitSturct.GPIO_Mode = GPIO_Mode_AF; //复用模式
GPIO_InitSturct.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitSturct.GPIO_PuPd = GPIO_PuPd_UP;//上拉 GPIO_Init(GPIOA,&GPIO_InitSturct); //RX
GPIO_InitSturct.GPIO_Pin = GPIO_Pin_3;
GPIO_InitSturct.GPIO_Mode = GPIO_Mode_AF;//复用模式
GPIO_InitSturct.GPIO_OType = GPIO_OType_OD;
GPIO_InitSturct.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOA,&GPIO_InitSturct); USART_InitSturct.USART_BaudRate = ;//波特率设置
USART_InitSturct.USART_WordLength = USART_WordLength_8b;//发送的字节长度为8位
USART_InitSturct.USART_StopBits = USART_StopBits_1;//停止位 1位
USART_InitSturct.USART_Parity = USART_Parity_No; //无奇偶位
USART_InitSturct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; //选择不是硬件控制流
USART_InitSturct.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; //发送接收使能 USART_Init(USART2,&USART_InitSturct);//进行初始化
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);//开启接收中断
USART_Cmd(USART2,ENABLE);//启动串口
} /**
* @brief Retargets the C library printf function to the USART1.
* @param None
* @retval None
*/
PUTCHAR_PROTOTYPE
{
/* Place your implementation of fputc here */
/* e.g. write a character to the USART */
USART_SendData(USART2, (uint8_t) ch); /* Loop until the end of transmission */
while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET)
{} return ch;
}
2)、I2C
.h
#ifndef __I2C_H
#define __I2C_H #include "stm32f2xx_i2c.h"
#include "stm32f2xx_gpio.h" typedef unsigned char uint8_t; #define TRUE 1
#define FALSE 0 void GPIO_I2C_Config(void);
static void I2C_delay(uint16_t cnt);
uint8_t I2C_Start(void);
void I2C_Stop(void);
void I2C_Ack(void);
void I2C_NoAck(void);
uint8_t I2C_WaitAck(void) ;
void I2C_SendByte(uint8_t SendByte);
uint8_t I2C_ReceiveByte(void);
uint8_t Write_One_Byte(uint8_t addr, uint16_t thedata, uint8_t bit_width);
uint16_t Read_One_Byte(uint8_t addr); #endif
.c
#include "I2C.h" //DELAY_TIME = 50 5us
//DELAY_TIME = 100 10us
#define T_Cycle 200
#define T_1QuarterCycle T_Cycle/4 //t = 1/4T, scl T = 10us f=100k
#define T_HalfCycle T_Cycle/2 #define SLAVE_ADDRESS_WRITE 0x90 //0x42
#define SLAVE_ADDRESS_READ SLAVE_ADDRESS_WRITE | 0x01
#define SDA GPIO_Pin_11
#define SCL GPIO_Pin_10
#define GPIO_SCL GPIOB
#define GPIO_SDA GPIOB
#define RCC_AHB1Periph_GPIO_SDA RCC_AHB1Periph_GPIOB
#define RCC_AHB1Periph_GPIO_SCL RCC_AHB1Periph_GPIOB #define SCL_H GPIOB->BSRRL = GPIO_Pin_10
#define SCL_L GPIOB->BSRRH = GPIO_Pin_10 #define SDA_H GPIOB->BSRRL = GPIO_Pin_11
#define SDA_L GPIOB->BSRRH = GPIO_Pin_11 #define SCL_read GPIOB->IDR & GPIO_Pin_10
#define SDA_read GPIOB->IDR & GPIO_Pin_11 void GPIO_I2C_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIO_SDA,ENABLE);
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIO_SCL,ENABLE); GPIO_InitStructure.GPIO_Pin = SDA; //sda
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; //输出模式
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; //OD门
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; //浮空
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIO_SDA,&GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = SCL; //scl
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; //输出模式
GPIO_InitStructure.GPIO_OType = GPIO_OType_OD; //OD门
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; //浮空
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIO_SCL,&GPIO_InitStructure);
} void I2C_delay(uint16_t cnt)
{
while(cnt!=)
cnt--;
} uint8_t I2C_Start(void)
{
SDA_H;
SCL_H;
I2C_delay(T_1QuarterCycle);
if(!SDA_read)return FALSE;
SDA_L;
I2C_delay(T_1QuarterCycle);
if(SDA_read) return FALSE;
SDA_L;
I2C_delay(T_1QuarterCycle); return TRUE;
} void I2C_Stop(void)
{
SCL_L;
I2C_delay(T_1QuarterCycle);
SDA_L;
I2C_delay(T_1QuarterCycle);
SCL_H;
I2C_delay(T_1QuarterCycle);
SDA_H;
I2C_delay(T_1QuarterCycle);
} void I2C_Ack(void)
{
SCL_L;
I2C_delay(T_1QuarterCycle);
SDA_L;
I2C_delay(T_1QuarterCycle);
SCL_H;
I2C_delay(T_1QuarterCycle);
SCL_L;
I2C_delay(T_1QuarterCycle);
} void I2C_NoAck(void)
{
SCL_L;
I2C_delay(T_1QuarterCycle);
SDA_H;
I2C_delay(T_1QuarterCycle);
SCL_H;
I2C_delay(T_1QuarterCycle);
SCL_L;
I2C_delay(T_1QuarterCycle);
} uint8_t I2C_WaitAck(void)
{
SCL_L;
I2C_delay(T_1QuarterCycle);
SDA_H; //SDA要拉高,以便读
I2C_delay(T_1QuarterCycle);
SCL_H;
I2C_delay(T_1QuarterCycle);
if(SDA_read)
{
SCL_L;
return FALSE;
}
I2C_delay(T_1QuarterCycle);
SCL_L; //这个地方不能去掉 return TRUE;
} void I2C_SendByte(uint8_t SendByte)
{
uint8_t i=;
while(i--)
{
SCL_L;
I2C_delay(T_1QuarterCycle);
if(SendByte&0x80)//在SCL为低时,改变SDA上的数据
SDA_H;
else
SDA_L;
SendByte<<=;
I2C_delay(T_1QuarterCycle);
SCL_H;
I2C_delay(T_HalfCycle);
} } uint8_t I2C_ReceiveByte(void)
{
uint8_t i=;
uint8_t ReceiveByte=; SDA_H; //上拉以便读数据
while(i--)
{
ReceiveByte<<=;
SCL_L;
I2C_delay(T_1QuarterCycle);
SCL_H;
I2C_delay(T_1QuarterCycle);
if(SDA_read)
{
ReceiveByte|=0x01;
}
I2C_delay(T_1QuarterCycle);
} return ReceiveByte;
} uint8_t Write_One_Byte(uint8_t addr, uint16_t thedata, uint8_t bit_width)
{
uint8_t data_8bit_H = 0x00;
uint8_t data_8bit_L = 0x00;
if(bit_width == ) //把数据进行分开,位宽都是8
{
data_8bit_L = thedata;
}
else if(bit_width == )
{
data_8bit_H = (thedata & 0xff00) >>;
data_8bit_L = thedata & 0x00ff;
}
else
return FALSE; if (!I2C_Start()) return FALSE;
I2C_SendByte(SLAVE_ADDRESS_WRITE);
if (!I2C_WaitAck())
{
I2C_Stop();
return FALSE;
}
I2C_SendByte(addr);
I2C_WaitAck(); if(bit_width == )
{
I2C_SendByte(data_8bit_L);
I2C_WaitAck();
}
else
{
I2C_SendByte(data_8bit_H);
I2C_WaitAck();
I2C_SendByte(data_8bit_L);
I2C_WaitAck();
} I2C_Stop(); return TRUE;
} uint16_t Read_One_Byte(uint8_t addr)
{
uint16_t thedata = 0x0000;
if (!I2C_Start()) return FALSE;
I2C_SendByte(SLAVE_ADDRESS_WRITE);
if (!I2C_WaitAck())
{
I2C_Stop();
return FALSE;
}
I2C_SendByte(addr);
I2C_WaitAck();
I2C_Start();
I2C_SendByte(SLAVE_ADDRESS_READ);
I2C_WaitAck(); thedata = I2C_ReceiveByte();
thedata = thedata<<;
I2C_Ack();
thedata |= I2C_ReceiveByte();
I2C_NoAck();
I2C_Stop(); return thedata;
}
3)、DMA
.h
#ifndef __DMA_H
#define __DMA_H #include "stm32f2xx_dma.h" void DMA_Config(void); #endif
.c
#include "DMA.h" #define N 2
extern vu16 ADC_ConvertedValue[N]; //#define PERIPH_BASE ((uint32_t)0x40000000)
//#define APB2PERIPH_BASE (PERIPH_BASE + 0x00010000)
//#define ADC1_BASE (APB2PERIPH_BASE + 0x2000)
//#define ADC1_DR_Address (ADC1_BASE+0x4c) #define ADC1_DR_Address ((u32)0x4001204C) void DMA_Config(void)
{
DMA_InitTypeDef DMA_InitStructure; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2,ENABLE);//使能DMA2时钟,ADC1是挂在DMA2上 DMA_DeInit(DMA2_Stream0);
DMA_InitStructure.DMA_Channel = DMA_Channel_0; //指定数据通 这里不能随便改成 DMA_Channel_1/2,得看手册DMA2 request mapping
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC1_DR_Address; //指定外设接口地址
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&ADC_ConvertedValue; //指定寄存器变量,转换后的数据就存放在这里
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;//指定外设到memory
DMA_InitStructure.DMA_BufferSize = ; //指定Buff大小
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;//指定外设增量,外设地址不变
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;//指定memory增量,递增
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
//ADC分辨率设置的是12bit,若这改为DMA_PeripheralDataSize_Byte就出错,会丢失一些数据
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;//循环传输
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;//是直接模式还是FIFO模式
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single; DMA_Init(DMA2_Stream0, &DMA_InitStructure);//DMA2初始化
//DMA_ITConfig(DMA2_Stream0, DMA_IT_TC, ENABLE);//开DMA中断
DMA_Cmd(DMA2_Stream0, ENABLE);//DMA2使能 }
4)、ADC
.h
#ifndef __ADC_H
#define __ADC_H #include "stm32f2xx_adc.h"
#include "stm32f2xx_gpio.h" void ADC_Config(void); #endif
.c
#include "adc.h" void ADC_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
ADC_InitTypeDef ADC_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure; RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);//使能GPIO接口时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);//使能ADC复用接口 /* Configure ADC1 Channel PA6 adn PA7 pin as analog input ******************************/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
//GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;//输入时不需设置速率
GPIO_Init(GPIOA, &GPIO_InitStructure); /* ADC Common Init **********************************************************/
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div4;//60M/4=15M max value is 30MHz
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
ADC_CommonInit(&ADC_CommonInitStructure); /* ADC1 Init ****************************************************************/
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b; //12bit
ADC_InitStructure.ADC_ScanConvMode = ENABLE;//DISABLE; //多通道扫描模式
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; //连续转换
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None; //软件触发
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; //右对齐
ADC_InitStructure.ADC_NbrOfConversion = ;//2个转换通道
ADC_Init(ADC1, &ADC_InitStructure); /* ADC1 regular channe6 configuration *************************************/
ADC_RegularChannelConfig(ADC1, ADC_Channel_6, , ADC_SampleTime_84Cycles);
ADC_RegularChannelConfig(ADC1, ADC_Channel_7, , ADC_SampleTime_84Cycles); /* Enable DMA request after last transfer (Single-ADC mode) */
ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE); // ADC_MultiModeDMARequestAfterLastTransferCmd(ENABLE); /* Enable ADC1 DMA */
ADC_DMACmd(ADC1, ENABLE); /* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE); }
5)、Main
.h
/**
******************************************************************************
* @file STM32F2xx_IAP/binary_template/inc/main.h
* @author MCD Application Team
* @version V1.0.0
* @date 02-May-2011
* @brief Header for main.c module
******************************************************************************
* @attention
*
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
* TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
* <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2>
******************************************************************************
*/ /* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __MAIN_H
#define __MAIN_H /* Includes ------------------------------------------------------------------*/
#include "stm32f2xx.h"
#include "USART.h"
#include "DMA.h"
#include "adc.h"
#include "stdint.h"
#include "i2c.h" void Delay(__IO uint32_t nCount);
void Clock_Config(void);
int CheckDatas(void);
void StopModeConfig(void);
void NVIC_Config(void);
void EXTI_Config(void);
void Display(void);
void USART_NVIC_Config(void); /* Exported types ------------------------------------------------------------*/
/* Exported constants --------------------------------------------------------*/
/* Exported macro ------------------------------------------------------------*/
/* Exported functions ------------------------------------------------------- */ #endif /* __MAIN_H */ /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
.c
/**
******************************************************************************
* @file STM32F2xx_IAP/binary_template/src/main.c
* @author MCD Application Team
* @version V1.0.0
* @date 02-May-2011
* @brief Main program body.
******************************************************************************
* @attention
*
* THE PRESENT FIRMWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
* WITH CODING INFORMATION REGARDING THEIR PRODUCTS IN ORDER FOR THEM TO SAVE
* TIME. AS A RESULT, STMICROELECTRONICS SHALL NOT BE HELD LIABLE FOR ANY
* DIRECT, INDIRECT OR CONSEQUENTIAL DAMAGES WITH RESPECT TO ANY CLAIMS ARISING
* FROM THE CONTENT OF SUCH FIRMWARE AND/OR THE USE MADE BY CUSTOMERS OF THE
* CODING INFORMATION CONTAINED HEREIN IN CONNECTION WITH THEIR PRODUCTS.
*
* <h2><center>© COPYRIGHT 2011 STMicroelectronics</center></h2>
******************************************************************************
*/ /* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "stm32f2xx_rcc.h"
#include "stm32f2xx_pwr.h"
#include "system_stm32f2xx.h"
#include "SensorReg.h"
#include <string.h>
#include <stdio.h> #define N 2
#define SENSOR_WEAK 0.6
#define SENSOR_MEDIUM 1
#define SENSOR_HEAVY 2
#define SENSOR_VERY_HEAVY 2.5
#define WEAK_COUNT 10 #define TURE 1
#define FALSE 0
#define BIT_8 8
#define BIT_16 16
//VCO = PLL input clock(HSE or HSI)/PLLM
//倍频电压后值V = VCO * PLLN
//sysclk = V / PLLP
//PLL48CK = V / PLLQ
#define PLLM 25 //分频因子
#define PLLN 240 //倍频因子
#define PLLP 2 //分频因子
#define PLLQ 5 //分频因子 vu16 ADC_ConvertedValue[N]; //定义数组,存放PA6和PA7的转换后的数据
float PA6_AD_value; //压力传感器
float PA7_AD_value; //锂电池电压
unsigned int SensorCnt = ; //uint8_t ReadBuffer[23];
/* Private functions ---------------------------------------------------------*/ /**
* @brief Main program.
* @param None
* @retval None
*/
int main(void)
{
uint16_t i = ;
uint16_t thedata = 0x0000;
uint8_t USART_Buffer = 0x00;
RCC_ClocksTypeDef rcc_clocks; Clock_Config();
USART_Config();
USART_NVIC_Config();
GPIO_I2C_Config();
DMA_Config();
ADC_Config();
ADC_SoftwareStartConv(ADC1);
RCC_GetClocksFreq(&rcc_clocks); printf("\r\nSYSCLK_Frequency = %d MHz\n",rcc_clocks.SYSCLK_Frequency/);
printf("\r\nHCLK_Frequency = %d MHz\n",rcc_clocks.HCLK_Frequency/);
printf("\r\nPCLK1_Frequency = %d MHz\n",rcc_clocks.PCLK1_Frequency/);
printf("\r\nPCLK2_Frequency = %d MHz\n",rcc_clocks.PCLK2_Frequency/); //MT9V024 I2C write
for(i=; i<sizeof(Sensor_Init_Aptina)/; i+=)
{
Write_One_Byte(Sensor_Init_Aptina[i],Sensor_Init_Aptina[i+], BIT_16);
Delay();
} //SP0A19 I2C write
// for(i=0; i<sizeof(Sensor_Init_Superpix)/2; i+=2)
// {
// Write_One_Byte(Sensor_Init_Superpix[i],Sensor_Init_Superpix[i+1], BIT_8);
// Delay(1000);
// } //I2C Read
for(i=; i<sizeof(Sensor_Init_Aptina)/; i+=)
{
thedata = Read_One_Byte(Sensor_Init_Aptina[i]);
printf("0x%02X = 0x%04X\n",Sensor_Init_Aptina[i], thedata);
Delay(0x51eb8);//10ms //0x333331);//100ms
} printf("\r\nAdc Test Start...\r\n");
while ()
{
Display();
Delay(0x1fffff0); /* delay 1000ms */
} } void Display(void)
{
PA6_AD_value = ADC_ConvertedValue[];
PA6_AD_value = (PA6_AD_value/)*3.3; //由于设置的分辨率是12bit,2^12=4096 PA7_AD_value = ADC_ConvertedValue[];
PA7_AD_value = (PA7_AD_value/)*3.3; //由于设置的分辨率是12bit,2^12=4096
printf("Battery /2 value = %4.4fV \r\n",PA7_AD_value); if(PA6_AD_value >= SENSOR_VERY_HEAVY)
{
SensorCnt = ;
printf("Sensor very heavy = %4.4fV \r\n",PA6_AD_value);
}
else if(PA6_AD_value >= SENSOR_HEAVY)
{
SensorCnt = ;
printf("Sensor heavy = %4.4fV \r\n",PA6_AD_value);
}
else if(PA6_AD_value >= SENSOR_MEDIUM)
{
SensorCnt = ;
printf("Sensor medium = %4.4fV \r\n",PA6_AD_value);
}
else if(PA6_AD_value >= SENSOR_WEAK)
{
SensorCnt = ;
printf("Sensor weak = %4.4fV \r\n",PA6_AD_value);
}
else
{
printf("Sensor 0 = %4.4fV \r\n",PA6_AD_value);
SensorCnt++; if(SensorCnt == WEAK_COUNT) //长时间没按下,进入停止模式
{
NVIC_Config();
EXTI_Config();
printf("Come into Stop Mode!\n");
// StopModeConfig();
PWR_EnterSTOPMode(PWR_Regulator_LowPower,PWR_STOPEntry_WFI); //停止模式
}
}
} void Clock_Config(void)
{
RCC_DeInit();//复位系统所有配置时钟
// RCC_HSICmd(DISABLE); //RCC_DeInit()后,HSI作为系统时钟,在这调用是不生效的
RCC_LSICmd(DISABLE);//关闭内部低速时钟
RCC_PLLI2SCmd(DISABLE);//关闭PLLI2S Clock
RCC_RTCCLKCmd(DISABLE);//disables the RTC clock RCC_HSEConfig(RCC_HSE_ON);//外部时钟使能
while(!RCC_WaitForHSEStartUp());//Waits for HSE start-up RCC_PLLConfig(RCC_PLLSource_HSE, PLLM, PLLN, PLLP, PLLQ);//配置PLL时钟
RCC_PLLCmd(ENABLE); //PLL时钟使能
while((RCC->CR & RCC_CR_PLLRDY) == );//等待PLL 时钟准备好 RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);//PLL CLK 作为系统时钟 sysclk =120M
RCC_HSICmd(DISABLE);//关闭内部高速时钟,如果内部高速时钟作为系统时钟,此函数不生效
RCC_HCLKConfig(RCC_SYSCLK_Div1); //AHB = SYSCLK 不分频 120M
RCC_PCLK1Config(RCC_HCLK_Div4); //PCLK1 4分频 = 30M
RCC_PCLK2Config(RCC_HCLK_Div2); //PCLK2 2分频 = 60M // RCC_PLLCmd(DISABLE); //关掉PLL
// RCC_LSEConfig(RCC_LSE_OFF);//关掉外部低速
// RCC_HSEConfig(RCC_HSE_OFF);//关掉外部高速
// RCC_HSICmd(ENABLE);//使能内部
// RCC_SYSCLKConfig(RCC_SYSCLKSource_HSI);
} void USART_NVIC_Config()
{
NVIC_InitTypeDef NVIC_InitStructure; #ifdef VECT_TAB_RAM
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0); //串口接收中断初始化
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = ;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = ;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure); }
void NVIC_Config(void)
{
NVIC_InitTypeDef NVIC_InitStructure; #ifdef VECT_TAB_RAM
/* Set the Vector Table base location at 0x20000000 */
NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else /* VECT_TAB_FLASH */
/* Set the Vector Table base location at 0x08000000 */
NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);
#endif NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = ;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = ;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure); //DMA中断初始化
//NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream0_IRQn;
//NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
//NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
//NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
//NVIC_Init(&NVIC_InitStructure); } void EXTI_Config(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
EXTI_InitTypeDef EXTI_InitStructure; while(!ADC_GetFlagStatus(ADC1,ADC_FLAG_EOC)); //等待ADC转换结束
while(!DMA_GetFlagStatus(DMA2_Stream0,DMA_FLAG_TCIF0));//等待DMA传输结束
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, DISABLE);//ADC复用接口 关闭
ADC_DMARequestAfterLastTransferCmd(ADC1, DISABLE);
ADC_DMACmd(ADC1, DISABLE);
ADC_Cmd(ADC1, DISABLE);
DMA_Cmd(DMA2_Stream0, DISABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);//使能PA6复用中断接口打开
//PA6作为触发接口
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA,&GPIO_InitStructure); /*PA6外部中断输入*/
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA,EXTI_PinSource6);
EXTI_InitStructure.EXTI_Line = EXTI_Line6;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
} void StopModeConfig(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
// PWR_EnterSTANDBYMode();
// __WFI();
RCC_AHB1PeriphClockLPModeCmd(RCC_AHB1Periph_GPIOA|RCC_AHB1Periph_GPIOB\
|RCC_AHB1Periph_GPIOC|RCC_AHB1Periph_GPIOD|RCC_AHB1Periph_GPIOH,ENABLE); RCC_APB1PeriphResetCmd(RCC_APB1Periph_PWR, ENABLE);
//GPIO -A
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2|GPIO_Pin_3\
|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10\
|GPIO_Pin_11|GPIO_Pin_12|GPIO_Pin_13;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_Init(GPIOA,&GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA,&GPIO_InitStructure);
//GPIO -B
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2\
|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_10\
|GPIO_Pin_11|GPIO_Pin_12|GPIO_Pin_13|GPIO_Pin_14|GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_Init(GPIOB,&GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3|GPIO_Pin_4;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOB,&GPIO_InitStructure); //GPIO -C
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_Init(GPIOC,&GPIO_InitStructure); //GPIO -D
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_Init(GPIOD,&GPIO_InitStructure); //GPIO-H //外部高速时钟PH0/1配置
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_DOWN;
GPIO_Init(GPIOH,&GPIO_InitStructure); PWR_PVDCmd(DISABLE);
PWR_BackupRegulatorCmd(DISABLE);
PWR_BackupAccessCmd(DISABLE);
PWR_FlashPowerDownCmd(DISABLE);
PWR_EnterSTOPMode(PWR_Regulator_LowPower,PWR_STOPEntry_WFI); //停止模式
} int CheckDatas(void)
{
if(ADC_GetFlagStatus(ADC1,ADC_FLAG_OVR) != RESET) //判断数据是否丢失
{
return FALSE;
}
else
return TURE;
}
/**
* @brief Inserts a delay time.
* @param nTime: specifies the delay time length, in milliseconds.
* @retval None
*/
void Delay(__IO uint32_t nCount)
{
while(nCount--)
{ }
} #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 ()
{
}
}
#endif /**
* @}
*/ /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/
6、中断
#include "stm32f2xx_it.h"
#include "main.h" /** @addtogroup STM32F2xx_IAP_Binary_Template
* @{
*/ /* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
/* Private functions ---------------------------------------------------------*/ /******************************************************************************/
/* Cortex-M3 Processor Exceptions Handlers */
/******************************************************************************/
extern unsigned int SensorCnt;
u8 rx_temp_buff[];
u8 i = ;
void EXTI9_5_IRQHandler(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
SensorCnt = ;
NVIC_InitStructure.NVIC_IRQChannel = EXTI9_5_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = ;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = ;
NVIC_InitStructure.NVIC_IRQChannelCmd = DISABLE;
NVIC_Init(&NVIC_InitStructure); //关掉中断 EXTI_ClearITPendingBit(EXTI_Line6);
Clock_Config();
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, DISABLE); //关闭中断复用
DMA_ClearFlag(DMA2_Stream0,DMA_FLAG_TCIF0);
ADC_ClearFlag(ADC1,ADC_FLAG_EOC);
DMA_Config();
ADC_Config();
ADC_SoftwareStartConv(ADC1);
printf("EXIT Stop Mode!\n");
} void DMA2_Stream0_IRQHandler()
{
NVIC_InitTypeDef NVIC_InitStructure;
DMA_ClearITPendingBit(DMA2_Stream0,DMA_IT_TCIF0);
NVIC_InitStructure.NVIC_IRQChannel = DMA2_Stream0_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = ;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = ;
NVIC_InitStructure.NVIC_IRQChannelCmd = DISABLE;
NVIC_Init(&NVIC_InitStructure); // DMA_ClearFlag(DMA2_Stream0,DMA_FLAG_TCIF0);
printf("Stream0 transfer complete!\n");
} void Data_Manage(u32 data_buff)
{
uint8_t address_8bit = 0x00;
uint16_t data_16bit = 0x00;
uint16_t thedata = 0x0000; address_8bit = (data_buff & 0xff0000) >> ;
data_16bit = data_buff;
Write_One_Byte(address_8bit,data_16bit, );
Delay();
thedata = Read_One_Byte(address_8bit);
printf("0x%02X = 0x%04X\n",address_8bit, thedata);
} void USART2_IRQHandler(void)
{
static u32 vlaue;
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
{
// USART_ClearITPendingBit(USART2,USART_IT_RXNE);
rx_temp_buff[i] = USART_ReceiveData(USART2);
i++;
if(i == ) //由于串口每次发送8bit数据,发送格式例0x04==0x02f0,
{ //为什么两个等号,就是为了凑8位,然后把真正的数据保留下来,0x 和==都不需要,
i = ; //所以只对rx_temp_buff[1]、rx_temp_buff[4]、rx_temp_buff[5]处理
vlaue = (rx_temp_buff[]<<) + (rx_temp_buff[] <<) + rx_temp_buff[];
Data_Manage(vlaue);
}
}
}
7、Sensor参数
#ifndef __SENSOR_REG_H
#define __SENSOR_REG_H typedef unsigned char uint8_t;
typedef unsigned short int uint16_t; const uint16_t Sensor_Init_Aptina[] =
{
0x0c, 0x0001,
0x0c, 0x0000, #if 0 //176*176
0x01, 0x0011, // COL_WINDOW_START_CONTEXTA_REG
0x02, 0x0014, // ROW_WINDOW_START_CONTEXTA_REG
0x03, 0x00b0, // ROW_WINDOW_SIZE_CONTEXTA_REG
0x04, 0x00b0, // COL_WINDOW_SIZE_CONTEXTA_REG
0x05, 0x02a9, //HORZ_BLANK_CONTEXTA_REG
#endif #if 1 //752*480
0x01, 0x0001,
0x02, 0x0004,
0x03, 0x01E0,
0x04, 0x02F0,
0x05, 0x005E,
#endif 0x06, 0x0039,
0x07, 0x0188,
0x08, 0x0190,
0x09, 0x01BD,
0x0A, 0x0164,
0x0B, 0x01C2,
0x0C, 0x0000,
0x0D, 0x0300,
0x0E, 0x0000,
0x0F, 0x0100,
0x10, 0x0040,
0x11, 0x8042,
0x12, 0x0022,
0x13, 0x2D2E,
0x14, 0x0E02,
0x15, 0x0E32,
0x16, 0x2802,
0x17, 0x3E38,
0x18, 0x3E38,
0x19, 0x2802,
0x1A, 0x0428,
0x1B, 0x0000,
0x1C, 0x0302,
0x1D, 0x0040,
0x1E, 0x0000,
0x1F, 0x0000,
0x20, 0x03C7,
0x21, 0x0020,
0x22, 0x0020,
0x23, 0x0010,
0x24, 0x001B,
0x25, 0x001A,
0x26, 0x0004,
0x27, 0x000C,
0x28, 0x0010,
0x29, 0x0010,
0x2A, 0x0020,
0x2B, 0x0003,
0x2C, 0x0004,
0x2D, 0x0004,
0x2E, 0x0007,
0x2F, 0x0003,
0x30, 0x0003,
0x31, 0x001F,
0x32, 0x001A,
0x33, 0x0012,
0x34, 0x0003,
0x35, 0x0020,
0x36, 0x0010,
0x37, 0x0000,
0x38, 0x0000,
0x39, 0x0025,
0x3A, 0x0020,
0x3B, 0x0003,
0x3C, 0x0003,
0x46, 0x231D,
0x47, 0x0080,
0x4C, 0x0002,
0x70, 0x0000,
0x71, 0x002A,
0x72, 0x0000,
0x7F, 0x0000,
0x80, 0x04F4,
0x81, 0x04F4,
0x82, 0x04F4,
0x83, 0x04F4,
0x84, 0x04F4,
0x85, 0x04F4,
0x86, 0x04F4,
0x87, 0x04F4,
0x88, 0x04F4,
0x89, 0x04F4,
0x8A, 0x04F4,
0x8B, 0x04F4,
0x8C, 0x04F4,
0x8D, 0x04F4,
0x8E, 0x04F4,
0x8F, 0x04F4,
0x90, 0x04F4,
0x91, 0x04F4,
0x92, 0x04F4,
0x93, 0x04F4,
0x94, 0x04F4,
0x95, 0x04F4,
0x96, 0x04F4,
0x97, 0x04F4,
0x98, 0x04F4,
0x99, 0x0000,
0x9A, 0x0096,
0x9B, 0x012C,
0x9C, 0x01C2,
0x9D, 0x0258,
0x9E, 0x02F0,
0x9F, 0x0000,
0xA0, 0x0060,
0xA1, 0x00C0,
0xA2, 0x0120,
0xA3, 0x0180,
0xA4, 0x01E0,
0xA5, 0x003A,
0xA6, 0x0002,
0xA8, 0x0000,
0xA9, 0x0002,
0xAA, 0x0002,
0xAB, 0x0040,
0xAC, 0x0001,
0xAD, 0x01E0,
0xAE, 0x0014,
0xAF, 0x0000,
0xB0, 0xABE0,
0xB1, 0x0002,
0xB2, 0x0010,
0xB3, 0x0010,
0xB4, 0x0000,
0xB5, 0x0000,
0xB6, 0x0000,
0xB7, 0x0000,
0xBF, 0x0016,
0xC0, 0x000A,
0xC2, 0x18D0,
0xC3, 0x007F,
0xC4, 0x007F,
0xC5, 0x007F,
0xC6, 0x0000,
0xC7, 0x4416,
0xC8, 0x4421,
0xC9, 0x0002,
0xCA, 0x0004,
0xCB, 0x01E0,
0xCC, 0x02EE,
0xCD, 0x0100,
0xCE, 0x0100,
0xCF, 0x0190,
0xD0, 0x01BD,
0xD1, 0x0064,
0xD2, 0x01C2,
0xD3, 0x0000,
0xD4, 0x0000,
0xD5, 0x0000,
0xD6, 0x0000,
0xD7, 0x0000,
0xD8, 0x0000,
0xD9, 0x0000, }; const uint16_t Sensor_Init_Superpix[] =
{
0x1C , 0x28,
0x32 , 0x00,
0x0f , 0x2f,
0x10 , 0x2e,
0x11 , 0x00,
0x12 , 0x18,
0x13 , 0x2f,
0x14 , 0x00,
0x15 , 0x3f,
0x16 , 0x00,
0x17 , 0x18,
0x25 , 0x40,
0x1a , 0x0b,
0x1b , 0x0c,
0x1e , 0x0b,
0x20 , 0x3f,
0x21 , 0x13,
0x22 , 0x19,
0x26 , 0x1a,
0x27 , 0xab,
0x28 , 0xfd,
0x30 , 0x00,
0x31 , 0x10,
0x03 , 0x01,
0x04 , 0x80,
0xfb , 0x31,
0x1f , 0x08,
0xfd , 0x00,
0x65 , 0x00,
0x66 , 0x00,
0x67 , 0x00,
0x68 , 0x00,
0xfd , 0x00,
0x09 , 0x02,
0x0a , 0xa9,
0xf0 , 0x62,
0xf1 , 0x00,
0xfd , 0x01,
0x90 , 0x0c,
0x92 , 0x01,
0x98 , 0x62,
0x99 , 0x00,
0x9a , 0x01,
0x9b , 0x00,
0xfd , 0x01,
0xce , 0x98,
0xcf , 0x04,
0xd0 , 0x98,
0xd1 , 0x04,
0xfd , 0x01,
0xc4 , 0x8f,
0xc5 , 0x80,
0xca , 0x40,
0xcb , 0x50,
0xcc , 0xb8,
0xcd , 0x70,
0xfd , 0x00,
0xfd , 0x01,
0x35 , 0x15,
0x36 , 0x15,
0x37 , 0x15,
0x38 , 0x15,
0x39 , 0x15,
0x3a , 0x15,
0x3b , 0x13,
0x3c , 0x15,
0x3d , 0x15,
0x3e , 0x15,
0x3f , 0x15,
0x40 , 0x18,
0x41 , 0x00,
0x42 , 0x04,
0x43 , 0x04,
0x44 , 0x00,
0x45 , 0x00,
0x46 , 0x00,
0x47 , 0x00,
0x48 , 0x00,
0x49 , 0xfd,
0x4a , 0x00,
0x4b , 0x00,
0x4c , 0xfd,
0xfd , 0x00,
0xfd , 0x01,
0x28 , 0xc5,
0x29 , 0x9b,
0x2e , 0x02,
0x2f , 0x16,
0x17 , 0x17,
0x18 , 0x19,
0x19 , 0x45,
0x2a , 0xef,
0x2b , 0x15,
0xfd , 0x01,
0x73 , 0x80,
0x1a , 0x80,
0x1b , 0x80,
0x65 , 0xd5,
0x66 , 0xfa,
0x67 , 0x72,
0x68 , 0x8a,
0x69 , 0xc6,
0x6a , 0xee,
0x6b , 0x94,
0x6c , 0xab,
0x61 , 0x7a,
0x62 , 0x98,
0x63 , 0xc5,
0x64 , 0xe6,
0x6d , 0xb9,
0x6e , 0xde,
0x6f , 0xb2,
0x70 , 0xd5,
0xfd , 0x01,
0x08 , 0x15,
0x09 , 0x04,
0x0a , 0x20,
0x0b , 0x12,
0x0c , 0x27,
0x0d , 0x06,
0x0f , 0x63,
0xfd , 0x00,
0x79 , 0xf0,
0x7a , 0xf0,
0x7b , 0xf0,
0x7c , 0xf0,
0xfd , 0x00,
0x57 , 0x06,
0x58 , 0x0d,
0x56 , 0x10,
0x59 , 0x10,
0x89 , 0x06,
0x8a , 0x0d,
0x9c , 0x10,
0x9d , 0x10,
0x81 , 0xe0,
0x82 , 0xe0,
0x83 , 0x80,
0x84 , 0x40,
0x85 , 0xe0,
0x86 , 0xc0,
0x87 , 0x80,
0x88 , 0x40,
0x5a , 0xff,
0x5b , 0xe0,
0x5c , 0x80,
0x5d , 0x00,
0xfd , 0x01,
0xe2 , 0x30,
0xe4 , 0xa0,
0xe5 , 0x04,
0xd3 , 0x04,
0xd7 , 0x04,
0xe6 , 0x04,
0xd4 , 0x04,
0xd8 , 0x04,
0xe7 , 0x08,
0xd5 , 0x08,
0xd9 , 0x08,
0xd2 , 0x10,
0xd6 , 0x10,
0xda , 0x10,
0xe8 , 0x20,
0xec , 0x35,
0xe9 , 0x20,
0xed , 0x35,
0xea , 0x20,
0xef , 0x30,
0xeb , 0x10,
0xf0 , 0x20,
0xfd , 0x01,
0xa0 , 0x80,
0xa1 , 0x00,
0xa2 , 0x00,
0xa3 , 0xf3,
0xa4 , 0x8e,
0xa5 , 0x00,
0xa6 , 0x00,
0xa7 , 0xe6,
0xa8 , 0x9a,
0xa9 , 0x00,
0xaa , 0x03,
0xab , 0x0c,
0xfd , 0x00,
0xfd , 0x00,
0x8b , 0x00,
0x8c , 0x0C,
0x8d , 0x19,
0x8e , 0x2C,
0x8f , 0x49,
0x90 , 0x61,
0x91 , 0x77,
0x92 , 0x8A,
0x93 , 0x9B,
0x94 , 0xA9,
0x95 , 0xB5,
0x96 , 0xC0,
0x97 , 0xCA,
0x98 , 0xD4,
0x99 , 0xDD,
0x9a , 0xE6,
0x9b , 0xEF,
0xfd , 0x01,
0x8d , 0xF7,
0x8e , 0xFF,
0xfd , 0x00,
0xe0 , 0x4c,
0xe1 , 0x3c,
0xe2 , 0x34,
0xe3 , 0x2e,
0xe4 , 0x2e,
0xe5 , 0x2c,
0xe6 , 0x2c,
0xe8 , 0x2a,
0xe9 , 0x2a,
0xea , 0x2a,
0xeb , 0x28,
0xf5 , 0x28,
0xf6 , 0x28,
0xfd , 0x01,
0x94 , 0xc0,
0x95 , 0x28,
0x9c , 0xc0,
0x9d , 0x28,
0xfd , 0x00,
0xed , 0x8c,
0xf7 , 0x88,
0xf8 , 0x80,
0xec , 0x7c,
0xef , 0x74,
0xf9 , 0x70,
0xfa , 0x68,
0xee , 0x64,
0xfd , 0x01,
0x30 , 0x40,
0x31 , 0x70,
0x32 , 0x40,
0x33 , 0xef,
0x34 , 0x05,
0x4d , 0x2f,
0x4e , 0x20,
0x4f , 0x16,
0xfd , 0x00,
0xb2 , 0x20,
0xb3 , 0x1f,
0xb4 , 0x30,
0xb5 , 0x45,
0xfd , 0x00,
0xbe , 0xff,
0xbf , 0x01,
0xc0 , 0xff,
0xc1 , 0xd8,
0xd3 , 0x88,
0xd4 , 0x88,
0xd6 , 0x80,
0xd7 , 0x60,
0xfd , 0x00,
0xdc , 0x00,
0xdd , 0x80,
0xde , 0xa8,
0xdf , 0x80,
0xfd , 0x00,
0x32 , 0x15,
0x34 , 0x16,
0x33 , 0xef,
0x5f , 0x51
}; #endif
8、总结:
(1)、USART配置时,RX和TX脚都设置为复用模式GPIO_Mode_AF,TX设置为推挽输出,RX设置为开漏,并都上拉。
(2)、I2C配置时,由于是用GPIO 模拟I2C,所以都设置为开漏、无上拉(SCL和SDA都外接了上拉4.7K的电阻)、输出模式,注意是输出,那就有疑问,配置成输出,那SDA怎么接收数据呢,也是可以接收数据的,只要在读数据时,先把SDA拉高,然后在利用uint8_t GPIO_ReadInputDataBit(GPIO_TypeDef* GPIOx, uint16_t GPIO_Pin)函数读(本程序是定义的宏GPIOB->IDR & GPIO_Pin_11),为什么这么写呢,只要找到GPIO_ReadInputDataBit()函数的源代码看下就知道了。I2C代码也是从网上借鉴来的,并稍微了改下,删掉及SCL拉低,因觉得没必要,但有地方不能删uint8_t I2C_WaitAck(void)最后的一个SCL_L,并在一些地方加了I2C_delay(T_HalfCycle);和I2C_delay(T_1QuarterCycle);,为什么呢,因为用示波器测试源代码波形时,发现SCL时钟脉宽在应答位时变窄。
(3)、在连续转换模式的ADC中,注意ADC转换结束及DMA传输结束,为什么要强调呢,因为当时没有考虑那么多,进入停止模式在唤醒后,发现两路ADC的数据串口打印出来是反的,所以在void EXTI_Config(void)函数中进行进行处理,等待ADC转换结束及DMA传输结束,在把外设时钟ADC及DMA都关掉,在打开复用的中断接口。
串口打印效果图:
出图效果:参数是默认参数,效果不是很好,但出图是没问题,然后可以直接在串口助手中发送参数,对Sensor效果进行调试,就脱离了软件编译再下载。
进入了停止模式后,USART是失效的。