Advertisement
pyrohaz

STM32F0 VGA Test

Feb 15th, 2015
275
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.44 KB | None | 0 0
  1. #include <stm32f0xx_gpio.h>
  2. #include <stm32f0xx_rcc.h>
  3. #include <stm32f0xx_tim.h>
  4. #include <stm32f0xx_misc.h>
  5.  
  6. #define V_HSYNC     GPIO_Pin_3
  7. #define V_VSYNC     GPIO_Pin_4
  8. #define V_RED       GPIO_Pin_0
  9. #define V_GREEN     GPIO_Pin_1
  10. #define V_BLUE      GPIO_Pin_2
  11.  
  12. #define V_HSTIM     TIM2
  13. #define V_VSTIM     TIM14
  14.  
  15. #define V_HSPS      GPIO_PinSource3
  16. #define V_VSPS      GPIO_PinSource4
  17. #define V_HSAF      GPIO_AF_2
  18. #define V_VSAF      GPIO_AF_4
  19. #define V_GPIO      GPIOA
  20.  
  21. #define V_HSPER     1056
  22. #define V_HSPRE     1
  23.  
  24. #define V_VSPER     60288
  25. #define V_VSPRE     10
  26.  
  27. GPIO_InitTypeDef G;
  28. TIM_TimeBaseInitTypeDef TB;
  29. TIM_OCInitTypeDef TO;
  30. NVIC_InitTypeDef N;
  31.  
  32. void TIM2_IRQHandler(void){
  33.     static uint8_t CS = 1;
  34.     static uint32_t CLine = 1;
  35.  
  36.     if(TIM_GetITStatus(V_HSTIM, TIM_IT_CC1) == SET){
  37.         TIM_ClearITPendingBit(V_HSTIM, TIM_IT_CC1);
  38.         switch(CS){
  39.         case 1:
  40.             GPIO_SetBits(V_GPIO, V_BLUE);
  41.             TIM_SetCompare1(V_HSTIM, 316);
  42.             CS = 2;
  43.             break;
  44.         case 2:
  45.             GPIO_ResetBits(V_GPIO, V_BLUE);
  46.             GPIO_SetBits(V_GPIO, V_GREEN);
  47.             TIM_SetCompare1(V_HSTIM, 416);
  48.             CS = 3;
  49.             break;
  50.         case 3:
  51.             GPIO_ResetBits(V_GPIO, V_GREEN);
  52.             GPIO_SetBits(V_GPIO, V_RED);
  53.             TIM_SetCompare1(V_HSTIM, 516);
  54.             CS = 4;
  55.             break;
  56.         case 4:
  57.             GPIO_ResetBits(V_GPIO, V_BLUE|V_GREEN|V_RED);
  58.             TIM_SetCompare1(V_HSTIM, 216);
  59.             CS = 1;
  60.             break;
  61.         }
  62.     }
  63. }
  64.  
  65. int main(void)
  66. {
  67.     SystemInit();
  68.     RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
  69.     RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14, ENABLE);
  70.     RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
  71.    
  72.     G.GPIO_Pin = V_HSYNC | V_VSYNC;
  73.     G.GPIO_OType = GPIO_OType_PP;
  74.     G.GPIO_Mode = GPIO_Mode_AF;
  75.     G.GPIO_Speed = GPIO_Speed_50MHz;
  76.     G.GPIO_PuPd = GPIO_PuPd_NOPULL;
  77.     GPIO_Init(V_GPIO, &G);
  78.  
  79.     G.GPIO_Pin = V_RED | V_GREEN | V_BLUE;
  80.     G.GPIO_OType = GPIO_OType_PP;
  81.     G.GPIO_Mode = GPIO_Mode_OUT;
  82.     G.GPIO_Speed = GPIO_Speed_50MHz;
  83.     G.GPIO_PuPd = GPIO_PuPd_NOPULL;
  84.     GPIO_Init(V_GPIO, &G);
  85.  
  86.     GPIO_ResetBits(V_GPIO, V_RED|V_BLUE|V_GREEN);
  87.  
  88.     TIM_Cmd(V_HSTIM, DISABLE);
  89.     TIM_Cmd(V_VSTIM, DISABLE);
  90.     GPIO_PinAFConfig(V_GPIO, V_HSPS, V_HSAF);
  91.     GPIO_PinAFConfig(V_GPIO, V_VSPS, V_VSAF);
  92.  
  93.     TIM_RemapConfig(V_VSTIM, TIM14_GPIO);
  94.  
  95.     TB.TIM_ClockDivision = TIM_CKD_DIV1;
  96.     TB.TIM_CounterMode = TIM_CounterMode_Up;
  97.     TB.TIM_Period = (V_HSPER - 1);
  98.     TB.TIM_Prescaler = (V_HSPRE - 1);
  99.     TIM_TimeBaseInit(V_HSTIM, &TB);
  100.  
  101.     TB.TIM_ClockDivision = TIM_CKD_DIV1;
  102.     TB.TIM_CounterMode = TIM_CounterMode_Up;
  103.     TB.TIM_Period = (V_VSPER - 1);
  104.     TB.TIM_Prescaler = (V_VSPRE);
  105.     TIM_TimeBaseInit(V_VSTIM, &TB);
  106.  
  107.     TO.TIM_OCIdleState = TIM_OCIdleState_Set;
  108.     TO.TIM_OCMode = TIM_OCMode_PWM2;
  109.     TO.TIM_OCPolarity = TIM_OCPolarity_High;
  110.     TO.TIM_OutputState = TIM_OutputState_Enable;
  111.     TO.TIM_Pulse = 128;
  112.     TIM_OC4Init(V_HSTIM, &TO);
  113.     TIM_SetCounter(V_HSTIM, 216);
  114.  
  115.     TIM_SelectOnePulseMode(V_HSTIM, TIM_OPMode_Repetitive);
  116.  
  117.     TO.TIM_OCIdleState = TIM_OCIdleState_Set;
  118.     TO.TIM_OCMode = TIM_OCMode_PWM2;
  119.     TO.TIM_OCPolarity = TIM_OCPolarity_High;
  120.     TO.TIM_OutputState = TIM_OutputState_Enable;
  121.     TO.TIM_Pulse = 384;
  122.     TIM_OC1Init(V_VSTIM, &TO);
  123.     TIM_SetCounter(V_VSTIM, 2592);
  124.  
  125.     TIM_SelectOnePulseMode(V_VSTIM, TIM_OPMode_Repetitive);
  126.  
  127.     TIM_ClearITPendingBit(V_HSTIM, TIM_IT_CC1);
  128.     TIM_ITConfig(V_HSTIM, TIM_IT_CC1, ENABLE);
  129.  
  130.     TIM_SetCompare1(V_HSTIM, 216);
  131.  
  132.     N.NVIC_IRQChannel = TIM2_IRQn;
  133.     N.NVIC_IRQChannelCmd = ENABLE;
  134.     N.NVIC_IRQChannelPriority = 0;
  135.     NVIC_Init(&N);
  136.  
  137.     V_VSTIM->CR1 |= TIM_CR1_CEN;
  138.     V_HSTIM->CR1 |= TIM_CR1_CEN;
  139.  
  140.     while(1)
  141.     {
  142.     }
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement