Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // This file is part of the GNU ARM Eclipse distribution.
- // Copyright (c) 2014 Liviu Ionescu.
- //
- // ----------------------------------------------------------------------------
- // School: University of Victoria, Canada.
- // Course: CENG 355 "Microprocessor-Based Systems".
- //
- // See "system/include/cmsis/stm32f0xx.h" for register/bit definitions.
- // See "system/src/cmsis/vectors_stm32f0xx.c" for handler declarations.
- // ----------------------------------------------------------------------------
- #include <stdio.h>
- #include "diag/Trace.h"
- #include "cmsis/cmsis_device.h"
- #include "stm32f0xx_spi.h"
- // ----- main() ---------------------------------------------------------------
- // Sample pragmas to cope with warnings. Please note the related line at
- // the end of this function, used to pop the compiler diagnostics status.
- #pragma GCC diagnostic push
- #pragma GCC diagnostic ignored "-Wunused-parameter"
- #pragma GCC diagnostic ignored "-Wmissing-declarations"
- #pragma GCC diagnostic ignored "-Wreturn-type"
- /* Clock prescaler for TIM2 timer: no prescaling */
- #define myTIM2_PRESCALER ((uint16_t)0x0000)
- /* Maximum possible setting for overflow */
- #define myTIM2_PERIOD ((uint32_t)0xFFFFFFFF)
- void myGPIOA_Init(void);
- void myTIM2_Init(void);
- void myEXTI_Init(void);
- void ADCDACinit(void);
- void ADCDACvalues(void);
- void SPIinit(void);
- void LCDinit(void);
- void LCDwrite(uint8_t Data);
- void Commandwrite(uint8_t Data);
- void CommandSend(uint8_t Data);
- void spiLCDsend(uint8_t Data);
- void DataSend(uint8_t Data);
- void Converter();
- double frequency = 0;
- double period = 0;
- unsigned int Res = 0;
- unsigned int delayVal = 50000;
- unsigned int Edgemaster = 1;
- uint8_t HexV[8];
- int main(int argc, char* argv[])
- {
- myGPIOA_Init();
- myTIM2_Init();
- myEXTI_Init();
- ADCDACinit();
- SPIinit();
- LCDinit();
- ADC1->CR |= ADC_CR_ADSTART;
- while (1){
- ADCDACvalues();
- Converter();
- //R:
- DataSend(0x52);
- DataSend(0x3A);
- //R Values
- DataSend(HexV[0]);
- DataSend(HexV[1]);
- DataSend(HexV[2]);
- DataSend(HexV[3]);
- //Oh
- DataSend(0x4F);
- DataSend(0x68);
- CommandSend(0xC0);
- //F:
- DataSend(0x46);
- DataSend(0x3A);
- //F Values
- DataSend(HexV[4]);
- DataSend(HexV[5]);
- DataSend(HexV[6]);
- DataSend(HexV[7]);
- //Hz
- DataSend(0x48);
- DataSend(0x7A);
- CommandSend(0x02);
- }
- return 0;
- }
- //SPI Initialization
- void SPIinit(){
- RCC->APB2ENR |= RCC_APB2ENR_SPI1EN; //spi clock
- SPI_InitTypeDef SPI_InitStructInfo;
- SPI_InitTypeDef* SPI_InitStruct = &SPI_InitStructInfo;
- SPI_InitStruct->SPI_Direction = SPI_Direction_1Line_Tx;
- SPI_InitStruct->SPI_Mode = SPI_Mode_Master;
- SPI_InitStruct->SPI_DataSize = SPI_DataSize_8b;
- SPI_InitStruct->SPI_CPOL = SPI_CPOL_Low;
- SPI_InitStruct->SPI_CPHA = SPI_CPHA_1Edge;
- SPI_InitStruct->SPI_NSS = SPI_NSS_Soft;
- SPI_InitStruct->SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
- SPI_InitStruct->SPI_FirstBit = SPI_FirstBit_MSB;
- SPI_InitStruct->SPI_CRCPolynomial = 7;
- SPI_Init(SPI1, SPI_InitStruct);
- SPI_Cmd(SPI1, ENABLE);
- }
- //ADC and DAC stuff
- void ADCDACinit(){
- RCC->APB2ENR |= RCC_APB2ENR_ADCEN; //ADC clock
- ADC1->CR |= ADC_CR_ADEN; //ADC enable
- ADC1->CFGR1 |= ADC_CFGR1_CONT; //Continuous config
- ADC1->CHSELR |= ADC_CHSELR_CHSEL5; //ADC out is PA5
- RCC->APB1ENR |= RCC_APB1ENR_DACEN; //clock enable
- GPIOA->MODER |= GPIO_MODER_MODER4; //PA4 is the output for DAC
- DAC->CR |= DAC_CR_EN1; //DAC enable
- }
- void ADCDACvalues(){
- ADC1->CR |= ADC_CR_ADSTART; //conversion is a go
- Res = ADC1->DR*1.221;
- DAC->SWTRIGR |= DAC_SWTRIGR_SWTRIG1; //SW Trigger enable
- DAC->DHR12R1 = ADC1->DR; //ADC write to DAC
- }
- //LCD stuff
- void LCDinit(){ //Initializing according to slides
- int i = 480000;
- while (i)
- i--;
- CommandSend(0x02); //4bit
- CommandSend(0x28); //function set
- CommandSend(0x0C); //display on
- CommandSend(0x06); //entry mode
- CommandSend(0x01); //clear
- }
- void Commandwrite(uint8_t Data){ //just writing stuff. Low high low
- int i;
- uint8_t High = Data | 0x80;
- uint8_t Low = Data | 0x00;
- spiLCDsend(Low);
- i = delayVal;
- while (i)
- i--;
- spiLCDsend(High);
- i = delayVal;
- while (i)
- i--;
- spiLCDsend(Low);
- i = delayVal;
- while (i)
- i--;
- }
- void LCDwrite(uint8_t Data){ //Just writing stuff. Low high low
- int i;
- uint8_t High = Data | 0xC0;
- uint8_t Low = Data | 0x40;
- spiLCDsend(Low);
- i = delayVal;
- while (i)
- i--;
- spiLCDsend(High);
- i = delayVal;
- while (i)
- i--;
- spiLCDsend(Low);
- i = delayVal;
- while (i)
- i--;
- }
- //SPI LCD thing.
- void spiLCDsend(uint8_t Data){ //It's the Latch thing.
- GPIOB->BRR |= GPIO_BRR_BR_4; //Latch = 0
- int i = delayVal;
- while (i)
- i--;
- while ((SPI1->SR & SPI_SR_BSY)!= 0); //Busy loop
- SPI_SendData8(SPI1, Data);
- while ((SPI1->SR & SPI_SR_BSY)!= 0); //Ready loop
- GPIOB->BSRR |= GPIO_BSRR_BS_4; //Latch = 1
- i = delayVal;
- while (i)
- i--;
- }
- //Sends
- void CommandSend(uint8_t Data){ //separating into low and high
- uint8_t High = (Data & 0xF0) >> 4;
- uint8_t Low = (Data & 0x0F);
- Commandwrite(High);
- Commandwrite(Low);
- }
- void DataSend(uint8_t Data){ //separating into low and high
- uint8_t High = (Data & 0xF0) >> 4;
- uint8_t Low = (Data & 0x0F);
- LCDwrite(High);
- LCDwrite(Low);
- }
- //THE CONVERTER OF DOOM
- void Converter(){
- //Resistance to Hex
- HexV[0] = (uint8_t)(Res/1000);
- HexV[1] = (uint8_t)((Res - (HexV[0] * 1000)) / 100);
- HexV[2] = (uint8_t)((Res - (HexV[0] * 1000) - (HexV[1] * 100)) / 10);
- HexV[3] = (uint8_t)(Res - (HexV[0] * 1000) - (HexV[1] * 100) - (HexV[2] * 10));
- //Frequency to Hex
- HexV[4] = (uint8_t)(frequency / 1000);
- HexV[5] = (uint8_t)((frequency - (HexV[4] * 1000)) / 100);
- HexV[6] = (uint8_t)((frequency - (HexV[4] * 1000) - (HexV[5] * 100)) / 10);
- HexV[7] = (uint8_t)(frequency - (HexV[4] * 1000) - (HexV[5] * 100) - (HexV[6] * 10));
- //Numbers to Hex
- for(int i=0; i<8; i++)
- {
- if(HexV[i] == 0)
- HexV[i] = 0x30;
- if(HexV[i] == 1)
- HexV[i] = 0x31;
- if(HexV[i] == 2)
- HexV[i] = 0x32;
- if(HexV[i] == 3)
- HexV[i] = 0x33;
- if(HexV[i] == 4)
- HexV[i] = 0x34;
- if(HexV[i] == 5)
- HexV[i] = 0x35;
- if(HexV[i] == 6)
- HexV[i] = 0x36;
- if(HexV[i] == 7)
- HexV[i] = 0x37;
- if(HexV[i] == 8)
- HexV[i] = 0x38;
- if (HexV[i] == 9)
- HexV[i] = 0x39;
- }
- }
- //Lab 1 stuff
- void myGPIOA_Init()
- {
- /* Enable clock for GPIOA peripheral */
- // Relevant register: RCC->AHBENR
- RCC->AHBENR |= RCC_AHBENR_GPIOAEN;
- GPIOA->MODER &= ~(GPIO_MODER_MODER6); //PA6 input
- GPIOA->PUPDR &= ~(GPIO_PUPDR_PUPDR6);
- GPIOA->MODER |= GPIO_MODER_MODER5; //PA5 analog
- GPIOA->PUPDR &= ~(GPIO_PUPDR_PUPDR5);
- GPIOA->MODER |= GPIO_MODER_MODER4; //PA4 analog
- GPIOA->PUPDR &= ~(GPIO_PUPDR_PUPDR4);
- /* Enable clock for GPIOB peripheral */
- // Relevant register: RCC->AHBENR
- RCC->AHBENR |= RCC_AHBENR_GPIOBEN;
- GPIOB->MODER |= GPIO_MODER_MODER3_1; //PB3 output
- GPIOB->AFR[2] &= ~(GPIO_AFRL_AFR3);
- GPIOB->MODER |= GPIO_MODER_MODER5_1; //PB5 output
- GPIOB->AFR[2] &= ~(GPIO_AFRL_AFR5);
- GPIOB->MODER |= GPIO_MODER_MODER4_0; //PB4 output
- GPIOB->PUPDR &= ~(GPIO_PUPDR_PUPDR4);
- }
- void myTIM2_Init()
- {
- /* Enable clock for TIM2 peripheral */
- // Relevant register: RCC->APB1ENR
- RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;
- /* Configure TIM2: buffer auto-reload, count up, stop on overflow,
- enable update events, interrupt on overflow only */
- // Relevant register: TIM2->CR1
- TIM2->CR1 = ((uint16_t)0x008C);
- /* Set clock prescaler value */
- TIM2->PSC = myTIM2_PRESCALER;
- /* Set auto-reloaded delay */
- TIM2->ARR = myTIM2_PERIOD;
- /* Update timer registers */
- // Relevant register: TIM2->EGR
- TIM2->EGR = ((uint16_t)0x0001);
- /* Assign TIM2 interrupt priority = 0 in NVIC */
- // Relevant register: NVIC->IP[3], or use NVIC_SetPriority
- NVIC_SetPriority(TIM2_IRQn, 0);
- /* Enable TIM2 interrupts in NVIC */
- // Relevant register: NVIC->ISER[0], or use NVIC_EnableIRQ
- NVIC_EnableIRQ(TIM2_IRQn);
- /* Enable update interrupt generation */
- // Relevant register: TIM2->DIER
- TIM2->DIER |= TIM_DIER_UIE;
- /* Start counting timer pulses */
- TIM2->CR1 |= TIM_CR1_CEN;
- }
- void myEXTI_Init()
- {
- /* map EXTI1 line to PA1 */
- // Relevant register: SYSCFG->EXTICR[0]
- SYSCFG->EXTICR[1] = ((uint32_t)0x0000);
- /* EXTI1 line interrupts: set rising-edge trigger */
- // Relevant register: EXTI->RTSR
- EXTI->RTSR = EXTI_RTSR_TR1;
- /* Unmask interrupts from EXTI1 line */
- // Relevant register: EXTI->IMR
- EXTI->IMR = EXTI_IMR_MR1;
- /* Assign EXTI1 interrupt priority = 0 in NVIC */
- // Relevant register: NVIC->IP[1], or use NVIC_SetPriority
- NVIC_SetPriority(EXTI0_1_IRQn, 0);
- /* Enable EXTI1 interrupts in NVIC */
- // Relevant register: NVIC->ISER[0], or use NVIC_EnableIRQ
- NVIC_EnableIRQ(EXTI0_1_IRQn);
- }
- void TIM2_IRQHandler(){
- /* Check if update interrupt flag is indeed set */
- if ((TIM2->SR & TIM_SR_UIF) != 0)
- {
- trace_printf("\n Overflow! \n");
- // Clear update interrupt flag
- // Relevant register TIM2-SR
- TIM2->SR &= ~(TIM_SR_UIF);
- //Restart stopped timer
- //Relevant register TIM2-CR1
- TIM2->CR1 = TIM_CR1_CEN;
- }
- }
- void EXTI0_1_IRQHandler(){
- // Is EXTI1 interrupt pending flag set?
- if ((EXTI->PR & EXTI_PR_PR1) != 0)
- {
- if (Edgemaster == 1){ // Check rising edge
- TIM2->CNT = ((uint32_t)0x00000000); // Clear count register
- TIM2->CR1 = ((uint32_t)0x00000001); // Start timer
- Edgemaster = 0; //rising edge 0
- }
- else{
- TIM2->CR1 = ((uint32_t)0x0); // Stop timer
- // Calculate frequency and period
- period = (((double)TIM2->CNT / (double)SystemCoreClock) * 1000000);
- frequency = (1.0/period) * 1000000.0;
- Edgemaster= 1; //rising edge 1
- }
- EXTI->PR = EXTI_PR_PR1;
- }
- }
- #pragma GCC diagnostic pop
- // ----------------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement