[SOLVED] changing port NAME using for/while loop

Status
Not open for further replies.

jsbhalla88

Member level 1
Joined
Mar 3, 2015
Messages
41
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Visit site
Activity points
314
hello everyone,
I am writing code to make commands for testing different PCBs.
there have to be all kinds of commands in order to read status of any port, write to any port, any pin..i have made code for 2 pins now, and I think it be a very lengthy code and might eat up all the memory..

I am curious to know if there is a way to change the port and pin using a for loop or while, or any other method.

for example, for accessing pin RD4, I have to write TRISD, ANSELD, PORTD., similar instructions..
is there a way I could pass the port name (A/B/C/D/E)??
OR PASS THE PIN NUMBERS?
I am using PIC18F45K22 with MikroC Pro for PIC.
 
Last edited:

Just an example:
Code:
typedef struct 
{
  GPIO_TypeDef * GPIOx; 
  GPIO_Pin_TypeDef PINx;
} PIN_TypeDef;
Code:
const PIN_TypeDef DIG[5] = {GPIOD, GPIO_PIN_4,          //1
                            GPIOD, GPIO_PIN_5,          //2
                            GPIOA, GPIO_PIN_3,          //3
                            GPIOD, GPIO_PIN_2,          //4
                            GPIOD, GPIO_PIN_3};         //5

const PIN_TypeDef SEGM[8] = {GPIOC, GPIO_PIN_6,         //A
                             GPIOB, GPIO_PIN_5,         //B
                             GPIOB, GPIO_PIN_4,         //C
                             GPIOC, GPIO_PIN_4,         //D
                             GPIOC, GPIO_PIN_3,         //E
                             GPIOC, GPIO_PIN_5,         //F
                             GPIOA, GPIO_PIN_2,         //G
                             GPIOA, GPIO_PIN_1};        //DP
Code:
  /* GPIO INIT */
  for (cnt = 0; cnt != 5;  cnt++) GPIO_Init(DIG[cnt].GPIOx,  DIG[cnt].PINx,  GPIO_MODE_OUT_PP_HIGH_FAST);
  for (cnt = 0; cnt != 8; cnt++){GPIO_Init(SEGM[cnt].GPIOx, SEGM[cnt].PINx, GPIO_MODE_OUT_PP_LOW_FAST); delay_ms(100);}
 


I think what you have mentioned is related to HAL libraries of STM or ARM.
I got your idea., but I am not sure if this will work with PIC18F...
moreover, I don't know how to implement it with PIC..
 
Last edited:

I told you - that was an example. Do the same for PIC. No big difference. Even more - pic's pereferial registers not declared as a structure with base address. It is just an defined address location for each register.
 

i tried, but couldn't start. I don't know how to write it with PIC.
I have TRISx, ANSELx, LATx, PORTx for accessing ports, where x is A/B/C/D/E
for pins, you simply write TRISx.Ry, where y is bit number from 0 to 7
similarly for ANSEL, LAT, PORT...
can you tell me what should i put in struct?
 

the link to mikroe.. had to modify it to pass pins a argument as well as configure the pins using the same....
 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…