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; } }
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;
}
}
#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();
}
I did read the document. As said, the "main" part should go to the while(1) loop, supplemented with meaningful action code.
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:Main() is the entry point when the OS calls the program
int main()
{
run_os () ;
}
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.
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 }
Yes. But inside main(). There can be only one repeatedly while(1) loop inside main() and the code goes there.you mean Like this
#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();
}
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?