hi-tech language , 7 segment

Thread Starter

micro1

Joined Feb 22, 2015
79
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:
#include<htc.h>

__CONFIG(FOSC_HS & WDTE_OFF & PWRTE_ON & BOREN_OFF & LVP_OFF);

#define _XTAL_FREQ 20000000
#define sw1 RB1
#define sw2 RB2

void main (void)
{

    TRISD =0x00;
    PORTD =0x00;
    TRISB1 =1;
    TRISB2 =1;


    while(1)
{
    if(sw1==0)
    {
            line1:

        PORTD =0b00111111;
        __delay_ms(1000);  //0

        PORTD =0b00000110;
        __delay_ms(1000);  //1

        PORTD =0b01011011; //2
        __delay_ms(1000);

        PORTD =0b01001111; //3
        __delay_ms(1000);

        PORTD =0b01100110; //4
        __delay_ms(1000);

        PORTD =0b01101101;  //5
        __delay_ms(1000);

        PORTD =0b01111101;  //6
        __delay_ms(1000);

        PORTD =0b00000111;  //7
        __delay_ms(1000);

        PORTD =0b01111111;  //8
        __delay_ms(1000);

        PORTD =0b01101111;   //9
        __delay_ms(1000);

        if(sw2==0)
        {
            goto line2;
        };

   }

    else if (sw2==0)
    {
        line2:

        PORTD =0b01101111;
        __delay_ms(1000); //9

        PORTD =0b01111111;
        __delay_ms(1000);  //8

        PORTD =0b00000111; //7
        __delay_ms(1000);

        PORTD =0b01111101; //6
        __delay_ms(1000);

        PORTD =0b01101101; //5
        __delay_ms(1000);

        PORTD =0b01100110;  //4
        __delay_ms(1000);

        PORTD =0b01001111;  //3
        __delay_ms(1000);

        PORTD =0b01011011;  //2
        __delay_ms(1000);

        PORTD =0b00000110;  //1
        __delay_ms(1000);

        PORTD =0b00111111;   //0
        __delay_ms(1000);

        if(sw1==0)
        {
            goto line1;
        };

       }
   }
}
 

takao21203

Joined Apr 28, 2012
3,702
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:
#include <xc.h>         /* XC8 General Include File */
#include "system.h"        /* System funct/params, like osc/peripheral config */
#include "user.h"          /* User funct/params, such as InitApp */

// 3 digits
// 18 bits

const unsigned char ph_PORTA[]={0x00,0x02,0x00};
const unsigned char ph_PORTB[]={0x80,0x00,0x01};

const unsigned char dig_PORTA[]={0x0,0xc,0x4,0x4,0x8,0x1,0x1,0x4,\
                                 0x0,0x0,0x0,0x9,0x1,0xc,0x1,0x1};

const unsigned char dig_PORTB[]={0x28,0x6e,0x58,0x4a,\
                                 0x0e,0x0a,0x08,0x4e,\
                                 0x08,0x0a,0x0c,0x08,\
                                 0x38,0x48,0x18,0x1c};
unsigned char v_digits[3];
unsigned char v_phase,v_refresh,v_PORTA,v_PORTB;
unsigned char v_digit_data,v_digit_data2;
unsigned char i;

#define clk PORTBbits.RB7
#define data PORTBbits.RB6

void get_serial()
{unsigned char i;
unsigned char sh_val=1;

    v_digit_data=0; // 6 bits transmission

    for(i=0;i<6;i++)
    {
    while(!clk);if(data)v_digit_data|=sh_val;
    while(clk);
    sh_val<<=1;
    }
}

void refresh()
{
         v_digit_data2=0xff;

    v_digit_data=v_digits[v_phase];
    if(v_digit_data&0x20)v_digit_data2=0xf7;
    v_digit_data&=0x1f;

          v_PORTA=ph_PORTA[v_phase]|dig_PORTA[v_digit_data];
          v_PORTB=(ph_PORTB[v_phase]|dig_PORTB[v_digit_data])&v_digit_data2;

    TRISA=0xff;TRISB=0xff;
         PORTA=v_PORTA;PORTB=v_PORTB;
    TRISA=0;TRISB=0;
         v_phase++;if(v_phase==3)v_phase=0;
}

void main(void)
{
    TRISA= 0xff;
    TRISB= 0xff;
    v_phase=0;
    v_refresh=0;

    for(i=0;i<3;i++)
    {
    get_serial();
    v_digits[i]=v_digit_data;
    }

    PORTA=0;
    PORTB=0;
    TRISB=0;TRISA=0;
    OPTION=4;

    while(1)
    {
          refresh();
    }
Its maybe too advanced, I'm sorry but it makes sense for me that way.
 

takao21203

Joined Apr 28, 2012
3,702
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:
void do_delay_and_set_portB(unsigned char port_value)
{
 __delay_ms(1000);
 PORTB=portB;
}
later, you call with
Code:
do_delay_and_set_portB(0b01101011);
you could do things like that:

Code:
#define ddb do_delay_and_set_portB
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!
 

takao21203

Joined Apr 28, 2012
3,702
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:
#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).
 

takao21203

Joined Apr 28, 2012
3,702
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.
 

takao21203

Joined Apr 28, 2012
3,702
There are always new surprises. Recently I learned

Code:
void get_serial()
{unsigned char i;
unsigned char sh_val=1;

v_digit_data=0; // 6 bits transmission

for(i=0;i<6;i++)
{
while(!clk);if(data)v_digit_data|=sh_val;
while(clk);
sh_val<<=1;
}
}
could be written like that:

Code:
void get_serial()
{unsigned char i;v_digit_data=0; // 6 bits transmission
 for(i=1;i<0x40;i<<=1){while(!clk);if(data)v_digit_data|=i;while(clk);}
}
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.
 

takao21203

Joined Apr 28, 2012
3,702
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.
 

sevenfold4

Joined Jan 12, 2015
80
There are always new surprises. Recently I learned

Code:
void get_serial()
{unsigned char i;
unsigned char sh_val=1;

v_digit_data=0; // 6 bits transmission

for(i=0;i<6;i++)
{
while(!clk);if(data)v_digit_data|=sh_val;
while(clk);
sh_val<<=1;
}
}
could be written like that:

Code:
void get_serial()
{unsigned char i;v_digit_data=0; // 6 bits transmission
for(i=1;i<0x40;i<<=1){while(!clk);if(data)v_digit_data|=i;while(clk);}
}
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.
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.
 

MCU88

Joined Mar 12, 2015
358
i like GOTO

But jumping into a branch is totally bad.

You should research "How to write C function" or "C functions".
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.
 

MCU88

Joined Mar 12, 2015
358
GOTO statements have been banned in Structured Programming since the 1970's.
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:
void multiplexDisplays()
{
  displayOff();
  blankDigit();
         
   switch (scan)
   {
      case 0:
         setDigit(ones);
         dslpA = 1;
         break;

      case 1:
         setDigit(tens);
         dslpB = 1;
         break;

      case 2:
         setDigit(hundreds);
         dslpC = 1;
         break;

      case 3:
         setDigit(thousands);
         dslpD = 1;
         break;
   }

   scan++;

   if (scan == 4)
   {
      scan = 0;
   }
}
 

MrChips

Joined Oct 2, 2009
30,824
The TS code only needs to test for sw1 and sw2. There is no need for an else statement.

Code:
if (sw1 == 0)
  {
  }

if (sw2 == 0)
  {
  }
 
Top