Dimitar Marinov
/
Nucleo_4_encoders_w_Nokia5110
4 Rotary encoders with 5110 LCD display. For Nucleo boards
main.cpp@4:cd50b7dfaf27, 2016-09-21 (annotated)
- Committer:
- triochi
- Date:
- Wed Sep 21 14:37:29 2016 +0000
- Revision:
- 4:cd50b7dfaf27
- Parent:
- 3:bf5e17e09fe3
- Child:
- 5:0daf19d95cc7
Add some timer functions with TIM1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
triochi | 0:b7e471386653 | 1 | #include "mbed.h" |
triochi | 0:b7e471386653 | 2 | #include "N5110.h" |
triochi | 1:d7d729404013 | 3 | #include "REnc.h" |
triochi | 3:bf5e17e09fe3 | 4 | //https://developer.mbed.org/users/mbed_official/code/mbed-src/file/c9b73cd93427/targets/hal/TARGET_STM/TARGET_STM32F4/TARGET_NUCLEO_F411RE/PeripheralPins.c |
triochi | 0:b7e471386653 | 5 | |
triochi | 0:b7e471386653 | 6 | //DigitalOut red(LED1); |
triochi | 0:b7e471386653 | 7 | //DigitalOut blue(LED2); |
triochi | 0:b7e471386653 | 8 | //DigitalOut green(LED3); |
triochi | 3:bf5e17e09fe3 | 9 | |
triochi | 0:b7e471386653 | 10 | int i; |
triochi | 0:b7e471386653 | 11 | |
triochi | 0:b7e471386653 | 12 | // VCC,SCE, RST, D/C, MOSI,SCLK,LED |
triochi | 0:b7e471386653 | 13 | N5110 lcd(PB_8,PA_4,PA_0,PA_1,PA_7,PA_5,PB_9); //PA_4 and PA_6 not used |
triochi | 1:d7d729404013 | 14 | REnc encoder(PC_3, PC_2); |
triochi | 3:bf5e17e09fe3 | 15 | //PwmOut led(PA_3); |
triochi | 4:cd50b7dfaf27 | 16 | //PwmOut green(LED1); |
triochi | 1:d7d729404013 | 17 | int temperature = 50; |
triochi | 4:cd50b7dfaf27 | 18 | TIM_HandleTypeDef tim_init; |
triochi | 4:cd50b7dfaf27 | 19 | |
triochi | 4:cd50b7dfaf27 | 20 | // Function declarations |
triochi | 4:cd50b7dfaf27 | 21 | void enable_tim(void); |
triochi | 1:d7d729404013 | 22 | |
triochi | 1:d7d729404013 | 23 | void CCW_Handle(void) |
triochi | 1:d7d729404013 | 24 | { |
triochi | 1:d7d729404013 | 25 | temperature--; |
triochi | 1:d7d729404013 | 26 | } |
triochi | 1:d7d729404013 | 27 | |
triochi | 1:d7d729404013 | 28 | void CW_Handle(void) |
triochi | 1:d7d729404013 | 29 | { |
triochi | 1:d7d729404013 | 30 | temperature++; |
triochi | 1:d7d729404013 | 31 | } |
triochi | 1:d7d729404013 | 32 | |
triochi | 0:b7e471386653 | 33 | |
triochi | 0:b7e471386653 | 34 | int main() { |
triochi | 0:b7e471386653 | 35 | // first need to initialise display |
triochi | 0:b7e471386653 | 36 | lcd.init(); |
triochi | 1:d7d729404013 | 37 | encoder.setHandleCCW(CCW_Handle); |
triochi | 2:7d10aa2795c5 | 38 | encoder.setHandleCC(CW_Handle); |
triochi | 4:cd50b7dfaf27 | 39 | |
triochi | 4:cd50b7dfaf27 | 40 | HAL_TIM_Base_MspInit(&tim_init); // initialize the TIM basic |
triochi | 4:cd50b7dfaf27 | 41 | enable_tim(); |
triochi | 4:cd50b7dfaf27 | 42 | |
triochi | 3:bf5e17e09fe3 | 43 | // led.period_us(100); |
triochi | 3:bf5e17e09fe3 | 44 | // led = 20; |
triochi | 4:cd50b7dfaf27 | 45 | // green = 0.5; |
triochi | 4:cd50b7dfaf27 | 46 | // green.period_us(10000); |
triochi | 4:cd50b7dfaf27 | 47 | // while(1) { |
triochi | 4:cd50b7dfaf27 | 48 | // green = green + 0.01; |
triochi | 4:cd50b7dfaf27 | 49 | // wait(0.2); |
triochi | 4:cd50b7dfaf27 | 50 | // if(green == 1.0) { |
triochi | 4:cd50b7dfaf27 | 51 | // green = 0; |
triochi | 4:cd50b7dfaf27 | 52 | // } |
triochi | 4:cd50b7dfaf27 | 53 | // } |
triochi | 0:b7e471386653 | 54 | |
triochi | 0:b7e471386653 | 55 | // while(1) { |
triochi | 0:b7e471386653 | 56 | // for (i=1; i<7; i++) { |
triochi | 0:b7e471386653 | 57 | // red = i & 1; |
triochi | 0:b7e471386653 | 58 | // blue = i & 2; |
triochi | 0:b7e471386653 | 59 | // green = i & 4; |
triochi | 0:b7e471386653 | 60 | // wait(0.2); |
triochi | 0:b7e471386653 | 61 | // } |
triochi | 0:b7e471386653 | 62 | // } |
triochi | 0:b7e471386653 | 63 | while(1) { |
triochi | 4:cd50b7dfaf27 | 64 | |
triochi | 4:cd50b7dfaf27 | 65 | HAL_TIM_Base_Start(&tim_init); // starts the timer |
triochi | 0:b7e471386653 | 66 | |
triochi | 0:b7e471386653 | 67 | // these are default settings so not strictly needed |
triochi | 0:b7e471386653 | 68 | lcd.normalMode(); // normal colour mode |
triochi | 0:b7e471386653 | 69 | lcd.setBrightness(0.5); // put LED backlight on 50% |
triochi | 0:b7e471386653 | 70 | |
triochi | 0:b7e471386653 | 71 | // can directly print strings at specified co-ordinates |
triochi | 0:b7e471386653 | 72 | lcd.printString("Hello, World!",0,0); |
triochi | 0:b7e471386653 | 73 | |
triochi | 0:b7e471386653 | 74 | char buffer[14]; // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14) |
triochi | 0:b7e471386653 | 75 | // so can display a string of a maximum 14 characters in length |
triochi | 0:b7e471386653 | 76 | // or create formatted strings - ensure they aren't more than 14 characters long |
triochi | 1:d7d729404013 | 77 | // int temperature = encoder.getVal(); |
triochi | 0:b7e471386653 | 78 | int length = sprintf(buffer,"T = %2d C",temperature); // print formatted data to buffer |
triochi | 0:b7e471386653 | 79 | // it is important the format specifier ensures the length will fit in the buffer |
triochi | 0:b7e471386653 | 80 | if (length <= 14) // if string will fit on display |
triochi | 0:b7e471386653 | 81 | lcd.printString(buffer,0,1); // display on screen |
triochi | 0:b7e471386653 | 82 | |
triochi | 0:b7e471386653 | 83 | float pressure = 1012.3; // same idea with floats |
triochi | 0:b7e471386653 | 84 | length = sprintf(buffer,"P = %.2f mb",pressure); |
triochi | 0:b7e471386653 | 85 | if (length <= 14) |
triochi | 0:b7e471386653 | 86 | lcd.printString(buffer,0,2); |
triochi | 0:b7e471386653 | 87 | |
triochi | 0:b7e471386653 | 88 | // can also print individual characters at specified place |
triochi | 0:b7e471386653 | 89 | lcd.printChar('X',5,3); |
triochi | 0:b7e471386653 | 90 | |
triochi | 0:b7e471386653 | 91 | // draw a line across the display at y = 40 pixels (origin top-left) |
triochi | 0:b7e471386653 | 92 | for (int i = 0; i < WIDTH; i++) { |
triochi | 0:b7e471386653 | 93 | lcd.setPixel(i,40); |
triochi | 0:b7e471386653 | 94 | } |
triochi | 0:b7e471386653 | 95 | // need to refresh display after setting pixels |
triochi | 0:b7e471386653 | 96 | lcd.refresh(); |
triochi | 0:b7e471386653 | 97 | |
triochi | 0:b7e471386653 | 98 | // can also check status of pixels using getPixel(x,y) |
triochi | 1:d7d729404013 | 99 | while(1) |
triochi | 1:d7d729404013 | 100 | { |
triochi | 1:d7d729404013 | 101 | wait(.5); |
triochi | 1:d7d729404013 | 102 | |
triochi | 1:d7d729404013 | 103 | length = sprintf(buffer,"T = %2d C",temperature); // print formatted data to buffer |
triochi | 1:d7d729404013 | 104 | |
triochi | 1:d7d729404013 | 105 | if (length <= 14) // if string will fit on display |
triochi | 1:d7d729404013 | 106 | lcd.printString(buffer,0,1); // display on screen |
triochi | 1:d7d729404013 | 107 | } |
triochi | 0:b7e471386653 | 108 | lcd.clear(); |
triochi | 0:b7e471386653 | 109 | |
triochi | 0:b7e471386653 | 110 | } |
triochi | 0:b7e471386653 | 111 | } |
triochi | 4:cd50b7dfaf27 | 112 | |
triochi | 4:cd50b7dfaf27 | 113 | void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) { |
triochi | 4:cd50b7dfaf27 | 114 | if(htim->Instance == TIM1){ |
triochi | 4:cd50b7dfaf27 | 115 | // if(curr_sec == usec){ |
triochi | 4:cd50b7dfaf27 | 116 | // HAL_TIM_Base_Stop(&tim_init); // stop the timer it has been 15 useconds |
triochi | 4:cd50b7dfaf27 | 117 | // // print the level of the signal to the console |
triochi | 4:cd50b7dfaf27 | 118 | // curr_sec = 0; |
triochi | 4:cd50b7dfaf27 | 119 | // } |
triochi | 4:cd50b7dfaf27 | 120 | // else |
triochi | 4:cd50b7dfaf27 | 121 | // curr_sec++; |
triochi | 4:cd50b7dfaf27 | 122 | } |
triochi | 4:cd50b7dfaf27 | 123 | } |
triochi | 4:cd50b7dfaf27 | 124 | |
triochi | 4:cd50b7dfaf27 | 125 | /* |
triochi | 4:cd50b7dfaf27 | 126 | * Configures the timer to pin PA3. |
triochi | 4:cd50b7dfaf27 | 127 | */ |
triochi | 4:cd50b7dfaf27 | 128 | void enable_tim(void){ |
triochi | 4:cd50b7dfaf27 | 129 | tim_init.Instance = TIM1; |
triochi | 4:cd50b7dfaf27 | 130 | tim_init.Init.CounterMode = TIM_COUNTERMODE_UP; |
triochi | 4:cd50b7dfaf27 | 131 | tim_init.Init.Prescaler = 16 - 1; // 1,000,000 Hz -> 1/1,000,000 seconds |
triochi | 4:cd50b7dfaf27 | 132 | tim_init.Init.Period = 0; |
triochi | 4:cd50b7dfaf27 | 133 | tim_init.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; // for now... |
triochi | 4:cd50b7dfaf27 | 134 | //tim_init.Init.RepetitionCounter = 0; |
triochi | 4:cd50b7dfaf27 | 135 | __TIM1_CLK_ENABLE(); // pin PA8 - PA10 |
triochi | 4:cd50b7dfaf27 | 136 | // configure the GPIO PA3 |
triochi | 4:cd50b7dfaf27 | 137 | __GPIOA_CLK_ENABLE(); |
triochi | 4:cd50b7dfaf27 | 138 | GPIO_InitTypeDef GPIO_InitStruct; |
triochi | 4:cd50b7dfaf27 | 139 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
triochi | 4:cd50b7dfaf27 | 140 | GPIO_InitStruct.Pin = GPIO_PIN_8; // TIM1_CH1 |
triochi | 4:cd50b7dfaf27 | 141 | GPIO_InitStruct.Pull = GPIO_NOPULL; |
triochi | 4:cd50b7dfaf27 | 142 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH; |
triochi | 4:cd50b7dfaf27 | 143 | GPIO_InitStruct.Alternate = GPIO_AF1_TIM1; |
triochi | 4:cd50b7dfaf27 | 144 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
triochi | 4:cd50b7dfaf27 | 145 | GPIO_InitStruct.Pin = GPIO_PIN_9; // TIM1_CH2 |
triochi | 4:cd50b7dfaf27 | 146 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
triochi | 4:cd50b7dfaf27 | 147 | GPIO_InitStruct.Pin = GPIO_PIN_10; // TIM1_CH3 |
triochi | 4:cd50b7dfaf27 | 148 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); |
triochi | 4:cd50b7dfaf27 | 149 | |
triochi | 4:cd50b7dfaf27 | 150 | GPIO_InitStruct.Pin = GPIO_PIN_13; // TIM1_CH1N |
triochi | 4:cd50b7dfaf27 | 151 | HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); |
triochi | 4:cd50b7dfaf27 | 152 | GPIO_InitStruct.Pin = GPIO_PIN_14; // TIM1_CH2N |
triochi | 4:cd50b7dfaf27 | 153 | HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); |
triochi | 4:cd50b7dfaf27 | 154 | GPIO_InitStruct.Pin = GPIO_PIN_15; // TIM1_CH3N |
triochi | 4:cd50b7dfaf27 | 155 | HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); |
triochi | 4:cd50b7dfaf27 | 156 | |
triochi | 4:cd50b7dfaf27 | 157 | HAL_NVIC_SetPriority(TIM1_UP_TIM10_IRQn, 0, 0); //Enable the peripheral IRQ |
triochi | 4:cd50b7dfaf27 | 158 | HAL_NVIC_EnableIRQ(TIM1_UP_TIM10_IRQn); |
triochi | 4:cd50b7dfaf27 | 159 | HAL_TIM_Base_Init(&tim_init); |
triochi | 4:cd50b7dfaf27 | 160 | //HAL_TIM_Base_Start_IT(&tim_init); //Start the timer |
triochi | 4:cd50b7dfaf27 | 161 | } |
triochi | 4:cd50b7dfaf27 | 162 | |
triochi | 4:cd50b7dfaf27 | 163 | void TIM1_IRQHandler(void) { |
triochi | 4:cd50b7dfaf27 | 164 | HAL_TIM_IRQHandler(&tim_init); |
triochi | 4:cd50b7dfaf27 | 165 | } |