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.

TMRO and switch function in MikroC

Status
Not open for further replies.

bbgil

Full Member level 2
Joined
Mar 11, 2006
Messages
135
Helped
13
Reputation
26
Reaction score
9
Trophy points
1,298
Activity points
2,321
mikroc 7 segment display

hi. I have this simple code which supposed to turn on 7-segment display (common anode) counting 0-9. i'm using switch function and the TMR0. the problem is i can't make it count. it stuck at 8. i guess the problem is my TMRO and how to count it so it control the switch function. Any help from the C gurus out there will be appreciated. still learning how to use C. here is the code. thnx in advance.

unsigned cnt;
unsigned cnt1;
void interrupt() {
cnt++;

if (cnt==400)
{cnt=0;
cnt1=cnt1++;} // Increment value of cnt on every interrupt
INTCON = 0x20;
TMR0 = 96; // Set T0IE, clear T0IF
}//~

void main() {
OPTION_REG = 0x84; // Assign prescaler to TMR0
TRISb = 0; // PORTB is output
PORTb = 0xFF; // Initialize PORTB
TMR0 = 0x96;
INTCON = 0xA0; // Enable TMRO interrupt
cnt = 0;
cnt1=0; // Initialize cnt

do {
if (cnt1<0x09)
switch (cnt1){
case 0: portb = 0x81;
case 1: portb = 0xf3;
case 2: portb = 0x49;
case 3: portb = 0x61;
case 4: portb = 0x33;
case 5: portb = 0x25;
case 6: portb = 0x05;
case 7: portb = 0xf1;
case 8: portb = 0x01;
case 9: portb = 0x31;
default: portb =0x81;
}
cnt1 = 0;
// Reset cnt
} while (1);

}//~!
 

mikroc tmr0 example

Hi,

Change this line first:

if (cnt1<0x09) with if (cnt1<0xa)... but still not good...

for your inspiration:


//Declarations------------------------------------------------------------------
//--------------------------------------------------------------end-declarations
/*
* Project name:
Display_1 (The 'Hello World' example for the 7Seg. display)
* Copyright:
(c) Mikroelektronika, 2005.
* Description:
This code demonstrates how to display number on one 7-segment display
(common cathode). Display is connected to PORTB (RB0..RB7, segment A to
RB0, segment B to RB1, etc); common cathode is connected to the pin RA1 on
PORTA. Number is incremented every 1s.
* Test configuration:
MCU: P16F877A
Dev.Board: EasyPIC3
Oscillator: HS, 08.0000 MHz
Ext. Modules: -
SW: mikroC v6.0
* NOTES:
None.
*/
unsigned short i;



//-------------- Returns mask for common cathode 7-seg. display
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;
} //case end
}//~




void main() {
INTCON = 0; // Disable PEIE,INTE,RBIE,T0IE
PORTA = 0;
TRISA = 0;
PORTB = 0;
TRISB = 0;
do {
for (i = 0; i<=9u; i++) {
PORTA = 0;
PORTB = mask(i);
PORTA = 2;
Delay_ms(1000);
}
} while (1); //endless loop
}//~!
 
Hi

I wrote this code to count down, but it stops at zero.it counts from 9 to 0 but stop at zero.No looping.

do{
for (i=9; i>=0; i--) { ///unsigned
PORTA = 0;
PORTB = mask(i);
PORTA = 2; ///5V to pin RA1
Delay_ms(1000);}
}while (1); //endless loop


Thanks in advance
 

Hi,
Change it to:
Code:
do{
   for (i=9; i>0; i--) { ///unsigned
     PORTA = 0;
     PORTB = mask(i);
     PORTA = 2; ///5V to pin RA1
     Delay_ms(1000);
   }
    if (i==0){
      PORTA = 0;
      PORTB = mask(i);
      PORTA = 2; ///5V to pin RA1
      Delay_ms(1000);
    }
}while (1); //endless loop

Hope this helps.
Tahmid.

---------- Post added at 14:52 ---------- Previous post was at 14:49 ----------

Or,
Code:
do{
   PORTA = 0;
   PORTB = mask(i);
   PORTA = 2;
   Delay_ms(1000);
   if (i > 0){
     i--;
   }
   else {
     i = 9;
   }
}while (1);

Hope this helps.
Tahmid.

---------- Post added at 14:54 ---------- Previous post was at 14:52 ----------

Try it. The problem is i is declared as unsigned. So, when it equals 0, the condition in the for loop is still valid, however, it can't be decremented. So, you see.....
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top