hi-tech language , 7 segment

Discussion in 'Embedded Systems and Microcontrollers' started by micro1, Mar 14, 2015.

  1. micro1

    Thread Starter Member

    Feb 22, 2015
    52
    0
    hello

    I did the programming in hi-tech language ,the project is 7 segment in sw1 the 7 segment work from 0 to 9 and in sw2 the 7 segment work from 9 to 0 ,
    the question is : if push on sw2 the seven segment working in the the 7 segment working I want push sw1 go to in function for sw1 ,is mean stop the function sw2 ?

    Capt1ure.PNG

    Capture.PNG

    Code (Text):
    1. #include<htc.h>
    2.  
    3. __CONFIG(FOSC_HS & WDTE_OFF & PWRTE_ON & BOREN_OFF & LVP_OFF);
    4.  
    5. #define _XTAL_FREQ 20000000
    6. #define sw1 RB1
    7. #define sw2 RB2
    8.  
    9. void main (void)
    10. {
    11.  
    12.     TRISD =0x00;
    13.     PORTD =0x00;
    14.     TRISB1 =1;
    15.     TRISB2 =1;
    16.  
    17.  
    18.     while(1)
    19. {
    20.     if(sw1==0)
    21.     {
    22.             line1:
    23.  
    24.         PORTD =0b00111111;
    25.         __delay_ms(1000);  //0
    26.  
    27.         PORTD =0b00000110;
    28.         __delay_ms(1000);  //1
    29.  
    30.         PORTD =0b01011011; //2
    31.         __delay_ms(1000);
    32.  
    33.         PORTD =0b01001111; //3
    34.         __delay_ms(1000);
    35.  
    36.         PORTD =0b01100110; //4
    37.         __delay_ms(1000);
    38.  
    39.         PORTD =0b01101101;  //5
    40.         __delay_ms(1000);
    41.  
    42.         PORTD =0b01111101;  //6
    43.         __delay_ms(1000);
    44.  
    45.         PORTD =0b00000111;  //7
    46.         __delay_ms(1000);
    47.  
    48.         PORTD =0b01111111;  //8
    49.         __delay_ms(1000);
    50.  
    51.         PORTD =0b01101111;   //9
    52.         __delay_ms(1000);
    53.  
    54.         if(sw2==0)
    55.         {
    56.             goto line2;
    57.         };
    58.  
    59.    }
    60.  
    61.     else if (sw2==0)
    62.     {
    63.         line2:
    64.  
    65.         PORTD =0b01101111;
    66.         __delay_ms(1000); //9
    67.  
    68.         PORTD =0b01111111;
    69.         __delay_ms(1000);  //8
    70.  
    71.         PORTD =0b00000111; //7
    72.         __delay_ms(1000);
    73.  
    74.         PORTD =0b01111101; //6
    75.         __delay_ms(1000);
    76.  
    77.         PORTD =0b01101101; //5
    78.         __delay_ms(1000);
    79.  
    80.         PORTD =0b01100110;  //4
    81.         __delay_ms(1000);
    82.  
    83.         PORTD =0b01001111;  //3
    84.         __delay_ms(1000);
    85.  
    86.         PORTD =0b01011011;  //2
    87.         __delay_ms(1000);
    88.  
    89.         PORTD =0b00000110;  //1
    90.         __delay_ms(1000);
    91.  
    92.         PORTD =0b00111111;   //0
    93.         __delay_ms(1000);
    94.  
    95.         if(sw1==0)
    96.         {
    97.             goto line1;
    98.         };
    99.  
    100.        }
    101.    }
    102. }
    103.  
     
  2. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    i like GOTO

    But jumping into a branch is totally bad.

    You should research "How to write C function" or "C functions".
     
    micro1 likes this.
  3. micro1

    Thread Starter Member

    Feb 22, 2015
    52
    0
    what I will do ?
    do you have in idea for this?
     
  4. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    sure. I could post some of my 7-seg C sources.
    But they are multiplex, so its more difficult.

    For 16f54, doesnt use interrupt.
    http://aranna.altervista.org/dragonsnest/serial-7segment-led-display-module/
    Here's the code for sending serial data to 7seg module.

    And here is the firmware for the serial display module.
    Code (Text):
    1.  
    2. #include <xc.h>         /* XC8 General Include File */
    3. #include "system.h"        /* System funct/params, like osc/peripheral config */
    4. #include "user.h"          /* User funct/params, such as InitApp */
    5.  
    6. // 3 digits
    7. // 18 bits
    8.  
    9. const unsigned char ph_PORTA[]={0x00,0x02,0x00};
    10. const unsigned char ph_PORTB[]={0x80,0x00,0x01};
    11.  
    12. const unsigned char dig_PORTA[]={0x0,0xc,0x4,0x4,0x8,0x1,0x1,0x4,\
    13.                                  0x0,0x0,0x0,0x9,0x1,0xc,0x1,0x1};
    14.  
    15. const unsigned char dig_PORTB[]={0x28,0x6e,0x58,0x4a,\
    16.                                  0x0e,0x0a,0x08,0x4e,\
    17.                                  0x08,0x0a,0x0c,0x08,\
    18.                                  0x38,0x48,0x18,0x1c};
    19. unsigned char v_digits[3];
    20. unsigned char v_phase,v_refresh,v_PORTA,v_PORTB;
    21. unsigned char v_digit_data,v_digit_data2;
    22. unsigned char i;
    23.  
    24. #define clk PORTBbits.RB7
    25. #define data PORTBbits.RB6
    26.  
    27. void get_serial()
    28. {unsigned char i;
    29. unsigned char sh_val=1;
    30.  
    31.     v_digit_data=0; // 6 bits transmission
    32.  
    33.     for(i=0;i<6;i++)
    34.     {
    35.     while(!clk);if(data)v_digit_data|=sh_val;
    36.     while(clk);
    37.     sh_val<<=1;
    38.     }
    39. }
    40.  
    41. void refresh()
    42. {
    43.          v_digit_data2=0xff;
    44.  
    45.     v_digit_data=v_digits[v_phase];
    46.     if(v_digit_data&0x20)v_digit_data2=0xf7;
    47.     v_digit_data&=0x1f;
    48.  
    49.           v_PORTA=ph_PORTA[v_phase]|dig_PORTA[v_digit_data];
    50.           v_PORTB=(ph_PORTB[v_phase]|dig_PORTB[v_digit_data])&v_digit_data2;
    51.  
    52.     TRISA=0xff;TRISB=0xff;
    53.          PORTA=v_PORTA;PORTB=v_PORTB;
    54.     TRISA=0;TRISB=0;
    55.          v_phase++;if(v_phase==3)v_phase=0;
    56. }
    57.  
    58. void main(void)
    59. {
    60.     TRISA= 0xff;
    61.     TRISB= 0xff;
    62.     v_phase=0;
    63.     v_refresh=0;
    64.  
    65.     for(i=0;i<3;i++)
    66.     {
    67.     get_serial();
    68.     v_digits[i]=v_digit_data;
    69.     }
    70.  
    71.     PORTA=0;
    72.     PORTB=0;
    73.     TRISB=0;TRISA=0;
    74.     OPTION=4;
    75.  
    76.     while(1)
    77.     {
    78.           refresh();
    79.     }
    80.  
    Its maybe too advanced, I'm sorry but it makes sense for me that way.
     
    micro1 likes this.
  5. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    Well we could just work on your source code, my programs just to see how it probably looks like in a more useable form.

    You never should jump with GOTO inside or out of a control structure such as IF kind conditional tests.

    It can make sense in some cases to jump back and repeat some tasks, for instance if error has occured, or the same code was run already with some special condition, and also can serve again.

    Its correct there shouldnt be many GOTOs and they should not be used to replace other means of flow control.

    You are just starting maybe? Didnt use Arrays or pointers I guess.
    But you should learn use of functions next, its quite simple.

    1. A function can return a value, but doesnt have to.
    2. It can have parameter but isnt required. Can use global variables in small programs.
    3. It can have its own, local variables. Actually, confusion is here: You can overwrite global variable scope, same name, then wonder why it never updates.

    A very simple function

    Code (Text):
    1. void do_delay_and_set_portB(unsigned char port_value)
    2. {
    3.  __delay_ms(1000);
    4.  PORTB=portB;
    5. }
    6.  
    later, you call with
    Code (Text):
    1. do_delay_and_set_portB(0b01101011);
    you could do things like that:

    Code (Text):
    1. #define ddb do_delay_and_set_portB
    2. ddb(0b00101100);
    since writing the long name many times is "reundant".

    When you learn C, you have to look at certain ways doing things, and find what's best for you,
    and 2. constantly improve code quality.

    When you are stuck, there is often alternative way, or do some things you understand more.
    Like a puzzle. Once you got 60% the rest is much easier!
     
    micro1 likes this.
  6. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    One obvious use of this: You can change the delay value one time, and its affecting all function calls.
    It takes too much time and doesnt make fun to replace numerical values many times.

    Though you could also use a
    Code (Text):
    1. #define
    but functions have many uses.

    See here are two ways, one using define, the other, put into a function (which you maybe also want to do for other reasons).
     
    micro1 likes this.
  7. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    Using a simulator, and variables watch, you can learn the basics in a few days, but you have to try hard at times, and often think, if there could be alternative way.

    Often, compare: What is existing data. What is wanted data. Is there a correlation.

    Look at state machine too (FSM). A MCU like a PIC always is in a certain "machine state".

    Most important the PC (program line counter). When you use IF or FOR, actually, there are hidden GOTO.
    You modify the machine state seriously by so called branching, means you alter the value of the PC.

    Sense of this lament, it is common practice to write some machine states together on a piece of paper, to visualize them all together. Its top down for easy programs but can have loops and so on, even small digital logic.

    You would typically draw arrows, symbolize a machine state change, from blobs or squares to others.
    Inside them, you write variables (mostly) which changed, or important calls / condition tests, counters, and so on.

    At each branch (a blob can branch out into more than one other, you ask: WHY is this happening (or what could be a reason why it is not actually happening. HOW is it happening. WHAT are conditions, or neccessary correct values, which are needed before branching to another blob.

    the complete machine state is: PC, ALU, Register file, possibly RAM (variables on PIC are inside the register file), as well function registers (which are also inside the register file).

    Its FSM (finite) since when you reset, it always starts with PC at some fixed location, and register file normally cleared.
     
    micro1 likes this.
  8. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    There are always new surprises. Recently I learned

    Code (Text):
    1.  
    2. void get_serial()
    3. {unsigned char i;
    4. unsigned char sh_val=1;
    5.  
    6. v_digit_data=0; // 6 bits transmission
    7.  
    8. for(i=0;i<6;i++)
    9. {
    10. while(!clk);if(data)v_digit_data|=sh_val;
    11. while(clk);
    12. sh_val<<=1;
    13. }
    14. }
    could be written like that:

    Code (Text):
    1. void get_serial()
    2. {unsigned char i;v_digit_data=0; // 6 bits transmission
    3.  for(i=1;i<0x40;i<<=1){while(!clk);if(data)v_digit_data|=i;while(clk);}
    4. }
    which by at least 3 means, shrinks the amount of lines dramatically.

    Its not so good for readability but not a problem if what the function does is well understood.

    Normally, when you made a breakthrough, you often think overnight, you have become a great programming whizard and nobody could ever even come close to your writing style. But it repeats, and you see, its a constant learning process.
     
  9. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    not to speak of; when you compact the source like that, GOTO is a total no-no.
    The compression introduces obfuscation, but has a benefit to compact the source, often to 1/3, so you could use it for parts which are well understood and and you dont scroll through 20 pages anymore.

    I'd also recommend to learn using .h files early, and use more than one .c source file.
    Keep each .c file small.
     
  10. sevenfold4

    Member

    Jan 12, 2015
    80
    7
    People who write C like that are mad in my opinion. A stupid thing to say, but I am,personally, really OCD about my coding, and if it doesn't look perfect, it's crap.
     
    ErnieM likes this.
  11. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    by using a distinct coding style, i prevent just taking my work.
    Sure its mad but it works.
     
  12. sevenfold4

    Member

    Jan 12, 2015
    80
    7
    You can just add a few enters between lines to have a proper em, construction?(can't remember the word i want to use...)
     
  13. takao21203

    Distinguished Member

    Apr 28, 2012
    3,577
    463
    sure but I often just remove them later
     
  14. MCU88

    Member

    Mar 12, 2015
    360
    35
    I didn't even realize C had the GOTO syntax. Not that I'd ever use it. I much prefer methods and functions. Well not actually methods here with microcontrollers because the languages are not OOP... (Object Orientated Programming) -- which is really nice, particularly the Microsoft .net C# language. I used to love C# and making video games in it.

    I think I sort of advise learning Java before doing any C. At least in my case.I formally studied some Java programming and was able to get my head around C after doing the Java, but prior to this I was very confused about C.
     
  15. MrChips

    Moderator

    Oct 2, 2009
    12,440
    3,360
    GOTO statements have been banned in Structured Programming since the 1970's.
     
  16. MCU88

    Member

    Mar 12, 2015
    360
    35
    My favorite way to structure a program is to use the switch statement ...

    For the OP's information:

    void multiplexDisplays() in the code below is an example of an function. The void basically means that there is no return variable of the function displayOff(); and blankDigit(); are examples of calling another function. setDigit(ones); is an example of passing an variable to another calling function.

    Code (Text):
    1.  
    2. void multiplexDisplays()
    3. {
    4.   displayOff();
    5.   blankDigit();
    6.          
    7.    switch (scan)
    8.    {
    9.       case 0:
    10.          setDigit(ones);
    11.          dslpA = 1;
    12.          break;
    13.  
    14.       case 1:
    15.          setDigit(tens);
    16.          dslpB = 1;
    17.          break;
    18.  
    19.       case 2:
    20.          setDigit(hundreds);
    21.          dslpC = 1;
    22.          break;
    23.  
    24.       case 3:
    25.          setDigit(thousands);
    26.          dslpD = 1;
    27.          break;
    28.    }
    29.  
    30.    scan++;
    31.  
    32.    if (scan == 4)
    33.    {
    34.       scan = 0;
    35.    }
    36. }
    37.  
     
  17. MrChips

    Moderator

    Oct 2, 2009
    12,440
    3,360
    The TS code only needs to test for sw1 and sw2. There is no need for an else statement.

    Code (Text):
    1.  
    2. if (sw1 == 0)
    3.   {
    4.   }
    5.  
    6. if (sw2 == 0)
    7.   {
    8.   }
    9.  
     
Loading...