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:
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
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
}
}
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: