Quick PIC code definition

Thread Starter

blah2222

Joined May 3, 2010
582
Hi, I just have a question regarding the syntax of this sample code that I was given to learn from. The code essentially is a 4-bit up-counter that lights up LEDs attached to pins RB12-15 on a PIC24HJ32P202 mcu.

The code is from Reese Micro, and after playing around with this, I thought I knew what was going on, but apparently I don't...

Anyway this is the code:

Rich (BB code):
// 4-bit binary counter on PortB LEDs
// PIC24HJ32GP202 running at ~40 MIPS

#include "pic24_all.h"

static inline void configure_inputs_from_switches(){
	CONFIG_RA0_AS_DIG_INPUT();
	ENABLE_RA0_PULLUP();	
	CONFIG_RA1_AS_DIG_INPUT();
	ENABLE_RA1_PULLUP();
	CONFIG_RA2_AS_DIG_INPUT();
	ENABLE_RA2_PULLUP();
	CONFIG_RA3_AS_DIG_INPUT();
	ENABLE_RA3_PULLUP();
	CONFIG_RA4_AS_DIG_INPUT();
	ENABLE_RA4_PULLUP();
}

static inline void configure_outputs_to_LEDs(){
	CONFIG_RB15_AS_DIG_OUTPUT();
	CONFIG_RB14_AS_DIG_OUTPUT();
	CONFIG_RB13_AS_DIG_OUTPUT();
	CONFIG_RB12_AS_DIG_OUTPUT();
}

int main (void)				
{

        configure_inputs_from_switches();
        configure_outputs_to_LEDs();

 	while (1) {					// Loop forever

		PORTB=LATB+1;	 // LATB is the current value on Port B
		DELAY_US(100);   // 100 microsecond delay gives approx 0.5
						 //second count period of 4 msb's of PortB
	}
}
I understand why you need to configure the pins, but what do RA0-4 have to do with RB12-15? Also, I tried playing around with the PORTB = LATB+1; line of code. I changed the '1' to a '2' and I thought that the counter would count up by '2' but instead it just sped up, still counting in binary by '1'. I don't really know what the difference between PORTB and LATB are.

So I guess my main questions are:

1) What do RA0-4 have to do with RB12-15?
2) What are PORTB and LATB doing?
3) What line of code actually causes the counter to... well... count?
4) Is there a way of just accessing 1 pin at a time?

I'm a major PIC noob and I realize there are a billion threads regarding PIC's but I am hoping this isn't too big of a mystery to solve.

Here is the USB PIC board that I'm using:



Cheers!
J
 
Last edited:

debjit625

Joined Apr 17, 2010
790
What do RA0-4 have to do with RB12-15?
Nothing,PORTA is been used for some sort of input like switch or any signal, and also the pins of PORTA are internally pulled up i.e.. they are connected to Vdd.
What are PORTB and LATB doing?
PORTB is an output register (memory),its each bit corresponds to a pin on your mcu.For example the lowest bit of PORTB i.e.. RB0 correspond to one of the pin in your mcu and if you make this bit high i.e. digitally 1 then the corresponding pin will also gets high. Now the LAT (latch register) is just another temporary register. When you want to write any value on any output register you don’t directly write them on output register you write them on LAT register and then transfer the value to the output register, for reading we also do the same, first transfer the value of output register to LAT register and then read it from the LAT in your software, why we do so? Because we may miss some information while reading it directly... in some case not always. Many of us read and write the output register directly, but Microchip doesn’t recommend this.

Understanding it may be difficult and explaining this is also some how difficult. You should Google for some PIC tutorials, their are a lot which will explain you stuff like registers.

What line of code actually causes the counter to... well... count?
Rich (BB code):
PORTB=LATB+1;
Is there a way of just accessing 1 pin at a time?
Access single bit like this...
Rich (BB code):
RB0 = 1; //it makes the lowest bit of PORTB high
Google for PIC tutorial and start with some 8 bit PIC like 16F84A (for tutorial).Also download the current datasheet for your mcu you will get a lots of information from it.

Good luck
 

Thread Starter

blah2222

Joined May 3, 2010
582
Thanks for the speedy reply! Just going back to the question about how it counts. Why did it speed up the counter period rather than count up by two when I changed the code to:

Rich (BB code):
PORTB = LATB+2;
?
 

ErnieM

Joined Apr 24, 2011
8,377
Because you don't have a LED on RB0. Your LEDs are on RB12-15, so the PIC has to count all the lower significant bits until it finally overflows to change RB12.

To make it count by two you need to change the increment to 8192. But that also changes the delay in your circuit as the pins now change 8192 times faster then the original program so you would want to change the delay time too, unless you can see a flash of only .0001 seconds.
 

Thread Starter

blah2222

Joined May 3, 2010
582
Because you don't have a LED on RB0. Your LEDs are on RB12-15, so the PIC has to count all the lower significant bits until it finally overflows to change RB12.

To make it count by two you need to change the increment to 8192. But that also changes the delay in your circuit as the pins now change 8192 times faster then the original program so you would want to change the delay time too, unless you can see a flash of only .0001 seconds.
Sorry to sound noobish, by why 8192? How did you come up with that increment?

I mean what significance does 8192 have? Other than it being \(2^{13}\), I have no idea why haha

J
 

debjit625

Joined Apr 17, 2010
790
blah2222 said:
Thanks for the speedy reply! Just going back to the question about how it counts. Why did it speed up the counter period rather than count up by two when I changed the code to:
ErnieM already explained but I will also try....

To really understand this you need to visualize the count pattern ,in your case the register is of 16 bits and it can count to 65535,which is very large pattern to visualize, so I will go with a very small example a register of 4 bits, we will mark the bits from highest (msb) to the lowest (lsb) i.e.. B3,B2,B1 and B0.And also assume that two LED are connected one at B3 and other at B2.Now when looking at the pattern ,when B3 and B2 will be 1 assume LEDs are on else off.

First I will start with the case of single increment i.e. +1,also look how many step it takes before it overflows

Rich (BB code):
B3 B2 B1 B0  Step
 0  0   0  0     0
 0  0   0  1     1
 0  0   1  0     2
 0  0   1  1     3
 0  1   0  0     4
 0  1   0  1     5
 0  1   1  0     6
 0  1   1  1     7
 1  0   0  0     8
 1  0   0  1     9
 1  0   1  0    10
 1  0   1  1    11
 1  1   0  0    12
 1  1   0  1    13
 1  1   1  0    14
 1  1   1  1    15
Now if you look only at the column B3 and B2 where LEDs are connected ,the count partern is like this,it takes 4 counts from step 0 to 3 for B3 to become 0 and B2 to become 1,then again 4 counts to become B3 = 1 and B2 = 0 i.e.. number 2 and so on,you get a count to number 3 i.e.. in binary 1 1 (B3 = 1,B2 = 1)and the counter overflows.

Now lets increment it by 2....
Rich (BB code):
B3 B2 B1 B0  Step
 0  0   0  0     0
 0  0   1  0     1
 0  1   0  0     2
 0  1   1  0     3
 1  0   0  0     4
 1  0   1  0     5
 1  1   0  0     6
 1  1   1  0     7
it takes only two step 0 and 1 to increment the count is same i.e.. 0,1,2,3 and overflows. So here your counting happens to be the same, just the step (i.e.. time) needed is less, so it counts faster.

I think now its a bit clear what’s happening try to look at the count patterns and how many steps it takes to complete the pattern.

if you really want to implement your logic i.e.. count up by '2' then connect your LEDs from RB0 - RB3 (consult your datasheet to see which pin corresponds to RB0 -RB3)

Rich (BB code):
// 4-bit binary counter on PortB LEDs
// PIC24HJ32GP202 running at ~40 MIPS
#include "pic24_all.h"
static inline void configure_inputs_from_switches(){
CONFIG_RA0_AS_DIG_INPUT();
ENABLE_RA0_PULLUP(); 
CONFIG_RA1_AS_DIG_INPUT();
ENABLE_RA1_PULLUP();
CONFIG_RA2_AS_DIG_INPUT();
ENABLE_RA2_PULLUP();
CONFIG_RA3_AS_DIG_INPUT();
ENABLE_RA3_PULLUP();
CONFIG_RA4_AS_DIG_INPUT();
ENABLE_RA4_PULLUP();
}
static inline void configure_outputs_to_LEDs(){
CONFIG_RB3_AS_DIG_OUTPUT();
CONFIG_RB2_AS_DIG_OUTPUT();
CONFIG_RB1_AS_DIG_OUTPUT();
CONFIG_RB0_AS_DIG_OUTPUT();
}
int main (void) 
{
        int i = 0; //loop variable
        configure_inputs_from_switches();
        configure_outputs_to_LEDs();
while (1) // Loop forever
{ 
if(LATB >= 15)
{
   LATB = 0;
   PORTB = LATB;
}
for(i=0;i<5;i++)
{
DELAY_MS(100); //0.5 sec or 500 milli sec delay for counter
}
PORTB=LATB+2; // LATB is the current value on Port B
}
 
}
One thing I don’t know which compiler you are using, so the function DELAY_MS() might not work, check it and tell us.

Good Luck
 
Last edited:

Thread Starter

blah2222

Joined May 3, 2010
582
Ahhhh! Now that makes perfect sense, thank you for the detailed explanation!. I forgot that I was dealing with a 16-bit register and my LED's are on the 4 MSB's.

The code you provided compiled but when I bootloaded the hex file none of the LEDs lit up, so I guess it has something to do with the library files.

Thanks again everyone!
J
 

debjit625

Joined Apr 17, 2010
790
The code you provided compiled but when I bootloaded the hex file none of the LEDs lit up
Have you wait for some time before counter to count 2...

so I guess it has something to do with the library files.
May be or not,as you are configuring your ports using lib functions it may cause.

Replace your main function with this and tell us if the LEDs are on or off.
Rich (BB code):
int main (void) 
{
        configure_inputs_from_switches();
        configure_outputs_to_LEDs();
        LATB = 0xFFFF;
        PORTB = LATB;
        while(1){}
}
if it works,then their may be some problem with the delay function.

Good Luck
 

Thread Starter

blah2222

Joined May 3, 2010
582
Have you wait for some time before counter to count 2...


May be or not,as you are configuring your ports using lib functions it may cause.

Replace your main function with this and tell us if the LEDs are on or off.
Rich (BB code):
int main (void) 
{
        configure_inputs_from_switches();
        configure_outputs_to_LEDs();
        LATB = 0xFFFF;
        PORTB = LATB;
        while(1){}
}
if it works,then their may be some problem with the delay function.

Good Luck
Yep, all the LEDs are ON. I guess it is the delay function.
 

Thread Starter

blah2222

Joined May 3, 2010
582
I guess another question comes to mind, with timing. The MPLAB build says this is running at 40MHz, is that the frequency that the while loop runs and the delay is just increasing the period of each count by 100us in this case?
 

debjit625

Joined Apr 17, 2010
790
I guess another question comes to mind, with timing. The MPLAB build says this is running at 40MHz, is that the frequency that the while loop runs and the delay is just increasing the period of each count by 100us in this case?
As already written above your code as a comment, if its really correct (I am not sure)
// PIC24HJ32GP202 running at ~40 MIPS
So it means that your PIC is executing ,approx 40 million instruction per second (MIPS) i.e.. almost each instruction (not all) takes about 25 nano seconds to execute we call this 1 instruction cycle, some instructions like branch or loops takes two or more instruction cycles to execute. So the "while()" loop is not executing at 40MHz its less than that, as their is the delay() for 100 micro seconds. But the count period is not 100 micro sec because it is using the 4 highest MSBs which divides the frequency of the "while()" loop further which actually gives a count period of 0.5 sec or 500 milli sec.

I am just curious why the delay function didn’t worked so again I am providing you a new code try it. This time I am using delay of micro sec rather milli sec (counter period is still same)

Rich (BB code):
int main (void) 
{
 int i = 0; //loop variable
 configure_inputs_from_switches();
 configure_outputs_to_LEDs();

while (1) // Loop forever
{ 
   if(LATB >= 0x000F)
    {
     LATB = 0x0000;
     PORTB = LATB;
    }

    for(i=0;i<5000;i++)
    {
     DELAY_US(100); //0.5 sec or 500 milli sec delay for counter
    }

    PORTB = LATB + 0x0002; // LATB is the current value on Port B
}

}
Good Luck
 

Thread Starter

blah2222

Joined May 3, 2010
582
Hey I loaded it on the PIC again. Compiled/built with no errors, but nothing lit up on the PIC. Not too sure what that is about...

Okay, so I have decided to try out assembly operations and I am following a good tutorial. The only thing is that this was made for a different PIC and though there is some similarity in the registers/ports, some of the command addresses seem hard to find.

For instance, there is a memory schematic on the Tutorial 2 page that shows the memory split into two types, Bank 1 and 2. To access either of the Banks, you have to enable the STATUS registers. I have searched through my PIC's datasheet and am having a hard time finding these registers. TRISA/B and PORTA/B are easy enough to address but these Bank STATUS' are hiding from me. You have also mentioned this LATA/B stuff, is that just a temporary working register similar to the W register in the Tutorial?

I essentially just want to blink RB15 on and off using this code framework, but I am missing the STATUS address:

Rich (BB code):
STATUS equ ??? ; don't know address... need this!
TRISB equ 02C8h ; constants
PORTB equ 02CAh ; constants

bsf STATUS,??? ; go to Bank 1
movlw 0000h ; move 0 to w
movwf TRISB ; move w to TRISB
bcf STATUS,??? ; go back to Bank 0

Start movlw 8000h ; move 8*(16^3) to w
       movwf PORTB ; turn on RB15
       movlw 0000h ; move 0 to w
       movwf PORTB ; reset PORTB
       goto Start ; repeat
 

debjit625

Joined Apr 17, 2010
790
You have a 16 bit PIC,which have many different features than many 8 bit PIC.The addressing mode is different you don’t have to set the status register to select banks to config the port's pin and its value. Here is an example from your datasheet look at page 96,

MOV 0xFF00, W0 ; Configure PORTB<15:8> as inputs
MOV W0, TRISBB ; and PORTB<7:0> as outputs


Your code will not blink the LED.
Start movlw 8000h ; move 8*(16^3) to w
movwf PORTB ; turn on RB15
movlw 0000h ; move 0 to w
movwf PORTB ; reset PORTB
goto Start ; repeat


Because, you turn on the LED on RB15 then you turn it off, you didn’t gave any delay between that, so the on and off action will happen very fast ,you will not able to visualize that. You need some kind of delay...

You have also mentioned this LATA/B stuff, is that just a temporary working register similar to the W register in the Tutorial?
No,W registers are different than LAT registers. In old days and also still today in many microcontrollers and microprocessors the temporary register used by the ALU is called accumulator register which is used to store result for arithmetic and logic stuff and also move content to memory i.e.. a temporary register used for ALU's operation.
Microchip named their accumulator register as working register. You have a 16 bit PIC which have 16 working registers W0,W1,W2.........W15,In many 8 bit PIC you will get only one i.e... W.

LAT registers are different, they belongs to some PORT register(like for PORTB their is LATB), they can only be used for writing and reading from a PORT. They shouldn’t be used for any other purpose.

In page 95 of your datasheet you will find

All port pins have three registers directly associated
with their operation as digital I/O. The data direction
register (TRISx) determines whether the pin is an input
or an output. If the data direction bit is .1., then the pin
is an input. All port pins are defined as inputs after a
Reset. Reads from the latch (LATx) read the latch.
Writes to the latch, write the latch. Reads from the port
(PORTx) read the port pins, while writes to the port pins
write the latch.
Why the delay is not working I dont have any idea,but later I will try to simulate the delay stuff and will post the result.

Good Luck​
 

Thread Starter

blah2222

Joined May 3, 2010
582
Thanks again! Very well explained, I can't thank you enough! Might the delay be too short, and it doesn't give the LED enough time to light up?

J
 

ErnieM

Joined Apr 24, 2011
8,377
<PARTY CRASHER>

Sorry to say that the line:
Rich (BB code):
// PIC24HJ32GP202 running at ~40 MIPS
does absolutely nothing to set the speed the core is running at. Further more, MPLAB doesn't have a check that the speed in that box is actually the speed the processor is running at.

Now there may well be code inside that "pic24_all.h" include file that sets the configuration bits so that this thing does indeed run at 40MHz, but without loading the project here I can't see that. (And I have yet to use a PIC24, I jumped from the PIC18's right to the PIC32's.)

I peeked a bit at the libs you downloaded but they are unreadable in notepad and my MPLAB is busy right now.

Inside MPLAB, if you hit "Configure | Configuration Bits..." and see a mess of bits that should get set in code. These can be very complex to understand and one good reason to stick to C and these header files.

</PARTY CRASHER>
 

Thread Starter

blah2222

Joined May 3, 2010
582
Ah, so there is no really way to quantify the frequency accurately?

Also, I came across this code segment for creating a delay in assembly, I am just curious what the instruction subroutine waiting is doing specifically line by line.

Rich (BB code):
1 - a_delay:
2 -   		    mov #0x0400, W1
3 - waiting:	    
4 -                dec W1, W1
5 - 		    bra NZ, waiting
6 - 		    return
Here is my guess:

1 - name of delay subroutine
2 - move 1024 to w1
3 - name of waiting subroutine
4 - decrement value of w1 by 1, and store in w1
5 - if w1 is nonzero, go back to waiting
6 - else (if zero), return to calling subroutine, exit a_delay

I just don't really get what 'bra' is doing and how that is connected to w1's value if it doesn't have w1 in that line.

Thanks!
J
 

debjit625

Joined Apr 17, 2010
790
Ah, so there is no really way to quantify the frequency accurately?
No there is,it depends on your system clock i.e.. the type of oscillator you are using and the configuration for the oscillator.See at page no 84 in your datasheet...

I just don't really get what 'bra' is doing and how that is connected to w1's value if it doesn't have w1 in that line.
bra is an instruction for branch operation,their are many format of branch instruction check the datasheet for instruction set.In your case its related to Z flag,the instruction (bra NZ, waiting) says that if Z flag is zero than branch i.e.. goto "waiting" else execute the next instruction.

Now how Z flag is set ?
Simple when you do a "dec" (dec W1, W1) operation and if the result is not zero the Z flag will be cleared i.e.. 0.

Good Luck
 

debjit625

Joined Apr 17, 2010
790
As I said when I will get time I will try to simulate the code so I did and got the problem I think so i.e.. why my given code was not running properly.

In PIC24HJ32GP202 you have many different peripheral which share the out ports pins, when a peripheral is enabled the associated out ports pin is disabled. In your case my last code (in post #7) is a 4 bit counter with the LED connected from RB0 - RB3,in that code we configure PORTB as digitally output port but we forgot that RB0-RB3 are associated with ADC peripheral and we need to disable the ADC peripheral.

Actually I didn’t forgot about that, it’s that you were using some helper function to configure your ports, I thought those configuration functions would configure the ports properly. Any way here is how to disable ADC peripheral pin configuration. You need to configure "AD1PCFGL" register. See page 173 of your datasheet.

You can disable the AN2-AN5 which are associated with RB0-RB3 ,just add this inside the configuration function or at the place your configuration function are called in the main function.
Rich (BB code):
Rich (BB code):
Rich (BB code):
AD1PCFGL = 0x003C;


Or disable all the ADC channels and set the PORT as digitally output port.
Rich (BB code):
AD1PCFGL = 0xFFFF;

Good Luck
 
Top