Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stm32f0xx_gpio.h>
- #include <stm32f0xx_rcc.h>
- #include <stm32f0xx_tim.h>
- #include <stm32f0xx_misc.h>
- #define V_HSYNC GPIO_Pin_3
- #define V_VSYNC GPIO_Pin_4
- #define V_RED GPIO_Pin_0
- #define V_GREEN GPIO_Pin_1
- #define V_BLUE GPIO_Pin_2
- #define V_HSTIM TIM2
- #define V_VSTIM TIM14
- #define V_HSPS GPIO_PinSource3
- #define V_VSPS GPIO_PinSource4
- #define V_HSAF GPIO_AF_2
- #define V_VSAF GPIO_AF_4
- #define V_GPIO GPIOA
- #define V_HSPER 1056
- #define V_HSPRE 1
- #define V_VSPER 60288
- #define V_VSPRE 10
- GPIO_InitTypeDef G;
- TIM_TimeBaseInitTypeDef TB;
- TIM_OCInitTypeDef TO;
- NVIC_InitTypeDef N;
- void TIM2_IRQHandler(void){
- static uint8_t CS = 1;
- static uint32_t CLine = 1;
- if(TIM_GetITStatus(V_HSTIM, TIM_IT_CC1) == SET){
- TIM_ClearITPendingBit(V_HSTIM, TIM_IT_CC1);
- switch(CS){
- case 1:
- GPIO_SetBits(V_GPIO, V_BLUE);
- TIM_SetCompare1(V_HSTIM, 316);
- CS = 2;
- break;
- case 2:
- GPIO_ResetBits(V_GPIO, V_BLUE);
- GPIO_SetBits(V_GPIO, V_GREEN);
- TIM_SetCompare1(V_HSTIM, 416);
- CS = 3;
- break;
- case 3:
- GPIO_ResetBits(V_GPIO, V_GREEN);
- GPIO_SetBits(V_GPIO, V_RED);
- TIM_SetCompare1(V_HSTIM, 516);
- CS = 4;
- break;
- case 4:
- GPIO_ResetBits(V_GPIO, V_BLUE|V_GREEN|V_RED);
- TIM_SetCompare1(V_HSTIM, 216);
- CS = 1;
- break;
- }
- }
- }
- int main(void)
- {
- SystemInit();
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14, ENABLE);
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
- G.GPIO_Pin = V_HSYNC | V_VSYNC;
- G.GPIO_OType = GPIO_OType_PP;
- G.GPIO_Mode = GPIO_Mode_AF;
- G.GPIO_Speed = GPIO_Speed_50MHz;
- G.GPIO_PuPd = GPIO_PuPd_NOPULL;
- GPIO_Init(V_GPIO, &G);
- G.GPIO_Pin = V_RED | V_GREEN | V_BLUE;
- G.GPIO_OType = GPIO_OType_PP;
- G.GPIO_Mode = GPIO_Mode_OUT;
- G.GPIO_Speed = GPIO_Speed_50MHz;
- G.GPIO_PuPd = GPIO_PuPd_NOPULL;
- GPIO_Init(V_GPIO, &G);
- GPIO_ResetBits(V_GPIO, V_RED|V_BLUE|V_GREEN);
- TIM_Cmd(V_HSTIM, DISABLE);
- TIM_Cmd(V_VSTIM, DISABLE);
- GPIO_PinAFConfig(V_GPIO, V_HSPS, V_HSAF);
- GPIO_PinAFConfig(V_GPIO, V_VSPS, V_VSAF);
- TIM_RemapConfig(V_VSTIM, TIM14_GPIO);
- TB.TIM_ClockDivision = TIM_CKD_DIV1;
- TB.TIM_CounterMode = TIM_CounterMode_Up;
- TB.TIM_Period = (V_HSPER - 1);
- TB.TIM_Prescaler = (V_HSPRE - 1);
- TIM_TimeBaseInit(V_HSTIM, &TB);
- TB.TIM_ClockDivision = TIM_CKD_DIV1;
- TB.TIM_CounterMode = TIM_CounterMode_Up;
- TB.TIM_Period = (V_VSPER - 1);
- TB.TIM_Prescaler = (V_VSPRE);
- TIM_TimeBaseInit(V_VSTIM, &TB);
- TO.TIM_OCIdleState = TIM_OCIdleState_Set;
- TO.TIM_OCMode = TIM_OCMode_PWM2;
- TO.TIM_OCPolarity = TIM_OCPolarity_High;
- TO.TIM_OutputState = TIM_OutputState_Enable;
- TO.TIM_Pulse = 128;
- TIM_OC4Init(V_HSTIM, &TO);
- TIM_SetCounter(V_HSTIM, 216);
- TIM_SelectOnePulseMode(V_HSTIM, TIM_OPMode_Repetitive);
- TO.TIM_OCIdleState = TIM_OCIdleState_Set;
- TO.TIM_OCMode = TIM_OCMode_PWM2;
- TO.TIM_OCPolarity = TIM_OCPolarity_High;
- TO.TIM_OutputState = TIM_OutputState_Enable;
- TO.TIM_Pulse = 384;
- TIM_OC1Init(V_VSTIM, &TO);
- TIM_SetCounter(V_VSTIM, 2592);
- TIM_SelectOnePulseMode(V_VSTIM, TIM_OPMode_Repetitive);
- TIM_ClearITPendingBit(V_HSTIM, TIM_IT_CC1);
- TIM_ITConfig(V_HSTIM, TIM_IT_CC1, ENABLE);
- TIM_SetCompare1(V_HSTIM, 216);
- N.NVIC_IRQChannel = TIM2_IRQn;
- N.NVIC_IRQChannelCmd = ENABLE;
- N.NVIC_IRQChannelPriority = 0;
- NVIC_Init(&N);
- V_VSTIM->CR1 |= TIM_CR1_CEN;
- V_HSTIM->CR1 |= TIM_CR1_CEN;
- while(1)
- {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement