AT32F403A VGA(一)

一、目的:

要用单片机实现VGA功能。

二、分析:

VGA需要两根时钟信号线,分别为Hsync和Vsync。

在800*600的显示模式下,Hsync周期为26.4us,Vsync周期为16.579ms。

Hsync同步脉冲时间为3.2us,Vsync同步脉冲时间为0.106ms,而且都是高电平有效。

在AT32F403A里面,主从时钟的可以配置为主时钟输出时钟,从时钟使用主时钟输出的时钟做为计数脉冲。

三、代码

1、主时钟的配置为:

void tmr_primary_mode_select(tmr_type *tmr_x, tmr_primary_select_type primary_mode)

tmr_type可以选择:

TMR1, TMR2, TMR3, TMR4, TMR5, TMR6, TMR7, TMR8

tmr_primary_select_type可以选择:

TMR_PRIMARY_SEL_RESET
  *         - TMR_PRIMARY_SEL_ENABLE
  *         - TMR_PRIMARY_SEL_OVERFLOW
  *         - TMR_PRIMARY_SEL_COMPARE
  *         - TMR_PRIMARY_SEL_C1ORAW
  *         - TMR_PRIMARY_SEL_C2ORAW
  *         - TMR_PRIMARY_SEL_C3ORAW
  *         - TMR_PRIMARY_SEL_C4ORAW

2、从时钟的配置为:

/*先择哪一个主时钟的输出做为输入时钟*/

void tmr_trigger_input_select(tmr_type *tmr_x, sub_tmr_input_sel_type trigger_select)

/*子时钟模式,可以是编码,挂起,触发等等。这里选择TMR_SUB_EXTERNAL_CLOCK_MODE_A*/

void tmr_sub_mode_select(tmr_type *tmr_x, tmr_sub_mode_select_type sub_mode)

/*开启同步模式*/

void tmr_sub_sync_mode_set(tmr_type *tmr_x, confirm_state new_state)

最后别忘记开起时钟

tmr_counter_enable(TMR3, TRUE);

四、效果:

Hsync:

Vsync:

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个简单的示例代码,可以在 stm32f103c8t6 上使用 VGA 显示一串数字: ```c #include "stm32f10x.h" #define HSYNC_PIN GPIO_Pin_0 #define HSYNC_PORT GPIOA #define VSYNC_PIN GPIO_Pin_1 #define VSYNC_PORT GPIOA #define COLOR_PIN GPIO_Pin_2 #define COLOR_PORT GPIOA #define HSYNC_TIM TIM2 #define HSYNC_TIM_RCC RCC_APB1Periph_TIM2 #define VGA_H_PIXELS 640 #define VGA_H_FRONT_PORCH 16 #define VGA_H_SYNC_PULSE 96 #define VGA_H_BACK_PORCH 48 #define VGA_H_TOTAL (VGA_H_PIXELS + VGA_H_FRONT_PORCH + VGA_H_SYNC_PULSE + VGA_H_BACK_PORCH) #define VGA_V_PIXELS 480 #define VGA_V_FRONT_PORCH 10 #define VGA_V_SYNC_PULSE 2 #define VGA_V_BACK_PORCH 33 #define VGA_V_TOTAL (VGA_V_PIXELS + VGA_V_FRONT_PORCH + VGA_V_SYNC_PULSE + VGA_V_BACK_PORCH) void VGA_Init(void); void VGA_DrawPixel(uint16_t x, uint16_t y, uint16_t color); void VGA_DrawChar(uint16_t x, uint16_t y, uint8_t c, uint16_t color); void VGA_DrawString(uint16_t x, uint16_t y, char* str, uint16_t color); void VGA_DrawNumber(uint16_t x, uint16_t y, uint32_t num, uint16_t color); void VGA_HSyncHandler(void); void VGA_VSyncHandler(void); volatile uint16_t vga_frame_buffer[VGA_V_TOTAL][VGA_H_TOTAL]; int main(void) { SystemInit(); VGA_Init(); while (1) { VGA_DrawNumber(VGA_H_PIXELS / 2 - 40, VGA_V_PIXELS / 2 - 8, 12345678, 0xFFFF); } } void VGA_Init(void) { GPIO_InitTypeDef GPIO_InitStructure; TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_OCInitTypeDef TIM_OCInitStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE); GPIO_InitStructure.GPIO_Pin = HSYNC_PIN; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(HSYNC_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = VSYNC_PIN; GPIO_Init(VSYNC_PORT, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = COLOR_PIN; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(COLOR_PORT, &GPIO_InitStructure); RCC_APB1PeriphClockCmd(HSYNC_TIM_RCC, ENABLE); TIM_TimeBaseStructure.TIM_Period = VGA_H_TOTAL - 1; TIM_TimeBaseStructure.TIM_Prescaler = 72 - 1; TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(HSYNC_TIM, &TIM_TimeBaseStructure); TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1; TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable; TIM_OCInitStructure.TIM_Pulse = VGA_H_SYNC_PULSE; TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High; TIM_OC1Init(HSYNC_TIM, &TIM_OCInitStructure); TIM_ITConfig(HSYNC_TIM, TIM_IT_Update, ENABLE); NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); TIM_Cmd(HSYNC_TIM, ENABLE); } void VGA_DrawPixel(uint16_t x, uint16_t y, uint16_t color) { vga_frame_buffer[y][x] = color; } void VGA_DrawChar(uint16_t x, uint16_t y, uint8_t c, uint16_t color) { uint8_t i, j; for (i = 0; i < 8; i++) { for (j = 0; j < 6; j++) { if ((Font6x8[c][j] >> i) & 0x01) { VGA_DrawPixel(x + j, y + i, color); } } } } void VGA_DrawString(uint16_t x, uint16_t y, char* str, uint16_t color) { while (*str != '\0') { VGA_DrawChar(x, y, *str++, color); x += 6; } } void VGA_DrawNumber(uint16_t x, uint16_t y, uint32_t num, uint16_t color) { char str[10]; sprintf(str, "%lu", num); VGA_DrawString(x, y, str, color); } void VGA_HSyncHandler(void) { static uint16_t h_count = 0; if (h_count < VGA_H_PIXELS) { GPIO_WriteBit(COLOR_PORT, COLOR_PIN, (BitAction)(vga_frame_buffer[VGA_V_TOTAL - 1][h_count] >> 15)); GPIO_WriteBit(COLOR_PORT, COLOR_PIN, (BitAction)(vga_frame_buffer[VGA_V_TOTAL - 1][h_count] >> 14)); GPIO_WriteBit(COLOR_PORT, COLOR_PIN, (BitAction)(vga_frame_buffer[VGA_V_TOTAL - 1][h_count] >> 13)); GPIO_WriteBit(COLOR_PORT, COLOR_PIN, (BitAction)(vga_frame_buffer[VGA_V_TOTAL - 1][h_count] >> 12)); GPIO_WriteBit(COLOR_PORT, COLOR_PIN, (BitAction)(vga_frame_buffer[VGA_V_TOTAL - 1][h_count] >> 11)); GPIO_WriteBit(COLOR_PORT, COLOR_PIN, (BitAction)(vga_frame_buffer[VGA_V_TOTAL - 1][h_count] >> 10)); GPIO_WriteBit(COLOR_PORT, COLOR_PIN, (BitAction)(vga_frame_buffer[VGA_V_TOTAL - 1][h_count] >> 9)); GPIO_WriteBit(COLOR_PORT, COLOR_PIN, (BitAction)(vga_frame_buffer[VGA_V_TOTAL - 1][h_count] >> 8)); GPIO_WriteBit(COLOR_PORT, COLOR_PIN, (BitAction)(vga_frame_buffer[VGA_V_TOTAL - 1][h_count] >> 7)); GPIO_WriteBit(COLOR_PORT, COLOR_PIN, (BitAction)(vga_frame_buffer[VGA_V_TOTAL - 1][h_count] >> 6)); GPIO_WriteBit(COLOR_PORT, COLOR_PIN, (BitAction)(vga_frame_buffer[VGA_V_TOTAL - 1][h_count] >> 5)); GPIO_WriteBit(COLOR_PORT, COLOR_PIN, (BitAction)(vga_frame_buffer[VGA_V_TOTAL - 1][h_count] >> 4)); GPIO_WriteBit(COLOR_PORT, COLOR_PIN, (BitAction)(vga_frame_buffer[VGA_V_TOTAL - 1][h_count] >> 3)); GPIO_WriteBit(COLOR_PORT, COLOR_PIN, (BitAction)(vga_frame_buffer[VGA_V_TOTAL - 1][h_count] >> 2)); GPIO_WriteBit(COLOR_PORT, COLOR_PIN, (BitAction)(vga_frame_buffer[VGA_V_TOTAL - 1][h_count] >> 1)); GPIO_WriteBit(COLOR_PORT, COLOR_PIN, (BitAction)vga_frame_buffer[VGA_V_TOTAL - 1][h_count]); } h_count++; if (h_count >= VGA_H_TOTAL) { h_count = 0; TIM_ClearITPendingBit(HSYNC_TIM, TIM_IT_Update); VGA_VSyncHandler(); } } void VGA_VSyncHandler(void) { static uint16_t v_count = 0; if (v_count < VGA_V_PIXELS) { v_count++; } if (v_count == VGA_V_FRONT_PORCH) { GPIO_WriteBit(VSYNC_PORT, VSYNC_PIN, Bit_RESET); } if (v_count == VGA_V_FRONT_PORCH + VGA_V_SYNC_PULSE) { GPIO_WriteBit(HSYNC_PORT, HSYNC_PIN, Bit_RESET); } if (v_count == VGA_V_FRONT_PORCH + VGA_V_SYNC_PULSE + VGA_V_BACK_PORCH) { GPIO_WriteBit(HSYNC_PORT, HSYNC_PIN, Bit_SET); } if (v_count == VGA_V_TOTAL - 1) { v_count = 0; GPIO_WriteBit(VSYNC_PORT, VSYNC_PIN, Bit_SET); } } void TIM2_IRQHandler(void) { if (TIM_GetITStatus(HSYNC_TIM, TIM_IT_Update) != RESET) { TIM_ClearITPendingBit(HSYNC_TIM, TIM_IT_Update); VGA_HSyncHandler(); } } ``` 注意,此代码仅显示数字,您需要在代码中添加更多功能来实现您的需求。此外,您还需要将 STM32F103C8T6 连接到 VGA 显示器,以便显示输出。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值