完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
LED 模块
GPIO初始化用 void LED_control(u8 led_str, u8 led_state) { if(led_state != 0) { GPIO_ResetBits(GPIOC,led_str<<8); //共阳极 用ResetBits 把GPIOC数据置0,状态显示用1表示亮。 GPIO_SetBits(GPIOD,GPIO_Pin_2); // PD = 1 GPIO_ResetBits(GPIOD,GPIO_Pin_2); // PD = 0 } else { GPIO_SetBits(GPIOC,led_str<<8); GPIO_SetBits(GPIOD,GPIO_Pin_2); // PD = 1 GPIO_ResetBits(GPIOD,GPIO_Pin_2); // PD = 0 } } }} led_str 表示选中8个led灯中的哪些(选中为1,没有选中为0) 共阳管,顾名思义,它们正极在一起呀!要想点亮,另一端电压应该为低呀,所以应该写“0” 蜂鸣器模块 void Buzzer_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_AFIO,ENABLE);开启时钟复用 RCC_APB2Periph_AFIO GPIO_PinRemapConfig(GPIO_Remap_SWJ_NoJTRST,ENABLE); //禁用NoJTRST,使用PB4做普通IO GPIO_InitStructure.GPIO_Pin=GPIO_Pin_4; GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP; GPIO_Init(GPIOB,&GPIO_InitStructure); } #define BEEP_ON() GPIO_ResetBits(GPIOB,GPIO_Pin_4) //置0 低电平打通 #define BEEP_OFF() GPIO_SetBits(GPIOB,GPIO_Pin_4) 置1 低电平打通 定时器模块 TIMTimeBase RCC_Configuration(); //中断使能 NVIC_Configuration(); // 中断向量表设置 Void TIM4_Init(void) { NVIC_InitTypeDef NVIC_InitStructure; /* Enable the TIM4 global Interrupt */ NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); TIM_TimeBaseStructure.TIM_Period = 1000-1; TIM_TimeBaseStructure.TIM_Prescaler = 71; TIM_TimeBaseStructure.TIM_ClockDivision = 0; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE); //更新中断 /* TIM2 enable counter */ TIM_Cmd(TIM4, ENABLE); } 中断处理函数 void TIM4_IRQHandler(void) { static u16 key_count=0,pwm_capture_count=0; //静态变量 if (TIM_GetITStatus(TIM4,TIM_IT_Update) != RESET) { key_count++;pwm_capture_count++; TIM_ClearITPendingBit(TIM4, TIM_IT_Update); if(key_count==10) { key_count=0; key_flag=1; } } } IIC模块 只需写两个函数 读函数和写函数 iic的使用不太熟悉。 void Write_AT24c02(unsigned char add,unsigned char data1) { I2CStart(); I2CSendByte(0xa0); I2CWaitAck(); I2CSendByte(add); I2CWaitAck(); I2CSendByte(data1); I2CWaitAck(); I2CStop(); } unsigned char Read_AT24c02(unsigned char add) { unsigned char temp; I2CStart(); I2CSendByte(0xa0); I2CWaitAck(); I2CSendByte(add); I2CWaitAck(); I2CStart(); I2CSendByte(0xa1); I2CWaitAck(); temp=I2CReceiveByte(); I2CWaitAck(); I2CStop(); return temp; } adc 模块ADCADC1_DMA 通道8 PB0 GPIO 和 ADC1 configuration RCC_APB2PeriphClockCmd void ADC_ch8(void) { ADC_InitTypeDef ADC_InitStructure; GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOB, ENABLE); /* Configure PC.04 (ADC Channel14) as analog input -------------------------*/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN; GPIO_Init(GPIOB, &GPIO_InitStructure); /* ADC1 configuration ------------------------------------------------------*/ ADC_InitStructure.ADC_Mode = ADC_Mode_Independent; ADC_InitStructure.ADC_ScanConvMode = ENABLE; ADC_InitStructure.ADC_ContinuousConvMode = ENABLE; 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 channel14 configuration */ ADC_RegularChannelConfig(ADC1, ADC_Channel_8, 1, ADC_SampleTime_55Cycles5); /* Enable ADC1 DMA */ ADC_DMACmd(ADC1, ENABLE); /* Enable ADC1 */ ADC_Cmd(ADC1, ENABLE); /* Enable ADC1 reset calibration register */ ADC_ResetCalibration(ADC1); /* Check the end of ADC1 reset calibration register */ while(ADC_GetResetCalibrationStatus(ADC1)); /* Start ADC1 calibration */ ADC_StartCalibration(ADC1); /* Check the end of ADC1 calibration */ while(ADC_GetCalibrationStatus(ADC1)); /* Start ADC1 Software Conversion */ ADC_SoftwareStartConvCmd(ADC1, ENABLE); } 使用 初始化ADC函数 定时器定时100 计数,读取adc值 adc_val = ADC_GetConversionValue(ADC1); sprintf((char *)lcd_buf,"ADC_volt=%.2f",(adc_val/4095.0f)*3.3f); 电压显示 LCD_DisplayStringLine(Line2,lcd_buf); sprintf((char *)lcd_buf,"ADC_val=%4d",(adc_val/(4096/4))); // 挡位显示 LCD_DisplayStringLine(Line3,lcd_buf); 按键模块 unsigned char Trg; unsigned char Cont; void Key_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB,ENABLE; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_8; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1|GPIO_Pin_2; GPIO_Init(GPIOB, &GPIO_InitStructure); } void Key_Read( void ) { unsigned char ReadData = (KEYPORT)^0xff; // 1. Read KEYPORT ^(xor) Trg = ReadData & (ReadData ^ Cont); // 2 Cont = ReadData; // 3 } #define KB1 GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0) #define KB2 GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_8) #define KB3 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_1) #define KB4 GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_2) #define KEYPORT KB1 | (KB2<<1) | (KB3<<2) | (KB4<<3) | 0xf0 extern unsigned char Trg; extern unsigned char Cont; if(Cont==0x01) //长按检测 一定要加松手检测 单触发 { key1_time++; if(key1_time>=50 && key1_release==1) //超过500ms且key1为弹起状态 { key1_time = 0; key1_release = 0; 用key¬¬_ release实现长按操作的单次触发,设置key1为按下 lcd_number+=10; //实现功能 } } if(Trg==0x00 && Cont==0x00) //松手检测 { If(key1_time>0 && key1_time<50 ) { lcd_number+=1; //实现功能 } key1_time=0; key1_release = 1; } 长按检测 一定要加松手检测 多次触发 不加按键弹起 if(Cont==0x01) //长按检测 一定要加松手检测 单触发 { key1_time++; if(key1_time>=50) //超过500ms且key1为弹起状态 { key1_time = 0; lcd_number+=10; //实现功能 } } if(Trg==0x00 && Cont==0x00) //松手检测 { If(key1_time>0 && key1_time<50 ) { lcd_number+=1; //实现功能 } key1_time=0; } |
|
|
|
RTC RTCLSI_Calib 抄RTC_Configuration()函数 最后一行代码可注释
顺序 RTCCalendar 先配置 NVIC_Configuration()函数 LSI_Calib 抄RTC_Configuration()函数 其次 Time_Adjust Time_Display() 函数 最后 void RTC_IRQHandler(void) Time_Adjust(23,59,50); //RTC设置初值 sprintf((char*)lcd_buf,“Time: %0.2d:%0.2d:%0.2d”, THH, TMM, TSS); LCD_DisplayStringLine(Line3,lcd_buf); PWM和PWM_capture PWM 翻转的形式 TIMTIM9_OCToggle TIM2_PWM_Init() TIM2_CH2 PA1 TIM2_CH3 PA2 TIM3_PWM_Init() TIM3_CH1 PA6 TIM3_CH2 PA7 GPIO_Configuration(); NVIC_Configuration(); TIM_OC2Init 注意是那个口 uint16_t TIM2_capture = 0; _Bool TIM2_CH2_flag=0, TIM2_CH3_flag=0; //初始化 TIM2_CH2和TIM2_CH3 float TIM2_CH2_duty=0.3, TIM2_CH3_duty=0.7; void TIM2_IRQHandler(void) { /* TIM2_CH2 toggling with frequency = 732.4 Hz */ if (TIM_GetITStatus(TIM2, TIM_IT_CC2) != RESET) { TIM_ClearITPendingBit(TIM2, TIM_IT_CC2 ); TIM2_capture = TIM_GetCapture2(TIM2); if(TIM2_CH2_flag==1) { TIM_SetCompare2(TIM2, TIM2_capture + (u16)(TIM2_CCR2_Val*TIM2_CH2_duty)); TIM2_CH2_flag=0; } else { TIM_SetCompare2(TIM2, TIM2_capture +(u16)(TIM2_CCR2_Val*(1-TIM2_CH2_duty))); TIM2_CH2_flag=1; } } if (TIM_GetITStatus(TIM2, TIM_IT_CC3) != RESET) { TIM_ClearITPendingBit(TIM2, TIM_IT_CC3); TIM2_capture = TIM_GetCapture3(TIM2); if(TIM2_CH3_flag==1) { TIM_SetCompare3(TIM2, TIM2_capture + (u16)(TIM2_CCR3_Val*TIM2_CH3_duty)); TIM2_CH3_flag=0; } else { TIM_SetCompare3(TIM2, TIM2_capture + (u16)(TIM2_CCR3_Val*(1-TIM2_CH3_duty))); TIM2_CH3_flag=1; } } } RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB| RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE); PWM_capture() 编程思路:(1)第一次上升沿中断,清零计数器,并改为下降沿中断(2)下降沿中断,通过TIM_GetCount获取计数器的值,并改为上升沿中断。(3)第二次上升沿中断,通过TIM_GetCounter获取计算器的值T2,通过T2获得PWM的频率,通过T2/T1可以获取PWM的占空比。 RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); __IO uint16_t TIM3_CH2_ReadValue1 = 0, TIM3_CH2_ReadValue2 = 0; __IO uint16_t TIM3_CH2_CaptureNumber = 0; __IO uint32_t TIM3_CH2_Freq = 0; __IO uint16_t TIM3_CH1_ReadValue1 = 0, TIM3_CH1_ReadValue2 = 0; __IO uint16_t TIM3_CH1_CaptureNumber = 0; __IO uint32_t TIM3_CH1_Freq = 0; u8 TIM3_CH1_Duty, TIM3_CH2_Duty; u8 capture_flag = 1; void TIM3_IRQHandler(void) { if(TIM_GetITStatus(TIM3, TIM_IT_CC2) == SET) { TIM_ClearITPendingBit(TIM3, TIM_IT_CC2); if(capture_flag == 2) { if(TIM3_CH2_CaptureNumber == 0) { TIM_SetCounter(TIM3, 0); TIM_OC2PolarityConfig(TIM3,TIM_ICPolarity_Falling); TIM3_CH2_CaptureNumber = 1; } else if (TIM3_CH2_CaptureNumber == 1) { /* Get the Input Capture value */ TIM3_CH2_ReadValue1 = TIM_GetCounter(TIM3); TIM_OC2PolarityConfig(TIM3,TIM_ICPolarity_Rising); TIM3_CH2_CaptureNumber = 2; } else if(TIM3_CH2_CaptureNumber == 2) { TIM3_CH2_ReadValue2 = TIM_GetCounter(TIM3); TIM3_CH2_Freq = (uint32_t) SystemCoreClock / TIM3_CH2_ReadValue2; TIM3_CH2_Duty = TIM3_CH2_ReadValue1 * 100 / TIM3_CH2_ReadValue2; TIM3_CH2_CaptureNumber = 0; } } } if(pwm_capture_count==100) //主函数 { pwm_capture_count=0; capture_flag = capture_flag % 2 + 1; TIM3_CH1_CaptureNumber=0; TIM3_CH2_CaptureNumber=0; } 串口: UtilitiesSTM32_EVALSTM3210B_EVAL void STM_EVAL_COMInit(COM_TypeDef COM, USART_InitTypeDef* USART_InitStruct) 复制函数STM_EVAL_COMInit 注意修改 void STM_EVAL_COMInit(USART_InitTypeDef* USART_InitStruct) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO, ENABLE); RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 ; PA2 发送 和PA3 接受 单片机接收 GPIO_Init(GPIOA, &GPIO_InitStructure); USART_Init(USART2, USART_InitStruct); USART_Cmd(USART2, ENABLE); } USARTPrintf void USART2_Init(void) { USART_InitTypeDef USART_InitStructure; NVIC_InitTypeDef NVIC_InitStructure; USART_InitStructure.USART_BaudRate = 115200; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART2_IRQn; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; STM_EVAL_COMInit(&USART_InitStructure); NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority =0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); USART_ITConfig (USART2,USART_IT_RXNE,ENABLE); //重要 容易被忽视 // UtilitiesSTM32_EVALSTM3210B_EVAL 中断向量表 } int fputc(int ch, FILE *f) { USART_SendData(USART2, (uint8_t) ch); /* Loop until the end of transmission */ while (USART_GetFlagStatus(USART2, USART_IT_RXNE) == RESET) {} return ch; } u8 rx_buf[15]; u8 rx_count=0; u8 rx_ideltime=0; _Bool rx_flag=0; void USART2_IRQHandler(void) { if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET) { USART_ClearITPendingBit(USART2,USART_IT_RXNE); rx_buf[rx_count]=USART_ReceiveData(USART2); if(rx_count<15 && rx_buf[14]=='S') { rx_flag=1; rx_count=0; } rx_count++; rx_ideltime=0; } } 主函数 rx_ideltime++; if(rx_ideltime>=50) { rx_ideltime=0; rx_count=0; memset(rx_buf,0,15); } if(rx_flag) { rx_flag=0; LCD_DisplayStringLine(Line6,rx_buf); } |
|
|
|
只有小组成员才能发言,加入小组>>
3278 浏览 9 评论
2955 浏览 16 评论
3455 浏览 1 评论
8987 浏览 16 评论
4050 浏览 18 评论
1102浏览 3评论
570浏览 2评论
const uint16_t Tab[10]={0}; const uint16_t *p; p = Tab;//报错是怎么回事?
568浏览 2评论
用NUC131单片机UART3作为打印口,但printf没有输出东西是什么原因?
2301浏览 2评论
NUC980DK61YC启动随机性出现Err-DDR是为什么?
1857浏览 2评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-22 00:22 , Processed in 1.316843 second(s), Total 81, Slave 61 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号