CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

SSD1306 OLED menu interfacing with button and LED using PIC
Goto page Previous  1, 2
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Sun Jun 23, 2019 11:57 am     Reply with quote

The thing that has me really confused is he seems to be trying to output
to the LED's on the same pins he is testing for the switches....
sradhika1998



Joined: 21 Jun 2019
Posts: 12

View user's profile Send private message AIM Address

PostPosted: Mon Jun 24, 2019 2:33 am     Reply with quote

Mike Walne wrote:
Quote:
If I just go with m2==2 then program get confuse with which menu it will go with, either ABC, DEF or GHI

Guide me what syntax do I use in it..
Awaiting your response.

You are the one confused, not the program.
I'm also confused, and can't figure out what you are trying to do or how you are trying to achieve it.

This is a help you forum NOT a do it for you one.

To help us:-

Explain clearly what you are trying to do.
Provide a current diagram or list of relevant connections.
Supply SHORT complete compilable code which shows your problem.
I.e. so that we can copy and paste to test it.

You appear to be struggling with a switch/LED menu loop.
That's all you need to include.
Leave out ALL other code, and I mean remove it, don't just comment it out.
It will then be much easier for us all to help you.

Mike


Here is the small code.
Code:
#include <18F4520.h>
#include <stdint.h>

#define SSD1306_RST   PIN_D4

#fuses HS,NOWDT,NOPROTECT,NOLVP                       
#use delay(clock=8000000)
#use I2C(MASTER, I2C1, FAST = 400000, stream = SSD1306_STREAM)  // Initialize I2C

#define sclk PIN_D2
#define sdata PIN_D3
#define cs1 PIN_D1
#define cs2 PIN_D0


// Include SSD1306 OLED driver source code
#include <SSD1306.c>
//#include <SSD1306OLED.c>

static int8 i = 0, j=1;
volatile unsigned long button_select, button_up, button_down;
uint8_t row_Ind, col_Ind;
int m1, m2, m3, m4, m5, m6, m7, m8, m9;// menu select index 
uint8_t start_row;
unsigned int pwm1;
void SendPotValue (unsigned short num);
void senddac1 (unsigned int dac1);
void senddac2 (unsigned int dac2);
char *ADC = "0000";

unsigned int aa;
const char MESSAGE[] = "VOLUME:";

uint8_t const starting_row = 1;
uint8_t const starting_col = 3;
int spkr = 0;


void cursor(uint8_t index);  // move cursor to the xth row and first column
void menu1 ();
void charging_s1();
void charging_AC();
void charging_Solr();
void discharging_s1();
void discharging_USB();
void discharging_DC();
void discharging_AC();
void discharging_Spkr();
void spkr_Vol();
void menu1_Back();
void charging_s1_Back();
void discharging_s1_Back();
void spkr_Back();
void date_s1();
void time_s1();
void alarm_s1();
void led_s1();
void backlight_s1();
void date_setting();
void time_setting();
void alarm_setting();
void scroll_down();
void scroll_up();
void ADC_Display(unsigned short, unsigned short);
void Debounce_Key();
void set();

void main() {
 
  delay_ms(200);
  // initilize variables
  m1 = 0; m2 = 0; m3 = 0, m4 =0, m5=0, m6=0, m7=0, m8=0, m9=0 ;
  start_row = 1;
  row_Ind = 1; //row index
  col_Ind = 3; //column index
 
 
  // Initialize the SSD1306 OLED with an I2C addr = 0x7A (default address)
  SSD1306_Init(SSD1306_SWITCHCAPVCC, SSD1306_I2C_ADDRESS); 
  // clear the display
  SSD1306_ClearDisplay();
  SSD1306_GotoXY(6, 4); //XCugas Power
  SSD1306_PutC("XCugas Power");
  delay_ms(400); // 4 seconds delay after XCugas Power

  // coding for selecting and scrolling through menus
  set_tris_d(0x8c); // set direction registers of bits RD2,RD3,RD4 to be input button RD2
  menu1();
   
   
  do {
      delay_ms(100);
      button_select = input (PIN_B5);
      button_up     = input (PIN_B6);
      button_down   = input (PIN_B7);
       
      if (button_down)
        {
         
          scroll_down(); delay_ms(100);
        }
      if (button_up)
        {
         
          scroll_up(); delay_ms(100);
        }   
     
     if (button_select)
       {
          delay_ms(50);
          if (m1==0 && m2==0 && m5==0 && m6==0 && m7==0 && m8==0 && m9==0){

              if (row_Ind == 1) {
                charging_s1(); m1++; delay_ms(100);}
              else if (row_Ind == 2){
                discharging_s1(); m2++; m4++; delay_ms(100);}
              else if (row_Ind == 3){
                time_s1(); m5++;         delay_ms(100);
                                          }
              else if (row_Ind == 4){
                date_s1();      m6++;   delay_ms(100);
                                          }
              else if (row_Ind == 5){
                alarm_s1();   m7++;     delay_ms(100);
                                          }
              else if (row_Ind == 6){
                led_s1();    m8++;     delay_ms(100);
                                          }
              else if (row_Ind == 7){
                backlight_s1();   m9++;     delay_ms(100);
                                          }
              else delay_ms(100);
             }   
            // main menu charging, discharging
         else if (m1==1) { 
               if (row_Ind == 1 ) {
                   charging_AC();  m1++; delay_ms(100);}
               else if (row_Ind == 2) {
                  charging_Solr(); m1++; delay_ms(100); } 
               else if (row_Ind == 3){
                  menu1_Back();  m1=0; delay_ms(100);}
               else delay_ms(100);
            } 
             //discharging menu
         else if (m2==1){

               if (row_Ind == 1) {

                   discharging_USB(); m2++; delay_ms(100);  }
 
               else if (row_Ind == 2){
                   discharging_DC(); m2++; delay_ms(100);  } 

               else if (row_Ind == 3){
                   discharging_AC();  m2++; delay_ms(100); }
               else if (row_Ind == 4){
                   discharging_Spkr(); m2++; spkr++; delay_ms(100);} 
               else if (row_Ind == 5){
                   menu1_Back(); m2 =0; m4=0; delay_ms(100);} 
               else  delay_ms(100);
            }
        // main menu Time Setting
         else if (m5==1) { 
               if (row_Ind == 1 ) {
                   time_s1();  m5++; delay_ms(100);}
               else if (row_Ind == 2) {
                  menu1_Back();  m5=0; delay_ms(100);}
               else delay_ms(100);
            } 
        // main menu Date Setting
         else if (m6==1) { 
               if (row_Ind == 1 ) {
                   date_s1();  m6++; delay_ms(100);}
               else if (row_Ind == 2) {
                  menu1_Back();  m6=0; delay_ms(100);}
               else delay_ms(100);
            }
        // main menu Alarm Setting
         else if (m7==1) { 
               if (row_Ind == 1 ) {
                   alarm_s1();  m7++; delay_ms(100);}
               else if (row_Ind == 2) {
                  menu1_Back();  m7=0;  delay_ms(100);}
               else delay_ms(100);
            }
         // main menu LED Setting
         else if (m8==1) { 
               if (row_Ind == 1 ) {
                   led_s1();  m8++; delay_ms(100);}
               else if (row_Ind == 3) {
                  menu1_Back();  m8=0;  delay_ms(100);}
               else delay_ms(100);
            }
          // main menu Backlight Setting
         else if (m9==1) { 
               if (row_Ind == 1 ) {
                   backlight_s1();  m9++; delay_ms(100);}
               else if (row_Ind == 3) {
                  menu1_Back();  m9=0; delay_ms(100);}
               else delay_ms(100);
            }
         else if (m1==2) { // back from charging_AC menu

              if (row_Ind == 3) {
                   charging_s1_Back(); m1--; delay_ms(100);} 
            }
         else if (m2==2) { // back from discharging_USB & discharging_Spkr menu
                if (row_Ind==3 && spkr==1){
                    spkr_Vol(); m2++; delay_ms(100); }
                else if (row_Ind==3){ // back from discharging_AC
                    discharging_s1_Back(); m2--; delay_ms(100);}
                else if (row_Ind == 4) {
                   discharging_s1_Back(); m2--; delay_ms(100);}
                else if (row_Ind==8) { // back from discharging_DC
                   discharging_s1_Back(); m2--; delay_ms(100); }   
           }
           //{
           //if(m2==1 && row_Ind==3 && button_select==0) {
           //m2++;
           //button_select==1;
           //if (row_Ind==3 && m2==2){discharging_s1_Back(); m2--; delay_ms(100);} } //Ac Output
           //else if (m2==1 && row_Ind == 4 && button_select==0) {if (row_Ind ==3 && spkr ==1){spkr_Vol(); m2++; delay_ms(100); }} //speaker
            //else if (row_Ind == 4) {
             //      discharging_s1_Back(); m2--; delay_ms(100);} //USB
               // else if (row_Ind==8) { // back from discharging_DC
                //   discharging_s1_Back(); m2--; delay_ms(100); }
           //}
           
        // else if (m2==2 && spkr==1){ // move to volume menu
//
  //              if (row_Ind==3){
    //                spkr_Vol(); m2++; delay_ms(100); }
      //          else if (row_Ind == 4){ //back from speaker menu
        //            discharging_s1_back(); spkr--; m2--; delay_ms(100);}
          // }     
         else if (m2==3) { // back from spkr_Vol() menu

               if(row_Ind == 4){
                  spkr_Back(); m2--; spkr--;  delay_ms(100);}
           }
         else { menu1_Back(); m1=0; m2=0; m5=0; m6=0; m7=0; m8=0; m9=0;}
       }
     } while(1);               
}

  void cursor(uint8_t index) {

       for (uint8_t i=1; i<9; i++){
           if (i == index) {
           SSD1306_GotoXY(1, index);
           SSD1306_PutC(">");
           delay_ms(5);
             continue;
           }
          SSD1306_GotoXY(1, i);
          SSD1306_PutC(" ");
          delay_ms(5); }
      delay_ms(50);
     }
  void menu1 () {
       
         SSD1306_ClearDisplay();
         cursor(starting_row);
         SSD1306_GotoXY(starting_col, starting_row);
         SSD1306_PutC("1. Charging");
         delay_ms(100);
         
         start_row +=1;
         SSD1306_GotoXY(starting_col, start_row);
         SSD1306_PutC("2. Discharging");
         delay_ms(100);   
         
         start_row +=1;
         SSD1306_GotoXY(starting_col, start_row);
         SSD1306_PutC("3. Time Setting");
         delay_ms(100);   
         
         start_row +=1;
         SSD1306_GotoXY(starting_col, start_row);
         SSD1306_PutC("4. Date Setting");
         delay_ms(100);   
         
         start_row +=1;
         SSD1306_GotoXY(starting_col, start_row);
         SSD1306_PutC("5. Alarm Setting");
         delay_ms(100);   
         
         start_row +=1;
         SSD1306_GotoXY(starting_col, start_row);
         SSD1306_PutC("6. LED Setting");
         delay_ms(100);   
         
         start_row +=1;
         SSD1306_GotoXY(starting_col, start_row);
         SSD1306_PutC("7. Backlight ");
         delay_ms(100);
         row_Ind = 1;
     }
  void charging_s1() {
     
        SSD1306_ClearDisplay();
        cursor(starting_row);
        SSD1306_GotoXY(starting_col, starting_row);
        SSD1306_PutC("1. AC Input");
        delay_ms(10);
        start_row = starting_row;
        start_row +=1;
        SSD1306_GotoXY(starting_col, start_row);
        SSD1306_PutC("2. Solar Input");
        delay_ms(10);
        start_row +=1;
        SSD1306_GotoXY(starting_col, start_row);
        SSD1306_PutC("3. Back");
        delay_ms(100);
        row_Ind = 1;
       
  }
  void discharging_s1() {
       
        SSD1306_ClearDisplay();
        cursor(starting_row);
        SSD1306_GotoXY(starting_col, starting_row);
        SSD1306_PutC("1. USB Output");
        delay_ms(10);
        start_row = starting_row;
        start_row +=1;
        SSD1306_GotoXY(starting_col, start_row);
        SSD1306_PutC("2. DC Output");
        delay_ms(10);
        start_row +=1;
        SSD1306_GotoXY(starting_col, start_row);
        SSD1306_PutC("3. AC Output");
        delay_ms(10);
        start_row +=1;
        SSD1306_GotoXY(starting_col, start_row);
        SSD1306_PutC("4. Speaker");
        delay_ms(10);
        start_row +=1;
        SSD1306_GotoXY(starting_col, start_row);
        SSD1306_PutC("5. Back");
        delay_ms(100); 
        row_Ind = 1;   
  }

void charging_AC() {
   
   
        SSD1306_ClearDisplay();
        cursor(starting_row);
        SSD1306_GotoXY(starting_col, starting_row);
        SSD1306_PutC("1. ON");
        delay_ms(10);
        start_row = starting_row;                       
        start_row +=j;
        SSD1306_GotoXY(starting_col, start_row);
        SSD1306_PutC("2. OFF");
        delay_ms(10);
        cursor(start_row);
        start_row +=j;
        SSD1306_GotoXY(starting_col, start_row);
        SSD1306_PutC("3. Back");
        delay_ms(100);}
void discharging_USB() {
       
        SSD1306_ClearDisplay();
        cursor(starting_row);
        SSD1306_GotoXY(starting_col, starting_row);
        SSD1306_PutC("1. 5V 1A");
        delay_ms(10);
        start_row = starting_row;
        start_row +=1;
        SSD1306_GotoXY(starting_col, start_row);
        SSD1306_PutC("2. 5V 2A");
        delay_ms(10);
        start_row +=1;
        SSD1306_GotoXY(starting_col, start_row);
        SSD1306_PutC("3. 5V 3A");
        delay_ms(10);
        start_row +=1;
        SSD1306_GotoXY(starting_col, start_row);
        SSD1306_PutC("4. Back");
        delay_ms(100);
if (m2==2){
       
        if( button_select==0 && row_Ind ==1 )
           {
           
           
           
            output_low(PIN_B0);
            output_low(PIN_B1);
            output_high(PIN_B2);
            button_select==1;
           }
           else if (button_select==0 && row_Ind ==2){
           
            output_high(PIN_B0);
            output_low(PIN_B1);
            output_low(PIN_B2);
            button_select==1;
           }
            else if (button_select==0 && row_Ind ==3){
           
            output_high(PIN_B0);
            output_low(PIN_B1);
            output_low(PIN_B2);
            button_select==1;
            }

       }
}
 void menu1_Back() {
       
       menu1();
  }
  void charging_s1_Back () {

       charging_s1 (); //m1 = 1; m2 =0; m3=1;
  }
  void discharging_s1_Back() {
     
       discharging_s1(); //m1 = 0; m2 = 1; m3 =1;
  }
  void spkr_Back() {
     
       discharging_Spkr();
  }
  void scroll_down(){   
       if (row_Ind >= 8)
            row_Ind = 8;
       else
           row_Ind++;
       cursor(row_Ind);
       delay_ms(50);
   }             
  void scroll_up(){
       if (row_Ind <= 1)
           row_Ind = 1;
       else
           row_Ind--;
       cursor(row_Ind);
       delay_ms(50);
   } 
 


In this look to the USB discharging section. I want to switch ON the led connected to the B0 port when 5V 1A is pressed and until then all two led is off. I want to switch ON the led connected to the B1 port when 5V 2A is pressed in the menu until then all other 2 LED is OFF. I want to switch ON the LED connected to the B2 port when 5V 3A is pressed in the menu until then all other two LED is off.

I am confuse with the syntax of loop that how m2 is framed to enter in the loop.
Look to this image: https://postimg.cc/yWxcnSJ8


Last edited by sradhika1998 on Mon Jun 24, 2019 2:54 am; edited 1 time in total
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Mon Jun 24, 2019 2:41 am     Reply with quote

The first thing is that normally buttons are wired to pull _down_. This
dates from TTL inputs, which tend to float high, so it draws less current
to have pull-up resistors and pull down with the switch than to have pull
down resistors and buttons pulling up. If your buttons 'pull down', then
the logic tests to know the buttons are 'on', become:

if (button_select)

would need to be:

if (button_select==0)

etc..

You are the only person who actually knows how your hardware is wired.
This affects everything.
dluu13



Joined: 28 Sep 2018
Posts: 395
Location: Toronto, ON

View user's profile Send private message Visit poster's website

PostPosted: Mon Jun 24, 2019 7:18 am     Reply with quote

OP says they have corrected the wiring of the buttons according to how PCM Programmer says to do so.

However, the first iteration of the schematic shows that the buttons are each wired so that one side of the button goes to the pin that the program checks, and the other end goes to the MCLR pin...

Coupled with the way they were using output_low(x)==0 before, I think OP has a lot of fundamentals to cover.

Do you have an updated schematic?
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Mon Jun 24, 2019 7:55 am     Reply with quote

Yes. The point I was making was that if the pins are wired as PCM_programmer
has suggested, his logic is completely inverted. He tests for TRUE being the
button pressed, but it'll actually be the 'FALSE' state that is happening
when the buttons are pressed.....
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Mon Jun 24, 2019 8:48 am     Reply with quote

You seem to be ignoring most of the advice / instructions you're being given,

I can't compile your code as I don't have your <SSD1306.C> file.

I asked for the MINIMUM possible code to show how you want to drive the LEDs.

90% of the code offered is not needed at this stage.

You need to make your switches pull down (as PCM P.).

Are you using LEDs with built in resistors. They also should pull down to turn on.

Life is easier if you avoid using RB5-7 as I/O.

****************************************************************************************

I believe the penny has dropped.

Are you using the location of the cursor on the OLED display to select which LED lights?

****************************************************************************************

Mike
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jun 24, 2019 8:51 am     Reply with quote

In his latest schematic (he edited his post), he has pull-down resistors
and the switches are connected to +5v. Here is the link to his schematic:
https://postimg.cc/yWxcnSJ8
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page Previous  1, 2
Page 2 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group