View previous topic :: View next topic |
Author |
Message |
rogersw8n
Joined: 15 Apr 2015 Posts: 3
|
e3 Board Programming |
Posted: Wed Apr 15, 2015 7:03 pm |
|
|
Hi,
I am new to microprocessor development. I purchased the Embedded C Programming book by Mark Siegesmund which came with an e3 board development kit. I have been able to run through the tutorials without too much issue. However, I would like to turn LED's on and off without using the output_high and output_low built in functions. I have read portions of the datasheet describing PORT, TRIS, and LATCH registers. I have set the appropriate PINS to output I believe with TRISC and set the logical value in the LATCH register but the LED does not change state.
Here is my code:
Code: |
#include <e3.h>
#include <ios.h>
#define _test
void main(void){
//set_tris_c(8);
struct {
int bit0;
int bit1;
int bit2;
int bit3;
int bit4;
int bit5;
int bit6;
int bit7;
} PORTC;
struct {
int bit0;
int bit1;
int bit2;
int bit3;
int bit4;
int bit5;
int bit6;
int bit7;
} LATC;
struct {
int bit0;
int bit1;
int bit2;
int bit3;
int bit4;
int bit5;
int bit6;
int bit7;
} TRISC;
TRISC = 0xF94;
LATC = 0xF8B;
PORTC = 0xF82;
TRISC.bit3 = 0;
//PORTC = 31763;
while (1){
LATC.bit3 = 1;
delay_ms(1000);
LATC.bit3 = 0;
delay_ms(1000);
}
|
I am trying to change the red led which is PIN_C3 and works great when using the built in functions.
Two more notes: when running with the built in functions the #define for PIN_C3 is 37163. I have been unable to find what 31763 references. In the datasheet. The address for the TRISC is F94h. The address for LATC is F8B. When viewing the .lst the assembler code for output_high makes changes to F94.3 and F8B.3 with BCF and BSF so I know I have the addresses correct that I assigned to TRISC and LATC in my code.
Thank you very much for your time. |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Apr 15, 2015 7:17 pm |
|
|
You need to re-do your structures like this:
Code: |
struct {
int bit0 : 1;
int bit1 : 1;
int bit2 : 1;
int bit3 : 1;
int bit4 : 1;
int bit5 : 1;
int bit6 : 1;
int bit7 : 1;
} PORTC; |
This tells the compiler that each element is only 1 bit. Do the other two
structures the same way.
You need to re-do your register assignments as #byte statements, like this:
Code: | #byte TRISC = 0xF94
#byte LATC = 0xF8B
#byte PORTC = 0xF82
|
All this stuff should be put above main().
Quote: | I have been unable to find what 31763 references. |
Using Windows Calculator, convert it to hex and divide it by 8. Then
you'll see the address. Also take the modulus (supported by Windows
calculator) and you'll see the bit number. |
|
|
rogersw8n
Joined: 15 Apr 2015 Posts: 3
|
|
Posted: Wed Apr 15, 2015 7:35 pm |
|
|
Thank you very much. All points were exactly right. I see blinking....
Also, I see that converting 31763 to hex is 7C13. Dividing by 8 gets me the PORTC address of F82. Awesome! Why the divide by 8? Thanks for your patience.
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Wed Apr 15, 2015 7:41 pm |
|
|
Because the bit number is encoded in the bottom 3 bits. That's why I
told you to take the modulus of 8, and you'll see the bit number. |
|
|
rogersw8n
Joined: 15 Apr 2015 Posts: 3
|
|
Posted: Wed Apr 15, 2015 7:46 pm |
|
|
Got it. Thanks again! |
|
|
|