基于STM32F103采集ADC电压在一块芯片为ST7789分辨率为172×320的屏幕上显示
时间: 2024-06-11 09:04:36 浏览: 245
STM32F103系列ADC使用.rar_STM32F103_adc_read pin stm32_stm32f103 adc使
1. 硬件连接
将ADC采集的电压信号连接到STM32F103的ADC通道上,同时将ST7789显示屏连接到STM32F103的SPI接口上。
2. 软件设计
(1)初始化ADC模块
首先需要初始化ADC模块,包括时钟使能、引脚配置、采样时间等参数的设置。
(2)配置SPI接口
然后需要配置SPI接口,包括时钟使能、引脚配置、SPI模式、数据位数等参数的设置。
(3)采集电压并显示
在主函数中,通过ADC采集电压值,并将其转换为相应的电压值,然后将电压值通过SPI接口发送到ST7789显示屏上进行显示。
3. 代码示例
以下是基于STM32F103采集ADC电压在一块芯片为ST7789分辨率为172×320的屏幕上显示的代码示例:
```
#include "stm32f10x.h"
#include "stm32f10x_adc.h"
#include "stm32f10x_spi.h"
#define ADC1_DR_Address ((u32)0x4001244C)
u16 ADC_ConvertedValue = 0;
float voltage = 0;
void ADC_Configuration(void)
{
ADC_InitTypeDef ADC_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1 | RCC_APB2Periph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
ADC_DeInit(ADC1);
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);
ADC_RegularChannelConfig(ADC1, ADC_Channel_0, 1, ADC_SampleTime_239Cycles5);
ADC_Cmd(ADC1, ENABLE);
}
void SPI_Configuration(void)
{
SPI_InitTypeDef SPI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_SPI1, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_7;
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_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
SPI_InitStructure.SPI_Direction = SPI_Direction_1Line_Tx;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI1, &SPI_InitStructure);
SPI_Cmd(SPI1, ENABLE);
}
void ADC_GetVoltage(void)
{
ADC_SoftwareStartConvCmd(ADC1, ENABLE);
while(!ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC));
ADC_ConvertedValue = ADC_GetConversionValue(ADC1);
voltage = ADC_ConvertedValue * 3.3 / 4096;
}
void ST7789_SendCommand(u8 command)
{
GPIO_ResetBits(GPIOA, GPIO_Pin_5);
SPI_I2S_SendData(SPI1, command);
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
GPIO_SetBits(GPIOA, GPIO_Pin_5);
}
void ST7789_SendData(u8 data)
{
GPIO_SetBits(GPIOA, GPIO_Pin_5);
SPI_I2S_SendData(SPI1, data);
while(SPI_I2S_GetFlagStatus(SPI1, SPI_I2S_FLAG_TXE) == RESET);
}
void ST7789_Init(void)
{
ST7789_SendCommand(0x36); // Memory data access control
ST7789_SendData(0x08);
ST7789_SendCommand(0x3A); // Interface pixel format
ST7789_SendData(0x05);
ST7789_SendCommand(0xB2); // Porch control
ST7789_SendData(0x0C);
ST7789_SendData(0x0C);
ST7789_SendData(0x00);
ST7789_SendData(0x33);
ST7789_SendData(0x33);
ST7789_SendCommand(0xB7); // Gate control
ST7789_SendData(0x35);
ST7789_SendCommand(0xBB); // VCOMS setting
ST7789_SendData(0x19);
ST7789_SendCommand(0xC0); // LCMCTRL
ST7789_SendData(0x2C);
ST7789_SendCommand(0xC2); // VDVVRHEN
ST7789_SendData(0x01);
ST7789_SendCommand(0xC3); // VRHS
ST7789_SendData(0x12);
ST7789_SendCommand(0xC4); // VDVS
ST7789_SendData(0x20);
ST7789_SendCommand(0xC6); // FRCTRL2
ST7789_SendData(0x0F);
ST7789_SendCommand(0xD0); // Power control
ST7789_SendData(0xA4);
ST7789_SendData(0xA1);
ST7789_SendCommand(0xE0); // PGAMCTRL
ST7789_SendData(0xD0);
ST7789_SendData(0x08);
ST7789_SendData(0x11);
ST7789_SendData(0x08);
ST7789_SendData(0x0C);
ST7789_SendData(0x15);
ST7789_SendData(0x39);
ST7789_SendData(0x33);
ST7789_SendData(0x50);
ST7789_SendData(0x36);
ST7789_SendData(0x13);
ST7789_SendData(0x14);
ST7789_SendData(0x29);
ST7789_SendData(0x2D);
ST7789_SendCommand(0xE1); // NGAMCTRL
ST7789_SendData(0xD0);
ST7789_SendData(0x08);
ST7789_SendData(0x10);
ST7789_SendData(0x08);
ST7789_SendData(0x06);
ST7789_SendData(0x06);
ST7789_SendData(0x39);
ST7789_SendData(0x44);
ST7789_SendData(0x51);
ST7789_SendData(0x0B);
ST7789_SendData(0x16);
ST7789_SendData(0x14);
ST7789_SendData(0x2F);
ST7789_SendData(0x31);
ST7789_SendCommand(0x29); // Display on
}
void ST7789_SetWindow(u16 x_start, u16 y_start, u16 x_end, u16 y_end)
{
ST7789_SendCommand(0x2A); // Column address set
ST7789_SendData(x_start >> 8);
ST7789_SendData(x_start & 0xFF);
ST7789_SendData(x_end >> 8);
ST7789_SendData(x_end & 0xFF);
ST7789_SendCommand(0x2B); // Row address set
ST7789_SendData(y_start >> 8);
ST7789_SendData(y_start & 0xFF);
ST7789_SendData(y_end >> 8);
ST7789_SendData(y_end & 0xFF);
ST7789_SendCommand(0x2C); // Memory write
}
void ST7789_DrawPixel(u16 x, u16 y, u16 color)
{
ST7789_SetWindow(x, y, x, y);
ST7789_SendData(color >> 8);
ST7789_SendData(color & 0xFF);
}
void ST7789_Clear(u16 color)
{
u16 i, j;
ST7789_SetWindow(0, 0, 172, 320);
for(i = 0; i < 172; i++)
{
for(j = 0; j < 320; j++)
{
ST7789_SendData(color >> 8);
ST7789_SendData(color & 0xFF);
}
}
}
int main(void)
{
ADC_Configuration();
SPI_Configuration();
ST7789_Init();
while(1)
{
ADC_GetVoltage();
ST7789_Clear(0xFFFF);
ST7789_DrawPixel(86, 160, 0x0000);
ST7789_DrawPixel(86, voltage / 3.3 * 160, 0x0000);
}
}
```
阅读全文