pongfactory

Lab 4-3 DEMO

Jan 27th, 2014
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.22 KB | None | 0 0
  1. #include "stm32f10x.h"
  2. #include "stm32f10x_tim.h"
  3. #include "stm32f10x_rcc.h"
  4. #include "stm32f10x_gpio.h"
  5. #include "stm32f10x_usart.h"
  6. #include "misc.h"
  7. #include "stm32f10x_exti.h"
  8. #include <stdio.h>
  9.  
  10. USART_InitTypeDef USART_InitStruct; // this is for the USART1 initilization
  11. NVIC_InitTypeDef NVIC_InitStructure; // this is used to configure the NVIC (nested vector interrupt controller)
  12. GPIO_InitTypeDef  GPIO_InitStructure;
  13. TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  14. EXTI_InitTypeDef EXTI_InitStructure;                    //EXTI structure to init EXT
  15. NVIC_InitTypeDef NVIC_InitStructure;                    //NVIC structure to set up NVIC controller
  16.  
  17. void INTTIM_Config(void);
  18. void GPIO_Config(void);
  19. uint32_t micro(void);
  20. void init_USART1(uint32_t baudrate);
  21. void USART_puts(USART_TypeDef* USARTx, volatile char *s);
  22. void init_EXTI(void);
  23. void init_NVIC(void);
  24. void EXTI0_IRQHandler(void);
  25. uint32_t data;
  26.  
  27. int time = 0;
  28. int last_time = 0;
  29. char text[10] = "";
  30. uint8_t buf[1024];
  31. uint32_t micro(void){
  32.     //return (u32)time;
  33.     return (u32)TIM_GetCounter(TIM3);
  34. }
  35.  
  36. int main(void)
  37. {
  38.     INTTIM_Config();
  39.     GPIO_Config();
  40.     init_USART1(9600); // initialize USART1 @ 9600 baud
  41.     USART_puts(USART1, "Start!\r\n"); // just send a message to indicate that it works
  42.     sprintf (text, "%d s\r\n",TIM_GetCounter(TIM3));
  43.     USART_puts(USART1, text);
  44.     while (1)
  45.     {
  46.       //USART_puts(USART1, "Test!\r\n");
  47.         // data = 0xFF6896;  // 1
  48.         data = 0xFF30CE;  // 4
  49.         sentButton(data);
  50.  
  51.  
  52.  
  53.       if ((u32)TIM_GetCounter(TIM3)-last_time > 500){
  54.           GPIOB->ODR ^= GPIO_Pin_14;
  55.           last_time = (u32)TIM_GetCounter(TIM3);
  56.       }
  57.     }
  58. }
  59.  
  60. void INTTIM_Config(void)
  61. {
  62.     /* TIM3 clock enable */
  63.     RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
  64.     /* Time base configuration */
  65.     TIM_TimeBaseStructure.TIM_Period = 0xFFFF - 1;  // 1 MHz down to 1 KHz (1 ms)
  66.     TIM_TimeBaseStructure.TIM_Prescaler = 72 - 1; // 24 MHz Clock down to 1 MHz (adjust per your clock)
  67.     TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
  68.     TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  69.     TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
  70.     /* TIM IT enable */
  71.     TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
  72.     /* TIM3 enable counter */
  73.     TIM_Cmd(TIM3, ENABLE);
  74. }
  75.  
  76. void GPIO_Config(void)
  77. {
  78.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_USART1 |
  79.             RCC_APB2Periph_GPIOA, ENABLE);
  80.     //Output B
  81.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
  82.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  83.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  84.     GPIO_Init(GPIOB, &GPIO_InitStructure);
  85.  
  86.     //USART1 (PA9)
  87.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //USART1_TX
  88.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  89.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  90.     GPIO_Init(GPIOA, &GPIO_InitStructure);
  91.  
  92.     //EXTI0 (PA0)
  93.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
  94.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  95.     GPIO_InitStructure.GPIO_Mode  = GPIO_Mode_IN_FLOATING;
  96.     GPIO_Init(GPIOA, &GPIO_InitStructure);
  97. }
  98.  
  99. void init_USART1(uint32_t baudrate){
  100.     USART_InitStruct.USART_BaudRate = baudrate;                                // the baudrate is set to the value we passed into this init function
  101.     USART_InitStruct.USART_WordLength = USART_WordLength_8b;// we want the data frame size to be 8 bits (standard)
  102.     USART_InitStruct.USART_StopBits = USART_StopBits_1;                // we want 1 stop bit (standard)
  103.     USART_InitStruct.USART_Parity = USART_Parity_No;                // we don't want a parity bit (standard)
  104.     USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // we don't want flow control (standard)
  105.     USART_InitStruct.USART_Mode = USART_Mode_Tx; // we want to enable the transmitter and the receiver
  106.     USART_Init(USART1, &USART_InitStruct);                                        // again all the properties are passed to the USART_Init function which takes care of all the bit setting
  107.  
  108.     USART_ITConfig(USART1, USART_IT_RXNE, DISABLE); // enable the USART1 receive interrupt
  109.     USART_Cmd(USART1, ENABLE);
  110. }
  111.  
  112. void USART_puts(USART_TypeDef* USARTx, volatile char *s){
  113.     while(*s){
  114.         // wait until data register is empty
  115.         while( !(USARTx->SR & 0x00000040) );
  116.         USART_SendData(USARTx, *s);
  117.         *s++;
  118.     }
  119. }
  120.  
  121. ////////////////////////////////////
  122. void init_EXTI(void){
  123.     GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0);
  124.     EXTI_InitStructure.EXTI_Line = EXTI_Line0;
  125.     EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;     //select interrupt mode
  126.     EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;  //generate interrupt on rising edge
  127.     EXTI_InitStructure.EXTI_LineCmd = ENABLE;               //enable EXTI line
  128.     EXTI_Init(&EXTI_InitStructure);                         //send values to registers
  129. }
  130.  
  131. void init_NVIC(void){
  132.     NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;        //select NVIC channel to configure
  133.     NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
  134.     NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
  135.     NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;         //enable IRQ channel
  136.     NVIC_Init(&NVIC_InitStructure);                         //update NVIC registers
  137. }
  138.  
  139. void EXTI0_IRQHandler(void){
  140.     int i;
  141.     //Check if EXTI_Line0 is asserted
  142.     if(EXTI_GetITStatus(EXTI_Line0) != RESET)
  143.     {
  144.         GPIO_WriteBit(GPIOB,GPIO_Pin_9,1);
  145.         for (i=0; i<100000; i++);
  146.         GPIO_WriteBit(GPIOB,GPIO_Pin_9,0);
  147.         for (i=0; i<100000; i++);
  148.     }
  149.     //we need to clear line pending bit manually
  150.     EXTI_ClearITPendingBit(EXTI_Line0);
  151. }
  152.  
  153. uint32_t data = 0xFF6896;
  154. void sentButton( uint32_t data ){
  155.     uint32_t input = data;
  156.     switch( input ){
  157.         case 0xFF629C:
  158.             sprintf(buf, "Button: UP\r\n");
  159.             USART_puts(USART1, buf);
  160.           //  SerialTransmit(buf);
  161.             break;
  162.         case 0xFFA856:
  163.             sprintf(buf, "Button: DOWN\r\n");
  164.             USART_puts(USART1, buf);
  165.          //   SerialTransmit(buf);
  166.             break;
  167.         case 0xFF22DC:
  168.             sprintf(buf, "Button: LEFT\r\n");
  169.             USART_puts(USART1, buf);
  170.          //   SerialTransmit(buf);
  171.             break;
  172.         case 0xFFC23C:
  173.             sprintf(buf, "Button: RIGHT\r\n");
  174.             USART_puts(USART1, buf);
  175.          //   SerialTransmit(buf);
  176.             break;
  177.         case 0xFF02FC:
  178.             sprintf(buf, "Button: OK\r\n");
  179.             USART_puts(USART1, buf);
  180.          //   SerialTransmit(buf);
  181.             break;
  182.         case 0xFF6896:
  183.             sprintf(buf, "Button: %d\r\n", 1);
  184.  
  185.             USART_puts(USART1, buf);
  186.         //    SerialTransmit(buf);
  187.             break;
  188.         case 0xFF9866:
  189.             sprintf(buf, "Button: %d\r\n", 2);
  190.             USART_puts(USART1, buf);
  191.         //    SerialTransmit(buf);
  192.             break;
  193.         case 0xFFB04E:
  194.             sprintf(buf, "Button: %d\r\n", 3);
  195.             USART_puts(USART1, buf);
  196.         //    SerialTransmit(buf);
  197.             break;
  198.         case 0xFF30CE:
  199.             sprintf(buf, "Button: %d\r\n", 4);
  200.             USART_puts(USART1, buf);
  201.         //    SerialTransmit(buf);
  202.             break;
  203.         case 0xFF18E6:
  204.             sprintf(buf, "Button: %d\r\n", 5);
  205.             USART_puts(USART1, buf);
  206.         //    SerialTransmit(buf);
  207.             break;
  208.         case 0xFF7A84:
  209.             sprintf(buf, "Button: %d\r\n", 6);
  210.             USART_puts(USART1, buf);
  211.         //    SerialTransmit(buf);
  212.             break;
  213.         case 0xFF10EE:
  214.             sprintf(buf, "Button: %d\r\n", 7);
  215.             USART_puts(USART1, buf);
  216.         //    SerialTransmit(buf);
  217.             break;
  218.         case 0xFF38C6:
  219.             sprintf(buf, "Button: %d\r\n", 8);
  220.             USART_puts(USART1, buf);
  221.        //     SerialTransmit(buf);
  222.             break;
  223.         case 0xFF5AA4:
  224.             sprintf(buf, "Button: %d\r\n", 9);
  225.             USART_puts(USART1, buf);
  226.        //     SerialTransmit(buf);
  227.             break;
  228.         case 0xFF4AB4:
  229.             sprintf(buf, "Button: %d\r\n", 0);
  230.             USART_puts(USART1, buf);
  231.         //    SerialTransmit(buf);
  232.             break;
  233.         case 0xFF42BC:
  234.             sprintf(buf, "Button: *\r\n");
  235.             USART_puts(USART1, buf);
  236.         //    SerialTransmit(buf);
  237.             break;
  238.         case 0xFF52AC:
  239.             sprintf(buf, "Button: #\r\n");
  240.             USART_puts(USART1, buf);
  241.         //    SerialTransmit(buf);
  242.             break;
  243.     }
  244. } // end-sentButton
Advertisement
Add Comment
Please, Sign In to add comment