Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

PSoC™ 6 Forum Discussions

aliissa02
Level 4
5 likes given 25 replies posted 50 sign-ins
Level 4

Hello

I am using an SSD 1306 OLED screen and it's working perfectly. But the problem that when I move it to another function I only see one page?(I want to display the variable i that is in another function but I am not able to) I have attached the project. Can anyone help please?

Thank you,

Ali Issa

0 Likes
1 Solution
PandaS
Moderator
Moderator 100 sign-ins 25 solutions authored 10 likes received
Moderator

Hi Ali Issa,

I replicated your setup at my desk. I think that you have missed to call the function screen() which is newly defined. I was able to display "ANS" on one page and "i=4" on another page. I have slightly modified the code to do so. Please refer to my code and see the attached video of the output generated by me. Please confirm, if this was the expected output. You could also tell me the expected behavior you want, we can work on it. Let us know if there are any other issues as well.


Thanks and regards
Sobhit

View solution in original post

3 Replies
PandaS
Moderator
Moderator 100 sign-ins 25 solutions authored 10 likes received
Moderator

Hi Ali Issa,

I replicated your setup at my desk. I think that you have missed to call the function screen() which is newly defined. I was able to display "ANS" on one page and "i=4" on another page. I have slightly modified the code to do so. Please refer to my code and see the attached video of the output generated by me. Please confirm, if this was the expected output. You could also tell me the expected behavior you want, we can work on it. Let us know if there are any other issues as well.


Thanks and regards
Sobhit

aliissa02
Level 4
5 likes given 25 replies posted 50 sign-ins
Level 4

Thanks a lot for this. Its working. I am now trying to set the screen so it can display N/A when  the switch is not pressed and measurement when the switch is pressed. I have attached the project. Can you help please?

Thanks

0 Likes
PandaS
Moderator
Moderator 100 sign-ins 25 solutions authored 10 likes received
Moderator

Hi @aliissa02 ,

Please refer to the code snippet below and read through the comments for clarification. Whenever you want to write some value to GPIO Pin do not change its configuration. Configure the GPIO once, either Open Drain, Pull-Up Pull-Down or Strong Drive. It should be decided based on, your application and circuit schematics (check for LED connection).

Always make sure to take care of Global and Local Variables. Global variables can be accessed through out the program, but local variables are specific and limited to a single function. Also, when using local variables as parameters to a function please use either return values or pass by reference mechanism to fetch the variables after modified within the called function.

/* ========================================
 *
 * Copyright YOUR COMPANY, THE YEAR
 * All Rights Reserved
 * UNPUBLISHED, LICENSED SOFTWARE.
 *
 * CONFIDENTIAL AND PROPRIETARY INFORMATION
 * WHICH IS THE PROPERTY OF your company.
 *
 * ========================================
*/
#include "project.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#define DISPLAY 1
#include "ssd1306.h"
#define DISPLAY_ADDRESS 0x3C
float ReflectometerReading; // This globaal variable was not declared in your code, erlier it was i initiatialised within AD8307()
float threshold[2] = {1,1}; // You need to also define this variable - it was not there in yout code

//Leds
#define NUMBER_OF_PINS  5
GPIO_PRT_Type* led_Port[NUMBER_OF_PINS] = {P10_4_PORT,P10_6_PORT,P10_5_PORT,P10_1_PORT,P10_0_PORT};
uint8 led_pin[NUMBER_OF_PINS] = {P10_4_PIN,P10_6_PIN,P10_5_PIN,P10_1_PIN,P10_0_PIN};

//SW - Like LEDs you need to also declare variables for Switches
#define NUMBER_SW_PINS  2
GPIO_PRT_Type* SW_Port[NUMBER_SW_PINS] = {P10_3_PORT,P6_3_PORT};
uint8 SW_Pin[NUMBER_SW_PINS] = {P10_3_PIN,P6_3_PIN};

// Function Prototypes
void screen();
void init_LEDs();
void reset_LEDs();
void update_LEDs();
void scanKeys(int* x);

int main(void)
{
    __enable_irq(); /* Enable global interrupts. */
    
    char buffer[100];
        init_LEDs();
        I2COLED_Start();
        display_init(DISPLAY_ADDRESS);
        display_clear();
       
        gfx_setTextSize(2);
            gfx_setTextColor(WHITE);
            gfx_setCursor(0,5);
            sprintf(buffer, "ANS");
            gfx_println(buffer);
        display_update();
        CyDelay(3000);
        display_clear();
        update_LEDs();
        screen();
}   

void read_AD8307()
{
  ReflectometerReading=4;
}

void screen()
{
    read_AD8307();
    char buffer[100];
    gfx_setTextSize(2);
    gfx_setTextColor(WHITE);
    
    int x[2];
    
    while(1){
        scanKeys(x);
        if(!x[0]){
            gfx_setCursor(0,20);
            sprintf(buffer, "R = %f",ReflectometerReading);
            gfx_println(buffer);
            display_update();
            CyDelay(2000);
            
            display_clear();
            display_update();
        }
        else{
            gfx_setCursor(0,20);
            sprintf(buffer, "i = N/A");
            gfx_println(buffer);
            display_update();
            CyDelay(2000);
            
            display_clear();
            display_update();
        }
    }  
}

void update_LEDs(){
   reset_LEDs();
   //Led will light according to the threshold
   for (int l=0; l<NUMBER_OF_PINS;l++)
    {
        if(ReflectometerReading >= threshold[l])
        {
            Cy_GPIO_Write(led_Port[l], led_pin[l],1); // this is to turn on the led.
	    }
    }
}

void reset_LEDs(){
    //Turn off all LEDs
    for (int i=0; i< NUMBER_OF_PINS; i++){
        Cy_GPIO_Write(led_Port[i], led_pin[i],0); //This to make led turn off.
    }
}

void init_LEDs(){
    for (int l=0; l<NUMBER_OF_PINS;l++){
    Cy_GPIO_SetDrivemode(led_Port[l], led_pin[l], CY_GPIO_DM_STRONG);
    }
}

void scanKeys(int* x){
    for (int k=0; k<=NUMBER_SW_PINS; k++){
        x[k] = Cy_GPIO_Read(SW_Port[k], SW_Pin[k]); // here x will store what is status of switch port.
    }
}

Thanks and regards

Sobhit

0 Likes