Advertisement
uv_vjeverica

main.c

May 22nd, 2013
292
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.92 KB | None | 0 0
  1. #include "stm32f2xx_gpio.h"
  2. #include "stm32f2xx_rcc.h"
  3. #include "stm32f2xx_tim.h"
  4. #include "misc.h"
  5.  
  6.  
  7. /* -------------------------------  */
  8. static volatile u32 vflag = 0;              /* vflag=1, draw on screen */
  9. #define VTOTAL 5;
  10. u8 buffer[10][10] = {
  11. {0x7, 0x6, 0x5, 0x4, 0x3, 0x2, 0x1},
  12. {0x7, 0x6, 0x5, 0x4, 0x3, 0x2, 0x1},
  13. {0x7, 0x6, 0x5, 0x4, 0x3, 0x2, 0x1},
  14. {0x7, 0x6, 0x5, 0x4, 0x3, 0x2, 0x1},
  15. {0x7, 0x6, 0x5, 0x4, 0x3, 0x2, 0x1},
  16. {0x7, 0x6, 0x5, 0x4, 0x3, 0x2, 0x1},
  17. {0x7, 0x6, 0x5, 0x4, 0x3, 0x2, 0x1},
  18. {0x7, 0x6, 0x5, 0x4, 0x3, 0x2, 0x1},
  19. {0x7, 0x6, 0x5, 0x4, 0x3, 0x2, 0x1},
  20. {0x7, 0x6, 0x5, 0x4, 0x3, 0x2, 0x1},
  21. };
  22.  
  23.  
  24.  
  25. static volatile u32 n = 0; 
  26. void delay(uint32_t n){
  27.     while(n--);
  28.     }
  29.  
  30.  
  31.  
  32.  
  33.  
  34. /****************************************************************************/
  35. void RCC_Configuration(void){
  36. /* TIM1/3 clock enable */
  37.  
  38.   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
  39.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_TIM1, ENABLE);
  40.    
  41. /* GPIOA clock enable */
  42. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); //syncs
  43.  
  44. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE); //test leds
  45. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOE, ENABLE); //gpio for outputting on screen
  46.  
  47.  
  48. }
  49.  
  50. void GPIO_Configuration(void){
  51.   GPIO_InitTypeDef                      GPIO_InitStructure;
  52.  
  53.   /*-------------------------- TIM1/3 syncs ----------------------------*/
  54. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_8;
  55. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
  56. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  57. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  58. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  59. GPIO_Init(GPIOA, &GPIO_InitStructure);
  60.    
  61.    
  62. GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_TIM3);
  63. GPIO_PinAFConfig(GPIOA, GPIO_PinSource8, GPIO_AF_TIM1);
  64.    
  65.    
  66. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
  67. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  68. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  69. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  70. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
  71. GPIO_Init(GPIOD, &GPIO_InitStructure);
  72.    
  73.     /* three bits */
  74. GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2;
  75. GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  76. GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
  77. GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
  78. GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  79. GPIO_Init(GPIOE, &GPIO_InitStructure);
  80.    
  81.  
  82. }
  83.  
  84.  
  85.  
  86.  
  87. void NVIC_Configuration(void){
  88.    
  89.     NVIC_InitTypeDef                        NVIC_InitStructure;
  90.    
  91.     /* interrupt tim1 */
  92.     NVIC_PriorityGroupConfig( NVIC_PriorityGroup_1);
  93.        
  94. NVIC_InitStructure.NVIC_IRQChannel = TIM1_CC_IRQn;
  95. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  96. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  97. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  98. NVIC_Init(&NVIC_InitStructure);
  99. TIM_ITConfig(TIM1, TIM_IT_CC2, ENABLE);
  100.    
  101.     /* Interrupt TIM3 */
  102.  
  103.        
  104. NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;
  105. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  106. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 2;
  107. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  108. NVIC_Init(&NVIC_InitStructure);
  109. TIM_ITConfig(TIM3, TIM_IT_CC3, ENABLE);
  110.    
  111. }
  112.  
  113.  
  114. void test_timer (uint16_t PRESCALER, uint16_t HPERIOD, uint16_t HPULSE, uint16_t HPORCH, uint16_t VPERIOD, uint16_t VPULSE, uint16_t VPORCH){
  115.    
  116.     TIM_TimeBaseInitTypeDef         TIM_TimeBaseStructure;
  117.     TIM_OCInitTypeDef                   TIM_OCInitStructure;
  118.    
  119.     u16 Channel1Pulse = 0, Channel2Pulse = 0, Channel3Pulse = 0;
  120.    
  121.    
  122.     Channel1Pulse = HPULSE;     /* HSYNC */
  123.     Channel2Pulse = HPULSE + HPORCH;        /* HSYNC + BACK PORCH */
  124.    
  125.     TIM_TimeBaseStructure.TIM_Prescaler = PRESCALER - 1;
  126.     TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  127.     TIM_TimeBaseStructure.TIM_Period = HPERIOD;
  128.     TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  129.     TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
  130.     TIM_TimeBaseInit(TIM1, &TIM_TimeBaseStructure);
  131.  
  132.     TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
  133.     TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  134.     TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
  135.     TIM_OCInitStructure.TIM_Pulse = Channel1Pulse;
  136.     TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
  137.     TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
  138.     TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
  139.     TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Set;
  140.  
  141.     TIM_OC1Init(TIM1, &TIM_OCInitStructure);
  142.    
  143.     TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Inactive;
  144.     TIM_OCInitStructure.TIM_Pulse = Channel2Pulse;
  145.     TIM_OC2Init(TIM1, &TIM_OCInitStructure);
  146.  
  147.     /* TIM1 counter enable and output enable */
  148.     TIM_CtrlPWMOutputs(TIM1, ENABLE);
  149.  
  150.     /* Select TIM1 as Master */
  151.     TIM_SelectMasterSlaveMode(TIM1, TIM_MasterSlaveMode_Enable);
  152.     TIM_SelectOutputTrigger(TIM1, TIM_TRGOSource_Update);
  153.    
  154.    
  155.     TIM_SelectSlaveMode(TIM3, TIM_SlaveMode_Gated);
  156.     TIM_SelectInputTrigger(TIM3, TIM_TS_ITR0);
  157.    
  158.     /* Vertical lines */
  159.     Channel2Pulse = VPULSE;     /* Sync pulse */
  160.     Channel3Pulse = VPULSE + VPORCH;        /* Sync pulse + Back porch */
  161.     TIM_TimeBaseStructure.TIM_Prescaler = 0;
  162.     TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  163.     TIM_TimeBaseStructure.TIM_Period = VPERIOD;
  164.     TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  165.     TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
  166.  
  167.     TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
  168.  
  169.     TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM2;
  170.     TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  171.     TIM_OCInitStructure.TIM_OutputNState = TIM_OutputNState_Enable;
  172.     TIM_OCInitStructure.TIM_Pulse = Channel2Pulse;
  173.     TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
  174.     TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
  175.     TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Reset;
  176.     TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Set;
  177.     TIM_OC2Init(TIM3, &TIM_OCInitStructure);
  178.    
  179.     TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_Inactive;
  180.     TIM_OCInitStructure.TIM_Pulse = Channel3Pulse;
  181.     TIM_OC3Init(TIM3, &TIM_OCInitStructure);
  182.  
  183.     /*  TIM3 counter enable and output enable */
  184.     TIM_CtrlPWMOutputs(TIM3, ENABLE);
  185.  
  186.  
  187.  
  188.  
  189.     TIM_Cmd(TIM3, ENABLE);
  190.     TIM_Cmd(TIM1, ENABLE);
  191. }
  192.  
  193.  
  194.  
  195. //* hporch interrupt
  196. void TIM1_CC_IRQHandler(void){
  197.     TIM1->SR &= ~(1<<2); //clear flag
  198.     if(vflag){     
  199.     GPIOE->ODR = 0x7;
  200.         delay(50);
  201.     GPIOE->ODR = 0x6;
  202.         delay(50);
  203.     GPIOE->ODR = 0x5;
  204.         delay(50);
  205.     GPIOE->ODR = 0x4;
  206.         delay(50);
  207.     GPIOE->ODR = 0x3;
  208.         delay(50);
  209.     GPIOE->ODR = 0x2;
  210.         delay(50);
  211.     GPIOE->ODR = 0x1;
  212.         delay(50);
  213.     GPIOE->ODR = 0x0;
  214.         delay(50);
  215.     GPIOE->ODR = 0x7;
  216.         delay(50);
  217.     GPIOE->ODR = 0x6;
  218.         delay(50);
  219.     GPIOE->ODR = 0x5;
  220.         delay(50);
  221. }
  222.  
  223. GPIO_ToggleBits(GPIOD, GPIO_Pin_15);
  224. }
  225.  
  226. //  vporch interrupt
  227. void TIM3_IRQHandler(void){
  228.     TIM3->SR &= ~(1<<2); //clear flag
  229.     vflag = 1; 
  230. GPIO_ToggleBits(GPIOD, GPIO_Pin_14);
  231.  
  232. }
  233.  
  234.  
  235.  
  236.  
  237. int main(void){
  238.  
  239.     RCC_Configuration();
  240.     GPIO_Configuration();
  241.     NVIC_Configuration();
  242.  
  243.     test_timer(32,65,6,1,666,6,23);
  244.    
  245. GPIOD->BSRRL = GPIO_Pin_12;
  246. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement