Nothing new but I hope it helps you to understand structures?
Regards Bill Legge in Australia
Code: Select all
////////////////////////////////////////////////////////////////////////////////
// Project: 0000_Structures_Pointers //
// Function: Test functionjs with structures and pointers //
// MCU: PIC18F8722 //
// Board: BigPIC5 //
// Power 5V //
// Compiler: mikroC PRO for PIC version 7.6.0 //
// Author: WVL //
// Date: January 2021 //
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Function - direct PrintOut stream to UART2 //
void U2(char c){UART2_Write(c);} //
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
struct date{ // to hold values from the RTC or GPS //
char day; // days 1 to 31 //
char mon; // months 1 to 12 //
char yer; // years 0 to 99 //
}; //
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
struct time{ // to hold values from the RTC or GPS //
char sec; // seconds 0 to 59 //
char min; // minutes 0 to 59 //
char hrs; // hours 0 to 23 (24-hour time) //
}; //
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
typedef struct position{ //
char x; //
char y; //
}; //
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Function - pass a pointer to the structure position //
void update_position(struct position *my_ptr){ //
my_ptr->x ++; //
my_ptr->y ++; //
} //
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
// Function - pass a pointer to a structure time //
void update_time(struct time *my_ptr){ //
my_ptr->sec++; //
my_ptr->min++; //
my_ptr->hrs++; //
} //
////////////////////////////////////////////////////////////////////////////////
void main() {
// Block variables
char n = 0;
char my_string[6];
// create pointers and variables with structures
struct date *PFC_date_ptr, PFC_date={11,12,13}, *GPS_date_ptr, GPS_date={11,12,13};
struct time *PFC_time_ptr, PFC_time={21,22,23}, *GPS_time_ptr, GPS_time={21,22,23};
// create a number of variables with the same structure
struct position pos_1;
struct position pos_2={1,2}, pos_3={3,4};
pos_1.x = 33;
GPS_date.day = 15;
Update_position(&pos_2);
// Init peripherals
INTCON2.RBPU = 1; // PORTB pullups disabled
ADCON1 = 0b00001101; // A0 and A1 are analog inputs
CMCON = 0b00000111; // disable comparators
// Init ports
TRISD = 0b00000000; // LEDs on PORTD active low
UART1_Init(4800); // UART1 on C6/tx and C7/rx
UART2_Init(4800); // UART2 on G1/tx and G2/rx
// Announce
UART2_Write_Text("\r\nTest pointers to structures\r\n");
while(1){
// Use a function to update a structure
update_time(&GPS_time);
UART2_Write_Text("GPS_time values:");
ByteToStr(GPS_time.sec,my_string);
UART2_Write_Text(my_string);
ByteToStr(GPS_time.min,my_string);
UART2_Write_Text(my_string);
ByteToStr(GPS_time.hrs,my_string);
UART2_Write_Text(my_string);
UART2_Write_Text("\r\n\n");
update_position(&Pos_2);
UART2_Write_Text("Pos_2:");
ByteToStr(Pos_2.x,my_string);
UART2_Write_Text(my_string);
ByteToStr(Pos_2.y,my_string);
UART2_Write_Text(my_string);
UART2_Write_Text("\r\n\n");
delay_ms(1000);
LATD.B0 =~LATD.B0;
}
}