4 Rotary encoders with 5110 LCD display. For Nucleo boards

Dependencies:   N5110 mbed

main.cpp

Committer:
triochi
Date:
2016-09-21
Revision:
4:cd50b7dfaf27
Parent:
3:bf5e17e09fe3
Child:
5:0daf19d95cc7

File content as of revision 4:cd50b7dfaf27:

#include "mbed.h"
#include "N5110.h"
#include "REnc.h"
//https://developer.mbed.org/users/mbed_official/code/mbed-src/file/c9b73cd93427/targets/hal/TARGET_STM/TARGET_STM32F4/TARGET_NUCLEO_F411RE/PeripheralPins.c

//DigitalOut red(LED1);
//DigitalOut blue(LED2);
//DigitalOut green(LED3);

int i;

//         VCC,SCE, RST, D/C, MOSI,SCLK,LED
N5110 lcd(PB_8,PA_4,PA_0,PA_1,PA_7,PA_5,PB_9);  //PA_4 and PA_6 not used
REnc encoder(PC_3, PC_2);
//PwmOut led(PA_3);
//PwmOut green(LED1);
int temperature = 50;
TIM_HandleTypeDef tim_init;

// Function declarations
void enable_tim(void);

void CCW_Handle(void)
{
    temperature--;
}
    
void CW_Handle(void)
{
    temperature++;
}
    

int main() {
     // first need to initialise display
        lcd.init();
        encoder.setHandleCCW(CCW_Handle);
        encoder.setHandleCC(CW_Handle);
        
        HAL_TIM_Base_MspInit(&tim_init); // initialize the TIM basic
        enable_tim();
        
  //      led.period_us(100);
    //    led = 20;
       // green = 0.5;
//        green.period_us(10000);
//        while(1) {
//            green = green + 0.01;
//            wait(0.2);
//            if(green == 1.0) {
//                green = 0;
//            }
//        }

//    while(1) {
//        for (i=1; i<7; i++) {
//            red = i & 1;
//            blue = i & 2;
//            green = i & 4;
//            wait(0.2);
//        }
//    }
    while(1) {
        
            HAL_TIM_Base_Start(&tim_init); // starts the timer

           // these are default settings so not strictly needed
           lcd.normalMode();      // normal colour mode
           lcd.setBrightness(0.5); // put LED backlight on 50%

           // can directly print strings at specified co-ordinates
           lcd.printString("Hello, World!",0,0);

           char buffer[14];  // each character is 6 pixels wide, screen is 84 pixels (84/6 = 14)
           // so can display a string of a maximum 14 characters in length
           // or create formatted strings - ensure they aren't more than 14 characters long
          // int temperature = encoder.getVal();
           int length = sprintf(buffer,"T = %2d C",temperature); // print formatted data to buffer
           // it is important the format specifier ensures the length will fit in the buffer
           if (length <= 14)  // if string will fit on display
               lcd.printString(buffer,0,1);           // display on screen

           float pressure = 1012.3;  // same idea with floats
           length = sprintf(buffer,"P = %.2f mb",pressure);
           if (length <= 14)
               lcd.printString(buffer,0,2);

           // can also print individual characters at specified place
           lcd.printChar('X',5,3);

           // draw a line across the display at y = 40 pixels (origin top-left)
           for (int i = 0; i < WIDTH; i++) {
               lcd.setPixel(i,40);
           }
           // need to refresh display after setting pixels
           lcd.refresh();

           // can also check status of pixels using getPixel(x,y)
           while(1)
           {               
                wait(.5);
               
                length = sprintf(buffer,"T = %2d C",temperature); // print formatted data to buffer
           
                if (length <= 14)  // if string will fit on display
                    lcd.printString(buffer,0,1);           // display on screen
           }
           lcd.clear();

       }
}

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
    if(htim->Instance == TIM1){
       // if(curr_sec == usec){
//            HAL_TIM_Base_Stop(&tim_init); // stop the timer it has been 15 useconds
//            // print the level of the signal to the console
//            curr_sec = 0;
//        }
//        else
//            curr_sec++;
    }
}

/*
 * Configures the timer to pin PA3.
 */
void enable_tim(void){
    tim_init.Instance = TIM1;
    tim_init.Init.CounterMode = TIM_COUNTERMODE_UP;
    tim_init.Init.Prescaler = 16 - 1; // 1,000,000 Hz -> 1/1,000,000 seconds
    tim_init.Init.Period = 0;
    tim_init.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; // for now...
    //tim_init.Init.RepetitionCounter = 0;
    __TIM1_CLK_ENABLE(); // pin PA8 - PA10
    // configure the GPIO PA3
    __GPIOA_CLK_ENABLE();
    GPIO_InitTypeDef GPIO_InitStruct;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pin = GPIO_PIN_8;  // TIM1_CH1
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF1_TIM1;    
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
    GPIO_InitStruct.Pin = GPIO_PIN_9;  // TIM1_CH2
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
    GPIO_InitStruct.Pin = GPIO_PIN_10;  // TIM1_CH3
    HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
    
    GPIO_InitStruct.Pin = GPIO_PIN_13;  // TIM1_CH1N
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
    GPIO_InitStruct.Pin = GPIO_PIN_14;  // TIM1_CH2N
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
    GPIO_InitStruct.Pin = GPIO_PIN_15;  // TIM1_CH3N
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
    
    HAL_NVIC_SetPriority(TIM1_UP_TIM10_IRQn, 0, 0); //Enable the peripheral IRQ
    HAL_NVIC_EnableIRQ(TIM1_UP_TIM10_IRQn);
    HAL_TIM_Base_Init(&tim_init);
    //HAL_TIM_Base_Start_IT(&tim_init); //Start the timer
}

void TIM1_IRQHandler(void) {
    HAL_TIM_IRQHandler(&tim_init);
}