Dimitar Marinov / Mbed 2 deprecated Nucleo_4_encoders_w_Nokia5110

Dependencies:   N5110 mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /*
00002  * Using STM32's counter peripherals to interface rotary encoders.
00003  * Encoders are supported on F4xx's TIM1,2,3,4,5. TIM2 & TIM5 have 32bit count, others 16bit.
00004  * Beware mbed uses TIM5 for system timer, SPI needs TIM1, others used for PWM.
00005  * Check your platform's PeripheralPins.c & PeripheralNames.h if you need both PWM & encoders.
00006  *
00007  * Edit HAL_TIM_Encoder_MspInitFx.cpp to suit your mcu & board's available pinouts & pullups/downs.
00008  *
00009  * Thanks to:
00010  * http://petoknm.wordpress.com/2015/01/05/rotary-encoder-and-stm32/
00011  *
00012  * References:
00013  * http://www.st.com/st-web-ui/static/active/en/resource/technical/document/user_manual/DM00122015.pdf
00014  * http://www.st.com/st-web-ui/static/active/en/resource/technical/document/reference_manual/DM00096844.pdf
00015  * http://www.st.com/web/en/resource/technical/document/application_note/DM00042534.pdf
00016  * http://www.st.com/web/en/resource/technical/document/datasheet/DM00102166.pdf
00017  * 
00018  * David Lowe Jan 2015
00019  */
00020 
00021 // Port definitions:
00022 // https://developer.mbed.org/users/mbed_official/code/mbed-src/file/c9b73cd93427/targets/hal/TARGET_STM/TARGET_STM32F4/TARGET_NUCLEO_F411RE/PeripheralPins.c
00023 
00024 #include "mbed.h"
00025 #include "N5110.h"
00026 #include "Encoder.h"
00027 
00028 //STM mbed bug: these macros are MISSING from stm32f3xx_hal_tim.h
00029 #ifdef TARGET_STM32F3
00030 #define __HAL_TIM_GET_COUNTER(__HANDLE__) ((__HANDLE__)->Instance->CNT)
00031 #define __HAL_TIM_IS_TIM_COUNTING_DOWN(__HANDLE__)            (((__HANDLE__)->Instance->CR1 &(TIM_CR1_DIR)) == (TIM_CR1_DIR))
00032 #endif
00033 
00034 TIM_Encoder_InitTypeDef encoder1, encoder2, encoder3, encoder4, encoder5;
00035 TIM_HandleTypeDef  timer1,  timer2,  timer3,  timer4,  timer5;
00036 
00037 
00038 //DigitalOut red(LED1);
00039 //DigitalOut blue(LED2);
00040 //DigitalOut green(LED3);
00041 int i;
00042 
00043 //pwrPin, scePin, rstPin, dcPin, mosiPin, sclkPin, ledPin)
00044 
00045 
00046 //         VCC,SCE, RST, D/C, MOSI,SCLK,LED
00047 
00048 //         N.C.,SCE, RST, D/C, MOSI,SCLK,N.C
00049 N5110 lcd(PA_4,PA_4,PB_14,PB_9,PB_15,PB_13,PA_4);  //PA_4 and PA_6 not used
00050 
00051 int temperature = 50;
00052 
00053 int main() {
00054     uint16_t count1=0, count3=0, count4=0;
00055     uint32_t count2=0;
00056     int8_t dir1, dir2, dir3, dir4;
00057     
00058     char buffer[16];  // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)
00059     // so can display a string of a maximum 14 characters in length
00060     // or create formatted strings - ensure they aren't more than 14 characters long
00061     // first need to initialise display
00062     lcd.init();
00063     lcd.printString("**STM Nucleo**",0,0);
00064     //examples
00065     
00066     //counting on A-input only, 2 ticks per cycle, rolls over at 100
00067     //EncoderInit(&encoder1, &timer1, TIM1, 99, TIM_ENCODERMODE_TI1);
00068     EncoderInit(&encoder1, &timer1, TIM1, 3600, TIM_ENCODERMODE_TI12);
00069 
00070     //counting on both A&B inputs, 4 ticks per cycle, full 32-bit count
00071 //    EncoderInit(&encoder2, &timer2, TIM2, 0xffffffff, TIM_ENCODERMODE_TI12);
00072     EncoderInit(&encoder2, &timer2, TIM2, 3600, TIM_ENCODERMODE_TI12);
00073 
00074     //counting on B-input only, 2 ticks per cycle, full 16-bit count
00075 //    EncoderInit(&encoder3, &timer3, TIM3, 0xffff, TIM_ENCODERMODE_TI2);
00076     EncoderInit(&encoder3, &timer3, TIM3, 3600, TIM_ENCODERMODE_TI12);
00077     
00078     //counting on both A&B inputs, 4 ticks per cycle, full 16-bit count
00079     //EncoderInit(&encoder4, &timer4, TIM4, 0xffff, TIM_ENCODERMODE_TI12);
00080     EncoderInit(&encoder4, &timer4, TIM4, 3600, TIM_ENCODERMODE_TI12);
00081     
00082     //TIM5 is used by mbed for systick
00083 //    EncoderInit(&encoder5, &timer5, TIM5, 0xffffffff, TIM_ENCODERMODE_TI12);
00084         
00085 //    printf("STM HAL encoder demo\n\r");
00086 
00087     // these are default settings so not strictly needed
00088     lcd.normalMode();      // normal colour mode
00089     lcd.setBrightness(0.7); // put LED backlight on 50%
00090    // __HAL_TIM_SET_COUNTER(&timer2,0);
00091 
00092     // can directly print strings at specified co-ordinates
00093 //    lcd.printString("**STM Nucleo**",0,0);
00094     while(1) 
00095     {                
00096         //OK 401 411 446 TICKER 030
00097         //count1=TIM1->CNT;
00098         //dir1=TIM1->CR1&TIM_CR1_DIR;
00099         count1=__HAL_TIM_GET_COUNTER(&timer1);
00100         dir1 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer1);
00101 
00102         //OK 401 411 446 NOK 030
00103         //count2=TIM2->CNT;
00104         //dir2=TIM2->CR1&TIM_CR1_DIR;
00105         count2=__HAL_TIM_GET_COUNTER(&timer2);
00106         dir2 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer2);
00107 
00108         //OK 401 411 446 030
00109         //count3=TIM3->CNT;
00110         //dir3=TIM3->CR1&TIM_CR1_DIR;
00111         count3=__HAL_TIM_GET_COUNTER(&timer3);
00112         dir3 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer3);
00113 
00114         //OK 401 411 446 N/A 030
00115         //count4=TIM4->CNT;
00116         //dir4=TIM4->CR1&TIM_CR1_DIR;
00117         count4=__HAL_TIM_GET_COUNTER(&timer4);
00118         dir4 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer4);
00119 
00120         //TICKER 401 411 446 N/A 030
00121 //        count5=__HAL_TIM_GET_COUNTER(&timer5);
00122 //        dir5 = __HAL_TIM_IS_TIM_COUNTING_DOWN(&timer5);
00123 //        printf("%d%s %d%s %d%s %d%s\r\n", count1, dir1==0 ? "+":"-",
00124 //                                             count2, dir2==0 ? "+":"-",
00125 //                                             count3, dir3==0 ? "+":"-",
00126 //                                             count4, dir4==0 ? "+":"-" );
00127 
00128         // int temperature = encoder.getVal();
00129         int length = sprintf(buffer,"C1 = %06d ",count1); // print formatted data to buffer
00130         // it is important the format specifier ensures the length will fit in the buffer
00131         
00132         lcd.printString(buffer,1,1);           // display on screen
00133         sprintf(buffer,"C2 = %06d ",count2); // print formatted data to buffer
00134         lcd.printString(buffer,1,2);           // display on screen
00135         sprintf(buffer,"C3 = %06d ",count3); // print formatted data to buffer
00136         lcd.printString(buffer,1,3);           // display on screen
00137         sprintf(buffer,"C4 = %06d ",count4); // print formatted data to buffer
00138         lcd.printString(buffer,1,4);           // display on screen
00139         
00140         // can also print individual characters at specified place
00141         //lcd.printChar('X',5,3);
00142 
00143         // draw a line across the display at y = 40 pixels (origin top-left)
00144         for (int i = 0; i < WIDTH; i++) 
00145         {
00146             lcd.setPixel(i,50);
00147         }
00148         // need to refresh display after setting pixels
00149         lcd.refresh();
00150         wait(.5);
00151 
00152         // can also check status of pixels using getPixel(x,y)
00153         
00154 //        lcd.clear();
00155     }
00156 }