4 ms·
I wonder if this also allows for grouping registers together as a macro, rather than having to allocate an array to track ports and registers like digitalWrite(
by mofosyne 5y ago
I wonder if this also allows for grouping registers together as a macro, rather than having to allocate an array to track ports and registers like digitalWrite() in arduino.
e.g. Instead of writing...
```
ANSELBbits.ANSB3 = 0; // RB3 is digital (pinMode)
TRISBbits.TRISB3 = 0; // RB3 is output (pinMode)
LATBbits.LATB3 = 1; // RB3 is set to high (digitalWrite)
```
we would be able to write the macro like below, where `ANSEL[RB3] --> ANSELBbits.ANSB3`
```
pinMode(RB3, OUTPUT);
digitalWrite(RB3, HIGH);
```
- AlbertoGP 5y agoI’m trying to see if this could be done without writing your own macro in C (put it inside «src/macros/», list it in «src/macros.h»), but I first need to understand it correctly. Since «ANSELBbits.ANSB3 = 0» sets the write to digital, would it not belong in «digitalWrite()» instead? Would it work if the generated code was like this? TRISBbits.TRISB3 = 0; // RB3 is output (pinMode) ANSELBbits.ANSB3 = 0; // RB3 is digital (digitalWrite) LATBbits.LATB3 = 1; // RB3 is set to high (digitalWrite) EDIT: Assuming I got the meaning right, you can get quite close with standard C macros, but since I’m here demoing Cedro we could as well use it: #pragma Cedro 1.0 enum { OUTPUT = 0, INPUT = 1 }; #define { pinMode(REGISTER, DIRECTION) TRISBbits.TRIS##REGISTER = DIRECTION #define } enum { LOW = 0, HIGH = 1, DIGITAL = 0, ANALOG = 1 }; #define { digitalWrite(REGISTER, LEVEL) ANSELBbits.ANS##REGISTER = DIGITAL; LATBbits.LAT##REGISTER = LEVEL #define } #define { analogWrite(REGISTER, LEVEL) ANSELBbits.ANS##REGISTER = ANALOG; LATBbits.LAT##REGISTER = LEVEL; #define } int main(void) { TRISBbits.TRISB3 = 0; // RB3 is output (pinMode) ANSELBbits.ANSB3 = 0; // RB3 is digital (digitalWrite) LATBbits.LATB3 = 1; // RB3 is set to high (digitalWrite) pinMode(B3, OUTPUT); digitalWrite(B3, HIGH); } If you put that in a file («regtest.c») and run it through Cedro and the standard pre-processor, you get this: $ ../bin/cedrocc -E regtest.c # 1 "<stdin>" # 1 "<built-in>" # 1 "<command-line>" # 31 "<command-line>" # 1 "/usr/include/stdc-predef.h" 1 3 4 # 32 "<command-line>" 2 # 1 "<stdin>" # 3 "regtest.c" enum { OUTPUT = 0, INPUT = 0 }; # 9 "regtest.c" enum { LOW = 0, HIGH = 1, DIGITAL = 0, ANALOG = 0 }; # 16 "regtest.c" # 21 "regtest.c" int main(void) { TRISBbits.TRISB3 = 0; ANSELBbits.ANSB3 = 0; LATBbits.LATB3 = 1; TRISBbits.TRISB3 = OUTPUT; ANSELBbits.ANSB3 = DIGITAL; LATBbits.LATB3 = HIGH; } The only part that uses Cedro is the block #defines for «pinMode()» and «digitalWrite()». EDIT2: I suspect that what you really need is a «macro» that checks that the register has been last set to the correct direction (INPUT/OUTPUT) and mode (DIGITAL/ANALOG) before calling the «…Write()»/«…Read()» functions. For instance, if B3 was last set to ANALOG and you call «digitalWrite(B3, …)», it would insert «ANSELBbits.ANSB3 = DIGITAL», but not if it was last set to DIGITAL, and so on for all function variants: «digitalWrite()», «digitalRead()», «analogWrite()», «analogRead()». You could write such a function in «src/macros/pic32.c», list it in «src/macros.h», and Cedro would run it.