Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stm32f2xx_gpio.h"
- #include "stm32f2xx_rcc.h"
- #include "stm32f2xx_tim.h"
- #include "misc.h"
- /* ------------------------------- */
- static volatile u32 vflag = 0; /* vflag=1, draw on screen */
- #define VTOTAL 5;
- u8 buffer[10][10] = {
- {0x7, 0x6, 0x5, 0x4, 0x3, 0x2, 0x1},
- {0x7, 0x6, 0x5, 0x4, 0x3, 0x2, 0x1},
- {0x7, 0x6, 0x5, 0x4, 0x3, 0x2, 0x1},
- {0x7, 0x6, 0x5, 0x4, 0x3, 0x2, 0x1},
- {0x7, 0x6, 0x5, 0x4, 0x3, 0x2, 0x1},
- {0x7, 0x6, 0x5, 0x4, 0x3, 0x2, 0x1},
- {0x7, 0x6, 0x5, 0x4, 0x3, 0x2, 0x1},
- {0x7, 0x6, 0x5, 0x4, 0x3, 0x2, 0x1},
- {0x7, 0x6, 0x5, 0x4, 0x3, 0x2, 0x1},
- {0x7, 0x6, 0x5, 0x4, 0x3, 0x2, 0x1},
- };
- static volatile u32 n = 0;
- void delay(uint32_t n){
- while(n--);
- }
- /****************************************************************************/
- void RCC_Configuration(void){
- /* TIM1/3 clock enable */
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
- /* GPIOA clock enable */
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); //syncs
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); //test leds
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE); //gpio for outputting on screen
- }
- void GPIO_Configuration(void){
- GPIO_InitTypeDef GPIO_InitStructure;
- /*-------------------------- TIM1/3 syncs ----------------------------*/
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_TIM3);
- GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_TIM1);
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
- GPIO_Init(GPIOD, &GPIO_InitStructure);
- /* three bits */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
- GPIO_Init(GPIOE, &GPIO_InitStructure);
- }
- void NVIC_Configuration(void){
- NVIC_InitTypeDef NVIC_InitStructure;
- /* interrupt tim1 */
- NVIC_PriorityGroupConfig( NVIC_PriorityGroup_1);
- NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- TIM_ITConfig(TIM1, TIM_IT_CC2, ENABLE);
- /* Interrupt TIM3 */
- NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- TIM_ITConfig(TIM3, TIM_IT_CC3, ENABLE);
- }
- void test_timer (uint16_t PRESCALER, uint16_t HPERIOD, uint16_t HPULSE, uint16_t HPORCH, uint16_t VPERIOD, uint16_t VPULSE, uint16_t VPORCH){
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_OCInitTypeDef TIM_OCInitStructure;
- u16 Channel1Pulse = 0, Channel2Pulse = 0, Channel3Pulse = 0;
- Channel1Pulse = HPULSE; /* HSYNC */
- Channel2Pulse = HPULSE + HPORCH; /* HSYNC + BACK PORCH */
- TIM_TimeBaseStructure.TIM_Prescaler = PRESCALER - 1;
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_TimeBaseStructure.TIM_Period = HPERIOD;
- TIM_TimeBaseStructure.TIM_ClockDivision = 0;
- TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
- TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
- TIM_OCInitStructure.TIM_Pulse = Channel1Pulse;
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
- TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
- TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
- TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Set;
- TIM_OC1Init(TIM1, &TIM_OCInitStructure);
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Inactive;
- TIM_OCInitStructure.TIM_Pulse = Channel2Pulse;
- TIM_OC2Init(TIM1, &TIM_OCInitStructure);
- /* TIM1 counter enable and output enable */
- TIM_CtrlPWMOutputs(TIM1, ENABLE);
- /* Select TIM1 as Master */
- TIM_SelectMasterSlaveMode(TIM1, TIM_MasterSlaveMode_Enable);
- TIM_SelectOutputTrigger(TIM1, TIM_TRGOSource_Update);
- TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Gated);
- TIM_SelectInputTrigger(TIM3, TIM_TS_ITR0);
- /* Vertical lines */
- Channel2Pulse = VPULSE; /* Sync pulse */
- Channel3Pulse = VPULSE + VPORCH; /* Sync pulse + Back porch */
- TIM_TimeBaseStructure.TIM_Prescaler = 0;
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_TimeBaseStructure.TIM_Period = VPERIOD;
- TIM_TimeBaseStructure.TIM_ClockDivision = 0;
- TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
- TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
- TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
- TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
- TIM_OCInitStructure.TIM_Pulse = Channel2Pulse;
- TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
- TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
- TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
- TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Set;
- TIM_OC2Init(TIM3, &TIM_OCInitStructure);
- TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Inactive;
- TIM_OCInitStructure.TIM_Pulse = Channel3Pulse;
- TIM_OC3Init(TIM3, &TIM_OCInitStructure);
- /* TIM3 counter enable and output enable */
- TIM_CtrlPWMOutputs(TIM3, ENABLE);
- TIM_Cmd(TIM3, ENABLE);
- TIM_Cmd(TIM1, ENABLE);
- }
- //* hporch interrupt
- void TIM1_CC_IRQHandler(void){
- TIM1->SR &= ~(1<<2); //clear flag
- if(vflag){
- GPIOE->ODR = 0x7;
- delay(50);
- GPIOE->ODR = 0x6;
- delay(50);
- GPIOE->ODR = 0x5;
- delay(50);
- GPIOE->ODR = 0x4;
- delay(50);
- GPIOE->ODR = 0x3;
- delay(50);
- GPIOE->ODR = 0x2;
- delay(50);
- GPIOE->ODR = 0x1;
- delay(50);
- GPIOE->ODR = 0x0;
- delay(50);
- GPIOE->ODR = 0x7;
- delay(50);
- GPIOE->ODR = 0x6;
- delay(50);
- GPIOE->ODR = 0x5;
- delay(50);
- }
- GPIO_ToggleBits(GPIOD, GPIO_Pin_15);
- }
- // vporch interrupt
- void TIM3_IRQHandler(void){
- TIM3->SR &= ~(1<<2); //clear flag
- vflag = 1;
- GPIO_ToggleBits(GPIOD, GPIO_Pin_14);
- }
- int main(void){
- RCC_Configuration();
- GPIO_Configuration();
- NVIC_Configuration();
- test_timer(32,65,6,1,666,6,23);
- GPIOD->BSRRL = GPIO_Pin_12;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement