stm32f407简易示波器代码
时间: 2023-09-04 17:10:38 浏览: 195
以下是一个简单的基于STM32F407的示波器代码,使用ADC采样输入信号并在LCD上显示波形:
```c
#include "stm32f4xx.h"
#include "stm32f4xx_adc.h"
#include "stm32f4xx_gpio.h"
#include "stm32f4xx_rcc.h"
#include "stm32f4xx_tim.h"
#include "stm32f4xx_usart.h"
#include "stm32f4xx_exti.h"
#include "stm32f4xx_syscfg.h"
#include "stm32f4xx_dma.h"
#include "misc.h"
#include "stdlib.h"
#include "stdio.h"
#define ADC1_DR_ADDRESS ((uint32_t)0x4001204C)
#define ADC1_CHANNEL ADC_Channel_0
uint16_t adc_buffer[1000];
void adc_init(void);
void lcd_init(void);
void lcd_write_cmd(uint8_t cmd);
void lcd_write_data(uint8_t data);
void lcd_write_string(char* str);
void lcd_set_cursor(uint8_t row, uint8_t col);
void lcd_clear(void);
int main(void)
{
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOB | RCC_AHB1Periph_DMA2, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_TIM1 | RCC_APB2Periph_TIM8 | RCC_APB2Periph_USART1, ENABLE);
adc_init();
lcd_init();
while(1)
{
for(int i=0; i<1000; i++)
{
ADC_SoftwareStartConv(ADC1);
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));
adc_buffer[i] = ADC_GetConversionValue(ADC1);
}
lcd_clear();
lcd_write_string("Waveform:");
for(int i=0; i<1000; i++)
{
lcd_set_cursor(1, i % 16);
lcd_write_data(adc_buffer[i] / 10);
}
}
}
void adc_init(void)
{
ADC_InitTypeDef ADC_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOA, &GPIO_InitStructure);
RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1, ENABLE);
RCC_APB2PeriphResetCmd(RCC_APB2Periph_ADC1, DISABLE);
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
ADC_CommonInit(&ADC_CommonInitStructure);
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE;
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_T1_CC1;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfConversion = 1;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_RegularChannelConfig(ADC1, ADC1_CHANNEL, 1, ADC_SampleTime_3Cycles);
ADC_Cmd(ADC1, ENABLE);
}
void lcd_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOB, &GPIO_InitStructure);
lcd_write_cmd(0x28); // 4-bit mode, 2 lines, 5x8 font
lcd_write_cmd(0x0C); // display on, cursor off, blink off
lcd_write_cmd(0x01); // clear display
lcd_write_cmd(0x06); // cursor direction: right
}
void lcd_write_cmd(uint8_t cmd)
{
GPIO_ResetBits(GPIOB, GPIO_Pin_12); // RS = 0 (command mode)
GPIO_ResetBits(GPIOB, GPIO_Pin_13); // RW = 0 (write mode)
GPIOB->ODR = (GPIOB->ODR & 0x00FF) | (cmd << 8); // send high nibble
GPIO_SetBits(GPIOB, GPIO_Pin_14); // E = 1 (enable)
GPIO_ResetBits(GPIOB, GPIO_Pin_14); // E = 0 (disable)
GPIOB->ODR = (GPIOB->ODR & 0x00FF) | ((cmd & 0x0F) << 12); // send low nibble
GPIO_SetBits(GPIOB, GPIO_Pin_14); // E = 1 (enable)
GPIO_ResetBits(GPIOB, GPIO_Pin_14); // E = 0 (disable)
}
void lcd_write_data(uint8_t data)
{
GPIO_SetBits(GPIOB, GPIO_Pin_12); // RS = 1 (data mode)
GPIO_ResetBits(GPIOB, GPIO_Pin_13); // RW = 0 (write mode)
GPIOB->ODR = (GPIOB->ODR & 0x00FF) | (data << 8); // send high nibble
GPIO_SetBits(GPIOB, GPIO_Pin_14); // E = 1 (enable)
GPIO_ResetBits(GPIOB, GPIO_Pin_14); // E = 0 (disable)
GPIOB->ODR = (GPIOB->ODR & 0x00FF) | ((data & 0x0F) << 12); // send low nibble
GPIO_SetBits(GPIOB, GPIO_Pin_14); // E = 1 (enable)
GPIO_ResetBits(GPIOB, GPIO_Pin_14); // E = 0 (disable)
}
void lcd_write_string(char* str)
{
while(*str)
{
lcd_write_data(*str++);
}
}
void lcd_set_cursor(uint8_t row, uint8_t col)
{
uint8_t address = (row == 0 ? 0x80 : 0xC0) + col;
lcd_write_cmd(address);
}
void lcd_clear(void)
{
lcd_write_cmd(0x01);
lcd_write_cmd(0x02);
}
```
该代码使用PA0作为输入信号的ADC通道,每次采样1000个值,将其显示在16x2的LCD屏幕上。需要注意的是,该代码只是一个简单的示波器,采样速度和精度都可以进一步优化。
阅读全文