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.

[SOLVED] problem with the following MikroC code

Status
Not open for further replies.

ramza038

Junior Member level 3
Joined
Dec 27, 2011
Messages
28
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,438
- - - Updated - - -

Code:
void main()
{
    
    int A = 0;
    TRISA = 0;
    TRISB = 0xff;
    PORTA = 0;
    PORTB.RB4 = 0;
    PORTB.RB5 = 0;
    PORTB.RB6 = 0;
    PORTB.RB7 = 0;
    
  while(1)
  {     A = PORTB;
        switch(A)
        {
                case 0x08:
                PORTA.RB0 = 1;
                break;

                case 0x04:
                PORTA.RB0 = 0;
                break;

                case 0x0c:
                PORTA.RB1 = 1;
                break;

                case 0x02:
                PORTA.RB1 = 0;
                break;

                case 0x0a:
                PORTA.RB2 = 1;
                break;

                case 0x06:
                PORTA.RB2 = 0;
                break;

                case 0x0e:
                PORTA.RB3 = 1;
                break;

                case 0x01:
                PORTA.RB3 = 0;
                break;

                case 0x0d:
                PORTA = 0x0f;
                break;

                case 0x03:
                PORTA = 0;
                break;

                case 0x00:case 0x05:case 0x07:case 0x09:case 0x0b:case 0x0f:
                break;
        }
  }
}

It didn't work properly. Plz correct me.
Moreover, should i enabled or disabled power-up timer,watch-dog timer?
 

Hi,

What is the error code you get ?
I guess ".RBx" should be ".Bx",

- - - Updated - - -
 

One issue you are experiencing is the following:

Code:
PORT[COLOR="#FF0000"]A[/COLOR].R[COLOR="#FF0000"]B[/COLOR]0 = 1;

The above line of code mixes both PORTA and PORTB nomenclature.

I would suggest using either:

Code:
RB0 = 1;  //For first pin of PORTB

or

Code:
RA0 = 1; // For first pin of PORTA

Make sure you properly configure the appropriate TRISX register, either TRISB or TRISA respectfully as output for the particular pin.

The adding code to maintain the Watchdog Timer is one of the final stages of a commercial software development, in an introductory educational setting I would strongly advise disabling it.

The PowerOn Timer should be disable during code development as well.


BigDog
 
didnt work properly meaning did it compile alright? or did it show errors? did it even do a success built?
did u know the in mikroC PORTA.RA0 syntax is not supported its PORTA.F0
might be i am using a different version of MikroC cause in my version it doesnt support PORTA.RA0 syntax.
PORTA.RA0 is a MPLAB command. correct me if im wrong
 

Code:
void main()
{
    
    int A = 0;
    TRISA = 0;
    TRISB = 0xff;
    PORTA = 0;
    PORTB.RB4 = 0;
    PORTB.RB5 = 0;
    PORTB.RB6 = 0;
    PORTB.RB7 = 0;
    
  while(1)
  {     A = PORTB;
        switch(A)
        {
                case 0x08:
                PORTA.F0 = 1;
                break;

                case 0x04:
                PORTA.F0 = 0;
                break;

                case 0x0c:
                PORTA.F1 = 1;
                break;

                case 0x02:
                PORTA.F1 = 0;
                break;

                case 0x0a:
                PORTA.F2 = 1;
                break;

                case 0x06:
                PORTA.F2 = 0;
                break;

                case 0x0e:
                PORTA.F3 = 1;
                break;

                case 0x01:
                PORTA.F3 = 0;
                break;

                case 0x0d:
                PORTA = 0x0f;
                break;

                case 0x03:
                PORTA = 0;
                break;

                case 0x00:case 0x05:case 0x07:case 0x09:case 0x0b:case 0x0f:
                break;
        }
  }
}
 

- - - Updated - - -

One issue you are experiencing is the following:

Code:
PORT[COLOR="#FF0000"]A[/COLOR].R[COLOR="#FF0000"]B[/COLOR]0 = 1;

The above line of code mixes both PORTA and PORTB nomenclature.

I would suggest using either:

Code:
RB0 = 1;  //For first pin of PORTB

or

Code:
RA0 = 1; // For first pin of PORTA

Make sure you properly configure the appropriate TRISX register, either TRISB or TRISA respectfully as output for the particular pin.

The adding code to maintain the Watchdog Timer is one of the final stages of a commercial software development, in an introductory educational setting I would strongly advise disabling it.

The PowerOn Timer should be disable during code development as well.


BigDog

Thank! I see my error now. I will correct and test it now. But my posted code worked in Proteus. So i developed a pcb and it didn't work properly. Moreover i want to know the concepts for PORT initialization(not TRIS) and timers (watch-dog and power-up). I used pic kit2 clone as the programmer.
 
Last edited:

I corrected and tested. But it also didn't work. I disabled timers. My new code is
Code:
void main()
{
    TRISA = 0;
    TRISB = 0xff;
    PORTA = 0;
    PORTB.RB4 = 0;
    PORTB.RB5 = 0;
    PORTB.RB6 = 0;
    PORTB.RB7 = 0;
    
  while(1)
  {     switch(PORTB)
        {
                case 0x08:
                PORTA.RA0 = 1;
                break;

                case 0x04:
                PORTA.RA0 = 0;
                break;

                case 0x0c:
                PORTA.RA1 = 1;
                break;

                case 0x02:
                PORTA.RA1 = 0;
                break;

                case 0x0a:
                PORTA.RA2 = 1;
                break;

                case 0x06:
                PORTA.RA2 = 0;
                break;

                case 0x0e:
                PORTA.RA3 = 1;
                break;

                case 0x01:
                PORTA.RA3 = 0;
                break;

                case 0x0d:
                PORTA = 0x0f;
                break;

                case 0x03:
                PORTA = 0;
                break;

                case 0x00:case 0x05:case 0x07:case 0x09:case 0x0b:case 0x0f:
                break;
        }
  }
}
My circuit design is untitled.JPG

- - - Updated - - -
 

Where are the current limiting resistors for the LEDs?

Driving an LED without a current limiting resistor may function in simulation, however I can assure you it will a source of issues in real hardware.

I would also strongly suggest utilizing bypass capacitors with the PIC and other ICs in your design.

BigDog
 

Where are the current limiting resistors for the LEDs?

Driving an LED without a current limiting resistor may function in simulation, however I can assure you it will a source of issues in real hardware.

I would also strongly suggest utilizing bypass capacitors with the PIC and other ICs in your design.

BigDog

Sorry! I used 470 ohms resistors in real hardware but no bypass capacitors? How to connect bypass cap?

- - - Updated - - -

Where are the current limiting resistors for the LEDs?

Driving an LED without a current limiting resistor may function in simulation, however I can assure you it will a source of issues in real hardware.

I would also strongly suggest utilizing bypass capacitors with the PIC and other ICs in your design.

BigDog

Sorry! I used 470 ohms resistors in real hardware but no bypass capacitors? How to connect bypass cap?

- - - Updated - - -

plz help me.
 

One issue you are experiencing is the following:

Code:
PORT[COLOR="#FF0000"]A[/COLOR].R[COLOR="#FF0000"]B[/COLOR]0 = 1;

The above line of code mixes both PORTA and PORTB nomenclature.

I would suggest using either:

Code:
RB0 = 1;  //For first pin of PORTB

or

Code:
RA0 = 1; // For first pin of PORTA

Although the line of code mentioned:

Code:
PORT[COLOR="#FF0000"]A[/COLOR].R[COLOR="#FF0000"]B[/COLOR]0 = 1;

should be avoided, it is not incorrect. RB0 is a constant which is equal to 0. So is RA0. So, RA0=RB0=constant 0. So, the above mentioned line should both compile and function properly, but should be avoided for the reason mentioned.

Code:
RB0 = 1;  //For first pin of PORTB

Code:
RA0 = 1; // For first pin of PORTA

These are incorrect, as RB0 and RA0 are constants. So, you cannot assign any value to them.

For PORTA0, any one of the following can be used:
Code:
PORTA.RA0
PORTA.RB0 ------ should be avoided
PORTA.F0
PORTA.B0
RA0_bit

Hope this helps.
Tahmid.
 
Last edited:

Try use sbit so that you can access each bits of a reg.

The mikroC PRO for PIC allows you to access individual bits of 8-bit variables. It also supports sbit and bit data types.

eg: sbit LED at PORTB.B0;
LED=1; //makes PORTB.B0 high
LED=0; //makes PORTB.B0 low
 

What you're saying is that it works in Proteus but not in practice when you're using PCB. Is that right?

Did you connect the PIC VDD and VSS to 5V and ground respectively?

The bypass capacitors should be connected across the VDD and VSS pins. You can use 0.1uF capacitor in parallel with a 10uF capacitor.
 

Yes i connected VDD and VSS to 5v and ground respectively. Plz look at my attached design. Did u say .1uF to VDD pin and 10uf to Vss?
 

A ceramic bypass or decoupling capacitor of 0.1uF attached as close as possible to the PIC between the Vdd (5V) and Vss (GND) pins will suffice for the Bypass requirements.

For further information pertaining to bypass/decoupling capacitors:

Decoupling capacitor

Choosing and Using Bypass Capacitors


BigDog
 

Should i ground the remaining port A and B pins? Pull-down resistors needed?
 

Should i ground the remaining port A and B pins? Pull-down resistors needed?

Although not a requirement, leaving any CMOS inputs floating is usually a poor option. Not only can they lead to spurious noise which can effect future firmware unexpectedly, they can lead to significant current consumption.

I usually opt for the solution of ensuring that all unused pins are configured as outputs. However, be aware that most PICs on reset configure their port I/O as inputs, TRISx are set 0xFF.

The following comment is from the Microchip KnowledgeBase:

There is no requirement to tie unused I/O pins high or low but you can do so to avoid floating I/O pins. Floating inputs can lead to excessive switching currents as a result of random charge collecting on the high impedance CMOS gates used to sense logic high and low levels. By terminating unused inputs, you will avoid unwanted current consumption.

A simpler solution is to turn the unconnected pins into outputs. Floating I/O pins can cause up to approximately 80uA per pin of extra current to be consumed by the part so there is a definite advantage if you do not leave them floating.

A discussion of the topic follows:

What should I do with pins that I am not using?


BigDog
 
One issue you are experiencing is the following:
Code:
PORT[COLOR="#FF0000"]A[/COLOR].R[COLOR="#FF0000"]B[/COLOR]0 = 1;
The above line of code mixes both PORTA and PORTB nomenclature.


Although the line of code mentioned:
Code:
PORT[COLOR="#FF0000"]A[/COLOR].R[COLOR="#FF0000"]B[/COLOR]0 = 1;
should be avoided, it is not incorrect. RB0 is a constant which is equal to 0. So is RA0. So, RA0=RB0=constant 0. So, the above mentioned line should both compile and function properly, but should be avoided for the reason mentioned.

No one said it is incorrect in the first place, I think you should familiarize yourself with the meaning of nomenclature:
  • A name.
  • A set of names or terms.
  • A set of rules used for forming the names or terms in a particular field of arts or sciences.
 

No one said it is incorrect in the first place,

Yes. No one said it is incorrect. And I haven't said that any one has said it is incorrect either. I just stated that it isn't incorrect and explained the reason why.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top