代码编织梦想

lab 11

使用FreeRTOS系统实现三个任务间的调度,这三个任务分别是:

  1. 任务1:通过串口输出 “Hello Free RTOS!\n”

  2. 任务2:LED1以1s的频率闪灯,并在串口输出:“LED1 is running!\n”

  3. 任务3:lab10中的光控台灯, 当用手上下接触LIGHT光敏电阻时,白灯的亮度发生改变,光敏电阻无遮挡时白灯亮度最暗,光敏电阻被完全遮挡时白灯亮度最亮。 并在串口输出:“ADC is running!\n”

/* Standard includes. */
#include <stdio.h>

#include <msp432p401r_classic.h>
#include <driverlib.h>

/* Kernel includes. */
#include "FreeRTOS.h"
#include "task.h"
#include "timers.h"
/*-----------------------------------------------------------*/

static void prvSetupHardware( void );
static void prvConfigureButton( void );
bool flag = false;
void PORT1_IRQHandler( void );


/*-----------------------------------------------------------*/ 
 
 //PWM
 #define TA1_PERIODS (62500)
#define PWM_PERIODS (62500/50)
 const Timer_A_UpModeConfig TA0 =     
{
        TIMER_A_CLOCKSOURCE_SMCLK,              // ??SMCLK?????
        TIMER_A_CLOCKSOURCE_DIVIDER_1,          // ?????1,Timer_A0=SMCLK/1=62.5k
        PWM_PERIODS-1,                          // ????=PWM_PERIODS/Timer_A0=20ms(?:????????100ms?400ms),???????,???????;
        TIMER_A_TAIE_INTERRUPT_DISABLE,         // ???????
        TIMER_A_CCIE_CCR0_INTERRUPT_DISABLE,    // ?????CCR0??
        TIMER_A_DO_CLEAR                        // ????
};

Timer_A_CompareModeConfig TA0_CCR1_PWM =
{
        TIMER_A_CAPTURECOMPARE_REGISTER_1,          // CCR1??
        TIMER_A_CAPTURECOMPARE_INTERRUPT_DISABLE,   // ??CCR??
        TIMER_A_OUTPUTMODE_RESET_SET,               // ????/??
        PWM_PERIODS                                 // ???,??????,??????val<PWM_PERIODS?,?????(?0?PWM_PERIODS-1,?PWM_PERIODS);?val>=PWM_PERIODS??????
};

Timer_A_CompareModeConfig TA0_CCR2_PWM =
{
        TIMER_A_CAPTURECOMPARE_REGISTER_2,         // CCR2??
        TIMER_A_CAPTURECOMPARE_INTERRUPT_DISABLE,  // ??CCR??
        TIMER_A_OUTPUTMODE_RESET_SET,              // ????/??
        PWM_PERIODS    
};

Timer_A_CompareModeConfig TA0_CCR3_PWM =
{
        TIMER_A_CAPTURECOMPARE_REGISTER_3,        // CCR3??
        TIMER_A_CAPTURECOMPARE_INTERRUPT_DISABLE, // ??CCR??
        TIMER_A_OUTPUTMODE_RESET_SET,             // ????/??
        PWM_PERIODS    
};

const Timer_A_UpModeConfig TA1 =
{
        TIMER_A_CLOCKSOURCE_SMCLK,              // ??SMCLK?????
        TIMER_A_CLOCKSOURCE_DIVIDER_1,          // ?????1,Timer_A1=SMCLK/1=62.5k
        TA1_PERIODS-1,                          // ????=TA1_PERIODS/Timer_A1=1s,???????,???????;
        TIMER_A_TAIE_INTERRUPT_DISABLE,         // ???????
        TIMER_A_CCIE_CCR0_INTERRUPT_ENABLE ,    // ?????CCR0??
        TIMER_A_DO_CLEAR                        // ????
};
 // PWM END


 
 
 
 
const eUSCI_UART_Config uartConfig =
{
        EUSCI_A_UART_CLOCKSOURCE_SMCLK,          //ѡ��SMCLK��1M��ʱ��Դ
        6,                                       // BRDIV = 6 ��clockPrescalarʱ�ӷ�Ƶϵ�� 
        8,                                       // UCxBRF = 8  firstModReg ��BRDIV��UCxBRF�� UCxBRS��SMCLK���������ô��ڲ����ʣ�
        17,                                      // UCxBRS = 17 secondModReg
        EUSCI_A_UART_NO_PARITY,                  // У��λNone
        EUSCI_A_UART_LSB_FIRST,                  // ��λ���ȣ�С��ģʽ
        EUSCI_A_UART_ONE_STOP_BIT,               // ֹͣλ1λ
        EUSCI_A_UART_MODE,                       // UART mode
        EUSCI_A_UART_OVERSAMPLING_BAUDRATE_GENERATION  // ����Ϊ������������ֵΪ1
};
/*-----------------------------------------------------------*/

/* printf�ض��� */
int fputc(int ch,FILE *f)
{
	UART_transmitData(EUSCI_A0_BASE,(uint8_t)ch);
	return ch;
}



const TickType_t xDelay100ms = pdMS_TO_TICKS( 100UL );
// Task1
void vTask1( void *pvParameters )
{
	for( ;; )
	{
		printf( "Hello Free RTOS!  \n" );

		for(int i = 0; i < 10; i++)
			vTaskDelay( xDelay100ms );
	}
}

/*
void CALLBACK_LED( TimerHandle_t xTimer )//�ص����� as TASK2
 	{ 
		uint32_t ulTimerID;
		configASSERT(xTimer);
		bool sign = flag;
		ulTimerID = (uint32_t)pvTimerGetTimerID(xTimer);//��ȡTimerID���ж��ĸ���ʱ������ص�����
		switch(ulTimerID)
		{
		 case 0://LED1���Ʋ���
				GPIO_toggleOutputOnPin(GPIO_PORT_P1,GPIO_PIN0);
				for(int i = 0; i < 100000; i++); //�㹻�Ŀ�ѭ�������Կɼ�LED����
				GPIO_toggleOutputOnPin(GPIO_PORT_P1,GPIO_PIN0);
				
				for(int i = 0; i < 10; i++)
					vTaskDelay( xDelay100ms );
			break;
		 
	 	}
 	}
	*/
	void vTask2( void *pvParameters )
{
	for( ;; )
	{
		printf( "LED1 is running!  \n" );
		GPIO_toggleOutputOnPin(GPIO_PORT_P1,GPIO_PIN0);
		for(int i = 0; i < 100000; i++); //�㹻�Ŀ�ѭ�������Կɼ�LED����
		GPIO_toggleOutputOnPin(GPIO_PORT_P1,GPIO_PIN0);
		
		for(int i = 0; i < 10; i++)
			vTaskDelay( xDelay100ms );
	}
}
	
//TASK3
static void prvConfigurePWM( void )	
{
	uint8_t port_mapping[]=
    {
        PM_TA0CCR1A, //???Timer_A0_CCR1,?Pin0??
        PM_TA0CCR2A, //???Timer_A0_CCR2,?Pin1??
        PM_TA0CCR3A, //???Timer_A0_CCR3,?Pin2??
        PM_NONE,     //???,????
        PM_NONE,
        PM_NONE,
        PM_NONE,
        PM_NONE
    };    
		
    MAP_PMAP_configurePorts((const uint8_t *)port_mapping,PMAP_P2MAP/*??2*/,1/*8???*/,PMAP_DISABLE_RECONFIGURATION);
    MAP_GPIO_setAsPeripheralModuleFunctionOutputPin(GPIO_PORT_P2,GPIO_PIN0|GPIO_PIN1|GPIO_PIN2,GPIO_PRIMARY_MODULE_FUNCTION); 
		
		
    MAP_Timer_A_configureUpMode(TIMER_A0_BASE,&TA0);
    
    
    MAP_Timer_A_configureUpMode(TIMER_A1_BASE,&TA1); 
    
    MAP_Interrupt_enableInterrupt(INT_TA1_0); 
    MAP_Interrupt_enableMaster(); 
    
    MAP_Timer_A_startCounter(TIMER_A0_BASE, TIMER_A_UP_MODE);/*????????TimerA0*/
    MAP_Timer_A_startCounter(TIMER_A1_BASE, TIMER_A_UP_MODE);/*????????TimerA1*/
	
}
volatile uint16_t adcResult;
static void prvConfigureADC( void )
{
	    /* Initializing ADC (MCLK/1/1) */
    ADC14_enableModule();
    ADC14_initModule(ADC_CLOCKSOURCE_MCLK, ADC_PREDIVIDER_1, ADC_DIVIDER_4,0);


    ADC14_configureSingleSampleMode(ADC_MEM0, true);

    ADC14_configureConversionMemory(ADC_MEM0, ADC_VREFPOS_AVCC_VREFNEG_VSS,ADC_INPUT_A1, 0);

    ADC14_enableSampleTimer(ADC_MANUAL_ITERATION); 
    ADC14_enableInterrupt(ADC_INT0); 
    
    Interrupt_enableInterrupt(INT_ADC14);
    Interrupt_enableMaster(); 
    
    ADC14_enableConversion(); 
    ADC14_toggleConversionTrigger(); 
}
void ADC14_IRQHandler(void)
{
    uint64_t status;    
    status = ADC14_getEnabledInterruptStatus();
    ADC14_clearInterruptFlag(status);

    if(status & ADC_INT0)
    {
        adcResult = ADC14_getResult(ADC_MEM0);  
        ADC14_toggleConversionTrigger();
    }     
}

