Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.91 KB | None | 0 0
  1. //
  2. // This file is part of the GNU ARM Eclipse distribution.
  3. // Copyright (c) 2014 Liviu Ionescu.
  4. //
  5.  
  6. // ----------------------------------------------------------------------------
  7. // School: University of Victoria, Canada.
  8. // Course: CENG 355 "Microprocessor-Based Systems".
  9. //
  10. // See "system/include/cmsis/stm32f0xx.h" for register/bit definitions.
  11. // See "system/src/cmsis/vectors_stm32f0xx.c" for handler declarations.
  12. // ----------------------------------------------------------------------------
  13. #include <stdio.h>
  14. #include "diag/Trace.h"
  15. #include "cmsis/cmsis_device.h"
  16. #include "stm32f0xx_spi.h"
  17. // ----- main() ---------------------------------------------------------------
  18. // Sample pragmas to cope with warnings. Please note the related line at
  19. // the end of this function, used to pop the compiler diagnostics status.
  20. #pragma GCC diagnostic push
  21. #pragma GCC diagnostic ignored "-Wunused-parameter"
  22. #pragma GCC diagnostic ignored "-Wmissing-declarations"
  23. #pragma GCC diagnostic ignored "-Wreturn-type"
  24.  
  25. /* Clock prescaler for TIM2 timer: no prescaling */
  26. #define myTIM2_PRESCALER ((uint16_t)0x0000)
  27. /* Maximum possible setting for overflow */
  28. #define myTIM2_PERIOD ((uint32_t)0xFFFFFFFF)
  29.  
  30. void myGPIOA_Init(void);
  31. void myTIM2_Init(void);
  32. void myEXTI_Init(void);
  33. void ADCDACinit(void);
  34. void ADCDACvalues(void);
  35. void SPIinit(void);
  36. void LCDinit(void);
  37. void LCDwrite(uint8_t Data);
  38. void Commandwrite(uint8_t Data);
  39. void CommandSend(uint8_t Data);
  40. void spiLCDsend(uint8_t Data);
  41. void DataSend(uint8_t Data);
  42. void Converter();
  43.  
  44. double frequency = 0;
  45. double period = 0;
  46. unsigned int Res = 0;
  47. unsigned int delayVal = 50000;
  48. unsigned int Edgemaster = 1;
  49. uint8_t HexV[8];
  50.  
  51.  
  52. int main(int argc, char* argv[])
  53. {
  54. myGPIOA_Init();
  55. myTIM2_Init();
  56. myEXTI_Init();
  57. ADCDACinit();
  58. SPIinit();
  59. LCDinit();
  60.  
  61. ADC1->CR |= ADC_CR_ADSTART;
  62. while (1){
  63. ADCDACvalues();
  64. Converter();
  65. //R:
  66. DataSend(0x52);
  67. DataSend(0x3A);
  68. //R Values
  69. DataSend(HexV[0]);
  70. DataSend(HexV[1]);
  71. DataSend(HexV[2]);
  72. DataSend(HexV[3]);
  73. //Oh
  74. DataSend(0x4F);
  75. DataSend(0x68);
  76.  
  77. CommandSend(0xC0);
  78.  
  79. //F:
  80. DataSend(0x46);
  81. DataSend(0x3A);
  82. //F Values
  83. DataSend(HexV[4]);
  84. DataSend(HexV[5]);
  85. DataSend(HexV[6]);
  86. DataSend(HexV[7]);
  87. //Hz
  88. DataSend(0x48);
  89. DataSend(0x7A);
  90. CommandSend(0x02);
  91.  
  92. }
  93. return 0;
  94. }
  95.  
  96.  
  97.  
  98. //SPI Initialization
  99.  
  100.  
  101.  
  102. void SPIinit(){
  103. RCC->APB2ENR |= RCC_APB2ENR_SPI1EN; //spi clock
  104. SPI_InitTypeDef SPI_InitStructInfo;
  105. SPI_InitTypeDef* SPI_InitStruct = &SPI_InitStructInfo;
  106. SPI_InitStruct->SPI_Direction = SPI_Direction_1Line_Tx;
  107. SPI_InitStruct->SPI_Mode = SPI_Mode_Master;
  108. SPI_InitStruct->SPI_DataSize = SPI_DataSize_8b;
  109. SPI_InitStruct->SPI_CPOL = SPI_CPOL_Low;
  110. SPI_InitStruct->SPI_CPHA = SPI_CPHA_1Edge;
  111. SPI_InitStruct->SPI_NSS = SPI_NSS_Soft;
  112. SPI_InitStruct->SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
  113. SPI_InitStruct->SPI_FirstBit = SPI_FirstBit_MSB;
  114. SPI_InitStruct->SPI_CRCPolynomial = 7;
  115. SPI_Init(SPI1, SPI_InitStruct);
  116. SPI_Cmd(SPI1, ENABLE);
  117. }
  118.  
  119.  
  120.  
  121. //ADC and DAC stuff
  122.  
  123.  
  124.  
  125. void ADCDACinit(){
  126. RCC->APB2ENR |= RCC_APB2ENR_ADCEN; //ADC clock
  127. ADC1->CR |= ADC_CR_ADEN; //ADC enable
  128. ADC1->CFGR1 |= ADC_CFGR1_CONT; //Continuous config
  129. ADC1->CHSELR |= ADC_CHSELR_CHSEL5; //ADC out is PA5
  130. RCC->APB1ENR |= RCC_APB1ENR_DACEN; //clock enable
  131. GPIOA->MODER |= GPIO_MODER_MODER4; //PA4 is the output for DAC
  132. DAC->CR |= DAC_CR_EN1; //DAC enable
  133. }
  134.  
  135. void ADCDACvalues(){
  136. ADC1->CR |= ADC_CR_ADSTART; //conversion is a go
  137. Res = ADC1->DR*1.221;
  138. DAC->SWTRIGR |= DAC_SWTRIGR_SWTRIG1; //SW Trigger enable
  139. DAC->DHR12R1 = ADC1->DR; //ADC write to DAC
  140. }
  141.  
  142.  
  143.  
  144. //LCD stuff
  145.  
  146.  
  147.  
  148. void LCDinit(){ //Initializing according to slides
  149. int i = 480000;
  150. while (i)
  151. i--;
  152. CommandSend(0x02); //4bit
  153. CommandSend(0x28); //function set
  154. CommandSend(0x0C); //display on
  155. CommandSend(0x06); //entry mode
  156. CommandSend(0x01); //clear
  157. }
  158.  
  159. void Commandwrite(uint8_t Data){ //just writing stuff. Low high low
  160. int i;
  161. uint8_t High = Data | 0x80;
  162. uint8_t Low = Data | 0x00;
  163.  
  164. spiLCDsend(Low);
  165. i = delayVal;
  166. while (i)
  167. i--;
  168. spiLCDsend(High);
  169. i = delayVal;
  170. while (i)
  171. i--;
  172. spiLCDsend(Low);
  173. i = delayVal;
  174. while (i)
  175. i--;
  176. }
  177.  
  178. void LCDwrite(uint8_t Data){ //Just writing stuff. Low high low
  179. int i;
  180. uint8_t High = Data | 0xC0;
  181. uint8_t Low = Data | 0x40;
  182.  
  183. spiLCDsend(Low);
  184. i = delayVal;
  185. while (i)
  186. i--;
  187. spiLCDsend(High);
  188. i = delayVal;
  189. while (i)
  190. i--;
  191. spiLCDsend(Low);
  192. i = delayVal;
  193. while (i)
  194. i--;
  195. }
  196.  
  197.  
  198.  
  199. //SPI LCD thing.
  200.  
  201.  
  202.  
  203. void spiLCDsend(uint8_t Data){ //It's the Latch thing.
  204. GPIOB->BRR |= GPIO_BRR_BR_4; //Latch = 0
  205.  
  206. int i = delayVal;
  207. while (i)
  208. i--;
  209.  
  210. while ((SPI1->SR & SPI_SR_BSY)!= 0); //Busy loop
  211. SPI_SendData8(SPI1, Data);
  212. while ((SPI1->SR & SPI_SR_BSY)!= 0); //Ready loop
  213.  
  214. GPIOB->BSRR |= GPIO_BSRR_BS_4; //Latch = 1
  215.  
  216. i = delayVal;
  217. while (i)
  218. i--;
  219. }
  220.  
  221.  
  222.  
  223. //Sends
  224.  
  225.  
  226.  
  227. void CommandSend(uint8_t Data){ //separating into low and high
  228. uint8_t High = (Data & 0xF0) >> 4;
  229. uint8_t Low = (Data & 0x0F);
  230. Commandwrite(High);
  231. Commandwrite(Low);
  232. }
  233.  
  234. void DataSend(uint8_t Data){ //separating into low and high
  235. uint8_t High = (Data & 0xF0) >> 4;
  236. uint8_t Low = (Data & 0x0F);
  237. LCDwrite(High);
  238. LCDwrite(Low);
  239. }
  240.  
  241.  
  242.  
  243. //THE CONVERTER OF DOOM
  244.  
  245.  
  246.  
  247. void Converter(){
  248. //Resistance to Hex
  249. HexV[0] = (uint8_t)(Res/1000);
  250. HexV[1] = (uint8_t)((Res - (HexV[0] * 1000)) / 100);
  251. HexV[2] = (uint8_t)((Res - (HexV[0] * 1000) - (HexV[1] * 100)) / 10);
  252. HexV[3] = (uint8_t)(Res - (HexV[0] * 1000) - (HexV[1] * 100) - (HexV[2] * 10));
  253. //Frequency to Hex
  254. HexV[4] = (uint8_t)(frequency / 1000);
  255. HexV[5] = (uint8_t)((frequency - (HexV[4] * 1000)) / 100);
  256. HexV[6] = (uint8_t)((frequency - (HexV[4] * 1000) - (HexV[5] * 100)) / 10);
  257. HexV[7] = (uint8_t)(frequency - (HexV[4] * 1000) - (HexV[5] * 100) - (HexV[6] * 10));
  258. //Numbers to Hex
  259. for(int i=0; i<8; i++)
  260. {
  261. if(HexV[i] == 0)
  262. HexV[i] = 0x30;
  263. if(HexV[i] == 1)
  264. HexV[i] = 0x31;
  265. if(HexV[i] == 2)
  266. HexV[i] = 0x32;
  267. if(HexV[i] == 3)
  268. HexV[i] = 0x33;
  269. if(HexV[i] == 4)
  270. HexV[i] = 0x34;
  271. if(HexV[i] == 5)
  272. HexV[i] = 0x35;
  273. if(HexV[i] == 6)
  274. HexV[i] = 0x36;
  275. if(HexV[i] == 7)
  276. HexV[i] = 0x37;
  277. if(HexV[i] == 8)
  278. HexV[i] = 0x38;
  279. if (HexV[i] == 9)
  280. HexV[i] = 0x39;
  281. }
  282. }
  283.  
  284.  
  285.  
  286.  
  287. //Lab 1 stuff
  288.  
  289.  
  290.  
  291.  
  292. void myGPIOA_Init()
  293. {
  294.  
  295. /* Enable clock for GPIOA peripheral */
  296. // Relevant register: RCC->AHBENR
  297. RCC->AHBENR |= RCC_AHBENR_GPIOAEN;
  298.  
  299. GPIOA->MODER &= ~(GPIO_MODER_MODER6); //PA6 input
  300. GPIOA->PUPDR &= ~(GPIO_PUPDR_PUPDR6);
  301. GPIOA->MODER |= GPIO_MODER_MODER5; //PA5 analog
  302. GPIOA->PUPDR &= ~(GPIO_PUPDR_PUPDR5);
  303. GPIOA->MODER |= GPIO_MODER_MODER4; //PA4 analog
  304. GPIOA->PUPDR &= ~(GPIO_PUPDR_PUPDR4);
  305.  
  306. /* Enable clock for GPIOB peripheral */
  307. // Relevant register: RCC->AHBENR
  308. RCC->AHBENR |= RCC_AHBENR_GPIOBEN;
  309.  
  310. GPIOB->MODER |= GPIO_MODER_MODER3_1; //PB3 output
  311. GPIOB->AFR[2] &= ~(GPIO_AFRL_AFR3);
  312. GPIOB->MODER |= GPIO_MODER_MODER5_1; //PB5 output
  313. GPIOB->AFR[2] &= ~(GPIO_AFRL_AFR5);
  314. GPIOB->MODER |= GPIO_MODER_MODER4_0; //PB4 output
  315. GPIOB->PUPDR &= ~(GPIO_PUPDR_PUPDR4);
  316.  
  317.  
  318.  
  319.  
  320.  
  321. }
  322.  
  323.  
  324.  
  325. void myTIM2_Init()
  326. {
  327. /* Enable clock for TIM2 peripheral */
  328. // Relevant register: RCC->APB1ENR
  329. RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;
  330.  
  331. /* Configure TIM2: buffer auto-reload, count up, stop on overflow,
  332. enable update events, interrupt on overflow only */
  333. // Relevant register: TIM2->CR1
  334. TIM2->CR1 = ((uint16_t)0x008C);
  335.  
  336. /* Set clock prescaler value */
  337. TIM2->PSC = myTIM2_PRESCALER;
  338. /* Set auto-reloaded delay */
  339. TIM2->ARR = myTIM2_PERIOD;
  340.  
  341. /* Update timer registers */
  342. // Relevant register: TIM2->EGR
  343. TIM2->EGR = ((uint16_t)0x0001);
  344.  
  345. /* Assign TIM2 interrupt priority = 0 in NVIC */
  346. // Relevant register: NVIC->IP[3], or use NVIC_SetPriority
  347. NVIC_SetPriority(TIM2_IRQn, 0);
  348.  
  349. /* Enable TIM2 interrupts in NVIC */
  350. // Relevant register: NVIC->ISER[0], or use NVIC_EnableIRQ
  351. NVIC_EnableIRQ(TIM2_IRQn);
  352.  
  353. /* Enable update interrupt generation */
  354. // Relevant register: TIM2->DIER
  355. TIM2->DIER |= TIM_DIER_UIE;
  356.  
  357. /* Start counting timer pulses */
  358. TIM2->CR1 |= TIM_CR1_CEN;
  359.  
  360. }
  361.  
  362.  
  363. void myEXTI_Init()
  364. {
  365. /* map EXTI1 line to PA1 */
  366. // Relevant register: SYSCFG->EXTICR[0]
  367.  
  368. SYSCFG->EXTICR[1] = ((uint32_t)0x0000);
  369.  
  370. /* EXTI1 line interrupts: set rising-edge trigger */
  371. // Relevant register: EXTI->RTSR
  372.  
  373. EXTI->RTSR = EXTI_RTSR_TR1;
  374.  
  375. /* Unmask interrupts from EXTI1 line */
  376. // Relevant register: EXTI->IMR
  377.  
  378. EXTI->IMR = EXTI_IMR_MR1;
  379.  
  380. /* Assign EXTI1 interrupt priority = 0 in NVIC */
  381. // Relevant register: NVIC->IP[1], or use NVIC_SetPriority
  382.  
  383. NVIC_SetPriority(EXTI0_1_IRQn, 0);
  384.  
  385. /* Enable EXTI1 interrupts in NVIC */
  386. // Relevant register: NVIC->ISER[0], or use NVIC_EnableIRQ
  387. NVIC_EnableIRQ(EXTI0_1_IRQn);
  388. }
  389.  
  390.  
  391. void TIM2_IRQHandler(){
  392. /* Check if update interrupt flag is indeed set */
  393. if ((TIM2->SR & TIM_SR_UIF) != 0)
  394. {
  395. trace_printf("\n Overflow! \n");
  396.  
  397. // Clear update interrupt flag
  398. // Relevant register TIM2-SR
  399. TIM2->SR &= ~(TIM_SR_UIF);
  400.  
  401. //Restart stopped timer
  402. //Relevant register TIM2-CR1
  403. TIM2->CR1 = TIM_CR1_CEN;
  404. }
  405. }
  406.  
  407.  
  408. void EXTI0_1_IRQHandler(){
  409. // Is EXTI1 interrupt pending flag set?
  410. if ((EXTI->PR & EXTI_PR_PR1) != 0)
  411. {
  412. if (Edgemaster == 1){ // Check rising edge
  413. TIM2->CNT = ((uint32_t)0x00000000); // Clear count register
  414. TIM2->CR1 = ((uint32_t)0x00000001); // Start timer
  415. Edgemaster = 0; //rising edge 0
  416. }
  417. else{
  418. TIM2->CR1 = ((uint32_t)0x0); // Stop timer
  419.  
  420. // Calculate frequency and period
  421. period = (((double)TIM2->CNT / (double)SystemCoreClock) * 1000000);
  422. frequency = (1.0/period) * 1000000.0;
  423. Edgemaster= 1; //rising edge 1
  424. }
  425. EXTI->PR = EXTI_PR_PR1;
  426. }
  427. }
  428.  
  429.  
  430.  
  431. #pragma GCC diagnostic pop
  432.  
  433.  
  434. // ----------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement