[SOLVED] Problem with project

Status
Not open for further replies.

dope40

Member level 4
Joined
Oct 3, 2011
Messages
76
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Location
Bulgaria
Activity points
1,777
Problem with project (gear indicator)

Hi , i am making gear indicator, but i cant make the code work properly... i am new to MicroC so i need some help.I am using 7 segment common cathode display and two buttons ,the buttons are connected to PORTA and the 7segment is connected to PORTB. the code i am using is this...

Code:
/*Header******************************************************/

unsigned short gear, memo ;      // Define variables
                                         

void initMain() {
    ANSEL = 0;                               // All I/O pins are configured as digital
    ANSELH = 0;
    PORTA = 255;                         // Port A initial state
    TRISA = 255;                         // All port A pins are configured as inputs
    PORTB = 0;                           // Initial state of port B
    TRISB = 0;                           // All port B pins are configured as outputs
    
}

void main() {
    initMain();
    gear = 0;                                  // Initial value of variable gear
    memo = 0;                              // Reset variable memo
    
    while (1) {                                 // Endless loop
        if (Button(&PORTA, 0,1,1))       // If the button connected to RA0 is pressed
            gear++ ;                          // increment variable gear
        
        if (Button(&PORTA, 1,1,1))       // If the pressed button is connected to RA1
            gear-- ;                          // decrement value gear
        
        if (memo != gear) {                              // If gear and memo are not  equal        
            gear = memo;                                // save the new value
            PORTB = gear;                              // and show it on port B
        }
        
        Delay_ms(200);                   // 200mS delay
    }
}

I dont know how to make it if gear=8 the seven segment shows 8 (PORTB = 127) and is the code correct....
 

You need to map the numerical value (0 to 8 or whatever) to the 7-seg display.
Something like:
Code:
#define SEGA 0x01
#define SEGB 0x02
#define SEGC 0x04
#define SEGD 0x08
#define SEGE 0x10
#define SEGF 0x20
#define SEGG 0x40

#define NUM0 SEGA+SEGB+SEGC+SEGD+SEGE+SEGF
#define NUM1 SEGB+SEGC
#define NUM2 SEGA+SEGB+SEGG+SEGE+SEGD
#define NUM3 SEGA+SEGB+SEGG+SEGC+SEGD
#define NUM4 SEGF+SEGG+SEGB+SEGC
#define NUM5 SEGA+SEGF+SEGG+SEGC+SEGD
#define NUM6 SEGA+SEGF+SEGE+SEGD+SEGC+SEGG
#define NUM7 SEGA+SEGB+SEGC
#define NUM8 SEGA+SEGB+SEGC+SEGD+SEGE+SEGF+SEGG
#define NUM9 SEGG+SEGF+SEGA+SEGB+SEGC+SEGD

const unsigned char numbitmap[]={NUM0, NUM1, NUM2, NUM3, NUM4, NUM5, NUM6, NUM7,NUM8, NUM9};

Then, your code can state:
PORTB=numbitmap[gear];

Try to read the above code to figure out what it is doing. The code assumes a mapping as shown in the diagram here.
You may need to adapt.


By the way, the above code is C. I don't know MicroC. Maybe it is C, and just the name of the compiler is MicroC.
 
Reactions: dope40

    dope40

    Points: 2
    Helpful Answer Positive Rating
I have made the code and it works great, thank for all the help
and this is the code

Code:
/*Header******************************************************/

unsigned short gear, memo ;      // Define variables
#define SEGA 0x01
#define SEGB 0x02
#define SEGC 0x04
#define SEGD 0x08
#define SEGE 0x10
#define SEGF 0x20
#define SEGG 0x40

#define NUM0 SEGA+SEGB+SEGC+SEGD+SEGE+SEGF
#define NUM1 SEGB+SEGC
#define NUM2 SEGA+SEGB+SEGG+SEGE+SEGD
#define NUM3 SEGA+SEGB+SEGG+SEGC+SEGD
#define NUM4 SEGF+SEGG+SEGB+SEGC
#define NUM5 SEGA+SEGF+SEGG+SEGC+SEGD
#define NUM6 SEGA+SEGF+SEGE+SEGD+SEGC+SEGG
#define NUM7 SEGA+SEGB+SEGC
#define NUM8 SEGA+SEGB+SEGC+SEGD+SEGE+SEGF+SEGG
#define NUM9 SEGG+SEGF+SEGA+SEGB+SEGC+SEGD

const unsigned char numbitmap[]={NUM0, NUM1, NUM2, NUM3, NUM4, NUM5, NUM6, NUM7,NUM8, NUM9};

void initMain() {
    ANSEL = 0;                               // All I/O pins are configured as digital
    ANSELH = 0;
    PORTA = 255;                         // Port A initial state
    TRISA = 255;                         // All port A pins are configured as inputs
    PORTD = 0;                           // Initial state of port B
    TRISD = 0;                           // All port D pins are configured as outputs

}

void main() {
    initMain();
    gear = 0;                                  // Initial value of variable gear
    memo = 0;                              // Reset variable memo

    while (1) {                                 // Endless loop
        if (Button(&PORTA, 0,1,1))       // If the button connected to RA0 is pressed
            gear++ ;                          // increment variable gear

        if (Button(&PORTA, 1,1,1))       // If the pressed button is connected to RA1
            gear-- ;                          // decrement value gear

        if (memo != gear) {                    // If gear and memo are not  equal
            memo = gear;                      // save the new value
            PORTD = numbitmap[gear];             // and show it on port D
     

        Delay_ms(200);                   // 200mS delay
    }
}


only now i am using PORTD for the seven segment
 
Last edited:

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…