📄 pwm_test_.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")));
extern void isrTIMER0(void);
extern void isrTIMER1(void);
/*------------------------------------------------------------------------------------------*/
/* 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);
//uart_printf(" ----- ");
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);
//uart_printf(" _____ ");
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);//;// No beep...
/* set interrupt handler */
pISR_TIMER0=(unsigned)isrTIMER0;//timer0_int;
pISR_TIMER1=(unsigned)isrTIMER0;//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: timer_stop_pwm
* func: stop PWM Timer0
* para: none
* ret: none
* modify:
* comment:
*********************************************************************************************/
void timer_stop_pwm(void)
{
rGPBCON = rGPBCON & 0xFFFFF0;
rINTMSK |=(BIT_TIMER0|BIT_TIMER1);
rTCON &= 0xffe; // stop timer
}
/*********************************************************************************************
* 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|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|BIT_TIMER1);
rINTMSK &=~(BIT_TIMER0|BIT_TIMER1);
rTCON |= (1<<11)|(1<<3)|(1<<8)|1; // start timer,auto reload
g_nKeyPress = 3; // Press SB1202 3 times, then exit
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(g_nKeyPress)
{
if(cTemp=uart_getkey()) break;
if(g_nKeyPress>6) break;
}
if(!g_nKeyPress|cTemp<'1'|cTemp>'8')
{
timer_stop_pwm(); // stop timer
uart_printf(" end.\n");
return;
}
if(cTemp<'5')
cEnChange0=1;
else
cEnChange1=1;
while(cEnChange0);
while(cEnChange1);
}
timer_stop_pwm(); // stop timer
uart_printf(" end.\n");
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -