⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pwm_test.c

📁 Embest EudKit-II教学系统配Samsung S3C2410处理器的部分测试程序。
💻 C
字号:
/*********************************************************************************************
* File:		pwm_test.c
* Author:	embest
* Desc:		s3c44b0x pwm timer test 
* History:	
*********************************************************************************************/

/*------------------------------------------------------------------------------------------*/
/*	 								include files						 				    */
/*------------------------------------------------------------------------------------------*/
#include "2410lib.h"

/*------------------------------------------------------------------------------------------*/
/*	 								function declare					 				    */
/*------------------------------------------------------------------------------------------*/
void timer0_int(void) __attribute__((interrupt("IRQ")));
void timer1_int(void) __attribute__((interrupt("IRQ")));

/*------------------------------------------------------------------------------------------*/
/*	 								global variables					 				    */
/*------------------------------------------------------------------------------------------*/
INT8T cTemp;
INT8T cEnChange0,cEnChange1;

/*********************************************************************************************
* name:		timer1_int
* func:		Timer0 Interrupt handler
* para:		none
* ret:		none
* modify:
* comment:		
*********************************************************************************************/
void timer0_int(void)
{
	ClearPending(BIT_TIMER0);
	if(cEnChange0)
	{
		switch(cTemp)
		{
			case '1':
				    rTCNTB0 = 792;
					rTCMPB0 = 396;
					uart_printf("\n You selected 1\n");
					break;
			case '2':
				    rTCNTB0 = 792;
					rTCMPB0 = 264;
					uart_printf("\n You selected 2\n");
					break;					
			case '3':
				    rTCNTB0 = 396;
					rTCMPB0 = 132;
					uart_printf("\n You selected 3\n");
					break;					
			case '4':
					rTCNTB0 = 396;
					rTCMPB0 = 198;
					uart_printf("\n You selected 4\n");
					break;		
			default:break;
		}
		cEnChange0=0;
	}
}

/*********************************************************************************************
* name:		timer1_int
* func:		Timer1 Interrupt handler
* para:		none
* ret:		none
* modify:
* comment:		
*********************************************************************************************/
void timer1_int(void)
{
	ClearPending(BIT_TIMER1);
	if(cEnChange1)
	{
		switch(cTemp)
		{
			case '5':
				    rTCNTB1 = 792;
					rTCMPB1 = 396;
					uart_printf("\n You selected 5\n");
					break;
			case '6':
				    rTCNTB1 = 792;
					rTCMPB1 = 264;
					uart_printf("\n You selected 6\n");
					break;					
			case'7':
				    rTCNTB1 = 396;
					rTCMPB1 = 132;
					uart_printf("\n You selected 7\n");
					break;					
			case'8':
					rTCNTB1 = 396;
					rTCMPB1 = 198;
					uart_printf("\n You selected 8\n");
					break;		
			default:break;
		}
		cEnChange1=0;
	}
}
/*********************************************************************************************
* name:		timer_init
* func:		initialize PWM Timer0
* para:		none
* ret:		none
* modify:
* comment:		
*********************************************************************************************/
void timer_init(void)
{		
	rGPBCON = rGPBCON & 0xFFFFF0|(1<<3);//|(1<<1);
	
	/* set interrupt handler */
    pISR_TIMER0=(unsigned)timer0_int;
    pISR_TIMER1=(unsigned)timer1_int;
    
	rTCFG0 = (39<<8)|(39);					// Prescaler0/1=39,39	
	rTCFG1 = (3<<4)|3;						// Interrupt; Devider value = 16
	// timer0 clock = PCLK/(prescaler+1)/16
	rTCON=0;								//disable dead zone,auto reload off,inverter off,not start

}

/*********************************************************************************************
* name:		pwm_test
* func:		initialize PWM Timer0
* para:		none
* ret:		none
* modify:
* comment:		
*********************************************************************************************/
void pwm_test(void)
{
	uart_printf("\n PWM Test Example, %d times\n",g_nTimeout);
	uart_printf(" Press SB1202 to start the motor \n");
	uart_printf(" Press SB1203 to change the direction of the motor\n");	
	// init timer0
	timer_init();	
	ClearPending(BIT_TIMER0);
	ClearPending(BIT_TIMER1);
	/* initialize PWM timer0 register */
	rTCNTB0 = 792;
	rTCMPB0 = 396;
	rTCNTB1 = 792;
	rTCMPB1 = 396;
	rTCON |= (1<<10)|(1<<9)|(1<<2)|(1<<1);	// manual update
	delay(10);
	rTCON  &= ~((1<<9)|(1<<1));				// auto reload
	ClearPending(BIT_TIMER0);
	ClearPending(BIT_TIMER1);
	rINTMSK &=~(BIT_TIMER0|BIT_TIMER1);
	rTCON   |= (1<<11)|(1<<3)|(1<<8)|1;		// start timer,auto reload

	g_nKeyPress=1;
	while(1)
	{
		uart_printf("\n You can change the duty cycle and rotation speed as following:\n");
		uart_printf(" 1-->PWM0: 1/2,100Hz(default)\n");
		uart_printf(" 2-->PWM0: 1/3,100Hz\n");
		uart_printf(" 3-->PWM0: 1/3,200Hz\n");
		uart_printf(" 4-->PWM0: 1/2,200Hz\n\n");
		uart_printf(" 5-->PWM1: 1/2,100Hz(default)\n");
		uart_printf(" 6-->PWM1: 1/3,100Hz\n");
		uart_printf(" 7-->PWM1: 1/3,200Hz\n");
		uart_printf(" 8-->PWM1: 1/2,200Hz\n");
		
		while(!(rUTRSTAT0 & 0x1))			// if receive data ready
			if(g_nKeyPress!=1) break;
		if(g_nKeyPress!=1) break;			// SB1202/SB1203 to exit board test
		
		cTemp = RdURXH0(); 					// or get value from UART0

		if(cTemp<'5')
			cEnChange0=1;
		else
			cEnChange1=1;
		while(cEnChange0);
		while(cEnChange1);
	}
	rGPBCON = rGPBCON & 0xFFFFFC;
	rINTMSK |=(BIT_TIMER0|BIT_TIMER1);
	rTCON &= 0xffe;							// stop timer
	uart_printf(" end.\n");
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -