stm32f103c8t6 VGA显示的完整程序,加注释
时间: 2023-09-30 21:04:46 浏览: 111
以下是一份基于STM32F103C8T6单片机的VGA显示完整程序,包括必要的注释。请注意,该代码需要使用外部SRAM芯片才能工作。
```
/* Includes */
#include "stm32f10x.h"
#include "stm32f10x_gpio.h"
#include "stm32f10x_rcc.h"
/* Defines */
#define HSYNC_PIN GPIO_Pin_6
#define VSYNC_PIN GPIO_Pin_7
#define RED_PIN GPIO_Pin_0
#define GREEN_PIN GPIO_Pin_1
#define BLUE_PIN GPIO_Pin_2
/* Function prototypes */
void GPIO_Configuration(void);
void FSMC_Configuration(void);
void VGA_Init(void);
void VGA_Draw_Pixel(uint16_t x, uint16_t y, uint8_t r, uint8_t g, uint8_t b);
/* Global variables */
uint16_t frame_buffer[480][640] __attribute__((section(".sdram")));
/* Main function */
int main(void)
{
GPIO_Configuration();
FSMC_Configuration();
VGA_Init();
while (1)
{
/* Draw a red square */
for (int x = 100; x < 200; x++)
{
for (int y = 100; y < 200; y++)
{
VGA_Draw_Pixel(x, y, 255, 0, 0);
}
}
}
}
/* Configure GPIO */
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Enable GPIOA and GPIOB clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB, ENABLE);
/* Configure HSYNC pin */
GPIO_InitStructure.GPIO_Pin = HSYNC_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure VSYNC pin */
GPIO_InitStructure.GPIO_Pin = VSYNC_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure RGB pins */
GPIO_InitStructure.GPIO_Pin = RED_PIN | GREEN_PIN | BLUE_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB, &GPIO_InitStructure);
}
/* Configure FSMC */
void FSMC_Configuration(void)
{
FSMC_NORSRAMInitTypeDef FSMC_NORSRAMInitStructure;
FSMC_NORSRAMTimingInitTypeDef FSMC_NORSRAMTimingInitStructure;
/* Enable FSMC clock */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE);
/* Configure FSMC timing */
FSMC_NORSRAMTimingInitStructure.FSMC_AddressSetupTime = 0;
FSMC_NORSRAMTimingInitStructure.FSMC_AddressHoldTime = 0;
FSMC_NORSRAMTimingInitStructure.FSMC_DataSetupTime = 7;
FSMC_NORSRAMTimingInitStructure.FSMC_BusTurnAroundDuration = 0;
FSMC_NORSRAMTimingInitStructure.FSMC_CLKDivision = 0;
FSMC_NORSRAMTimingInitStructure.FSMC_DataLatency = 0;
FSMC_NORSRAMTimingInitStructure.FSMC_AccessMode = FSMC_AccessMode_B;
/* Configure FSMC NOR/SRAM bank */
FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM1;
FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Disable;
FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_SRAM;
FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_16b;
FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable;
FSMC_NORSRAMInitStructure.FSMC_AsynchronousWait = FSMC_AsynchronousWait_Disable;
FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low;
FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable;
FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState;
FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable;
FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable;
FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Disable;
FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable;
FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &FSMC_NORSRAMTimingInitStructure;
FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &FSMC_NORSRAMTimingInitStructure;
/* Initialize FSMC */
FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure);
/* Enable FSMC bank */
FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM1, ENABLE);
}
/* Initialize VGA */
void VGA_Init(void)
{
/* Configure HSYNC pin */
GPIO_SetBits(GPIOA, HSYNC_PIN);
/* Configure VSYNC pin */
GPIO_SetBits(GPIOA, VSYNC_PIN);
/* Wait for VSYNC */
while ((GPIOA->IDR & VSYNC_PIN) == 0);
/* Wait for VSYNC to end */
while ((GPIOA->IDR & VSYNC_PIN) != 0);
}
/* Draw a pixel on the screen */
void VGA_Draw_Pixel(uint16_t x, uint16_t y, uint8_t r, uint8_t g, uint8_t b)
{
/* Save RGB values to the frame buffer */
frame_buffer[y][x] = (r << 11) | (g << 5) | b;
}
/* Set up the SDRAM controller */
void SystemInit_ExtMemCtl(void)
{
/* Enable GPIOC, GPIOD, GPIOE, GPIOF and GPIOG interface clock */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOC | RCC_AHBPeriph_GPIOD | RCC_AHBPeriph_GPIOE | RCC_AHBPeriph_GPIOF | RCC_AHBPeriph_GPIOG, ENABLE);
/* Connect PCx, PDx, PEx, PFx and PGx pins to SDRAM module */
GPIO_PinAFConfig(GPIOC, GPIO_PinSource0, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource2, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource3, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource4, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource5, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource6, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource7, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource8, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource9, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource12, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource13, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource14, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOC, GPIO_PinSource15, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource0, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource1, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource3, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource4, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource5, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource6, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource7, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource8, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource9, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource10, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource11, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource12, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource13, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource14, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOD, GPIO_PinSource15, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOE, GPIO_PinSource0, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOE, GPIO_PinSource1, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOE, GPIO_PinSource3, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOE, GPIO_PinSource4, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOE, GPIO_PinSource5, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOE, GPIO_PinSource6, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOE, GPIO_PinSource7, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOE, GPIO_PinSource8, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOE, GPIO_PinSource9, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOE, GPIO_PinSource10, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOE, GPIO_PinSource11, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOE, GPIO_PinSource12, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOE, GPIO_PinSource13, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOE, GPIO_PinSource14, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOE, GPIO_PinSource15, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOF, GPIO_PinSource0, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOF, GPIO_PinSource1, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOF, GPIO_PinSource2, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOF, GPIO_PinSource3, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOF, GPIO_PinSource4, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOF, GPIO_PinSource5, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOF, GPIO_PinSource12, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOF, GPIO_PinSource13, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOF, GPIO_PinSource14, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOF, GPIO_PinSource15, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOG, GPIO_PinSource0, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOG, GPIO_PinSource1, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOG, GPIO_PinSource2, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOG, GPIO_PinSource3, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOG, GPIO_PinSource4, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOG, GPIO_PinSource5, GPIO_AF_FSMC);
GPIO_PinAFConfig(GPIOG, GPIO_PinSource10, GPIO_AF_FSMC);
/* Enable FSMC clock */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_FSMC, ENABLE);
/* FSMC configuration */
FSMC_NORSRAMInitTypeDef FSMC_NORSRAMInitStructure;
FSMC_NORSRAMTimingInitTypeDef FSMC_NORSRAMTimingInitStructure;
FSMC_NORSRAMTimingInitStructure.FSMC_AddressSetupTime = 0x02;
FSMC_NORSRAMTimingInitStructure.FSMC_AddressHoldTime = 0x00;
FSMC_NORSRAMTimingInitStructure.FSMC_DataSetupTime = 0x05;
FSMC_NORSRAMTimingInitStructure.FSMC_BusTurnAroundDuration = 0x00;
FSMC_NORSRAMTimingInitStructure.FSMC_CLKDivision = 0x00;
FSMC_NORSRAMTimingInitStructure.FSMC_DataLatency = 0x00;
FSMC_NORSRAMTimingInitStructure.FSMC_AccessMode = FSMC_AccessMode_A;
FSMC_NORSRAMInitStructure.FSMC_Bank = FSMC_Bank1_NORSRAM1;
FSMC_NORSRAMInitStructure.FSMC_DataAddressMux = FSMC_DataAddressMux_Enable;
FSMC_NORSRAMInitStructure.FSMC_MemoryType = FSMC_MemoryType_SRAM;
FSMC_NORSRAMInitStructure.FSMC_MemoryDataWidth = FSMC_MemoryDataWidth_16b;
FSMC_NORSRAMInitStructure.FSMC_BurstAccessMode = FSMC_BurstAccessMode_Disable;
FSMC_NORSRAMInitStructure.FSMC_AsynchronousWait = FSMC_AsynchronousWait_Disable;
FSMC_NORSRAMInitStructure.FSMC_WaitSignalPolarity = FSMC_WaitSignalPolarity_Low;
FSMC_NORSRAMInitStructure.FSMC_WrapMode = FSMC_WrapMode_Disable;
FSMC_NORSRAMInitStructure.FSMC_WaitSignalActive = FSMC_WaitSignalActive_BeforeWaitState;
FSMC_NORSRAMInitStructure.FSMC_WriteOperation = FSMC_WriteOperation_Enable;
FSMC_NORSRAMInitStructure.FSMC_WaitSignal = FSMC_WaitSignal_Disable;
FSMC_NORSRAMInitStructure.FSMC_ExtendedMode = FSMC_ExtendedMode_Disable;
FSMC_NORSRAMInitStructure.FSMC_WriteBurst = FSMC_WriteBurst_Disable;
FSMC_NORSRAMInitStructure.FSMC_ReadWriteTimingStruct = &FSMC_NORSRAMTimingInitStructure;
FSMC_NORSRAMInitStructure.FSMC_WriteTimingStruct = &FSMC_NORSRAMTimingInitStructure;
/* Initialize FSMC */
FSMC_NORSRAMInit(&FSMC_NORSRAMInitStructure);
/* Enable FSMC bank */
FSMC_NORSRAMCmd(FSMC_Bank1_NORSRAM1, ENABLE);
}
```
需要注意的一些点:
- 该代码使用了外部SDRAM芯片来存储像素数据,因此需要在系统初始化时调用SystemInit_ExtMemCtl函数以对SDRAM进行初始化。
- VGA_Draw_Pixel函数将RGB值存储到帧缓冲区中,帧缓冲区是一个二维数组,每个元素代表一个像素。
- 该程序仅绘制了一个红色正方形,可以根据需要修改以绘制不同的图形。
阅读全文