把初始的rcc时钟开启放在一个函数中
void RCC_initCTY() { RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC|RCC_APB2Periph_GPIOD, ENABLE); }先初始化对应GPIOC 8~15 引脚, 及锁存器的引脚GPIOD 2
void LED_initCTY() { GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = 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_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOC, &GPIO_InitStructure); GPIO_Write(GPIOC,0Xffff); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_Init(GPIOD, &GPIO_InitStructure); GPIO_SetBits(GPIOD,GPIO_Pin_2); GPIO_ResetBits(GPIOD,GPIO_Pin_2); }led控制程序,当lednum为9时为控制所有的灯
void LED_control(u8 lednum,u16 stat) { if(lednum==9) LED_sta=stat<<8; else{ if(stat==1) LED_sta |= (1<<(lednum-1+8)); else LED_sta &= ~(1<<(lednum-1+8)); } GPIO_Write(GPIOC,LED_sta); GPIO_SetBits(GPIOD,GPIO_Pin_2); GPIO_ResetBits(GPIOD,GPIO_Pin_2); }在文件开头宏定义
#define BT1 GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0) #define BT2 GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_8) #define BT3 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1) #define BT4 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_2) 先配置对应引脚PA0 PA8 PB1 PB2
void KEY_initCTY() { GPIO_InitTypeDef GPIO_InitStructure; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_8; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_2; GPIO_Init(GPIOB, &GPIO_InitStructure); }扫描函数
void KEY_scanCTY() { if(BT1==0) //BT44444444444 { Delay_Ms(5); { while(BT1==0); } } // ....中间省略 if(BT4==0) //BT44444444444 { Delay_Ms(5); { while(BT4==0); } } }用的是官方的历程ADC>RegSimul_DualMode修改来初始化
void ADC_initCTY() { ADC_InitTypeDef ADC_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1,ENABLE); //####################### GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 ; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; GPIO_Init(GPIOB, &GPIO_InitStructure); ADC_InitStructure.ADC_Mode = ADC_Mode_Independent ; ADC_InitStructure.ADC_ScanConvMode = DISABLE; ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None; ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right; ADC_InitStructure.ADC_NbrOfChannel = 1; ADC_Init(ADC1, &ADC_InitStructure); /* ADC1 regular channels configuration */ ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 1, ADC_SampleTime_13Cycles5); ADC_Cmd(ADC1, ENABLE); ADC_ResetCalibration(ADC1); while(ADC_GetResetCalibrationStatus(ADC1)); ADC_StartCalibration(ADC1); while(ADC_GetCalibrationStatus(ADC1)); }ADC读取函数,这个要自己写(从xxx adc.h中找)
void ADC_readCTY() { ADC_SoftwareStartConvCmd(ADC1,ENABLE); //####################### Delay_Ms(5); ADCvalue= ADC_GetConversionValue(ADC1)*3.3/0X0FFF; //####################### }利用systick,每200ms采集一次,在main函数 while中体现如下
if(GetadcF==1) { GetadcF=0; ADC_readCTY(); }先初始化配置
void PWM_hubuIint() { GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1|RCC_APB2Periph_AFIO,ENABLE); 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_14; GPIO_Init(GPIOB, &GPIO_InitStructure); TIM_TimeBaseStructure.TIM_Period = 1000-1; TIM_TimeBaseStructure.TIM_Prescaler =72-1; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure); TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable; //####################### TIM_OCInitStructure.TIM_Pulse = 500; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High; //####################### TIM_OC2Init(TIM1, &TIM_OCInitStructure); TIM_Cmd(TIM1, ENABLE); TIM_CCPreloadControl(TIM1,ENABLE); //####################### TIM_CtrlPWMOutputs(TIM1,ENABLE); //######################################## }结合寄存器动态调节周期和频率
TIMx->CCR2 = TIM_OCInitStruct->TIM_Pulse; 调占空比 TIMx->ARR = TIM_TimeBaseInitStruct->TIM_Period ; 调周期也可以读写拓展板的三轴传感器,地址为0x38 0x39 不过需要先初始化器件 iic_write(0x20,0x47) 读0x29 0x2B 0x2D 对应x y z 轴加速度
eeprom地址为0xa0 0xa1 自己写读写函数
不要忘了在main函数开头调用i2c_init();
void IICwriteCTY(u8 add,u8 dat) { I2CStart(); I2CSendByte(0xa0); I2CWaitAck(); I2CSendByte(add); I2CWaitAck(); I2CSendByte(dat); I2CWaitAck(); I2CStop(); Delay_Ms(10); } u8 IICreadCTY(u8 add) { u8 tem=0; I2CStart(); I2CSendByte(0xa0); I2CWaitAck(); I2CSendByte(add); I2CWaitAck(); I2CStart(); I2CSendByte(0xa1); I2CWaitAck(); tem=I2CReceiveByte(); I2CStop(); Delay_Ms(10); return tem; }在需要存储变量值的地方以如下方式存储并植入密码
if(BT2==0) //BT2222222222222 { Delay_Ms(5); if(BT2==0) { if(interF==1) interF=2; else { interF=1; TIM1->ARR = 1000/outFreq -1; TIM1->CCR2 = pwmA9*10/outFreq; //*1000/100=10 IICwriteCTY(0x08,outFreq); IICwriteCTY(0x10,12); //####################### } BTV=2; while(BT2==0); } }main函数开始时这样写,意为若已在0x10中写入密码12,则读取0x08值给变量。否则变量值为最开始的初始值
if(IICreadCTY(0x10)==12) { //####################### Delay_Ms(10); outFreq=IICreadCTY(0x08); TIM1->ARR = 1000/outFreq -1; }可以直接放在按键程序里clear lcd
void LCD_showarc() { if(showsta==2){ //####################### LCD_Clear(Blue); showsta=1; } LCD_DisplayStringLine(Line2 ,(unsigned char *)" argv "); sprintf(LCD_string,"collect V: %.2f ",ADCvalue); LCD_DisplayStringLine(Line4 ,LCD_string); }可检测输入pwm的频率和占空比,初始化用TIM>PWM_Input中代码,并配置中断
void pwminput_init() { TIM_ICInitTypeDef TIM_ICInitStructure; GPIO_InitTypeDef GPIO_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); /* TIM3 channel 2 pin (PA.07) configuration */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); TIM_ICInitStructure.TIM_Channel = TIM_Channel_2; TIM_ICInitStructure.TIM_ICPolarity = TIM_ICPolarity_Rising; TIM_ICInitStructure.TIM_ICSelection = TIM_ICSelection_DirectTI; TIM_ICInitStructure.TIM_ICPrescaler = TIM_ICPSC_DIV1; TIM_ICInitStructure.TIM_ICFilter = 0x0; TIM_PWMIConfig(TIM3, &TIM_ICInitStructure); /* Select the TIM3 Input Trigger: TI2FP2 */ TIM_SelectInputTrigger(TIM3, TIM_TS_TI2FP2); /* Select the slave Mode: Reset Mode */ TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Reset); /* Enable the Master/Slave Mode */ TIM_SelectMasterSlaveMode(TIM3, TIM_MasterSlaveMode_Enable); /* TIM enable counter */ TIM_Cmd(TIM3, ENABLE); /* Enable the CC2 Interrupt Request */ TIM_ITConfig(TIM3, TIM_IT_CC2, ENABLE); }中断嵌套配置
NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure);中断处理, 采集处理
u32 DutyCycle = 0; u32 Frequency = 0; void TIM3_IRQHandler(void) { u16 IC2Value; IC2Value = TIM_GetCapture2(TIM3); if (IC2Value != 0) { DutyCycle = (TIM_GetCapture1(TIM3) * 100) / IC2Value; //####################### Frequency = SystemCoreClock / IC2Value; //####################### } else { DutyCycle = 0; Frequency = 0; } TIM_ClearITPendingBit(TIM3, TIM_IT_CC2); //####################### }初始化配置,用USART>Interrupt
void usart_initCTY() { GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure); USART_InitStructure.USART_BaudRate = 9600; 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; /* Configure USARTy */ USART_Init(USART2, &USART_InitStructure); USART_ITConfig(USART2, USART_IT_RXNE, ENABLE); USART_Cmd(USART2, ENABLE); }同样需要中断配置NVIC
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure);中断处理接受数据
extern u8 UST; extern u8 USRf; void USART2_IRQHandler(void) { if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) { USART_ClearITPendingBit(USART2, USART_IT_RXNE); //####################### UST = USART_ReceiveData(USART2); //####################### USRf=1; } }发送数据函数 (自己写)
void usart_sendStr() { u8 index=0; index=0; do{ USART_SendData( USART2, UsrsendBuf[index++]); //####################### while(USART_GetFlagStatus(USART2, USART_FLAG_TXE)==0); //####################### }while(UsrsendBuf[index]!='\0'); //####################### }函数运行调用(在上位机显示换行需发 “\r\n”)
if(USRf==1) { USRf=0; if(UST=='C') { sprintf(UsrsendBuf,"\r\nUplimt temperature: