Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

how does int main work in C language ?

Status
Not open for further replies.

vead

Full Member level 5
Joined
Nov 27, 2011
Messages
285
Helped
3
Reputation
6
Reaction score
3
Trophy points
1,298
Location
india
Activity points
3,815
Hello Everyone
I need help to understand C program.
there are two flags we can set or reset flags.
Can anyone explain ,how does int main work in C language? what is happeneing in program ?

Code C (Mac) - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int main()
{
 
      if (  ( Task1flag == 1 ) && ( Task1serviced == 0 ) &&  ( Task2flag                                     
            == 0 ) ) {     
    
     Task1serviced = 1;
}
 
if (  (Task2flag == 1 ) && (Task2serviced == 0 )  && (Task1serviced == 1)){
  
    Task2serviced = 2;
  
    Task1flag = Task1serviced = Task2flag = Task2serviced = 0; 
}
}

 
Last edited:

the 'main()' is the function called by loader(of OS) when you start executing the program.
 

Main() is the entry point when the OS calls the program and 'int main()' means it is expected to return an integer value to the OS when it exits.

Your code doesn't return any particular value so the 'int' it returns will either be zero or some value decided by the compiler. Note that returning any value is only applicable in cases where the program is called from elsewhere, in embedded applications there is usually nowhere to return the value to!

Brian.
 
  • Like
Reactions: vead

    vead

    Points: 2
    Helpful Answer Positive Rating
Assumptions, that task2 never is serviced unless task1 has been serviced.
All flags cleared after both task have been serviced.

Code:
CY_ISR (AlarmIsrHandler)
 
{
     if ( ( Task1flag == 0 ) && (Task2flag == 0 ) ) {            

              Task1flag = 1;
             return;
     }

     if ( ( Task1flag == 1 ) && (Task2flag == 0 ) ) {           

              Task2flag = 1;
             return;
     }
}
int main()
{
 
      if (  ( Task1flag == 1 ) && ( Task1serviced == 0 ) &&  ( Task2flag                                     
            == 0 ) ) {     
    
     Task1serviced = 1;
}
 
if (  (Task2flag == 1 ) && (Task2serviced == 0 )  && (Task1serviced == 1)){
  
    Task2serviced = 2;
  
    Task1flag = Task1serviced = Task2flag = Task2serviced = 0; 
}
}
 
Last edited:

A C program consisting of the shown code will do nothing.

- the flags aren't set to other than the initial values before main() is entered
- the variable writes have no effect because the application terminates when leaving main()

No idea where you copied the code snippets, but apparently you cut all meaningful details.
 

that is part of program I did not post all program.

I have tested code for one alarm , its working but when I try to set next alarm , I am getting problem

https://www.cypress.com/forum/psoc-4-architecture/how-execute-two-alarms-different-flags

also look the attachment

here I am trying to make code using attachment
Code:
#include <project.h>
#include <stdio.h>

/* Time: 02:59:50 */
#define TIME_HOUR           (0x02u)
#define TIME_MIN            (0x00u)
#define TIME_SEC            (0x00u)
#define HOUR_OFFSET         (16u)
#define MIN_OFFSET          (8u)
#define TIME_HR_MIN_SEC     ((uint32)(TIME_HOUR << HOUR_OFFSET) | \
                            (uint32)(TIME_MIN << MIN_OFFSET)    | \
                             TIME_SEC)
/* Date: 03/22/2016 */
#define DATE_MONTH          (RTC_MARCH)
#define DATE_DAY            (0x22u)
#define DATE_YEAR           (0x2016u)
#define MONTH_OFFSET        (24u)
#define DAY_OFFSET          (16u)
#define DATE_MONTH_DAY_YEAR ((uint32)(DATE_MONTH << MONTH_OFFSET)   | \
                            (uint32)(DATE_DAY << DAY_OFFSET)        | \
                             DATE_YEAR)

/* Alarm1 Time: 02:59:55 */
#define ALARM_HOUR                  (0x02u)
#define ALARM_MIN                   (0x01u)
#define ALARM_SEC                   (0x50u)
#define ALARM_TIME_HR_MIN_SEC       ((uint32)(ALARM_HOUR << HOUR_OFFSET) | \
                                    (uint32)(ALARM_MIN << MIN_OFFSET)    | \
                                     ALARM_SEC)
                                   
/* Alarm2 Time: 02:59:55 */
#define NALARM_HOUR                  (0x02u)
#define NALARM_MIN                   (0x15u)
#define NALARM_SEC                   (0x50u)
#define NALARM_TIME_HR_MIN_SEC       ((uint32)(ALARM_HOUR << HOUR_OFFSET) | \
                                    (uint32)(ALARM_MIN << MIN_OFFSET)    | \
                                     ALARM_SEC)                                    
                                    
/* Alarm Date: 03/22/2016 */
#define ALARM_DATE_MONTH_DAY_YEAR   (DATE_MONTH_DAY_YEAR)


#define SYSTICK_EACH_10_HZ  (10u)
#define SYSTICK_RELOAD      (CYDEV_BCLK__SYSCLK__HZ / SYSTICK_EACH_10_HZ)

/* Alarm1 and alarm2 structure declaration */
RTC_DATE_TIME alarmTimeDate;
RTC_DATE_TIME alarmTime;


/* Alarm flags initialization */
 uint8 Alarm1flag = 0 ;
 uint8 Alarm1serviced = 0;
 uint8 Alarm2flag = 0;
 uint8 Alarm2serviced = 0;

/* Interrupt prototype */
CY_ISR_PROTO(SysTickIsrHandler);

/* Interrupt prototypes */
CY_ISR_PROTO(AlarmIsrHandler);


CY_ISR (AlarmIsrHandler)
 
{
     if ( ( Alarm1flag == 0 ) && (Alarm2flag == 0 ) ) {               // Only set Alarm1flag if both it and Alarm2flag clear

              Alarm1flag = 1;
             return;
     }

     if ( ( Alarm1flag == 1 ) && (Alarm2flag == 0 ) ) {             // Only set Alarm2flag if Alarm1flag has been set

              Alarm2flag = 1;
             return;
     }
}




int main()
{
    /* Place your initialization/startup code here (e.g. MyInst_Start()) */

    char timeBuffer[16u];
    char dateBuffer[16u];

    uint32 time;
    uint32 date;
    uint32 i;

    
   /* Starts SysTick component */
    CySysTickStart();

    /* Configure SysTick timer to generate interrupt every 100 ms */
    CySysTickSetReload(SYSTICK_RELOAD);

    /* Find unused callback slot. */
    for (i = 0u; i < CY_SYS_SYST_NUM_OF_CALLBACKS; ++i)
    {
        if (CySysTickGetCallback(i) == NULL)
        {
            /* Set callback */
            CySysTickSetCallback(i, SysTickIsrHandler);
            break;
        }
    }


      if (  ( Alarm1flag == 1 ) && ( Alarm1serviced == 0 ) &&  ( Alarm2flag == 0 ) ) {     // Only service if conditions met
    
     Alarm1serviced = 1;
}

if (  ( Alarm2flag == 1 ) && ( Alarm2serviced == 0 )  && ( Alarm1serviced == 1)){
  
    Alarm2serviced = 2;
  
    Alarm1flag =Alarm1serviced = Alarm2flag =Alarm2serviced = 0;     // Reset all flags
}

    /* Alarm1 structure initialization */
    alarmTimeDate.time = ALARM_TIME_HR_MIN_SEC;
    alarmTimeDate.date = ALARM_DATE_MONTH_DAY_YEAR;
    
    
    /* Alarm2 structure initialization */
    alarmTime.time = NALARM_TIME_HR_MIN_SEC;
    alarmTime.date = ALARM_DATE_MONTH_DAY_YEAR;
    

    
    /* Starts RTC component */
    RTC_Start();
    
    LCD_Start();
    /* Set Date and Time */
    RTC_SetDateAndTime(TIME_HR_MIN_SEC,DATE_MONTH_DAY_YEAR);

    
      /* Set alarm mask */
    RTC_SetAlarmMask(RTC_ALARM_SEC_MASK   | RTC_ALARM_MIN_MASK        |
                       RTC_ALARM_HOUR_MASK  | RTC_ALARM_DAYOFMONTH_MASK |
                       RTC_ALARM_MONTH_MASK | RTC_ALARM_YEAR_MASK);

    
  
    
    /* Set Alarm Date and Time */
    RTC_SetAlarmDateAndTime(&alarmTimeDate);
  
  


   /* Set function AlarmIsrHandler to be called when alarm triggers */
    RTC_SetAlarmHandler(AlarmIsrHandler);

    /* Enable global interrupts */
    CyGlobalIntEnable;

    while(1)
    {

        /* Get Date and Time from RTC */
        time = RTC_GetTime();
        date = RTC_GetDate();

        /* Print Date and Time to UART */
        sprintf(timeBuffer, "%02lu:%02lu:%02lu", RTC_GetHours(time), RTC_GetMinutes(time), RTC_GetSecond(time));
        sprintf(dateBuffer, "%02lu/%02lu/%02lu", RTC_GetMonth(date), RTC_GetDay(date), RTC_GetYear(date));

           LCD_Position( 0u, 2u);
        LCD_PrintString(timeBuffer);
        LCD_Position( 1u, 2u );
        LCD_PrintString(dateBuffer);
        

        CyDelay(200u);
    }
}

   
        
    void SysTickIsrHandler(void)
{
    RTC_Update();
}
 

Attachments

  • Alarm Logic RTC.doc
    27.5 KB · Views: 71
Last edited:

It looks like you copied some template code to your main() without understanding it's meaning. To perform real action, it must be included in the continuously executed while(1) loop, but you put it to the initialization part before it. And you didn't write actual code for the "service alarm" placeholder, so there isn't any alarm action.
 

as I told you in previous post. I have been tested code on LCD using LED for the first alarm , its work ,there are the API. I have just used API of RTC component I asked question for second Alarm, I found document, that I have attached in my post

I want to ask you ? what the information in document ? also I tried to make code according the document

please look the document ?
 

I did read the document. As said, the "main" part should go to the while(1) loop, supplemented with meaningful action code.
 

I did read the document. As said, the "main" part should go to the while(1) loop, supplemented with meaningful action code.

sorry actualy I am not understanding what do you mean. can you explain with example
 

I mean that the alamflags related code in main()
- must be executed repeatedly, not only once
- should perform real action, e.g. light a lamp, sound a buzzer, etc.
 

betwixt,
Main() is the entry point when the OS calls the program
I was always under that the it isn't the 'OS' that calls the 'main' function - but instead the main function calls the 'OS' (that is by itself a function) like this:
Code:
int main()
{
  run_os () ;
}
In our C project We have only one 'main' function and nothing "runs" outside of it - the OS is no exception.
Am I wrong?
 

Yes, you are I'm afraid, althogh initially you didn't explain this is an embedded program, the code you showed could be part of an application running on another system.

'main' is itself a function and like all 'C' functions it should return either a data type or be a 'void' function to indicate it returns nothing. You are stating it returns an integer value when it exits which doesn't make sense if it is intended to run forever. In normal 'C' you would declare it 'void main()' and get a compilation error if there was no 'return' passing a value back but it is possible your compiler requires it to be done differently.

The only instance you would call the OS from inside 'main' is if you have written the code for the operating system itself. Normally, the OS is the program that decides which program you want to run and calls it's entry point (main). In an embedded application, you are quite right there is no 'outside' operating system because all the work is done inside your program but as I pointed out, where is the return value expected to go if it never exits?

Brian.
 
  • Like
Reactions: shaiko

    shaiko

    Points: 2
    Helpful Answer Positive Rating
I meant embedded.
Sorry for not making it clear.
 

I mean that the alamflags related code in main()
- must be executed repeatedly, not only once
- should perform real action, e.g. light a lamp, sound a buzzer, etc.

you mean Like this , I have removed from main function

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
while(1)
    {
if (  ( Alarm1flag == 1 ) && ( Alarm1serviced == 0 ) &&  ( Alarm2flag == 0 ) ) {     // Only service if conditions met
    
     Alarm1serviced = 1;
}
 
if (  ( Alarm2flag == 1 ) && ( Alarm2serviced == 0 )  && ( Alarm1serviced == 1)){
  
    Alarm2serviced = 2;
  
    Alarm1flag =Alarm1serviced = Alarm2flag =Alarm2serviced = 0;     // Reset all flags
}

 

you mean Like this
Yes. But inside main(). There can be only one repeatedly while(1) loop inside main() and the code goes there.

And you have to write action code for "service alarm1" and "service alarm2".
 

thank you for helping me. can you tell me, the sequence of ISR . I have placed ISR in above of int main function . Is that in correct place ?
Code:
#include <project.h>
#include <stdio.h>

/* Time: 02:59:50 */
#define TIME_HOUR           (0x02u)
#define TIME_MIN            (0x00u)
#define TIME_SEC            (0x00u)
#define HOUR_OFFSET         (16u)
#define MIN_OFFSET          (8u)
#define TIME_HR_MIN_SEC     ((uint32)(TIME_HOUR << HOUR_OFFSET) | \
                            (uint32)(TIME_MIN << MIN_OFFSET)    | \
                             TIME_SEC)
/* Date: 03/22/2016 */
#define DATE_MONTH          (RTC_MARCH)
#define DATE_DAY            (0x22u)
#define DATE_YEAR           (0x2016u)
#define MONTH_OFFSET        (24u)
#define DAY_OFFSET          (16u)
#define DATE_MONTH_DAY_YEAR ((uint32)(DATE_MONTH << MONTH_OFFSET)   | \
                            (uint32)(DATE_DAY << DAY_OFFSET)        | \
                             DATE_YEAR)

/* Alarm1 Time: 02:59:55 */
#define ALARM_HOUR                  (0x02u)
#define ALARM_MIN                   (0x01u)
#define ALARM_SEC                   (0x50u)
#define ALARM_TIME_HR_MIN_SEC       ((uint32)(ALARM_HOUR << HOUR_OFFSET) | \
                                    (uint32)(ALARM_MIN << MIN_OFFSET)    | \
                                     ALARM_SEC)
                                   
/* Alarm2 Time: 02:59:55 */
#define NALARM_HOUR                  (0x02u)
#define NALARM_MIN                   (0x15u)
#define NALARM_SEC                   (0x50u)
#define NALARM_TIME_HR_MIN_SEC       ((uint32)(ALARM_HOUR << HOUR_OFFSET) | \
                                    (uint32)(ALARM_MIN << MIN_OFFSET)    | \
                                     ALARM_SEC)                                    
                                    
/* Alarm Date: 03/22/2016 */
#define ALARM_DATE_MONTH_DAY_YEAR   (DATE_MONTH_DAY_YEAR)


#define SYSTICK_EACH_10_HZ  (10u)
#define SYSTICK_RELOAD      (CYDEV_BCLK__SYSCLK__HZ / SYSTICK_EACH_10_HZ)

/* Alarm1 and alarm2 structure declaration */
RTC_DATE_TIME alarmTimeDate;
RTC_DATE_TIME alarmTime;


/* Alarm flags initialization */
 uint8 Alarm1flag = 0 ;
 uint8 Alarm1serviced = 0;
 uint8 Alarm2flag = 0;
 uint8 Alarm2serviced = 0;

/* Interrupt prototype */
CY_ISR_PROTO(SysTickIsrHandler);

/* Interrupt prototypes */
CY_ISR_PROTO(AlarmIsrHandler);


CY_ISR (AlarmIsrHandler)
 
