1 | #include "stm32f10x.h"
|
2 | #include "stm32f10x_conf.h"
|
3 | #include "stm32f10x_gpio.h"
|
4 | #include "stm32f10x_rcc.h"
|
5 | #include "stm32f10x_usart.h"
|
6 | #include "stm32f10x_tim.h"
|
7 | //#include "stm32f10x_spi.h"
|
8 | //#include "stm32f10x_i2c.h"
|
9 | //#include <stdio.h>
|
10 |
|
11 | /***************************************************************************//**
|
12 | * Declare function prototypes
|
13 | ******************************************************************************/
|
14 | void SetupClock(void);
|
15 | void SetupUSART(void);
|
16 | void SetupTimer(void);
|
17 | void USART1_SendString(char* data);
|
18 |
|
19 |
|
20 |
|
21 | // *************** main *************
|
22 |
|
23 | int main(void)
|
24 | {
|
25 | volatile int i;
|
26 |
|
27 |
|
28 | SetupClock();
|
29 | SetupUSART();
|
30 | SetupTimer();
|
31 |
|
32 | GPIO_InitTypeDef GPIO_InitStructure;
|
33 | /* Initialize LED which connected to PB11, Enable the Clock*/
|
34 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
|
35 | /* Configure the GPIO_LED pin */
|
36 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
|
37 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
|
38 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
39 | GPIO_Init(GPIOB, &GPIO_InitStructure);
|
40 |
|
41 | while (1) {
|
42 |
|
43 | //GPIOB->ODR ^= GPIO_Pin_10;
|
44 | /* delay */
|
45 | //for(i=0;i<0x300000;i++);
|
46 |
|
47 | //USART1_SendString("in while\n\r");
|
48 |
|
49 |
|
50 | }
|
51 | }
|
52 |
|
53 | void USART1_SendString(char* data)
|
54 | {
|
55 | while (*data)
|
56 | {
|
57 | USART_SendData(USART1, (uint16_t) *data);
|
58 | while(USART_GetFlagStatus(USART1, USART_FLAG_TC)==RESET);
|
59 | data++;
|
60 | }
|
61 | }
|
62 |
|
63 | void SetupTimer(void)
|
64 | {
|
65 |
|
66 | TIM_TimeBaseInitTypeDef TIM_TimeBase_InitStructure;
|
67 |
|
68 | /* Enable TIM2 clock */
|
69 | RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); // sollten 72Mhz sein
|
70 |
|
71 | TIM_TimeBase_InitStructure.TIM_Prescaler = 7200; // (72Mhz/7200 = 10khz)
|
72 | TIM_TimeBase_InitStructure.TIM_ClockDivision = 10; // nach Prescaler=1kHz
|
73 | TIM_TimeBase_InitStructure.TIM_CounterMode = TIM_CounterMode_Down;
|
74 | TIM_TimeBase_InitStructure.TIM_Period = 1000; // Eine Sekunde
|
75 |
|
76 | TIM_TimeBaseInit(TIM2, &TIM_TimeBase_InitStructure);
|
77 |
|
78 | TIM_ClearFlag(TIM2, TIM_FLAG_Update);
|
79 |
|
80 |
|
81 |
|
82 | TIM_Cmd(TIM2, ENABLE);
|
83 | USART1_SendString("in SetupTimer\n\r");
|
84 | }
|
85 |
|
86 | //timer 2 interrupt
|
87 | void TIM2_IRQHandler(void)
|
88 | {
|
89 | USART1_SendString("in TIM1\n\r");
|
90 | //if interrupt happens the do this
|
91 | if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET)
|
92 | {
|
93 | //clear interrupt and start counting again to get precise freq
|
94 | TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
|
95 |
|
96 | //toggle led
|
97 | GPIOB->ODR ^= GPIO_Pin_10;
|
98 | USART1_SendString("in TIM2\n\r");
|
99 | }
|
100 | }
|
101 |
|
102 | /***************************************************************************//**
|
103 | * @brief Setup clocks
|
104 | ******************************************************************************/
|
105 | void SetupClock()
|
106 | {
|
107 | RCC_DeInit (); /* RCC system reset(for debug purpose)*/
|
108 | RCC_HSEConfig (RCC_HSE_ON); /* Enable HSE (external Quarz) */
|
109 |
|
110 | while (RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET); // Wait till HSE is ready
|
111 |
|
112 | /* Enable USART1 and GPIOA clock */
|
113 | RCC_APB2PeriphClockCmd (RCC_APB2Periph_USART1 | RCC_APB2Periph_GPIOA, ENABLE);
|
114 |
|
115 | /* Configure PLLs *********/
|
116 | /* PLL2 configuration: PLL2CLK = (HSE / 5) * 8 = 40 MHz */
|
117 | RCC_PREDIV2Config(RCC_PREDIV2_Div5);
|
118 | RCC_PLL2Config(RCC_PLL2Mul_8);
|
119 | RCC_PLL2Cmd(ENABLE); // Enable PLL2
|
120 | while (RCC_GetFlagStatus(RCC_FLAG_PLL2RDY) == RESET); // Wait till PLL2 is ready
|
121 |
|
122 | /* PLL1 configuration: PLLCLK = (PLL2 / 5) * 9 = 72 MHz */
|
123 | RCC_PREDIV1Config(RCC_PREDIV1_Source_PLL2, RCC_PREDIV1_Div5);
|
124 | RCC_PLLConfig(RCC_PLLSource_PREDIV1, RCC_PLLMul_9);
|
125 | RCC_PLLCmd (ENABLE);
|
126 | while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET); // Wait till PLL is ready
|
127 |
|
128 | }
|
129 | /***************************************************************************//**
|
130 | * @brief Init USART1
|
131 | ******************************************************************************/
|
132 | void SetupUSART()
|
133 | {
|
134 | GPIO_InitTypeDef GPIO_InitStructure;
|
135 | USART_InitTypeDef USART_InitStructure;
|
136 |
|
137 | /* Enable GPIOA clock */
|
138 | RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
|
139 |
|
140 | /* Configure USART1 Rx (PA10) as input floating */
|
141 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
|
142 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
143 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
|
144 | GPIO_Init(GPIOA, &GPIO_InitStructure);
|
145 |
|
146 | /* Configure USART1 Tx (PA9) as alternate function push-pull */
|
147 | GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
|
148 | GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
|
149 | GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
|
150 | GPIO_Init(GPIOA, &GPIO_InitStructure);
|
151 |
|
152 | /* USART1 configured as follow:
|
153 | - BaudRate = 115200 baud
|
154 | - Word Length = 8 Bits
|
155 | - One Stop Bit
|
156 | - No parity
|
157 | - Hardware flow control disabled (RTS and CTS signals)
|
158 | - Receive and transmit enabled
|
159 | - USART Clock disabled
|
160 | - USART CPOL: Clock is active low
|
161 | - USART CPHA: Data is captured on the middle
|
162 | - USART LastBit: The clock pulse of the last data bit is not output to
|
163 | the SCLK pin
|
164 | */
|
165 | USART_InitStructure.USART_BaudRate = 115200;
|
166 | USART_InitStructure.USART_WordLength = USART_WordLength_8b;
|
167 | USART_InitStructure.USART_StopBits = USART_StopBits_1;
|
168 | USART_InitStructure.USART_Parity = USART_Parity_No ;
|
169 | USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
|
170 | USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
|
171 | USART_Init(USART1, &USART_InitStructure);
|
172 | USART_Cmd(USART1, ENABLE);
|
173 | }
|