Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

mkroC 4 Digits example

Status
Not open for further replies.

janosandi

Full Member level 4
Full Member level 4
Joined
Jan 23, 2010
Messages
210
Helped
2
Reputation
4
Reaction score
2
Trophy points
1,298
Activity points
2,788
hello Guys
im new to MikroC & by learning it from examples i've got nothing
i need an example of 4 digit which have 4 variables i can use for 4 digits seperately

thx for help
 

learn from here select which controller you use and https://learn.mikroe.com/ebooks/

i've used "pic programming in C & the first example i've tried to compile has errors then on mikroe forum one of the mikroE team replied to a message tht i've posted about the example errors & have sent me an corrected example as she said but it idnt work also
so its such trusted Book really to start learning of
looking for someone worked with it to help me
if you work with it you can check example9 which had errors
so how they think anyone can learn anything from tht book

thx for your reply
 

As soon you will show your code and schematics, as far you will get any usefull help.
 

As soon you will show your code and schematics, as far you will get any usefull help.

i've tried learning C from example9 :two digit led counter
but its not working even after correction of MikroE team

here is the schematic
pic-microcontrollers-programming-in-c-chapter-04-image-42.gif
Code:
/*Header******************************************************/
 
unsigned short mask(unsigned short num);
unsigned short digit_no, digit10, digit1, digit, i;
 
void interrupt() {
 if (digit_no==0) {
 PORTA = 0; // Turn off both displays
 PORTD = digit1; // Set mask for displaying ones on PORTD
 PORTA = 1; // Turn on display for ones (LSD)
 digit_no = 1;
 } else {
 PORTA = 0; // Turn off both displays
 PORTD = digit10; // Set mask for displaying tens on PORTD
 PORTA = 2; // Turn on display for tens (MSD)
 digit_no = 0;
 }
 TMR0 = 0; // Reset counter TMRO
 INTCON = 0x20; // Bit T0IF=0, T0IE=1
}
 
void main() {
 OPTION_REG = 0x80; // Set timer TMR0
 TMR0 = 0;
 INTCON = 0xA0; // Disable interrupt PEIE,INTE,RBIE,T0IE
 PORTA = 0; // Turn off both displays
 TRISA = 0; // All port A pins are configured as outputs
 PORTD = 0; // Turn off all display segments
 TRISD = 0; // All port D pins are configured as outputs
 
 do {
 for (i = 0; i<=99; i++) { // Count from 0 to 99
 digit = i % 10u;
 digit1 = mask(digit); // Prepare mask for displaying ones
 digit = (char)(i / 10u) % 10u;
 digit10 = mask(digit); // Prepare mask for displaying tens
 Delay_ms(1000);
 }
 } while (1); // Endless loop
}

& here is the corrected 1
Code:
/*Header******************************************************/

unsigned short mask(unsigned short num);
unsigned short digit_no, digit10, digit1, digit, i;

void interrupt() {
 if (digit_no==0) {
 PORTA = 0; // Turn off both displays
 PORTD = digit1; // Set mask for displaying ones on PORTD
 PORTA = 1; // Turn on display for ones (LSD)
 digit_no = 1;
 } else {
 PORTA = 0; // Turn off both displays
 PORTD = digit10; // Set mask for displaying tens on PORTD
 PORTA = 2; // Turn on display for tens (MSD)
 digit_no = 0;
 }
 TMR0 = 0; // Reset counter TMRO
 INTCON = 0x20; // Bit T0IF=0, T0IE=1
}

void main() {
 OPTION_REG = 0x80; // Set timer TMR0
 TMR0 = 0;
 INTCON = 0xA0; // Disable interrupt PEIE,INTE,RBIE,T0IE
 PORTA = 0; // Turn off both displays
 TRISA = 0; // All port A pins are configured as outputs
 PORTD = 0; // Turn off all display segments
 TRISD = 0; // All port D pins are configured as outputs

 do {
 for (i = 0; i <= 99; i++) { // Count from 0 to 99
 digit = i % 10u;
 digit1 = mask(digit); // Prepare mask for displaying ones
 digit = (char)(i / 10u) % 10u;
 digit10 = mask(digit); // Prepare mask for displaying tens
 Delay_ms(1000);
 }
 } while (1); // Endless loop
}
here is the segment mask
Code:
/*Header******************************************************/
unsigned short mask(unsigned short num) {
switch (num) {
case 0 : return 0x3F;
case 1 : return 0x06;
case 2 : return 0x5B;
case 3 : return 0x4F;
case 4 : return 0x66;
case 5 : return 0x6D;
case 6 : return 0x7D;
case 7 : return 0x07;
case 8 : return 0x7F;
case 9 : return 0x6F;
}
}

if you want to help me i need 2 things
1:a good reference to learn embedded C for PIC with examples wil be great.
2:a working example of 4 digits which i can assign values to each digit by seperate virables.

all of your help will b appriciated
John

here is a link to my thread on mikroE forum
https://forum.mikroe.com/viewtopic.php?f=88&t=67533
 
Last edited:

After reviewing some of your messages I would suggest you first go and learn to program in C
before trying to learn to program embedded code.
I would suggest if you cannot recognise the obvious error in the code above that you
may be putting yourself and possibly others in danger.

Suggested course of action
1. Learn to program in C
2. Learn basic electronics
3. Learn how to tie the 2 together
4. Go back to Mikro and try again.

Trying to learn programming and electronics together
at the same time is probably a big mistake.
 

After reviewing some of your messages I would suggest you first go and learn to program in C
before trying to learn to program embedded code.
I would suggest if you cannot recognise the obvious error in the code above that you
may be putting yourself and possibly others in danger.

Suggested course of action
1. Learn to program in C
2. Learn basic electronics
3. Learn how to tie the 2 together
4. Go back to Mikro and try again.

Trying to learn programming and electronics together
at the same time is probably a big mistake.

its ok as i guess
i've learned assembly & did alot of small projects before
learning is easy when you know what you need

would you plz tel me what kind of danger can i put others?
& what you mean by basic electronics ?

thx for reply anyway
 
Last edited:

i've tried learning C from example9 :two digit led counter
but its not working even after correction of MikroE team

Have you included the file mask.c into the project? If not, include it and bild it again.

Did the code built successfully? If not, post the screen shot of theoutput.
 

Have you included the file mask.c into the project? If not, include it and bild it again.

Did the code built successfully? If not, post the screen shot of theoutput.

would you plz follow the link to my thread on MikroE Forum ?
i diid all of tht even there was an error in the code & they have fixed it but still not working

thx for reply
 

You need to use common cathode 7 Segments.

FYI,
At power up, all the ports are configured as input. Therefore,
PORTA = 0; // Turn off both displays
TRISA = 0; // All port A pins are configured as outputs
PORTD = 0; // Turn off all display segments
TRISD = 0; // All port D pins are configured as outputs

will not do what you expect. (But here this is not the problem.). You need to set ports as output first then set them zero.
Code:
 TRISA = 0; // All port A pins are configured as outputs
 PORTA = 0; // Turn off both displays
 TRISD = 0; // All port D pins are configured as outputs
 PORTD = 0; // Turn off all display segments

To find a solution plz tell what is the problem exactly. Did you simulated this or implemented this? What can u see on the segments etc...
If you implemented this in hardware there can be a problem in programming the fuses also.
 

its hard to find solution this way
i just need an example of 4 7segments which i can assign values to each one seperately
something like tht

Code:
Digit1 = O
Digit2 = P
Digit3 = E
Digit4 = N

start:
PORTA.0 = 1 //enable digit one
PORTB = Digit1
2ms delay
PORTB.0 = 0 //disable digit one
PORTB.1 = 0 //enable digit two
PORTB = Digit2
2ms delay
PORTB.1 = 0 //disable digit two
PORTB.1 = 0 //enable digit three
PORTB = Digit3
2ms delay
PORTB.0 = 0 //disable digit three
PORTB.1 = 0 //enable digit four
PORTB = Digit4
goto start

thx guys
 
Last edited:

Try to modify your code as per the hints below. Also add two more current sinking transistors driving from RA2 and RA3 respectively for two more 7 segment displays. Declare the variable 'i' as unsigned int instead of short.

ISR:

Code:
void interrupt() {
 if (digit_no==0) {
 PORTA = 0; // Turn off both displays
 PORTD = digit1; // Set mask for displaying ones on PORTD
 PORTA = 1; // Turn on display for ones 
 digit_no = 1;
 } 
else if (digit_no == 1) {
 PORTA = 0; // Turn off both displays
 PORTD = digit2; // Set mask for displaying tens on PORTD
 PORTA = 2; // Turn on display for tens
 digit_no = 2;
 }
else if (digit_no == 3) {
 PORTA = 0; // Turn off both displays
 PORTD = digit3; // Set mask for displaying  hundreds  on PORTD
 PORTA = 4; // Turn on display for hundreds 
 digit_no = 3;
 }
else {
 PORTA = 0; // Turn off both displays
 PORTD = digit4; // Set mask for displaying thousands on PORTD
 PORTA = 8; // Turn on display for thousands 
 digit_no = 0;
 }


 TMR0 = 0; // Reset counter TMRO
 INTCON = 0x20; // Bit T0IF=0, T0IE=1
}

MAIN:

Code:
do {
 for (i = 0; i <= 9999; i++) { // Count from 0 to 9999
 digit = i % 10u;
 digit1 = mask(digit); // Prepare mask for displaying ones
 digit = (char)(i / 10u) % 10u;
 digit2 = mask(digit); // Prepare mask for displaying tens
digit = (char)(i / 100u) % 10u;
 digit3 = mask(digit); // Prepare mask for displaying hundreds
digit = (char)(i / 1000u) % 10u;
 digit4 = mask(digit); // Prepare mask for displaying thousands


 Delay_ms(1000);
 }
 } while (1);
 
Last edited:
Try to modify your code as per the hints below. Also add two more current sinking transistors driving from RA2 and RA3 respectively for two more 7 segment displays. Declare the variable 'i' as unsigned int instead of short.

ISR:

Code:
void interrupt() {
 if (digit_no==0) {
 PORTA = 0; // Turn off both displays
 PORTD = digit1; // Set mask for displaying ones on PORTD
 PORTA = 1; // Turn on display for ones 
 digit_no = 1;
 } 
else if (digit_no == 1) {
 PORTA = 0; // Turn off both displays
 PORTD = digit2; // Set mask for displaying tens on PORTD
 PORTA = 2; // Turn on display for tens
 digit_no = 2;
 }
else if (digit_no == 3) {
 PORTA = 0; // Turn off both displays
 PORTD = digit3; // Set mask for displaying  hundreds  on PORTD
 PORTA = 4; // Turn on display for hundreds 
 digit_no = 3;
 }
else {
 PORTA = 0; // Turn off both displays
 PORTD = digit4; // Set mask for displaying thousands on PORTD
 PORTA = 8; // Turn on display for thousands 
 digit_no = 0;
 }


 TMR0 = 0; // Reset counter TMRO
 INTCON = 0x20; // Bit T0IF=0, T0IE=1
}

MAIN:

Code:
do {
 for (i = 0; i <= 9999; i++) { // Count from 0 to 9999
 digit = i % 10u;
 digit1 = mask(digit); // Prepare mask for displaying ones
 digit = (char)(i / 10u) % 10u;
 digit2 = mask(digit); // Prepare mask for displaying tens
digit = (char)(i / 100u) % 10u;
 digit3 = mask(digit); // Prepare mask for displaying hundreds
digit = (char)(i / 1000u) % 10u;
 digit4 = mask(digit); // Prepare mask for displaying thousands


 Delay_ms(1000);
 }
 } while (1);

thx
i've already did it tomorrow i'll send it here but ur code is very useful for me also

thx Guys
John
 

hello Guys
I've got 2 digit counter code from a friend which i've edit it & now it ok with 4 seperate virables
with this code OPEN is shown on 4digits
would you check if its ok ?
Code:
char select = 0, Digitt1, Digitt2, Digitt3, Digitt4;
unsigned char common_cathode_mask[21] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F, 0x77, 0x7C, 0x39, 0x5E, 0x79, 0x71, 0x73, 0x50, 0x5C, 0x37, 0x40};
//                                      0      ,1    ,2    ,3    ,4    ,5    ,6    ,7    ,8    ,9    ,A    ,b    ,C    ,d    ,E    ,F    ,P    ,r    ,o    ,N    ,-
//char digit[2];
char digit[4];

//Timer1
//Prescaler 1:1; TMR1 Preload = 63536; Actual Interrupt Time : 2 ms
//Place/Copy this part in declaration section

void InitTimer1() {
    //T1CON = 0x01;
    T1CON = 0x01;
    TMR1IF_bit = 0;
    TMR1H = 0xF8;
    TMR1L = 0x30;
    TMR1IE_bit = 1;
    INTCON = 0xC0;
}

void Interrupt(){
    if(TMR1IF_bit) {
      TMR1H = 0xF8;
      TMR1L = 0x30;

      PORTD = 0x00;
}

      //Enter your code here
      switch(select) {
          case 0:
                PORTC = digit[0];
                PORTD = 0x01;
                break;
          case 1:
                PORTC = digit[1];
                PORTD = 0x02;
                break;
          case 2:
                PORTC = digit[2];
                PORTD = 0x04;
                break;
          case 3:
                PORTC = digit[3];
                PORTD = 0x08;
                break;
      };

      if(++select == 4)select = 0;

      TMR1IF_bit = 0;
    }

void Dopen() {
     Digitt1 = 0;
     Digitt2 = 16;
     Digitt3 = 14;
     Digitt4 = 19;
     PORTB.RB5 = 1;
}
void main() {

    CMCON = 0x07;
    ADCON1 = 0x87;

    TRISA = 0x00;
    TRISB = 0x03;
    TRISC = 0x00;
    TRISD = 0x00;
    TRISE = 0x00;

    PORTA = 0x00;
    PORTB = 0x00;
    PORTC = 0x00;
    PORTD = 0x00;
    PORTE = 0x00;

    InitTimer1();

    Dopen();
    
         digit[0] = common_cathode_mask[Digitt1];
         digit[1] = common_cathode_mask[Digitt2];
         digit[2] = common_cathode_mask[Digitt3];
         digit[3] = common_cathode_mask[Digitt4];
    }
 

I am not an expert in C. However, it seems the code is ok.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top