{
     if ( ( Alarm1flag == 0 ) && (Alarm2flag == 0 ) ) {               // Only set Alarm1flag if both it and Alarm2flag clear

              Alarm1flag = 1;
             return;
     }

     if ( ( Alarm1flag == 1 ) && (Alarm2flag == 0 ) ) {             // Only set Alarm2flag if Alarm1flag has been set

              Alarm2flag = 1;
             return;
     }
}




int main()
{
    /* Place your initialization/startup code here (e.g. MyInst_Start()) */

    char timeBuffer[16u];
    char dateBuffer[16u];

    uint32 time;
    uint32 date;
    uint32 i;

    
   /* Starts SysTick component */
    CySysTickStart();

    /* Configure SysTick timer to generate interrupt every 100 ms */
    CySysTickSetReload(SYSTICK_RELOAD);

    /* Find unused callback slot. */
    for (i = 0u; i < CY_SYS_SYST_NUM_OF_CALLBACKS; ++i)
    {
        if (CySysTickGetCallback(i) == NULL)
        {
            /* Set callback */
            CySysTickSetCallback(i, SysTickIsrHandler);
            break;
        }
    }


     
    /* Alarm1 structure initialization */
    alarmTimeDate.time = ALARM_TIME_HR_MIN_SEC;
    alarmTimeDate.date = ALARM_DATE_MONTH_DAY_YEAR;
    
    
    /* Alarm2 structure initialization */
    alarmTime.time = NALARM_TIME_HR_MIN_SEC;
    alarmTime.date = ALARM_DATE_MONTH_DAY_YEAR;
    

    
    /* Starts RTC component */
    RTC_Start();
    
    LCD_Start();
    /* Set Date and Time */
    RTC_SetDateAndTime(TIME_HR_MIN_SEC,DATE_MONTH_DAY_YEAR);

    
      /* Set alarm mask */
    RTC_SetAlarmMask(RTC_ALARM_SEC_MASK   | RTC_ALARM_MIN_MASK        |
                       RTC_ALARM_HOUR_MASK  | RTC_ALARM_DAYOFMONTH_MASK |
                       RTC_ALARM_MONTH_MASK | RTC_ALARM_YEAR_MASK);

    
  
    
    /* Set Alarm Date and Time */
    RTC_SetAlarmDateAndTime(&alarmTimeDate);
  
  


   /* Set function AlarmIsrHandler to be called when alarm triggers */
    RTC_SetAlarmHandler(AlarmIsrHandler);

    /* Enable global interrupts */
    CyGlobalIntEnable;

    while(1)
    {

        /* Get Date and Time from RTC */
        time = RTC_GetTime();
        date = RTC_GetDate();

        /* Print Date and Time to UART */
        sprintf(timeBuffer, "%02lu:%02lu:%02lu", RTC_GetHours(time), RTC_GetMinutes(time), RTC_GetSecond(time));
        sprintf(dateBuffer, "%02lu/%02lu/%02lu", RTC_GetMonth(date), RTC_GetDay(date), RTC_GetYear(date));

           LCD_Position( 0u, 2u);
        LCD_PrintString(timeBuffer);
        LCD_Position( 1u, 2u );
        LCD_PrintString(dateBuffer);
        CyDelay(200u);
   
 if (  ( Alarm1flag == 1 ) && ( Alarm1serviced == 0 ) &&  ( Alarm2flag == 0 ) ) {     // Only service if conditions met
    
     Alarm1serviced = 1;
}

if (  ( Alarm2flag == 1 ) && ( Alarm2serviced == 0 )  && ( Alarm1serviced == 1)){
  
    Alarm2serviced = 2;
  
    Alarm1flag =Alarm1serviced = Alarm2flag =Alarm2serviced = 0;     // Reset all flags
}
  
   }

}

   
        
    void SysTickIsrHandler(void)
{
    RTC_Update();
}
 

I can't say anything about the compiler specific interrupt handler syntax used in your code. Refer to a the compiler documentation and example code.

It seems to me that the external interrupt used to sense a RTC alarm isn't setup anywhere in the code.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top