void TA1_0_IRQHandler(void)
{
				TA0_CCR1_PWM.compareValue =PWM_PERIODS*(adcResult*3.3/16384/5);
				TA0_CCR2_PWM.compareValue =PWM_PERIODS*(adcResult*3.3/16384/5);
				TA0_CCR3_PWM.compareValue =PWM_PERIODS*(adcResult*3.3/16384/5);
				MAP_Timer_A_initCompare(TIMER_A0_BASE, &TA0_CCR1_PWM);  /*???????TimerA0 CCR1(??LED2???)?PWM???*/
				MAP_Timer_A_initCompare(TIMER_A0_BASE, &TA0_CCR2_PWM);  /*???????TimerA0 CCR2(??LED2???)?PWM???*/
				MAP_Timer_A_initCompare(TIMER_A0_BASE, &TA0_CCR3_PWM);  /*???????TimerA0 CCR3(??LED2???)?PWM???*/
    
				MAP_Timer_A_clearCaptureCompareInterrupt(TIMER_A1_BASE,TIMER_A_CAPTURECOMPARE_REGISTER_0);/*??TimerA1 CCR0????*/
}
void vTask3( void *pvParameters )
{
	for( ;; )
	{
		if(TA0_CCR1_PWM.compareValue > 250)
		{//when you dont shade it won't print
			printf( "ADC is running!  \n" );
			//shade it will output the string
		}
		for(int i = 0; i < 10; i++)
			vTaskDelay( xDelay100ms );
	}
}



/*-----------------------------------------------------------*/
static void prvConfigureClocks( void )
{
	CS_setDCOFrequency(4000000);
	CS_initClockSignal( CS_HSMCLK, CS_DCOCLK_SELECT, CS_CLOCK_DIVIDER_4 );
	CS_initClockSignal( CS_SMCLK, CS_DCOCLK_SELECT, CS_CLOCK_DIVIDER_4 );
	CS_initClockSignal( CS_MCLK, CS_DCOCLK_SELECT, CS_CLOCK_DIVIDER_1 );
	CS_initClockSignal( CS_ACLK, CS_REFOCLK_SELECT, CS_CLOCK_DIVIDER_1 );

}

int main( void )
{
	/* Ӳ����ʼ�� */
	prvSetupHardware();
	/* ʱ�ӳ�ʼ�� */
	prvConfigureClocks();
	prvConfigurePWM();
	prvConfigureADC();
	
	//����������ʱ�� 
	
	xTaskCreate( vTask1, "Task 1", 64, NULL, 1, NULL ); 
	//TimerHandle_t LED1_Timers;
	//LED1_Timers = xTimerCreate("Task 2", (pdMS_TO_TICKS(1000UL)), pdTRUE, (void*)0, CALLBACK_LED); //1s
	xTaskCreate( vTask2, "Task 2", 64, NULL, 1, NULL ); 
	xTaskCreate( vTask3, "Task 3", 64, NULL, 1, NULL ); 
	
	
	/* ��鶨ʱ���Ƿ񴴽��ɹ�. 
	if(LED1_Timers == NULL)
	{
			printf("Error ocurred!\n");
		
	}else
	{
		if((xTimerStart(LED1_Timers,0)!= pdPASS))
		{
			printf("Error ocurred!\n");
			
		}
	}*/
	
	vTaskStartScheduler();
	
	
	return 0;
}
/*-----------------------------------------------------------*/

static void prvSetupHardware( void )
{
extern void FPU_enableModule( void );

	/* ͣ�ÿ��Ź�. */
	MAP_WDT_A_holdTimer();

	/* ʹ��FPU. */
	FPU_enableModule();

	/* ѡ��P1.2 ��P1.3����ΪUARTģʽ */
	MAP_GPIO_setAsPeripheralModuleFunctionInputPin( GPIO_PORT_P1, GPIO_PIN2 | GPIO_PIN3, GPIO_PRIMARY_MODULE_FUNCTION );
	MAP_UART_initModule(EUSCI_A0_BASE, &uartConfig);
	MAP_UART_enableModule(EUSCI_A0_BASE);
	
	/* ����P1.0��P2.1, P2.2ΪGPIO��������� */
	MAP_GPIO_setOutputLowOnPin( GPIO_PORT_P1, GPIO_PIN0 );
	MAP_GPIO_setAsOutputPin( GPIO_PORT_P1, GPIO_PIN0 );
	MAP_GPIO_setOutputLowOnPin( GPIO_PORT_P2, GPIO_PIN2 );
	MAP_GPIO_setAsOutputPin( GPIO_PORT_P2, GPIO_PIN2 );
	MAP_GPIO_setOutputLowOnPin( GPIO_PORT_P2, GPIO_PIN1 );
	MAP_GPIO_setAsOutputPin( GPIO_PORT_P2, GPIO_PIN1 );
}



static void prvConfigureButton( void )
{
	/* ����P1.1��s1������Ϊ����ģʽ������ */
	GPIO_setAsInputPinWithPullUpResistor( GPIO_PORT_P1, GPIO_PIN1 );
	/* ʹ��GPIO1.1�ж� */
	GPIO_enableInterrupt( GPIO_PORT_P1, GPIO_PIN1 );
	Interrupt_enableInterrupt( INT_PORT1 );
}
/*-----------------------------------------------------------*/







/*-----------------------------------------------------------*/

void vApplicationMallocFailedHook( void )
{
	/* vApplicationMallocFailedHook() will only be called if
	configUSE_MALLOC_FAILED_HOOK is set to 1 in FreeRTOSConfig.h.  It is a hook
	function that will get called if a call to pvPortMalloc() fails.
	pvPortMalloc() is called internally by the kernel whenever a task, queue,
	timer or semaphore is created.  It is also called by various parts of the
	demo application.  If heap_1.c or heap_2.c are used, then the size of the
	heap available to pvPortMalloc() is defined by configTOTAL_HEAP_SIZE in
	FreeRTOSConfig.h, and the xPortGetFreeHeapSize() API function can be used
	to query the size of free heap space that remains (although it does not
	provide information on how the remaining heap might be fragmented). */
	//taskDISABLE_INTERRUPTS();
	for( ;; );
}
/*-----------------------------------------------------------*/

void vApplicationIdleHook( void )
{
	/* vApplicationIdleHook() will only be called if configUSE_IDLE_HOOK is set
	to 1 in FreeRTOSConfig.h.  It will be called on each iteration of the idle
	task.  It is essential that code added to this hook function never attempts
	to block in any way (for example, call xQueueReceive() with a block time
	specified, or call vTaskDelay()).  If the application makes use of the
	vTaskDelete() API function (as this demo application does) then it is also
	important that vApplicationIdleHook() is permitted to return to its calling
	function, because it is the responsibility of the idle task to clean up
	memory allocated by the kernel to any task that has since been deleted. */
}
/*-----------------------------------------------------------*/

void vApplicationStackOverflowHook( TaskHandle_t pxTask, char *pcTaskName )
{
	( void ) pcTaskName;
	( void ) pxTask;

	/* Run time stack overflow checking is performed if
	configCHECK_FOR_STACK_OVERFLOW is defined to 1 or 2.  This hook
	function is called if a stack overflow is detected. */
	taskDISABLE_INTERRUPTS();
	for( ;; );
}
/*-----------------------------------------------------------*/

void *malloc( size_t xSize )
{
	/* There should not be a heap defined, so trap any attempts to call
	malloc. */
	Interrupt_disableMaster();
	for( ;; );
}
/*-----------------------------------------------------------*/

void vPreSleepProcessing( uint32_t ulExpectedIdleTime )
{
}
/*-----------------------------------------------------------*/

	void vApplicationTickHook( void )
	{
		/* This function will be called by each tick interrupt if
		configUSE_TICK_HOOK is set to 1 in FreeRTOSConfig.h.  User code can be
		added here, but the tick hook is called from an interrupt context, so
		code must not attempt to block, and only the interrupt safe FreeRTOS API
		functions can be used (those that end in FromISR()). */

		/* Only the full demo uses the tick hook so there is no code is
		executed here. */
	}

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/JamSlade/article/details/129416505