前言
本博文介绍mini2440开发板,在keil5平台上进行PWM开发的一些基本配置。
PWM配置
开发板上PWM原理图
PWM频率设置为31.25Hz,占空比为50%,代码如下
#include "S3C2440.h"
//pwm配置
void pwm_Init(void)
{
//设置GPB0为定时器0输出
GPBCON &= ~(3<<0);
GPBCON |= (1<<1);
/*
定时器输入时钟频率 = PCLK / {预分频值+1} / {分频值}
= 50000000/(99+1)/16
= 31250
{预分频值} = 0~255
{分频值} = 2, 4, 8, 16
*/
TCFG0 = 99;//预分频值99
TCFG1 &= 0x0;//分频值16
TCFG1 |= 0x3;
/*
pwm频率 = 定时器输入时钟频率/TCNTB0
= 31250/1000
= 31.25
*/
TCNTB0 = 1000;
/*
TCMPB0 = TCNTB0 * 占空比
= 1000 * 50%
= 500
*/
TCMPB0 = 500;
TCON |= (1<<1);//加载初值
TCON &= ~(1<<1);//清零
TCON |= (1<<0)|(1<<3);//设置为自动加载并启动Timer0
}