本帖最后由 lustao 于 2019-3-21 13:37 编辑
接上一个
把红绿灯亮及闪作个按rtc时钟中断每秒8次的程序,按键中断显时
- /***** Includes *****/
- #include
- #include
- #include "mxc_config.h"
- #include "nvic_table.h"
- #include "board.h"
- #include "rtc.h"
- #include "led.h"
- #include "pb.h"
- #include "tmr_utils.h"
- #include "gpio.h"
- /***** Definitions *****/
- #define LED_ALARM 0
- #define TIME_OF_DAY_SEC 2
- #define SUBSECOND_MSEC_0 125
- #define SUBSECOND_MSEC_1 250
- #define SECS_PER_MIN 60
- #define SECS_PER_HR (60 * SECS_PER_MIN)
- #define SECS_PER_DAY (24 * SECS_PER_HR)
- #define MSEC_TO_RSSA(x) (0 - ((x * 256) / 1000)) /* Converts a time in milleseconds to the equivalent RSSA register value. */
- /*LED Control PIN red*/
- #define GPIO_PORT_OUT PORT_0
- #define GPIO_PIN_OUT PIN_9
- /***** Globals *****/
- uint32_t ss_interval = SUBSECOND_MSEC_0;
- volatile int buttonPressed = 0;
- volatile int rtcalarm = 0;
- /***** Functions *****/
- void RTC_IRQHandler(void)
- {
- int flags = RTC_GetFlags();
- rtcalarm = 1;
- /* Check sub-second alarm flag. */
- if (flags & MXC_F_RTC_CTRL_ALSF) {
- RTC_ClearFlags(MXC_F_RTC_CTRL_ALSF);
- }
- /* Check time-of-day alarm flag. */
- if (flags & MXC_F_RTC_CTRL_ALDF) {
- RTC_ClearFlags(MXC_F_RTC_CTRL_ALDF);
- /* Set a new alarm 10 seconds from current time. */
- int time = RTC_GetSecond();
- if (RTC_SetTimeofdayAlarm(MXC_RTC, time + TIME_OF_DAY_SEC) != E_NO_ERROR) {
- /* Handle Error */
- }
- // Toggle the sub-second alarm interval.
- if (RTC_SetSubsecondAlarm(MXC_RTC, MSEC_TO_RSSA(ss_interval)) != E_NO_ERROR) {
- /* Handle Error */
- }
- }
- }
- void buttonHandler(void *pb)
- {
- buttonPressed = 1;
- }
- void printTime(void)
- {
- int day, hr, min, sec;
- double subsec;
- subsec = RTC_GetSubSecond() / 256.0;
- sec = RTC_GetSecond();
- day = sec / SECS_PER_DAY;
- sec -= day * SECS_PER_DAY;
- hr = sec / SECS_PER_HR;
- sec -= hr * SECS_PER_HR;
- min = sec / SECS_PER_MIN;
- sec -= min * SECS_PER_MIN;
- subsec += sec;
- printf("Current Time (dd:hh:mm:ss): %02d:%02d:%02d:%05.2fn", day, hr, min, subsec);
- }
- int main(void)
- {
- const sys_cfg_tmr_t sys_tmr_cfg = {0}; // Do not enable timer output.
- sys_cfg_rtc_t sys_cfg;
- gpio_cfg_t gpio_out0;
- /* Setup output pin FOR led. */
- gpio_out0.port = GPIO_PORT_OUT;
- gpio_out0.mask = GPIO_PIN_OUT;
- gpio_out0.pad = GPIO_PAD_NONE;
- gpio_out0.func = GPIO_FUNC_OUT;
- GPIO_Config(&gpio_out0);
- int count = 0;
- printf("nn***** RTC led ******nn");
- printf(
- "1. This example outputs the same state onto P0.9 (led_R) and outputs the same n state onto P0.13 (led_G).n");
- printf("2. An interrupt is set up on P0.12. when that interrupt occurs.nn");
- printf("The RTC is enabled and the sub-second alarm set to trigger every %d ms.n", SUBSECOND_MSEC_1);
- NVIC_EnableIRQ(RTC_IRQn);
- // Setup callback to receive notification of when button is pressed.
- PB_RegisterCallback(0, buttonHandler);
- // Turn LED off initially
- GPIO_OutClr(&gpio_out0);
- LED_Off(LED_ALARM);
- sys_cfg.tmr = MXC_TMR0;
- if (RTC_Init(MXC_RTC, 0, 0, &sys_cfg) != E_NO_ERROR) {
- printf("Failed RTC_Setup().n");
- return -1;
- }
- printf("RTC started.n");
- printTime();
- if (RTC_SetTimeofdayAlarm(MXC_RTC, TIME_OF_DAY_SEC) != E_NO_ERROR) {
- printf("Failed RTC_SetTimeofdayAlarm().n");
- return -1;
- }
- if (RTC_EnableTimeofdayInterrupt(MXC_RTC) != E_NO_ERROR) {
- printf("Failed RTC_EnableTimeofdayInterrupt().n");
- return -1;
- }
- if (RTC_SetSubsecondAlarm(MXC_RTC, (uint32_t)MSEC_TO_RSSA(SUBSECOND_MSEC_0)) != E_NO_ERROR) {
- printf("Failed RTC_SetSubsecondAlarm().n");
- return -1;
- }
- if (RTC_EnableSubsecondInterrupt(MXC_RTC) != E_NO_ERROR) {
- printf("Failed RTC_EnableSubsecondInterrupt().n");
- return -1;
- }
- if (RTC_EnableRTCE(MXC_RTC) != E_NO_ERROR) {
- printf("Failed RTC_EnableRTCE().n");
- return -1;
- }
- int a[7][3]={ {1,1,40}, {10,1,40}, {10,0,2},{10,1,2}, {10,0,2}, {10,1,2} , {11,1,10}}; //(red=1,green=10 ,yellow=11), (on=1 ,off=0), time=s/0.25
- int ledout=a[0][0],ledis=a[0][1] ,ledtime=a[0][2];
- if(ledis){
- switch(ledout){
- case 1: GPIO_OutSet(&gpio_out0);printf("LED_R is ONn"); break;
- case 10: LED_On(LED_ALARM);printf("LED_G is ONn"); break;
- case 11: GPIO_OutSet(&gpio_out0);LED_On(LED_ALARM);printf(" LED_Y is ONn"); break;
- default:printf("errorn"); break;
- }
- }
- else{GPIO_OutClr(&gpio_out0);LED_Off(LED_ALARM);}
- int i=0;
- while (1) {
- if (buttonPressed) {
- // Show the time elapsed.
- printTime();
- // Delay for switch debouncing.
- TMR_Delay(MXC_TMR0, MSEC(100), &sys_tmr_cfg);
- printf("buttonPressed count = %dn", count++);
- // Re-arm switch detection.
- buttonPressed = 0;
- }
- if (rtcalarm) {
- // Show the time elapsed.
- printTime();
- // Delay for switch debouncing.
- //TMR_Delay(MXC_TMR0, MSEC(100), &sys_tmr_cfg);
- //printf("rtcalarm count = %dn", count++);
- // Re-arm switch detection.
- rtcalarm = 0;
- count++;
- if ( ledtime){
- ledtime--;
- }
- else{
- GPIO_OutClr(&gpio_out0);LED_Off(LED_ALARM);
- i++;
- if (i>6){
- i=0;}
- ledout=a[i][0],ledis=a[i][1] ,ledtime=a[i][2];
- if(ledis){
- switch(ledout){
- case 1: GPIO_OutSet(&gpio_out0);printf("LED_R is ON rtcalarm count = %dn", count); break;
- case 10: LED_On(LED_ALARM);printf("LED_G is ON rtcalarm count = %dn", count); break;
- case 11: GPIO_OutSet(&gpio_out0);LED_On(LED_ALARM);printf(" LED_Y is ON rtcalarm count = %dn", count); break;
- default:printf("errorn"); break;
- }
- }else{GPIO_OutClr(&gpio_out0);LED_Off(LED_ALARM);printf("LED_Offn");}
- }
- }
- }
- }
复制代码
单步调试数据
能正常工作
com口
0
|
|
|
|